This question comes up again, and again. This wasn't an issue on .NET and Silverlight, but on every other platform since, I've never seen a way to specify known types without physically typing them in to your ServiceContract. This means that this list can't be altered dynamically at runtime. It's a problem in Xamarin, UWP, and probably other platforms. So, let's look at this.
Originally, one solution for this problem on .NET and Silverlight was to specify a method for getting the known types on ServiceKnownType like this:
[ServiceKnownType("GetKnownTypes", typeof(GetTypesHelper))]
This has always worked well on .NET and Silverlight, but it does not work on UWP, or Xamarin. I tried this today, and this is the error I get:
System.InvalidOperationException: ServiceKnownTypeAttribute specifies method GetKnownTypes in type Adapt.XivicClient.WCF.ServiceContracts.GetTypesHelper which does not exist. The method must be static and takes one parameter of type ICustomAttributeProvider
Of course, PCL, and .NET Standard libraries do not have a ICustomAttributeProvider class, so this can not be done. So, I tried this other possible solution: https://stackoverflow.com/a/2104482/1878141
This works by specifying a Service Behaviour. But, again, PCL, and Standard do not have a IServiceBehavior class, and neither does say Android.
I tried this code because I thought I could replace the DataContractSerializer, but I get a NotImplementedException on Android.
dataAccessServiceClient.Endpoint.EndpointBehaviors.Add(new XivicServicBehaviour());
public class XivicServicBehaviour : IEndpointBehavior
{
public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
{
}
public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
}
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
{
}
public void Validate(ServiceEndpoint endpoint)
{
}
}
So, what are our options?