2012-07-03 66 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應用程序) –