【问题标题】:Architecture for remoting to call a Windows Service method远程调用 Windows 服务方法的体系结构
【发布时间】:2010-11-24 06:58:03
【问题描述】:

我已将远程调用添加到我的 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());
        }
    }
}

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

【问题讨论】:

标签: c# architecture windows-services remoting


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-27
    • 2017-11-25
    • 2015-01-09
    • 1970-01-01
    相关资源
    最近更新 更多