为什么在调用远程对象时不能使用异步函数?

时间:2019-04-01 16:07:25

标签: c# .net

我构造了一个远程对象,其中包含一个异步函数,该异步函数在客户端调用它时会引发异常。

异常信息:

System.Runtime.Serialization.SerializationException:程序集“ mscorlib,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089”中的“ System.Threading.Tasks.Task`1 [[System.Boolean,mscorlib,Version” “ = 4.0.0.0,文化=中性,PublicKeyToken = b77a5c561934e089]]”未标记为可序列化。”

[Serializable]
public class ClassA
{
    public String Str = "Hello World";
}
[Serializable]
public class DataObj : MarshalByRefObject
{
    public ClassA A = new ClassA();

    public override object InitializeLifetimeService()
    {
        return null;
    }


    public async Task<bool> DoSth()
    {
        await Task.Delay(1);
        Console.WriteLine("aaa");

        return false;
    }
}
public class Server
{
    static void Main(String[] args)
    {
        HttpServerChannel channel = new HttpServerChannel("A",10000);
        ChannelServices.RegisterChannel(channel, false);
        RemotingConfiguration.RegisterWellKnownServiceType(typeof(DataObj), "DataObj", WellKnownObjectMode.Singleton);

        Console.Read();
    }
}
class Client
{
    static void Main(string[] args)
    {
        HttpClientChannel channel = new HttpClientChannel();
        ChannelServices.RegisterChannel(channel, false);
        DataObj obj = (DataObj)Activator.GetObject(typeof(DataObj), @"http://127.0.0.1:10000/DataObj");

        //An exception is thrown when the client calls
        obj.DoSth();
        Console.Read();
    }
}

0 个答案:

没有答案