c# - 尝试序列化参数 http ://tempuri. org/:callback 时出错

标签 c# wcf

我有一个异步 WCF 服务器(我派生了自己的客户端基类),我从客户端应用程序调用它。

但是,当调用该方法时:

public IAsyncResult Beginxxx(string path, AsyncCallback callback, object state)  
{
    return Channel.Beginxxx(path, callback, state); 
}

我得到这个异常:

{"There was an error while trying to serialize parameter http://tempuri.org/:callback. The InnerException message was 'Type 'System.DelegateSerializationHolder+DelegateEntry' with data contract name 'DelegateSerializationHolder.DelegateEntry:http://schemas.datacontract.org/2004/07/System' is not expected. Add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.'. Please see InnerException for more details."}

如果我有自己的类,并且有自己的属性,则此异常是有意义的,但这是标准的 .NET 类型(AsyncCallback 我相信该异常正在提示)。我正在尝试做的代码示例没有这个问题(我将其更改为我正在使用的相同类型的绑定(bind) - 命名管道)。

出了什么问题?

最佳答案

您可能忘记在 [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();
    }
}

关于c# - 尝试序列化参数 http ://tempuri. org/:callback 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5999249/

相关文章:

c# - 如何使用其他构造函数的参数在类中检索 HubContext (SignalR)?

c# - 多线程计算从多个文件加载数据。如何在 C# 中使用和同步 StreamReader?

c# - 如何在 Azure 辅助角色托管的 WCF 服务中的公共(public)域名上发布 WSDL?

c# - 插入后如何取回标识值

c# - 使用System.Net.Mail.MailMessage时设置 "From"地址?

c# - DataReader 获取类型规范 SQL Server

c# - 带身份验证的 WCF 流

c# - 我如何调查 WCF 通过 GET 给出 400 个错误请求?

c# - WCF 窗口凭据

wcf - 在 WCF 中使用 Schema 进行消息验证