Debugging adding known types through config
-
29-10-2019 - |
Question
I've been going round this for a while now and I can't figure out why I can use the config to add references to known types, or how to go about debugging it. Any pointers might save my sanity.
I have a WCF service hosted in a windows service, and I have a config file for the service. In the config I have this:
<configuration>
...
<system.runtime.serialization>
<dataContractSerializer>
<declaredTypes>
<add type="My.Full.Interface.Name, My.Assembly.Name.With.No.dll">
<knownType type="My.Full.Implementation.Class.Name, My.Assembly.Name.With.No.dll"/>
</add>
</declaredTypes>
</dataContractSerializer>
</system.runtime.serialization>
...
</configuration>
I have tried adding the Version=1.0.0.0,Culture=neutral,PublicKeyToken=null
to both declarations, but I can't seem to make the data contract serializer pick them up.
I have verified that this class can be returned (by adding a [ServiceKnownType(typeof(My.Implementation.Class))]
) and that works, but I have another implementation that I want to use that is in anothet assembly that would create a circular reference if I added it, so can't use that.
How can I debug why the datacontract serializer is not finding my type? Or what is wrong with my declaration?
Solution 2
So it seems that my issue was that my base type is an interafce, and as KnownType attributes can only be applied to classes this is why it was not working.
I was able to work around it by using the suggestion in this answer
[ServiceKnownType ("GetKnownTypes", typeof (KnownTypesProvider))]
on the service inteface and then providing a class and method to provide the types:
internal static class KnownTypesProvider
{
public static IEnumerable<Type> GetKnownTypes (ICustomAttributeProvider provider)
{
return new[] {Type.GetType ("Assembly qualified type name")};
}
}
which also allowed resolution of types which are in different assemblies. Whilst this works it's not great so I'll probably end up going to a solution which uses configuration like in this answer, or by adding soem attribute to my classes and using that to identify known types.
OTHER TIPS
If you are looking for a way to debug your windows service you could try adding the line System.Diagnostics.Debugger.Break();
at service startup. I don't know whether this is sufficiently early to find out why the serialization infrastructure isn't picking up your dataContractSerializer.
I cannot see any errors in the <system.runtime.serialization>
element that you have posted. Have you investigated the rest of the exe.config file for semantic errors?