我已经向我的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());
}
}
}
我应该如何构造我的类,以便我的频道可以调用我的服务方法?