WCF KnownTypes in Mono 2.10 / MonoTouch 4.0 - wcf

I have an application that uses WCF. It runs great on Mono 2.8.2 on Ubuntu and .NET 3.0 on Windows.
It decorates the WCF service contract with the 'ServiceKnownType' attribute, which points to a method returning the derived types needed to use the server:
[ServiceKnownType("GetKnownTypes", typeof(KnownTypeProvider)]
I am porting my app to MonoTouch. I am using the latest 4.0, based on Mono 2.10.1. This no longer works: my WCF call fails because the DataContractSerializer on the client doesn't recognise the return type.
I set a breakpoint in my 'GetKnownTypes' method and see that it is never called. I assume that this part of System.ServiceModel is not present / not working in MonoTouch?
I try to work around this by decorating the base return type with the 'KnownType' attribute instead.
[KnownType("GetKnownTypes")]
Again, it doesn't work and my 'GetKnownTypes' method is never called. Looking at the source for DataContractSerializer, it seems if you use a method name rather than specifying the type, it just gets ignored?!
I will try again by specifying the type instead of using a method (looking at the source of DataContractSerializer, it looks like this will work), however this is tricky for me in MonoTouch because I will need to use typeof rather than Type.GetType("fully.qualified.typename").
Is there another solution? Workaround?
I did see this: How do you configure WCF known types programmatically?, however it seems MonoTouch is missing some of the required interfaces...
Thanks in advance

It looks firmly like either a bug in MonoTouch or a regression in Mono (I don't have an environment to test in Mono 2.10 on the desktop right now)
Worked around the issue by restructuring my projects and using
[KnownType(typeof(MyDerivedType))]
On the base type. This works just fine on MonoTouch 4.0.

Related

Windows Phone 8 and REST

I need to use REST web services under Windows Phone 8, but there is no System.ServiceModel.Web namespace in WP framework so I cannot use WebGet and WebInvoke annotations.
How am I supposed to do HTTP GET calls using WCF? Is WCF supported in Windows Phone 8?
EDIT
The only approach that I can think off now is to use direct approach using HttpWebRequest and handle everything by hand. I wanted to avoid this since this approach brings too much boilerplate code inside the production code. This is why WCF is here. I can't even use RealProxy to implement some basic facilities myself since it's not supported either. The only solution that brings some automation and removes boilerplate code is to use dynamic and DLR features with DynamicObject, that will resolve the calls in runtime, but this will strip me of from static compiler checking.
Why did Microsoft remove crucial parts of framework from WP8? I don't get it.
Why did Microsoft remove crucial parts of framework from WP8?
They were never present. Such APIs would be considered features. And with frameworks like RestSharp there's little need to trouble yourself with writing anything by hand.

Monotouch/WCF Error on iPhone Hardware

I created a WCF Client on Monotouch with the Silverlight SLSvcUtil.exe Tool similar to http://docs.xamarin.com/ios/tutorials/Working_with_Web_Services#Consuming_WCF_Services.
On the Simulator everything works fine but when i started it on an iPhone 4S i got the error :
Attempting to JIT compile method '(wrapper delegate-begin-invoke) :begin_invoke_IAsyncResult_this__TimeSpan_AsyncCallback_object (System.TimeSpan,System.AsyncCallback,object)' while running with --aot-only.
Any ideas?
I've called a few wcf services from monotouch without hitting any issues like this- so it can be done.
The jit error within monotouch normally indicates either that something has been removed by the linker, or that some virtual abstract method has been called (at least in my recent experience).
Can you check the linker settings for your real iPhone project? Does the problem go away if you set it to -nolink - see http://docs.xamarin.com/ios/advanced_topics/linker
If that does help, then the next step is to take a look within the wcf generated file - can you find and post some more code about the callback method in this case?
Such issues are rarely linker related, even less if the signature is a wrapper. However you can confirm this easily with Stuart's instructions.
IMO you are likely hitting a case where the AOT compiler could not predict the required code. That can happens when mixing generics and value-types (and you have a TimeSpan in generated signature). This will work on the simulator (where the JIT is used) but not when AOT'ing the application.
The best way to solve such issue is to open a bug report at http://bugzilla.xamarin.com and attach a test case that allow us to replicate the issue.

Silverlight 4 and System.Runtime.Serialization

I have a Silverlight 4 project that contains some business objects. I added a Test project. One of the tests is to serialize the business objects from the Silverlight project using DataContractSerializer. To reference DataContractAttribute, I have to add a reference to System.Runtime.Serialization. However, there are different and apparently incompatible versions in the Silverlight runtime and in the .NET 4 Runtime of the Test project.
What's the best strategy to serialize objects in a Silverlight project such that they can be consumed by a standard .NET 4 runtime?
For this purpose i would use the DataContractJsonSerializer. This serializes your business objects to json. It it supported in Silverlight and .NET Framework since 3.5.
An example how to use the serializer can be found here.
If your objects allow it (regarding constraints mentioned below), try a serializer such as protobuf-bet or one of the other protocol buffer implementations.
http://code.google.com/p/protobuf-net/
They place some constraints on the implementation of your business objects (contracts, rather), for example that null and empty collections are considered equivalent to name one example.
I realized this morning that I had added a reference to a Silverlight project from a non-Silverlight project and VS warned about that not being a good idea, but still allowed the non-Silverlight project to run. Because a different version of System.Runtime.Serialization was referenced in each project, the unit test aborted with an inability to load that DLL.

Migrating project from .NET 2.0 to .NET 4.0

I'm migrating my solutions from .NET 2.0 to .NET 4.0. I open the solution file in VS 2010 and follow migration wizard.
While building the solution and comparing the changes with the server copy (since solution is under source control VSTS), I surprisingly found that
1) the public classes were changed to 'internal' and
2) the 'public static' properties were changed to 'internal static'
It gave me problem because the output assembly is referred in other projects.
Can someone explain why does it happen? Also, are there any other such changes taking place behind the scene? Or Am I doing something wrong ?
(Note: I have ReSharper 5.1 installed but I think it has no place in this case.)
ReSharper may have given the suggestion to make methods static for those methods which are not using any instance variable or method within it. This is what I have observed.
Similarly, if the methods are not accessed from any other project/assembly, it may give you suggestion to make them internal.

NetDataContractSerializer equivalent for Silverlight?

Has anyone developed or know of a place where I could find a Silverlight implementation of NetDataContractSerializer?
This WCF and Silverlight Comparison states that Silverlight does not ship with an implementation of NetDataContractSerializer.
I'm hoping to avoid developing one myself.
Thanks!
I might be wrong, but I think the Silverlight version of CSLA includes some form of binary serializer. It's not compatible with NetDataContractSerializer I think, so you'll need to use it on both the server and client (CSLA comes in a Windows flavour too). You can get the full framework (with source) for free at http://www.lhotka.net/cslalight/