2

我正在运行托管在 WPF 应用程序中的 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 对其进行测试时,似乎什么都没有发生。我哪里错了?

4

2 回答 2

1

事实证明我的端点配置不正确。这个页面帮助我解决了我的问题 - http://www.switchonthecode.com/tutorials/wcf-tutorial-basic-interprocess-communication

于 2012-07-04T23:34:05.387 回答
0

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

这是一个视频演示如何使用 wsDualHttpBinding 进行回调

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

我从来没有用 netNamedPipeBinding 做过这个,但我认为这个过程是相似的。

完成实施后,请记住更新您的 app.config 文件和服务引用。

希望有帮助!

于 2012-07-03T01:28:43.263 回答