C# WCF主机中的触发事件

C# WCF主机中的触发事件,c#,wcf,wcf-client,C#,Wcf,Wcf Client,我正在运行一个WPF应用程序中托管的WCF服务,并试图在向该服务发送消息时在主机中引发一个事件,但遇到了很大的困难 我的代码如下: 服务- namespace BatService { [ServiceContract] public interface IBatServ { [OperationContract] void UseGadget(string name); } [ServiceBehavior(Instan

我正在运行一个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;
    }
}
命名空间服务
{
[服务合同]
公共接口IBatServ
{
[经营合同]
作废UseGadget(字符串名称);
}
[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
公共类BatServ:IBatServ
{
public void UseGadget(字符串名称)
{
OnUsedGadget(名称);
}
公共静态事件处理程序UsedGadget;
公共静态void OnUsedGadget(字符串名称)
{
if(UsedGadget!=null)
UsedGadget(null,new BatArgs(){BatGadget=name});
}
}
公共类BatArgs:EventArgs
{
公共字符串工具;
}
}
宿主-

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!");
        }
    }
}
wpf
{
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
ServiceHost主机=新的ServiceHost(typeof(BatServ));
BatServ.UsedGadget+=新事件处理程序(BatServ_UsedGadget);
host.Open();
}
void BatServ_UsedGadget(对象发送方,BatArgs e)
{
Show(例如BatGadget+“已使用!”);
}
}
}
服务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中“引发事件”,但您可以创建双工通信体系结构。NetCpBinding、netNamedPipBinding和wsDualHttpBinding支持此功能

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

我从未使用netNamedPipeBinding完成过这项工作,但我假设过程类似

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


希望有帮助

结果是我的端点配置不正确。这个页面帮助我解决了问题-

恐怕没有帮助。OP不是询问服务和使用者之间的事件,而是在服务内部,从服务实现和服务主机容器(在本例中是winforms应用程序)询问事件。这似乎不适用于wcf 4.5