简体   繁体   English

无法在C#中通过.NET远程检索int属性

[英]Cannot retrieve an int property over .NET remoting in C#

I am having an issue with reading a property over a .NET remoting channel. 我在通过.NET远程通道读取属性时遇到问题。 Here is the beginning of my class: 这是我班的开始:

[Serializable]
public class MachineID
{
    private string mySystemDeviceSerial = null;
    /// <summary>
    /// Returns the hard drive serial that Windows is installed on.
    /// </summary>
    public string SystemDeviceSerial
    {
        get { return mySystemDeviceSerial; }
    }

    private string mySystemName = null;
    /// <summary>
    /// Returns the current name of the system.
    /// </summary>
    public string SystemName
    {
        get { return mySystemName; }
    }

    private string myLastError = string.Empty;
    /// <summary>
    /// Returns the last error that occurred. Returns string.Empty if no error has occurred.
    /// </summary>
    public string LastError
    {
        get { return myLastError; }
    }

    private int myPort = -2;
    public int Port
    {
       get { return this.myPort; }
       set { this.myPort = value; }
    }

All properties are accessible just fine on the local machine. 可以在本地计算机上访问所有属性。 However, when I try to read the Port property from a client over a .NET remoting channel, I do not get the value that has been assigned to myPort, but rather, the initial value of -2. 但是,当我尝试通过.NET远程通道从客户端读取Port属性时,我没有获得已分配给myPort的值,而是获得了初始值-2。 I am stumped on this, as the all of the string properties are able to be read fine. 我对此感到困惑,因为所有字符串属性都可以很好地读取。 Any ideas? 有任何想法吗? Am I not serializing this class properly? 我不能正确地序列化此类吗?

It is worthy to note that all I have done to make this class and members serializable is add the [Serializable] attribute at the top of the class. 值得一提的是,我所做的所有使类和成员可序列化的操作都是在类顶部添加[Serializable]属性。 I am not sure if this is the proper way to do this, so perhaps this could be a part of the issue? 我不确定这是否是正确的方法,所以也许这可能是问题的一部分?

My guess (and I stress it is just a guess) is that you have the order wrong- that you are 我的猜测(我强调这只是一个猜测)是您的订单有误-您是

  1. Retrieving an item from the remoting host 从远程主机检索项目
  2. Updating it (on the host), then 更新它(在主机上),然后
  3. Expecting the value in the client to be updated to the value you changed on the host. 期望客户端中的值将更新为您在主机上更改的值。

When you use [Serializable] , you are turning the object into a byte stream, transmitting it over the wire to the remoting client, then reconstituting it into a new object on the client. 使用[Serializable] ,会将对象转换为字节流,通过电线将其传输到远程客户端,然后将其重构为客户端上的新对象。 That means that the client's copy is completely disconnected from the server's copy. 这意味着客户端的副本已与服务器的副本完全断开连接。 If you want the client's copy to always show the updated values from the server (without re-transmitting the whole object), make it inherit from MarshalByRefObject like @Hans-Passant suggests. 如果希望客户端的副本始终显示服务器中的更新值(而不重新传输整个对象),请使其像@ Hans-Passant所建议的那样从MarshalByRefObject继承。 That makes the client copy a transparent proxy that queries the server each time you access one of it's properties. 这样一来,客户端就可以复制一个透明的代理,每次访问服务器的一个属性时,该代理就会查询服务器。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM