2012-07-03 35 views
2

我正在运行WPF应用程序中托管的WCF服务,并且在向服务发送消息时尝试在主机中引发事件,但遇到很大困难。在WCF主机中触发事件

我的代码如下:

服务 -

namespace BatService 
{ 
    [ServiceContract] 
    public interface IBatServ 
    { 
     [OperationContract] 
     void UseGadget(string name); 
    } 

    [ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)] 
    public class BatServ : IBatServ 
    { 
     public void UseGadget(string name) 
     { 
      OnUsedGadget(name); 
     } 

     public static event EventHandler<BatArgs> UsedGadget; 
     public static void OnUsedGadget(string name) 
     { 
      if (UsedGadget != null) 
       UsedGadget(null, new BatArgs() { BatGadget = name }); 
     } 
    } 

    public class BatArgs : EventArgs 
    { 
     public string BatGadget; 
    } 
} 

主机 -

namespace BatHostWPF 
{ 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 

      ServiceHost host = new ServiceHost(typeof(BatServ)); 
      BatServ.UsedGadget += new EventHandler<BatArgs>(BatServ_UsedGadget); 
      host.Open(); 
     } 

     void BatServ_UsedGadget(object sender, BatArgs e) 
     { 
      MessageBox.Show(e.BatGadget + " was used!"); 
     } 
    } 
} 

服务的App.config -

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 

    <system.web> 
    <compilation debug="true" /> 
    </system.web> 
    <!-- When deploying the service library project, the content of the config file must be added to the host's 
    app.config file. System.Configuration does not support config files for libraries. --> 
    <system.serviceModel> 
    <services> 
     <service name="BatService.BatServ"> 
     <endpoint address="" binding="wsHttpBinding" contract="BatService.IBatServ"> 
      <identity> 
      <dns value="localhost" /> 
      </identity> 
     </endpoint> 
     <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> 
     <host> 
      <baseAddresses> 
      <add baseAddress="http://localhost:8732/Design_Time_Addresses/BatService/Service1/" /> 
      </baseAddresses> 
     </host> 
     </service> 
    </services> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior> 
      <!-- To avoid disclosing metadata information, 
      set the value below to false and remove the metadata endpoint above before deployment --> 
      <serviceMetadata httpGetEnabled="True"/> 
      <!-- To receive exception details in faults for debugging purposes, 
      set the value below to true. Set to false before deployment 
      to avoid disclosing exception information --> 
      <serviceDebug includeExceptionDetailInFaults="False" /> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    </system.serviceModel> 

</configuration> 

主机的App.config中 -

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <system.serviceModel> 
     <behaviors> 
      <serviceBehaviors> 
       <behavior name="NewBehavior0"> 
        <serviceMetadata httpGetEnabled="true" /> 
       </behavior> 
      </serviceBehaviors> 
     </behaviors> 
     <services> 
      <service behaviorConfiguration="NewBehavior0" name="BatService.BatServ"> 
       <clear /> 
       <endpoint address="net.pipe://localhost/battserv" binding="netNamedPipeBinding" 
        bindingConfiguration="" contract="BatService.IBatServ" /> 
       <host> 
        <baseAddresses> 
         <add baseAddress="http://localhost:8888/batserv" /> 
        </baseAddresses> 
       </host> 
      </service> 
     </services> 
    </system.serviceModel> 
</configuration> 

正如您大概猜测的那样,当我从客户端调用UseGadget()时,我期待看到一个MessageBox。每当我尝试用VS的WcfTestClient.exe进行测试时,似乎什么也没有发生。我哪里错了?

+0

这似乎不适用于wcf 4.5 –

回答

0

我不确定您是否可以在wcf中“引发事件”,但是您可以创建双工通信架构。 netTCPBinding,netNamedPipBinding和wsDualHttpBinding支持此功能。

这里是一个视频演示如何做回调与wsDualHttpBinding

http://www.youtube.com/watch?v=NO2JsLrP75E

我从来没有与netNamedPipeBinding做到了这一点,但我相信过程是相似的。

记住在完成实现后更新app.config文件和服务引用。

希望有帮助!

+0

没有帮助恐怕。 OP不询问服务和消费者之间的事件,而是在服务内部从服务实现和服务主机容器(在这种情况下是Winforms应用程序) –