WCF sending object through pipe communication

670 Views Asked by At

I got a big issue trying to make this work. I have a WCF scenario, with callbacks, trying to send a Message that is a struct made of a string and an object. Seems like the object can't be serialized.

I get the following error trying to call a function sending that data:

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.

The code for my CLIENT IS:

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
            }
        }
    }
}

And my SERVER code:

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();
            }
        }
    }
}

Why do I have this issue with objects? They are annonymous I know but in the Types Supported by the Data Contract Serializer from MSDN!(http://msdn.microsoft.com/en-us/library/ms731923.aspx) they say .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. so I don't know why I cant.

Thank you, I appreciate your help

0

There are 0 best solutions below