【问题标题】:Firing Event in WCF HostWCF 主机中的触发事件
【发布时间】:2012-07-05 07:34:05
【问题描述】:

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

【问题讨论】:

  • 这似乎不适用于 wcf 4.5

标签: c# wcf wcf-client


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

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

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

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

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

    希望有帮助!

    【讨论】:

    • 恐怕没有帮助。 OP 不是询问服务和消费者之间的事件,而是询问服务内部的事件,来自服务实现和服务主机容器(在本例中为 winforms 应用程序)
    猜你喜欢
    • 2013-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多