【发布时间】:2010-05-10 19:18:25
【问题描述】:
我有一个我认为是简单的 .NET 远程处理客户端/服务器(下面的代码)...在控制台应用程序中托管/运行时它工作正常,但是当托管在 Windows 服务中时,对代理成员的所有调用从 Activator.GetObject 返回导致 NullReferenceException。
为了简化事情,我将所有这些都放在了一个控制台中,它运行良好...创建了一个基本的 Windows 服务并将相同的代码放在 OnStart 方法上,一旦我访问“TheString”属性,我就会得到一个 NullReferenceException。
我可以确认没有其他异常,端口可用,并且服务以管理员身份运行。另外,我的解决方案需要一个单例,这就是我使用它的原因。
目前这是在 Windows 7 上托管的,这可能是一个因素。如果我可以了解如何查看更多可能导致此问题的潜在错误,我可能会弄清楚......我如何才能看到下面可能发生的事情(例如接收器、格式化程序等......)?
服务器代码:
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);
客户代码:
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; }
}
任何想法都将不胜感激。
【问题讨论】: