WCF service returning an array of dictionary

后端 未结 3 1340
抹茶落季
抹茶落季 2020-12-09 22:33

I\'ve been trying to use a SilverLight client to call an ASP.Net WCF service that would return a Dictionary. That worked fine when the val

3条回答
  •  鱼传尺愫
    2020-12-09 22:59

    First of all you should configure WCF tracing in app cofig file that can help you to understand what happends under the hood of service communications. In this case you can easily get all errors that occurs during communication process.

    Now, lets try to solve your problem. I almost confident that problem in in known types. In service-oriented world you should manually define all concrete types that can participate in service contract, because DataContractSerializer doesn't provide such information in serialized objects.

    In this case it mean, that you should do following:

    [ServiceKnownType(typeof(string))] 
    [ServiceKnownType(typeof(Guid))]
    [ServiceKnownType(typeof(int))] // but I think DataContractSerializer can deal himself with primitives
    [ServiceKnownType(typeof(YourClass))] //UserDefined types you should add manually
    public Dictionary GetObject()
    

    Also I recommended to you do not use object in service contract because it VERY error prone. Consider, that later you or one of your colleagues modifies one line of code:

    new Dictionary()
    {
     { "pty1", 4 },
     { "pty2", Guid.NewGuid() },
     { "pty3", new SomeClass() }, //OOPS!!!
    }
    

    In this case, when your service tries to return this Dictionary you encounter runtime failure because DataContractSerializer doesn't expect SomeClass in this contract.

    One way to solve this problem is to create separate type:

    [DataContract]
    [KnownType(typeof(Guid))]
    [KnownType(typeof(SomeClass1))]
    [KnownType(typeof(SomeClass2))]
    public class MyType
    {
      private MyType(object obj) {
         Object = obj;
      }
    
      public static MyType FromSomeClass1(SomeClass1 c1) {
        return new MyType(c1);
      }
    
      public static MyType FromSomeClass2(SomeClass2 c2) {
        return new MyType(c2);
      }
    
      public static MyType FromGuid(Guid guid) {
        return new MyType(guid);
      }
    
      [DataMember]
      public object Object { get; private set; }
    }
    

    In this case if you want to add some new type, you should add factory method and KnownTypeAttribute (this approach more verbose, but less error prone).

    However, if your client and service written on WCF, you can sacrifice main service-oriented principles and use NetDataContractSerializer instead DataContractSerializer. NetDataContractSerializer is designed to complement DataContractSerializer. You can serialize a type using NetDataContractSerializer and deserialize with DataContractSerializer. But NetDataContractSerializer includes CLR type information in the serialized XML, whereas the DataContractSerializer does not. Therefore, the NetDataContractSerializer can used in serialization and deserialization with any CLR types without any KnownTypes stuff.

提交回复
热议问题