简体   繁体   English

尝试序列化参数http://tempuri.org/:callback时发生错误

[英]There was an error while trying to serialize parameter http://tempuri.org/:callback

I have an asynchronous WCF server (I've derived my own clientbase class) and I call this from my client application. 我有一个异步WCF服务器(派生了自己的clientbase类),并从客户端应用程序中调用了它。

However, when this method is called: 但是,当调用此方法时:

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

I get this exception: 我得到这个例外:

{"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."} {“尝试序列化参数http://tempuri.org/:callback时发生错误。InnerException消息是'Type'System.DelegateSerializationHolder + DelegateEntry',数据协定名称为'DelegateSerializationHolder.DelegateEntry:http:// schemas。 datacontract.org/2004/07/System'。将任何静态未知的类型添加到已知类型的列表中-例如,通过使用KnownTypeAttribute属性或将它们添加到传递给DataContractSerializer的已知类型的列表中。 。请参阅InnerException了解更多详细信息。“}

If I had my own class with its own properties, this exception would make sense but this is a standard .NET type (AsyncCallback I believe the exception is complaining about). 如果我有自己的具有自己属性的类,则此异常有意义,但这是标准的.NET类型(我相信该异常正在抱怨AsyncCallback)。 A code sample of what I am trying to do doesn't have this problem (and I changed that to the same type of binding I am using - named pipes). 我尝试执行的代码示例没有此问题(我将其更改为我正在使用的相同类型的绑定-命名管道)。

What is wrong? 怎么了?

You likely forgot to add the (AsyncPattern = true) property in the [OperationContract] attribute. 您可能忘记了在[OperationContract]属性中添加(AsyncPattern = true)属性。 The example below shows two clients, one (wrong) with the exact error you're seeing, one (correct) which works. 下面的示例显示了两个客户端,一个(错误)具有您看到的确切错误,一个(正确)有效。 The only difference is the AsyncPattern = true in the operation contract. 唯一的区别是操作协定中的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();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试序列化参数http://tempuri.org/:exception时发生错误。 在C#中 - There was an error while trying to serialize parameter http://tempuri.org/:exception. in C# Web服务中的http://tempuri.org/? - http //tempuri.org/ in Web services? 将Xml反序列化为对象时出错-{ <string xmlns='http://tempuri.org/'> 没想到。} - Error Deserializing Xml to Object - {<string xmlns='http://tempuri.org/'> was not expected.} 在某些地方使用了[WebService(Namespace =“ http://tempuri.org/”)]? - does [WebService(Namespace = “http://tempuri.org/”)] is used some where? 如何隐藏名称空间 <string xmlns=“http://tempuri.org/”> 在网络服务? - How to hide the namespace <string xmlns=“http://tempuri.org/”> in webservice? 服务需要tempuri.org的名称空间 - Service expects namespace of tempuri.org 为什么WCF服务代理文件包含http://tempuri.org? - Why does WCF service proxy flie contain http://tempuri.org? ASMX Web服务 - “此Web服务使用http://tempuri.org/作为其默认命名空间。”消息 - 但它不应该是 - ASMX Web Service - “This web service is using http://tempuri.org/ as its default namespace.” message - but it shouldn't be 如何在WCF 4.5中完全更改tempuri.org命名空间 - How to completely change tempuri.org namespace in WCF 4.5 尝试序列化参数时出错 - There was an error while trying to serialize parameter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM