2012-07-03 2 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은이 기능을 지원합니다. 여기

내가 netNamedPipeBinding와 함께이 일을 결코 한 wsDualHttpBinding

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

로 콜백을 수행하는 방법 비디오 데모입니다,하지만 난 절차가 유사하다 가정합니다.

구현이 완료되면 app.config 파일과 서비스 참조를 업데이트해야합니다.

희망 하시겠습니까?

+0

도움이되지 않습니다. OP는 서비스와 소비자 간의 이벤트가 아니라 내부적으로 서비스 구현과 서비스 호스트 컨테이너 (이 경우 winforms 앱)에서 이벤트를 묻습니다. –