C# 尝试序列化参数时出错http://tempuri.org/:callback
我有一个异步WCF服务器(我派生了自己的clientbase类),我从我的客户机应用程序调用它 但是,调用此方法时:C# 尝试序列化参数时出错http://tempuri.org/:callback,c#,wcf,C#,Wcf,我有一个异步WCF服务器(我派生了自己的clientbase类),我从我的客户机应用程序调用它 但是,调用此方法时: public IAsyncResult Beginxxx(string path, AsyncCallback callback, object state) { return Channel.Beginxxx(path, callback, state); } 我得到一个例外: {"尝试序列化参数时出错。InnerException消息为'Type'System
public IAsyncResult Beginxxx(string path, AsyncCallback callback, object state)
{
return Channel.Beginxxx(path, callback, state);
}
我得到一个例外:
{"尝试序列化参数时出错。InnerException消息为'Type'System.DelegateSerializationHolder+DelegateEntry',数据协定名称为'DelegateSerializationHolder.DelegateEntry:http://schemas.datacontract.org/2004/07/System'不应为。请将任何静态未知的类型添加到已知类型列表中-以供检查例如,使用KnownTypeAttribute属性或将它们添加到传递给DataContractSerializer的已知类型列表中。有关详细信息,请参阅InnerException。“}
如果我有自己的类和它自己的属性,这个异常是有意义的,但这是一个标准的.NET类型(我相信这个异常正在抱怨AsyncCallback)。我尝试执行的代码示例没有这个问题(我将它更改为我使用的相同类型的绑定-命名管道)
怎么了?您可能忘记在[OperationContract]属性中添加(AsyncPattern=true)属性。下面的示例显示了两个客户端,一个(错误)与您看到的错误完全一致,另一个(正确)有效。唯一的区别是操作契约中的AsyncPattern=true
public class StackOverflow_5999249_751090
{
[ServiceContract(Name = "ITest", Namespace = "")]
public interface ITest
{
[OperationContract]
string Echo(string path);
}
public class Service : ITest
{
public string Echo(string path) { return path; }
}
[ServiceContract(Name = "ITest", Namespace = "")]
public interface ITestClient_Wrong
{
[OperationContract]
IAsyncResult BeginEcho(string path, AsyncCallback callback, object state);
string EndEcho(IAsyncResult asyncResult);
}
[ServiceContract(Name = "ITest", Namespace = "")]
public interface ITestClient_Correct
{
[OperationContract(AsyncPattern = true)]
IAsyncResult BeginEcho(string path, AsyncCallback callback, object state);
string EndEcho(IAsyncResult asyncResult);
}
static void PrintException(Exception e)
{
int indent = 2;
while (e != null)
{
for (int i = 0; i < indent; i++)
{
Console.Write(' ');
}
Console.WriteLine("{0}: {1}", e.GetType().FullName, e.Message);
indent += 2;
e = e.InnerException;
}
}
public static void Test()
{
string baseAddress = "net.pipe://localhost/Service";
ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress));
host.AddServiceEndpoint(typeof(ITest), new NetNamedPipeBinding(), "");
host.Open();
Console.WriteLine("Host opened");
AutoResetEvent evt = new AutoResetEvent(false);
Console.WriteLine("Correct");
ChannelFactory<ITestClient_Correct> factory1 = new ChannelFactory<ITestClient_Correct>(new NetNamedPipeBinding(), new EndpointAddress(baseAddress));
ITestClient_Correct proxy1 = factory1.CreateChannel();
proxy1.BeginEcho("Hello", delegate(IAsyncResult ar)
{
Console.WriteLine("Result from correct: {0}", proxy1.EndEcho(ar));
evt.Set();
}, null);
evt.WaitOne();
Console.WriteLine("Wrong");
ChannelFactory<ITestClient_Wrong> factory2 = new ChannelFactory<ITestClient_Wrong>(new NetNamedPipeBinding(), new EndpointAddress(baseAddress));
ITestClient_Wrong proxy2 = factory2.CreateChannel();
try
{
proxy2.BeginEcho("Hello", delegate(IAsyncResult ar)
{
try
{
Console.WriteLine("Result from wrong: {0}", proxy2.EndEcho(ar));
}
catch (Exception e)
{
PrintException(e);
}
evt.Set();
}, null);
evt.WaitOne();
}
catch (Exception e2)
{
PrintException(e2);
}
Console.WriteLine("Press ENTER to close");
Console.ReadLine();
host.Close();
}
}
公共类StackOverflow_5999249_751090
{
[ServiceContract(Name=“ITest”,Namespace=”“)]
公共接口测试
{
[经营合同]
字符串回显(字符串路径);
}
公共类服务:ITest
{
公共字符串回显(字符串路径){返回路径;}
}
[ServiceContract(Name=“ITest”,Namespace=”“)]
公共接口ITestClient\u错误
{
[经营合同]
IAsyncResult BeginEcho(字符串路径、异步回调、对象状态);
字符串EndEcho(IAsyncResult asyncResult);
}
[ServiceContract(Name=“ITest”,Namespace=”“)]
公共接口ITestClient\u正确
{
[操作契约(AsyncPattern=true)]
IAsyncResult BeginEcho(字符串路径、异步回调、对象状态);
字符串EndEcho(IAsyncResult asyncResult);
}
静态无效打印异常(异常e)
{
int缩进=2;
while(e!=null)
{
对于(int i=0;i
我在看到你的帖子之前就意识到了这一点,无论如何,谢谢!