【问题标题】:Properties in Remoting远程处理中的属性
【发布时间】:2010-05-11 10:42:57
【问题描述】:

服务器:

Host h = new Host();
h.Name = "JARR!!";
TcpChannel channel = new TcpChannel(8080);
ChannelServices.RegisterChannel(channel);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(Host), "Server",
               WellKnownObjectMode.Singleton);

客户:

TcpChannel chan = new TcpChannel();
            ChannelServices.RegisterChannel(chan);
            remoteHost = (Host)Activator.GetObject(typeof(Host),
"tcp://127.0.0.1:8080/Server");

类:

[Serializable]
    public class Host: MarshalByRefObject
    {
        public string Name{get; set;}
        public Host(){}

        public Host(string n)
        {
            Name = n;
        }

        public override string ToString()
        {
            return Name;
        }
    }

连接正常,8080端口打开,客户端remoteHost不为空,而是remoteHost.Name == ""

为什么?

【问题讨论】:

    标签: c# .net properties remoting


    【解决方案1】:

    您需要将您的特定服务器实例 (h) 编组到通道中,否则将创建一个默认实例。

    System.Runtime.Remoting.RemotingServices.Marshal(...);

    【讨论】:

    • 插入 RemotingServices.Marshal(h, "Server");注册知名服务后。没有任何变化
    【解决方案2】:

    您需要修复您的类以执行实际代码以返回如图所示的属性,我添加了变量myHostName 字符串类型以用于Name 的属性

    [可序列化] 公共类主机:MarshalByRefObject { 私人字符串 myHostName; 公共字符串名称{ 获取{返回 this.myHostName; } 设置{ this.myHostName = 值; } } 公共主机(字符串 n) { 这个.myHostName = n; } 公共覆盖字符串 ToS​​tring() { 返回 this.myHostName; } }

    【讨论】:

    • 抛出连接异常
    • 再一次 remoteHost.Name == "" 我认为班级成员编组存在问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多