Unity Container Type Registration Quirk - vb.net

I'm trying to automatically register all reports in a unity container.
All reports implement IReport and also have a Report() attribute which defines the title, description and unique key (so I can read these without instantiating a concrete class).
So...
I get the report types like this
Public Shared Function GetClassesWhichimplementInterface(Of T)() As IEnumerable(Of Type)
Dim InterfaceType = GetType(T)
Dim Types As IEnumerable(Of Type)
Types = Reflection.Assembly.GetCallingAssembly.GetTypes()
Return Types.Where(Function(x) InterfaceType.IsAssignableFrom(x))
End Function
And register them like this:
Public Sub RegisterReports()
Dim ReportTypes = ReflectionHelper.GetClassesWhichimplementInterface(Of IReport)()
For Each ReportType In ReportTypes
''Previously I was reading the report attribute here and using the defined unique key. I've stopped using this code to remove possible problems while debugging.
Container.RegisterType(GetType(IReport), ReportType, ReportType.Name)
Next
End Sub
There are types being returned by the call to GetClassesWhichimplementInterface() and the Container.RegisterType() call is made without errors. If I call Container.Resolve(of Interfaces.IReport) immediately after the register call, I get the following exception:
Resolution of the dependency failed, type = "MyProject.Interfaces.IReport", name = "(none)".
Exception occurred while: while resolving.
Exception is: InvalidOperationException - The current type, MyProject.Interfaces.IReport, is an interface and cannot be constructed. Are you missing a type mapping?
-----------------------------------------------
At the time of the exception, the container was:
Resolving MyProject.Interfaces.IReport,(none)
Can anyone tell me why the container isn't preserving the registration?

The registration is in the container. The thing is that you are calling resolve without passing a named registration as a parameter.
As all your registrations were performed using the following code:
Container.RegisterType(GetType(IReport), ReportType, ReportType.Name)
Then all of them have a name. You must provide the name along with the type to be able to resolve the dependency from the container.
The error you are getting is because there is no type mapping registered without a name.

Related

VB.NET Connected SAP WSDL Preventing Solution from building

I'm trying to build a .NET Class Library that utilises a SAP generated WSDL.
In the reference.vb file that one of the WSDLs generates, I'm getting the following error in this line:
<System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified, Order:=0)> _
With the error being BC30369 Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class. on System.
This only occurs within one of the generated Partial Public Classes that it generates, and not the rest.
After removing System it works:
'''
<System.Xml.Serialization.XmlElementAttribute(Form:=Xml.Schema.XmlSchemaForm.Unqualified)>
Public Property MESSAGE_V4() As String
Get
Return Me.mESSAGE_V4Field
End Get
Set
Me.mESSAGE_V4Field = Value
End Set
End Property

Geode native client deserialise PdxInstanceImpl

I have a REST client that populates a Geode region with Json data which the Geode REST API automatically converts to a PdxInstance type.
The region triggers a C# native client listener AfterCreate(EntryEvent<TKey, TVal> ev) in which the TVal type ev.NewValue is seen as type PdxInstanceImpl and looks like:
PDX[7534066,__GEMFIRE_JSON]{#type=MyClass, Field1=Value1, Field2=Value2}
I've seen from here that the following code can get at the individual Pdx fields
IPdxInstance pdx = (IPdxInstance)ev.NewValue;
pdx.GetField("Field1");
and that works on a Field level, but I want to convert the PdxInstanceImpl that is received to PdxInstance so it can be put into another region directly, or I want to convert all the fields back to Json (as a string) in 1 go and put a Json string into another region, or use it as I like.
So there is apparently a way to autoserialize a PdxInstance to MyClass but if I try
MyClass c = (MyClass)pdx;
then I get System.InvalidCastException: Unable to cast object of type 'Apache.Geode.Client.Internal.PdxInstanceImpl' to type 'MyClass'
I've seen from some Java client examples you can use type PdxInstanceImpl to get at the data but in the C# native client that gives an error: PdxInstanceImpl is inaccessible due to its protection level.
I've added the autoserializer and the results are the same.
Any idea what I am missing here? Thanks
In the end I've used a field by field approach:
IPdxInstance pdx = (IPdxInstance)ev.NewValue;
pdx.GetField("Field1");
pdx.GetField("Field2");
pdx.GetField("Field3");
etc...
Outside of the event handlers, to create a PDX instance I used:
IPdxInstanceFactory writer = Setup.g.GetCache().CreatePdxInstanceFactory("myType");
writer.WriteString("String", "s");
writer.WriteChar("Char", 'c');
writer.WriteDouble("Double", Convert.ToDouble(1000));
IPdxInstance pdx = writer.Create();
To read a PDX instance its:
IPdxInstance pdx = Setup.gpg.GeodeGetPdx("myType", key);
MyType t = new MyType();
t.String1 = (string)pdx.GetField("String1");
t.Int1 = (int)pdx.GetField("Int1");
t.Date1 = (DateTime)pdx.GetField("Date1");
etc...

Consuming WCF return value of List(Of String) in WinForms

I've just started to get rid of Web Services and implement WCF instead. But once I tried to add the following method with a List(Of String) return value, my WCF ServiceReference - which was added successfully - had became unreadable in my WinForms client application code. i.e. It was not defined.
The scenario is pretty simple:
I am creating an OperationContract named ServerMessages with a list of string return value.
This is the interface:
<ServiceContract()>
Public Interface ICommunicationHandler
<OperationContract()>
Function ServerMessages() As List(Of String)
End Interface
And there is the implementation class:
Public Class CommunicationHandler
Implements ICommunicationHandler
Public Function ServerMessages() As List(Of String) _
Implements ICommunicationHandler.ServerMessages
Dim messages As New List(Of String)
messages.Add("First Message")
messages.Add("Second Message")
Return messages
End Function
End Class
There is my client application code:
Dim locServ As New LocalReference.CommunicationHandlerClient
TextBox1.Text += "First Server Messages:" & vbLf & locaServ.ServerMessages(0)
I've tried removing the service reference from the project and re-adding it, but this doesn't solved the problem. On the other hand, removing that specific method ServerMessages() makes the ServiceReference available again in my code. I've even tried to define the type List(Of String) in the Service Interface by adding this attribute:
<ServiceKnownType(GetType(List(Of String)), ServiceContract()>
but nothing had changed.
EDIT
I've noticed this warning in the client application Error window:
Warning 2 Custom tool warning: Cannot import wsdl:port Detail: There
was an error importing a wsdl:binding that the wsdl:port is dependent
on. XPath to wsdl:binding:
//wsdl:definitions[#targetNamespace='http://tempuri.org/']/wsdl:binding[#name='BasicHttpBinding_ICommunicationHandler']
XPath to Error Source:
//wsdl:definitions[#targetNamespace='http://tempuri.org/']/wsdl:service[#name='CommunicationHandler']/wsdl:port[#name='BasicHttpBinding_ICommunicationHandler'] D:\WCTApp\Service
References\LocalReference\Reference.svcmap
Thanks in advance.
My problem was solved by unchecking the [Reuse types in referenced assemblies] in the WCF ServiceReference configuration of my client application. Thanks Steve for letting me having another check on it.
The problem - in this case - was that I've added some references in the service (Google Drive references) which was not defined in my application and therefore causing the ServiceReference not to read the service appropriately. Since I decided not to use these references, I should had eliminated them both from my client application as well as the WCF Service. Now that I stopped using these references in my client application, everything worked as it is supposed to be.
This was my mistake but maybe someone else will fall into the exact same issue and find this situation helpful.

Errors running Google API calendar example for vb.net

When I try to run this example in VBExpress 2010, I get the following intellisense errors.
scopes.Add(CalendarService.Scopes.Calendar.GetStringValue())
This line generates:
Error 7 Overload resolution failed because no accessible
'GetStringValue' is most specific for these arguments:
Extension method 'Public Function GetStringValue() As String' defined in 'Google.Apis.Util.Utilities': Not most specific.
Extension method 'Public Function GetStringValue() As String' defined in 'Google.Apis.Util.Utilities': Not most specific.
Additionally, these two lines each generate a "not defined" error.
Dim credentials As FullClientCredentials = promptingClientCredentials.EnsureFullClientCredentials()
Dim initializer As New BaseClientService.Initializer()
Error 9 Type 'BaseClientService.Initializer' is not defined.
Error 8 Type 'FullClientCredentials' is not defined.
Finally, this line:
Dim state As IAuthorizationState = AuthorizationMgr.GetCachedRefreshToken(STORAGE, KEY)
generates the error:
Error 15 'AuthorizationMgr' is not declared. It may be inaccessible
due to its protection level.
As to the first error, both google.apis.silverlight.google.apis.util.utilities and google.apis.google.apis.util.utilities has a GetStringValue(system.enum) as String method.
Any ideas about any of these errors?
UPDATE: Excluding Silverlight dll seems to resolve first error ("Not most specific")
We just published a new sample using VB.NET and OAuth2.
It works in VS Professional 2012. Take a look - http://samples.google-api-dotnet-client.googlecode.com/hg/Calendar.VB.ConsoleApp/README.html

Code Analysis Error: Declare types in namespaces

Is VS2010, I analyzed my code and got this error:
Warning 64 CA1050 : Microsoft.Design : 'ApplicationVariables' should be declared inside a namespace. C:\My\Code\BESI\BESI\App_Code\ApplicationVariables.vb 10 C:\...\BESI\
Here is some reference info on the error. Essentially, I tried to create a class to be used to access data in the Application object in a typed way.
The warning message said unless I put my (ApplicationVariables) class in a Namespace, that I wouldn't be able to use it. But I am using it, so what gives?
Also, here is a link to another StackOverflow article that talks about how to disable this warning in VS2008, but how would you disable it for 2010? There is no GlobalSuppressions.vb file for VS2010.
Here is the code it is complaining a bout:
Public Class ApplicationVariables
'Shared Sub New()
'End Sub 'New
Public Shared Property PaymentMethods() As PaymentMethods
Get
Return CType(HttpContext.Current.Application.Item("PaymentMethods"), PaymentMethods)
End Get
Set(ByVal value As PaymentMethods)
HttpContext.Current.Application.Item("PaymentMethods") = value
End Set
End Property
'Etc, Etc...
End Class
I suspect that the code you entered is in your App_Code fodler of your web app. This code is accessible from your web code as you have deomnstrated but is not accessible from any other assembly.
You can suppress the instance of the error by right mouse clicking on the particular error and selecting "Suppress Message In Source." That'll result in code being added to your source that says "the next time you check this error-fuggedabodit!"
When to Suppress Warnings
--------------------------------------------------------------------------------
While it is never necessary to suppress a warning from this rule, it is safe to do this when the assembly will never be used with other assemblies.
To suppress the error on all occurences, select "Suppress in Project Suppression File"