代码之家  ›  专栏  ›  技术社区  ›  Petrus Theron

用于远程调用Windows服务方法的体系结构

  •  2
  • Petrus Theron  · 技术社区  · 15 年前

    我已经向我的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());
            }
        }
    }
    

    我应该如何构造我的类,以便我的频道可以调用我的服务方法?

    1 回复  |  直到 15 年前
        1
  •  0
  •   Shiraz Bhaiji    15 年前

    在这种情况下,您应该使用WCF。WCF取代了。网络远程处理。

    如果客户端和服务都在同一台计算机上,则可以使用命名管道绑定。

    如果它们在不同的机器上,您可以使用nettcpip绑定。