简体   繁体   中英

Unable to access Remoting object members when hosting in Windows Service

I have what I thought was a simple .NET Remoting Client/Server (Code Below)... When hosting/running in a Console application it works fine, but when hosted in a Windows Service, all calls to members of proxies returned from Activator.GetObject result in a NullReferenceException.

To simplify things I placed all this in a single Console and it worked fine... Created a basic Windows Service and placed the same code on the OnStart method and once I access the "TheString" property I get a NullReferenceException.

I can confirm there are no other exceptions, the ports are available, and the service is run as an administrator. Also, my solution will require a singleton which is why I am using that.

At the moment this is being hosted on Windows 7 which may be a factor. If I could learn how to see more of what underlying error may be causing this, I may be able to figure it out... How can I see what might be happening underneath (ex. sink, formatter, etc...)?

Server Code:

var provider = new BinaryServerFormatterSinkProvider
{
    TypeFilterLevel = TypeFilterLevel.Full
};

IDictionary properties = new Hashtable();
properties["port"] = 20001;
properties["exclusiveAddressUse"] = false;

_channel = new TcpChannel(properties, null, provider);

ChannelServices.RegisterChannel(_channel, false);

RemotingConfiguration.RegisterWellKnownServiceType(typeof(HostClass), "TheHost", WellKnownObjectMode.Singleton);

Client Code:

var retInstance = (HostClass)Activator.GetObject(typeof(HostClass),
    string.Format("tcp://{0}:{1}/TheHost", "MyHostName", 20001));

string host = retInstance.TheString; //This is where the NullReference is experienced

Remoting Object:

public class HostClass : MarshalByRefObject, IHostClass
{
    public HostClass()
    {
      TheString = "Hello World";
    }

    public override object InitializeLifetimeService()
    {
        return null;
    }

    public string TheString { get; set; }
}

Any ideas would be appreciated.

事实证明,该限制与远程引擎无法序列化和代理接口类型有关,虽然这不是我的样本的一部分(对不起),但最终是问题的根源。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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