WCF System.Object的序列化 [英] WCF System.Object Serialization

查看:232
本文介绍了WCF System.Object的序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要求,在我所使用System.Object的在WCF参数。因为它是不可序列化,我越来越不支持该消息的操作,因为它使用System.Object的。任何解决这一问题的。

I have a requirement in which i have to use System.Object as a parameter in WCF. As it is not serializable,I am getting the message as the operation is not supported as it uses System.Object. Any solution to this problem.

推荐答案

在通过网络发送的消息,WCF在默认情况下,只会序列化有什么足以让信息传达,即合同的成员。如果您的消息采用一个对象作为一个参数,额外的信息需要被发送的导线类型的信息。如果您使用的同一组件的在客户端和服务器上,您可以使用NetDataContractSerializer(而不是默认的DataContractSerializer)的服务器(和客户端),他们将能够交换任意目的,如下面的code。但是,正如@MarcGravell提到的,这可能不是WCF的最佳用法...

When sending messages over the wire, WCF by default will only serialize what's enough to get the message across, i.e., the members of the contracts. 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. But, as @MarcGravell mentioned, this may not be the best usage of WCF...

在code,使 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();
    }
}

这篇关于WCF System.Object的序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆