用于远程调用 Windows 服务方法的体系结构
我已向 Windows 服务添加了远程调用,以便我的 GUI 应用程序可以与其通信。它工作得很好,但我的渠道实施不了解我的服务。
我应该如何分层我的类,以便我的远程处理通道实现可以调用我的服务类中的方法?
通道接口:
public interface IMyService
{
string Ping();
string SomeMethod(string input);
}
通道实现:
public class MyServiceChannel : MarshalByRefObject, IMyService
{
public string Ping()
{
return "Pong";
}
public string SomeMethod(string input)
{
MethodForChannelToCall(input); // in Service class. How to reference?
return "Some Output";
}
}
服务类
class MyService : ServiceBase
{
public void MethodForChannelToCall(string input)
{
// do service stuff for remoting call
}
public MyService()
{
// Set up remoting channel
try
{
TcpChannel tcpChannel = new TcpChannel(12345);
ChannelServices.RegisterChannel(tcpChannel, false);
RemotingConfiguration.RegisterWellKnownServiceType(
typeof(MyServiceChannel),
"MyServiceChannel",
WellKnownObjectMode.SingleCall);
// Should I pass an instance of my service to my channel somehow here?
}
catch (Exception ex)
{
this.EventLog.WriteEntry("Remoting error: " + ex.ToString());
}
}
}
我应该如何构造我的类,以便我的通道可以调用我的服务方法?
I have added remoting calls to my Windows Service so my GUI application can talk to it. It works great, but my channel implementation has no knowledge of my service.
How should I layer my classes so that my remoting channel implementation can call methods in my service class?
Channel Interface:
public interface IMyService
{
string Ping();
string SomeMethod(string input);
}
Channel Implementation:
public class MyServiceChannel : MarshalByRefObject, IMyService
{
public string Ping()
{
return "Pong";
}
public string SomeMethod(string input)
{
MethodForChannelToCall(input); // in Service class. How to reference?
return "Some Output";
}
}
Service Class
class MyService : ServiceBase
{
public void MethodForChannelToCall(string input)
{
// do service stuff for remoting call
}
public MyService()
{
// Set up remoting channel
try
{
TcpChannel tcpChannel = new TcpChannel(12345);
ChannelServices.RegisterChannel(tcpChannel, false);
RemotingConfiguration.RegisterWellKnownServiceType(
typeof(MyServiceChannel),
"MyServiceChannel",
WellKnownObjectMode.SingleCall);
// Should I pass an instance of my service to my channel somehow here?
}
catch (Exception ex)
{
this.EventLog.WriteEntry("Remoting error: " + ex.ToString());
}
}
}
How should I structure my classes so that my channel can call my service methods?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这种情况下,您应该使用 WCF。 WCF 取代了 .net 远程处理。
如果您的客户端和服务位于同一台计算机上,您可以使用命名管道绑定。
如果它们位于不同的计算机上,您可以使用 net tcpip 绑定。
In this case you should use WCF. WCF replaces .net remoting.
If both your client and service are on the same machine you can use named pipes binding.
If they are on different machines you can use net tcpip binding.