代码之家  ›  专栏  ›  技术社区  ›  Francis B.

WCF:从服务器访问服务实例

  •  6
  • Francis B.  · 技术社区  · 15 年前

    语境:

    我需要开发一个监控服务器来监控我们的一些应用程序(这些应用程序在C中)。所以我决定用WCF开发一个适合我需要的系统。

    这些应用程序在启动时必须注册到监控服务器。之后,监控服务器可以调用这些应用程序的启动或停止方法。

    所有操作都在同一台机器上完成,不需要远程执行。

    所以我开发了一个好的原型,一切都很好。每个应用程序向监控服务器注册自己。

    问题:

    ApplicationRegistrationService (见下面的代码)是监控服务的实现,由于 ServiceBehavior 属性。

    这里我的问题是:我想访问 应用程序注册服务 例如,从我的服务器连接的应用程序数( ConsoleMonitoringServer 在示例中)。但是,我不知道如何做到这一点。

    我是否需要在服务器中创建一个到服务的通道,就像在客户机中一样( ConsoleClient )或者它存在一个更好的方法来实现这一点?

    代码:

    就本问题而言,代码非常简化:

    //The callback contract interface
    public interface IApplicationAction
    {
        [OperationContract(IsOneWay = true)]
        void Stop();
    
        [OperationContract(IsOneWay = true)]
        void Start();
    }
    
    [ServiceContract(SessionMode = SessionMode.Required, 
        CallbackContract = typeof(IApplicationAction))]
    public interface IApplicationRegistration
    {
        [OperationContract]
        void Register(Guid guid, string name);
    
        [OperationContract]
        void Unregister(Guid guid);
    }
    
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, 
        ConcurrencyMode = ConcurrencyMode.Multiple)]
    public class ApplicationRegistrationService : IApplicationRegistration
    {
        //IApplicationRegistration Implementation
    }
    
    public class ApplicationAction : IApplicationAction
    {
        //IApplicationAction Implementation
    }
    

    本例的控制台应用程序

    class ConsoleClient
    {
        static void Main(string[] args)
        {
            ApplicationAction actions = new ApplicationAction();
    
            DuplexChannelFactory<IApplicationRegistration> appRegPipeFactory =
                new DuplexChannelFactory<IApplicationRegistration>(actions,
                new NetNamedPipeBinding(), new EndpointAddress("net.pipe://localhost/AppReg"));
    
            IApplicationRegistration proxy = appRegPipeFactory.CreateChannel();
            proxy.Register(Guid.Empty, "ThisClientName");
    
            //Do stuffs
        }
    }
    

    本例中的控制台服务器

    class ConsoleMonitoringServer
    {
        static void Main(string[] args)
        {
            using (ServiceHost host = new ServiceHost(typeof(ApplicationRegistrationService),
                new Uri[]{ new Uri("net.pipe://localhost")}))
            {
                host.AddServiceEndpoint(typeof(IApplicationRegistration), 
                    new NetNamedPipeBinding(), "AppReg");
    
                host.Open();
    
                //Wait until some write something in the console
                Console.ReadLine();
    
                host.Close();
            }
        }
    }
    
    2 回复  |  直到 15 年前
        1
  •  9
  •   Francis B.    15 年前

    最后,我找到了答案,这很容易。我只需要创建服务实例并将引用传递给servicehost的构造函数。

    因此,我需要替换以下代码:

    using (ServiceHost host = new ServiceHost(typeof(ApplicationRegistrationService),
                    new Uri[]{ new Uri("net.pipe://localhost")}))
    

    通过:

    ApplicationRegistrationService myService = new ApplicationRegistrationService();
    
    using (ServiceHost host = new ServiceHost(myService,
                    new Uri[]{ new Uri("net.pipe://localhost")}))
    
        2
  •  0
  •   Khalid Abuhakmeh    15 年前

    如果您的意思是希望在监视服务和注册的服务或节点之间进行双向通信,那么您可能应该在WCF中使用双向通信,也称为双工通信。很酷的东西。

    http://www.codeproject.com/KB/WCF/WCF_Duplex_UI_Threads.aspx