TcpChannel 使用 写操作失败,请参见内部异常

namespace ClassRef
{
    /// <summary>
    /// 代理
    /// </summary>
   public  class Proxy: MarshalByRefObject
   {
       public void SayHello(Consignor consignor)
       {
           Console.WriteLine("正在执行委托任务。。。");
           consignor.SayHello();
           Console.WriteLine("执行完成");
       }
        
    }
}

 

namespace ClassRef
{
    /// <summary>
    /// 委托人
    /// </summary>
    public class Consignor : MarshalByRefObject
    {
        public void SayHello()
        {
            Console.WriteLine("Hello");
        }
    }
}

 

namespace ServerForCode
{
    class Program
    {
        static void Main(string[] args)
        {
            //设置安全限制
            BinaryServerFormatterSinkProvider provider = new BinaryServerFormatterSinkProvider();
            provider.TypeFilterLevel = TypeFilterLevel.Full;
            IDictionary props = new Hashtable();
            props["port"] = 8085;
            //注册信道
            TcpChannel chan = new TcpChannel(props, null, provider);
            ChannelServices.RegisterChannel(chan, true);

            //设置服务器提供的注册类型Proxy
            RemotingConfiguration.RegisterWellKnownServiceType(
    typeof(Proxy),
    "RemoteTestRef",
    WellKnownObjectMode.SingleCall);

            Console.WriteLine("Press <ENTER> to exit...");
            Console.ReadLine();
        }
    }
}

 

namespace ClientForCode
{
    class Program
    {
        static void Main(string[] args)
        {
            //注册信道
            TcpChannel chan = new TcpChannel(8087);
            ChannelServices.RegisterChannel(chan, true);

            //将Proxy注册为可在服务器激活的类型
            RemotingConfiguration.RegisterWellKnownClientType(typeof(Proxy), "tcp://localhost:8085/RemoteTestRef");

            //声明服务器对象
            Proxy proxy = new Proxy();

            //声明以引用方式传到服务器的对象
            Consignor consignor = new Consignor();

            proxy.SayHello(consignor);

            Console.WriteLine("Press <ENTER> to exit...");
            Console.ReadLine();

        }
    }
}

求助,客服端这边运行总是一次成功,一次失败的。异常信息是“写操作失败,请参见内部异常。”。是哪里代码写错了。

三国纷争
浏览 514回答 2
2回答

qq_花开花谢_0

将你的代码做了下整理分成三个程序集,Client ,Server ,Contract 三部分,再把解决方案设置成多启动项目,运行了几次,没出现问题, 不知道你是怎么架设项目的。主要有两点、服务器端要确保启动、端口可用、通信完后要关闭连接。

慕桂英546537

你的客户端没有关闭TcpChannel吗? 把你的客户端异常的StackTrace贴出来。
打开App,查看更多内容
随时随地看视频慕课网APP