これを機能させようとして大きな問題が発生しました。文字列とオブジェクトで構成される構造体であるメッセージを送信しようとする、コールバックを使用した WCF シナリオがあります。オブジェクトをシリアル化できないようです。
そのデータを送信する関数を呼び出そうとすると、次のエラーが発生します。
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();
}
}
}
}
オブジェクトでこの問題が発生するのはなぜですか? 彼らは私が知っている匿名ですが、Types Supported by the Data Contract Serializer
MSDNから!( 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.
ので、なぜできないのかわかりません。
ありがとう、助けてくれてありがとう