繁体   English   中英

WCF和Windows服务通信

[英]WCF and Windows service communication

拜托,有人可以建议我吗? 我创建了WCF库,该库实现了与外部WCF客户端一起工作的端点网络管道。我将lib放置在Windows Service中。 但是我不知道如何在不使用WCF客户端-服务器机制的情况下从库进行数据交换。 如何在Windows服务中通过运行WCF服务放置或获取数据? 例如,我想在事件日志中注册WCF错误或为服务提供参数以将其传递给连接到WCF的客户端。

在我的项目中,我需要创建Silverlight或WPF客户端,以便在我的工厂中进行实时工业监控。 为此,我想在我的OPC客户端上的wcf服务中使用双工通道

我的Windows Service项目中有两个对象。 一个对象是OPC客户端和数据库客户端。 如果我在代码中创建此对象-一切正常。 另一个对象是带有net.pipe绑定的WCF服务库,用于对该服务的外部连接。 好。 我在代码中定义:

// window service class   
 public partial class SyncSiemensService : ServiceBase
    {
        private OpcServiceClass opcServer = new OpcServiceClass();

        public SyncSiemensService()
        {
        InitializeComponent();
        }

    protected override void OnStart(string[] args)
    {
              // OPC client- database client object
              opcServer.Start();

             // WCF interface for external communication
             // with this windows service
              using (ServiceHost host = new ServiceHost(typeof(DuplexPipeWcfService)))
                         {
                             host.Open();
                             eventLog.WriteEntry("<Bla-Bla-Bla", EventLogEntryType.Information);
                         }
    }
}

WCF服务

    [OperationContract(IsOneWay = true)]
    void GetActValuesFromLine(string line);

字符串行-OPC对象的外部数据

和他的回调

public interface IDuplexPipeWcfServiceCallback
{
    [OperationContract(IsOneWay = true)]
    void ActualValuesUpdate(WcfDuplexActualValues value);
}

但是在矿井中出现“ WcfDuplexActualValues值”-来自OPC对象的数据,用于WCF中的外部连接。 现在我不知道,如何在不使用OPC对象与WCF服务库之间的客户端服务通信的情况下从opc对象检索数据。

非常感谢

您的OnStart方法将打开主机,然后立即将其再次关闭。

 public partial class SyncSiemensService : ServiceBase
    {
        private ServiceHost _host;
        private OpcServiceClass opcServer = new OpcServiceClass();

        public SyncSiemensService()
        {
        InitializeComponent();
        }

 protected override void OnStart(string[] args)
 {
          // OPC client- database client object
          opcServer.Start();

         // WCF interface for external communication
         // with this windows service
          _host = new ServiceHost(typeof(DuplexPipeWcfService)))

         _host.Open();
        eventLog.WriteEntry("<Bla-Bla-Bla", EventLogEntryType.Information);

 }

}

您需要保持打开状态,并且仅在关闭服务时将其处置。

我不是100%确定我正确理解了您的问题,但是我认为您正在尝试在使用WCF的情况下在OpcServiceClass实例和DuplexPipeWcfService之间交换数据。

您可以通过自己创建实例来使用另一种方式来托管服务:

protected override void OnStart(string[] args)
 {
          // OPC client- database client object
          opcServer.Start();

          var serviceInstance = new DuplexPipeWcfService();
          _host = new ServiceHost(serviceInstance)

         _host.Open();
        eventLog.WriteEntry("<Bla-Bla-Bla", EventLogEntryType.Information);
 }

现在,您拥有两个实例,并且可以通过传递引用之间进行数据交换。 例如,您可以更改OpcServiceClass的构造函数:

protected override void OnStart(string[] args)
 {
          var serviceInstance = new DuplexPipeWcfService();
          opcServer.Start(serviceInstance );

          _host = new ServiceHost(serviceInstance)

         _host.Open();
        eventLog.WriteEntry("<Bla-Bla-Bla", EventLogEntryType.Information);
 }

您只需要更改OpcServiceClass.Start()方法的签名即可采用IDuplexPipeWcfServiceCallback类型的对象。 这样,您可以与DuplexPipeWcfService进行通信,而无需WCF开销。

像这样更改签名:

OpcServiceClass.Start(IDuplexPipeWcfServiceCallback wcfService)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM