wcf unknownmessage使用iis主机接收

时间:2012-07-09 12:12:37

标签: wcf

如果我在IIS上托管了WCF应用程序,我该如何处理 UnknownMessageReceived个事件?

我知道如何在构建控制台主机时执行此操作。

1 个答案:

答案 0 :(得分:0)

您可以使用服务主机工厂来访问IIS下使用的服务主机实例。您可以在http://blogs.msdn.com/b/carlosfigueira/archive/2011/06/14/wcf-extensibility-servicehostfactory.aspx找到有关服务主机工厂的更多信息,下面的代码显示了一个监听UnknownMessageReceived事件的工厂示例。

public class MyFactory : ServiceHostFactory
{
    protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
    {
        ServiceHost host = base.CreateServiceHost(serviceType, baseAddresses);
        host.UnknownMessageReceived += new EventHandler<UnknownMessageReceivedEventArgs>(host_UnknownMessageReceived);
        return host;
    }

    void host_UnknownMessageReceived(object sender, UnknownMessageReceivedEventArgs e)
    {
        // do something with the message
    }
}