Note that there are some explanatory texts on larger screens.

plurals
  1. POWCF sending object through pipe communication
    primarykey
    data
    text
    <p>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.</p> <p>I get the following error trying to call a function sending that data:</p> <p><code>Type '&lt;&gt;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.</code></p> <p>The code for my CLIENT IS:</p> <pre><code>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&lt;IMessageHandler&gt; pipeFactory = new DuplexChannelFactory&lt;IMessageHandler&gt;( 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 } } } } </code></pre> <p>And my SERVER code:</p> <pre><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&lt;ICallbacks&gt;(); 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 &lt;ENTER&gt; to exit."); Console.ReadLine(); host.Close(); } } } } </code></pre> <p>Why do I have this issue with objects? They are annonymous I know but in the <code>Types Supported by the Data Contract Serializer</code> from MSDN!(<a href="http://msdn.microsoft.com/en-us/library/ms731923.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/ms731923.aspx</a>) they say <code>.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.</code> so I don't know why I cant.</p> <p>Thank you, I appreciate your help</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload