WCF通过管道通信发送对象

时间:2013-07-25 12:40:55

标签: c# wcf object serialization anonymous-objects

我试图让这项工作成为一个大问题。 我有一个带有回调的WCF场景,试图发送一个Message,它是一个由字符串和一个对象组成的结构。 好像对象无法序列化。

尝试调用发送该数据的函数时出现以下错误:

Type '<>f__AnonymousType01[System.String]' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. See the Microsoft .NET Framework documentation for other supported types.

我的客户端代码:

using System;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Runtime.Serialization;
using System.IO;

namespace WCFClient
{
    [DataContract]
    public struct Message
    {
        private string messageType;
        private object param;

        public Message(string _messageType, object _param)
        {
            // TODO: Complete member initialization
            this.messageType = _messageType;
            this.param = _param;
        }
        [DataMember]
        public string type { get { return messageType; } set { messageType = type; } }
        [DataMember]
        public object obj { get { return param; } set { param = obj; } }
    }

    [ServiceContract(SessionMode = SessionMode.Required,
      CallbackContract = typeof(ICallbacks))]
    public interface IMessageHandler
    {
        [OperationContract]
        void HandleMessage(string value);
    }

    public interface ICallbacks
    {
        [OperationContract(IsOneWay = true)]
        void QueuePaths_Callback(string cPath, string EPath, string RPath, string IPath, string OPath);
        [OperationContract(IsOneWay = true)]
        void MyCallbackFunction(Message msg);
    }

    public class Callbacks : ICallbacks
    {
        public void QueuePaths_Callback(string cPath, string EPath, string RPath, string IPath, string OPath)
        {
            Console.WriteLine("Callback Received: ");
        }
        public void MyCallbackFunction(Message msg)
        {
            Console.WriteLine("Callback Received: ");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Callbacks myCallbacks = new Callbacks();

            DuplexChannelFactory<IMessageHandler> pipeFactory =
               new DuplexChannelFactory<IMessageHandler>(
                  myCallbacks,
                  new NetNamedPipeBinding(),
                  new EndpointAddress(
                     "net.pipe://localhost/PipeReverse"));

            IMessageHandler pipeProxy =
              pipeFactory.CreateChannel();

            while (true)
            {
                string str = Console.ReadLine(); 
                pipeProxy.HandleMessage(str);//send the type for example
            }
        }
    }
}

我的服务器代码:

using System;
using System.ServiceModel;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Text;

namespace WCFServer
{
    [DataContract]
    public struct Message
    {
        private string messageType;
        private object param;

        public Message(string _messageType, object _param)
        {
            // TODO: Complete member initialization
            this.messageType = _messageType;
            this.param = _param;
        }
        [DataMember]
        public string type { get { return messageType; } set { messageType = type; } }
        [DataMember]
        public object obj { get { return param; } set { param = obj; } }
    }
    [ServiceContract(SessionMode = SessionMode.Required,
       CallbackContract = typeof(ICallbacks))]
    public interface IMessageHandler
    {
        [OperationContract]
        void HandleMessage(string value);
    }

    public interface ICallbacks
    {
        [OperationContract(IsOneWay = true)]
        void QueuePaths_Callback(string cPath, string EPath, string RPath, string IPath, string OPath);
        [OperationContract(IsOneWay = true)]
        void MyCallbackFunction(Message msg);
    }

    public class StringReverser : IMessageHandler
    {
        public void HandleMessage(string value)//handle the type and do the request
        {
            ICallbacks callbacks = OperationContext.Current.GetCallbackChannel<ICallbacks>();
            if (value.CompareTo("1") == 0)
            {
                callbacks.QueuePaths_Callback("path1", "path2", "path3", "path4", "path5");

            }
            else
            {
                Console.WriteLine("In the else");
                //BinaryFormatter bformatter = new BinaryFormatter();
                //MemoryStream stream = new MemoryStream();

                //bformatter.Serialize(stream, new Message(value, new { PathCompleted = "path" }));

                callbacks.MyCallbackFunction(new Message(value, new { PathCompleted = "path" }));
            }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            using (ServiceHost host = new ServiceHost(
              typeof(StringReverser),
              new Uri[]{
          new Uri("net.pipe://localhost")
        }))
            {

                host.AddServiceEndpoint(typeof(IMessageHandler),
                  new NetNamedPipeBinding(),
                  "PipeReverse");

                host.Open();

                Console.WriteLine("Service is available. " +
                  "Press <ENTER> to exit.");
                Console.ReadLine();

                host.Close();
            }
        }
    }
}

为什么我的对象有这个问题?他们是匿名的我知道但是在MSDN的Types Supported by the Data Contract Serializer中!(http://msdn.microsoft.com/en-us/library/ms731923.aspx)他们说.NET Framework primitive types. The following types built into the .NET Framework can all be serialized and are considered to be primitive types: Byte, SByte, Int16, Int32, Int64, UInt16, UInt32, UInt64, Single, Double, Boolean, Char, Decimal, Object, and String.所以我不知道为什么我不能。

谢谢,感谢您的帮助

0 个答案:

没有答案