简体   繁体   English

WCF System.Object序列化

[英]WCF System.Object Serialization

I have a requirement in which i have to use System.Object as a parameter in WCF. 我有一个要求,我必须使用System.Object作为WCF中的参数。 As it is not serializable,I am getting the message as the operation is not supported as it uses System.Object. 由于它不可序列化,因此我收到消息,因为它使用System.Object,因此不支持该操作。 Any solution to this problem. 任何解决此问题的方法。

When sending messages over the wire, WCF by default will only serialize what's enough to get the message across, ie, the members of the contracts. 通过有线发送消息时,默认情况下,WCF将仅序列化足以使消息通过的内容,即合同的成员。 If your message takes an "object" as a parameter, extra information needs to be sent over the wire with the type information. 如果您的消息采用“对象”作为参数,则需要通过电线发送额外的信息以及类型信息。 If you use the same assemblies on the client and the server, you can use the NetDataContractSerializer (instead of the default DataContractSerializer) in the server (and the client), and they'll be able to exchange arbitrary objects, as shown in the code below. 如果在客户端和服务器上使用相同的程序集 ,则可以在服务器(和客户端)中使用NetDataContractSerializer(而不是默认的DataContractSerializer),它们将能够交换任意对象,如代码所示下面。 But, as @MarcGravell mentioned, this may not be the best usage of WCF... 但是,正如@MarcGravell所提到的,这可能不是WCF的最佳用法。

The code to enable the NetDataContractSerializer : 启用NetDataContractSerializer的代码:

public class Post_8b2c7ad7_b1c3_410b_b907_f25cee637110
{
    public class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public override string ToString()
        {
            return string.Format("Person[Name={0},Age={1}]", Name, Age);
        }
    }
    [ServiceContract]
    public interface ITest
    {
        [OperationContract]
        object Echo(object obj);
    }
    public class Service : ITest
    {
        public object Echo(object obj)
        {
            return obj;
        }
    }
    public class ReplaceSerializerOperationBehavior : DataContractSerializerOperationBehavior
    {
        public ReplaceSerializerOperationBehavior(OperationDescription operation)
            : base(operation)
        {
        }
        public override XmlObjectSerializer CreateSerializer(Type type, string name, string ns, IList<Type> knownTypes)
        {
            return new NetDataContractSerializer(name, ns);
        }
        public override XmlObjectSerializer CreateSerializer(Type type, XmlDictionaryString name, XmlDictionaryString ns, IList<Type> knownTypes)
        {
            return new NetDataContractSerializer(name, ns);
        }
        public static void ReplaceSerializer(ServiceEndpoint endpoint)
        {
            foreach (var operation in endpoint.Contract.Operations)
            {
                for (int i = 0; i < operation.Behaviors.Count; i++)
                {
                    if (operation.Behaviors[i] is DataContractSerializerOperationBehavior)
                    {
                        operation.Behaviors[i] = new ReplaceSerializerOperationBehavior(operation);
                        break;
                    }
                }
            }
        }
    }
    public static void Test()
    {
        string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
        ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress));
        var endpoint = host.AddServiceEndpoint(typeof(ITest), new BasicHttpBinding(), "");
        ReplaceSerializerOperationBehavior.ReplaceSerializer(endpoint);
        host.Open();
        Console.WriteLine("Host opened");

        ChannelFactory<ITest> factory = new ChannelFactory<ITest>(new BasicHttpBinding(), new EndpointAddress(baseAddress));
        ReplaceSerializerOperationBehavior.ReplaceSerializer(factory.Endpoint);
        ITest proxy = factory.CreateChannel();
        Console.WriteLine(proxy.Echo("Hello"));
        Console.WriteLine(proxy.Echo(123.456));
        Console.WriteLine(proxy.Echo(new Uri("http://tempuri.org")));
        Console.WriteLine(proxy.Echo(new Person { Name = "John Doe", Age = 33 }));

        ((IClientChannel)proxy).Close();
        factory.Close();

        Console.Write("Press ENTER to close the host");
        Console.ReadLine();
        host.Close();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM