54

My client/server application is using WCF for communication, which has been great. However one shortcoming of the current architecture is that I must use known type configuration for certain transmitted types. I'm using an in-house Pub/Sub mechanism and this requirement is unavoidable.

The problem is that it's easy to forget to add the known type, and if you do, WCF fails silently with few clues as to what's going wrong.

In my application, I know the set of types that are going to be sent. I would like to perform the configuration programmatically, rather than declaratively through the App.config file which currently contains something like this:

<system.runtime.serialization>
  <dataContractSerializer>
    <declaredTypes>
      <add type="MyProject.MyParent, MyProjectAssembly">
        <knownType type="MyProject.MyChild1, MyProjectAssembly"/>
        <knownType type="MyProject.MyChild2, MyProjectAssembly"/>
        <knownType type="MyProject.MyChild3, MyProjectAssembly"/>
        <knownType type="MyProject.MyChild4, MyProjectAssembly"/>
        <knownType type="MyProject.MyChild5, MyProjectAssembly"/>
      </add>
    </declaredTypes>
  </dataContractSerializer>
</system.runtime.serialization>

Instead, I'd like to do something like this:

foreach (Type type in _transmittedTypes)
{
    // How would I write this method?
    AddKnownType(typeof(MyParent), type);
}

Can someone please explain how I might do this?

EDIT Please understand that I'm trying to set the known types dynamically at run time rather than declaratively in config or using attributes in the source code.

This is basically a question about the WCF API, not a style question.

EDIT 2 This MSDN page page states:

You can also add types to the ReadOnlyCollection, accessed through the KnownTypes property of the DataContractSerializer.

Unfortunately that's all it says and it doesn't make terribly much sense given that KnownTypes is a readonly property, and the property's value is a ReadOnlyCollection.

abatishchev
  • 92,232
  • 78
  • 284
  • 421
Drew Noakes
  • 266,361
  • 143
  • 616
  • 705
  • 1
    On your edit 2: I guess they mean you can pass in extra known types through the DataContractSerializer constructor. That won't help much in your case though, as WCF makes its serializer itself. – Kurt Schelfthout Apr 21 '09 at 15:07

5 Answers5

71

Add [ServiceKnownType] to your [ServiceContract] interface:

[ServiceKnownType("GetKnownTypes", typeof(KnownTypesProvider))]

then create a class called KnownTypesProvider:

internal static class KnownTypesProvider
{
    public static IEnumerable<Type> GetKnownTypes(ICustomAttributeProvider provider)
    {
         // collect and pass back the list of known types
    }
}

and then you can pass back whatever types you need.

Drew Noakes
  • 266,361
  • 143
  • 616
  • 705
Miki Watts
  • 1,656
  • 1
  • 15
  • 26
  • Perhaps my question wasn't clear. This is not 'programmatically' -- it's still declaratively. I need the ability to *add* known types, not *get* them, at run time. – Drew Noakes Apr 21 '09 at 10:38
  • 7
    @ Drew Noakes - Huh? In the GetKnownTypes method, which is just code, you can return the known types at that point in time. The attribute is just there to tell WCF what method to call to get the known types. This is as programmatically as you can have it in WCF, I think (short of programmatically editing the config file and reloading it). – Kurt Schelfthout Apr 21 '09 at 13:53
  • Agreed with Miki and Kurt, this is as good as you're going to get in WCF. – Rob Apr 21 '09 at 13:59
  • 4
    I apologise. This is actually a valid answer, I just misread it as a means of accessing the existing known types. Looking at it again I've no idea what I was thinking. – Drew Noakes Apr 21 '09 at 14:00
  • @Drew: this answers shows exactly that. The types returned by the GetKnownTypes method are added to the list of known types supported by the service. You can use reflection to build up that type list, based on whatever criteria suits you. Keep in mind that any *new* types require regenerating the client proxy in order to use them. I don't think it's possible to work around this. – Dan C. Apr 21 '09 at 14:02
  • @DanC - The list of known types must be available on the sender's side as well, so presumably the service host must be recreated for *new* types too. – Drew Noakes Apr 21 '09 at 14:35
  • Correct (however you can work around that, provided you can "detect" when you get a new type and recreate the host). At any rate, I used the above pattern for loading the types at service startup (just wanted to get rid of having to specify all derived classes declaratively). If you want to dynamically add types while the service is running, then you're probably facing a lot more problems. – Dan C. Apr 21 '09 at 15:27
  • 1
    Just wanted to chime in again and say thanks very much for this answer. I've only just gotten around to using this and it worked perfectly first time. I edited your answer slightly to widen the return type to `IEnumerable` and indicate that the class can be internal and still work ok. Thanks again. – Drew Noakes Aug 06 '09 at 11:33
  • 4
    What is the purpose of parameter "ICustomAttributeProvider provider"? – Michael Freidgeim Jun 23 '11 at 03:35
  • @MichaelFreidgeim The `ICustomAttributeProvider` parameter is part of the method signature. You don't have to use the parameter value, but the parameter must be present for Reflection to find the method on the class specified in the `ServiceKnownType` attribute. In contrast, specifying a method name in the `KnownType` attribute requires an empty parameters list. – Suncat2000 Dec 26 '18 at 15:20
18

There are 2 additional ways to solve your problem:

I. Use KnownTypeAttribute(string):

[DataContract]
[KnownType("GetKnownTypes")]
public abstract class MyParent
{
    static IEnumerable<Type> GetKnownTypes()
    {
        return new Type[] { typeof(MyChild1), typeof(MyChild2), typeof(MyChild3) };
    }
}

II. Use constructor DataContractSerializer

[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class | 
                AttributeTargets.Interface)]
public class MyHierarchyKnownTypeAttribute : Attribute, IOperationBehavior, IServiceBehavior, IContractBehavior
{
    private void IOperationBehavior.AddBindingParameters(
            OperationDescription description, 
            BindingParameterCollection parameters)
    {
    }

    void IOperationBehavior.ApplyClientBehavior(
            OperationDescription description, 
            ClientOperation proxy)
    {
        ReplaceDataContractSerializerOperationBehavior(description);
    }

    private void IOperationBehavior.ApplyDispatchBehavior(
            OperationDescription description, 
            DispatchOperation dispatch)
    {
        ReplaceDataContractSerializerOperationBehavior(description);
    }

    private void IOperationBehavior.Validate(OperationDescription description)
    {
    }

    private void IServiceBehavior.AddBindingParameters(
          ServiceDescription serviceDescription,
          ServiceHostBase serviceHostBase,
          System.Collections.ObjectModel.Collection<ServiceEndpoint> endpoints,
          BindingParameterCollection bindingParameters)
    {
        ReplaceDataContractSerializerOperationBehavior(serviceDescription);
    }

    private void IServiceBehavior.ApplyDispatchBehavior(
            ServiceDescription serviceDescription, 
            ServiceHostBase serviceHostBase)
    {
        ReplaceDataContractSerializerOperationBehavior(serviceDescription);
    }

    private void IServiceBehavior.Validate(ServiceDescription serviceDescription,
            ServiceHostBase serviceHostBase)
    {
    }

    private void IContractBehavior.AddBindingParameters(
            ContractDescription contractDescription,
            ServiceEndpoint endpoint, 
            BindingParameterCollection bindingParameters)
    {
    }

    private void IContractBehavior.ApplyClientBehavior(
            ContractDescription contractDescription,
            ServiceEndpoint endpoint, ClientRuntime clientRuntime)
    {
        ReplaceDataContractSerializerOperationBehavior(contractDescription);
    }

    private void IContractBehavior.ApplyDispatchBehavior(
            ContractDescription contractDescription,
            ServiceEndpoint endpoint, DispatchRuntime dispatchRuntime)
    {
        ReplaceDataContractSerializerOperationBehavior(contractDescription);
    }

    private void IContractBehavior.Validate(ContractDescription contractDescription,
            ServiceEndpoint endpoint)
    {
    }    

    private static void ReplaceDataContractSerializerOperationBehavior(
            ServiceDescription description)
    {
        foreach (var endpoint in description.Endpoints)
        {
            ReplaceDataContractSerializerOperationBehavior(endpoint);
        }
    }

    private static void ReplaceDataContractSerializerOperationBehavior(
            ContractDescription description)
    {
        foreach (var operation in description.Operations)
        {
            ReplaceDataContractSerializerOperationBehavior(operation);
        }
    }

    private static void ReplaceDataContractSerializerOperationBehavior(
            ServiceEndpoint endpoint)
    {
        // ignore mex
        if (endpoint.Contract.ContractType == typeof(IMetadataExchange))
        {
            return;
        }
        ReplaceDataContractSerializerOperationBehavior(endpoint.Contract);
    }

    private static void ReplaceDataContractSerializerOperationBehavior(
            OperationDescription description)
    {
        var behavior = 
         description.Behaviors.Find<DataContractSerializerOperationBehavior>();
        if (behavior != null)
        {
            description.Behaviors.Remove(behavior);
            description.Behaviors.Add(
                new ShapeDataContractSerializerOperationBehavior(description));
        }
    }

    public class ShapeDataContractSerializerOperationBehavior 
            : DataContractSerializerOperationBehavior
    {
        public ShapeDataContractSerializerOperationBehavior(
                OperationDescription description)
            : base(description) { }

        public override XmlObjectSerializer CreateSerializer(Type type, 
                string name, string ns, IList<Type> knownTypes)
        {
            var shapeKnownTypes = 
                new List<Type> { typeof(Circle), typeof(Square) };
            return new DataContractSerializer(type, name, ns, shapeKnownTypes);
        }

        public override XmlObjectSerializer CreateSerializer(Type type, 
                XmlDictionaryString name, XmlDictionaryString ns, 
                IList<Type> knownTypes)
        {
            //All magic here!
            var knownTypes = 
                new List<Type> { typeof(MyChild1), typeof(MyChild2), typeof(MyChild3) };
            return new DataContractSerializer(type, name, ns, knownTypes);
        }
    }
}

[ServiceContract()]
[MyHierarchyKnownTypeAttribute]
public interface IService {...}

NOTE: You must use this attribute on both sides: client side and service side!

abatishchev
  • 92,232
  • 78
  • 284
  • 421
Sergey Teplyakov
  • 10,737
  • 29
  • 45
  • Excellent! I've been looking for this code for maybe like 8 years! Unfortunately, I'm not sure if it's going to work on all the platforms I want to implement it for. – Christian Findlay Sep 09 '16 at 01:21
13

I needed to do this to allow inheritance to work properly. I didn't want to have to maintain the list of derived types.

 [KnownType("GetKnownTypes")]
 public abstract class BaseOperationResponse
 {

    public static Type[] GetKnownTypes()
    {
        Type thisType = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType;
        return thisType.Assembly.GetTypes().Where(t => t.IsSubclassOf(thisType)).ToArray();
    }

I know the first line of the function is overkill but it just means I can paste it into any base class without modification.

matt burns
  • 22,440
  • 9
  • 91
  • 102
Paul McNamara
  • 131
  • 1
  • 2
3

Web .Config

<applicationSettings>
<HostProcess.Properties.Settings>
<setting name="KnowTypes" serializeAs="Xml">
<value>
 <ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <string>a.AOrder,a</string>
   <string>b.BOrder,b</string>
   <string>c.COrder,c</string>
 </ArrayOfString>
</value>
</setting>
</HostProcess.Properties.Settings>

static class Helper
{
    public static IEnumerable<Type> GetKnownTypes(ICustomAttributeProvider provider)
    {
        System.Collections.Generic.List<System.Type> knownTypes =
        new System.Collections.Generic.List<System.Type>();
        // Add any types to include here.
        Properties.Settings.Default.KnowTypes.Cast<string>().ToList().ForEach(type =>
            {
                knownTypes.Add(Type.GetType(type));
            });

        return knownTypes;
    }
}


[ServiceContract]
[ServiceKnownType("GetKnownTypes", typeof(Helper))]
public interface IOrderProcessor
{
    [OperationContract]
    string ProcessOrder(Order order);
}

The Order is the abstract base class


[DataContract]
public abstract class Order
{
    public Order()
    {
        OrderDate = DateTime.Now;
    }
    [DataMember]
    public string OrderID { get; set; }
    [DataMember]
    public DateTime OrderDate { get; set; }
    [DataMember]
    public string FirstName { get; set; }
    [DataMember]
    public string LastName { get; set; }
}
Amit Bagga
  • 650
  • 3
  • 11
0

a bit overkill, but works and is kind of future proof

var knownTypes =
    AppDomain.CurrentDomain
    .GetAssemblies()
    .Where(a =>
    {
        var companyAttribute = a.GetCustomAttribute<AssemblyCompanyAttribute>();
        if (companyAttribute == null) return false;
        return companyAttribute.Company.ToLower().Contains("[YOUR COMPANY NAME]");
    })
    .SelectMany(a => a.GetTypes()).Where(t => t.IsSerializable && !t.IsGenericTypeDefinition);

var serializer = new DataContractSerializer(type, knownTypes);
aeroson
  • 839
  • 10
  • 16