无法在Windows服务主机时访问远程对象的成员 [英] Unable to access Remoting object members when hosting in Windows Service

查看:139
本文介绍了无法在Windows服务主机时访问远程对象的成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有什么,我认为是一个简单的.NET Remoting的客户机/服务器(code以下)...当托管/在一个控制台应用程序,它工作正常运行,但是当在Windows服务托管,所有调用代理的成员Activator.GetObject导致一个NullReferenceException返回。

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.

要简化事情,我把所有这一切在一个单一的控制台和它工作得很好...创建一个基本的Windows服务,并放置在同一code在OnStart方法,一旦我进入了TheString属性,我收到了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.

目前正在被托管在视窗7的时刻可能是一个因素。如果我能学会多看看有什么潜在的错误可能会导致此,我或许可以推测出来......我如何可以看到什么可能发生下(例如接收器,格式,等...)?

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...)?

服务器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);

客户端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

远程对象:

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

    public override object InitializeLifetimeService()
    {
        return null;
    }

    public string TheString { get; set; }
}

任何想法,将AP preciated。

Any ideas would be appreciated.

推荐答案

事实证明的限制涉及到远程处理引擎无法序列化和代理接口类型,虽然不属于我的样品(对不起)是最终的根的问题。

As it turns out the limitation relates to the remoting engines inability to serialize and proxy interface types, which while not part of my sample (sorry) was ultimately the root of the issue.

这篇关于无法在Windows服务主机时访问远程对象的成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆