2012-08-29 136 views
0

请问,有人可以提醒我吗?我创建了WCF库,实现了使用外部WCF客户端进行作业的端点网络管道。我在Windows服务中放置了lib。但是我不知道如何在不使用WCF客户机 - 服务器机制的情况下从库中进行数据交换。如何在Windows服务中放置或获取运行WCF服务的数据?例如,我想在Eventlog中注册WCF错误,或者将参数提供给服务,以便将它们传送给连接到WCF的客户端。WCF和Windows服务通讯

在我的项目中,我需要在工厂中实时创建用于工业监控的Silverlight或WPF客户端。为此,我想在我的OPC客户端的wcf服务中使用双工通道。我的Windows服务项目中有两个对象。一个对象是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对象中检索数据。

非常感谢

+0

你可以添加你是如何托管服务一些额外的信息?在WAS中,自行托管在Windows服务中? – Chris

+0

是的。我使用Windows服务来托管Wcf服务。 –

回答

2

您的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); 

} 

} 

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

+0

是的。我在我的代码中犯了错误。谢谢。但我怎么能从opcServer发送数据到_host并接收回来。 –

1

我不是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) 
+0

一切都好。十分感谢。 –