如何使用ServiceHostFactory在IIS托管WCF服务中向客户端端点添加行为

时间:2013-07-02 03:04:18

标签: wcf wcf-extensions

我正在考虑实施IDispatchMessageInpector& IClientMessageInpector用于查看AfterReceiveRequest和BeforeSendRequest方法中的消息对象。 我的要求是在WCF服务的代码级别进行更改。没有配置更改。 如何将此行为附加到调用此服务并为其自身提供服务的所有端点。实施IContractBehaviour对我有帮助吗?

编辑1: WCF服务托管在IIS上。是否可以通过代码添加行为?

编辑2: 好像使用ServiceHostFactory我们可以实现这一点。 如何向webconfig中定义的客户端端点添加行为?

2 个答案:

答案 0 :(得分:3)

是的,可以为IIS中托管的服务添加行为。行为与服务的托管环境无关。 Carlos Figueira的博客提供了可应用于服务,端点,合同和操作的所有类型行为的示例。我为IIS中托管的服务尝试的示例代码(在web.config文件中定义了端点) - 此处的配置文件需要将行为添加为ExtensionElement

public class MyEndpointBehavior : BehaviorExtensionElement, IEndpointBehavior
{
            public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
            {
                Console.WriteLine("applying dispatch behavior");
                endpointDispatcher.DispatchRuntime.MessageInspectors.Add(new MyInspector());
                endpointDispatcher.DispatchRuntime.OperationSelector = new MyOperationSelector();
            }

        public void  AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
        {
        }

        public void  ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
        {
        }

        public void  Validate(ServiceEndpoint endpoint)
        {
        }

        public override Type BehaviorType
        {
            get {  return this.GetType(); }
        }

        protected override object CreateBehavior()
        {
           return new MyEndpointBehavior();
        }
}

    public class MyOperationSelector : IDispatchOperationSelector
    {
        public string SelectOperation(ref Message message)
        {
            Console.WriteLine("good luck");
            string action = message.Headers.Action;
            return action.Substring(action.LastIndexOf('/') + 1);
        }
    }

    public class MyInspector : IDispatchMessageInspector
    {

        public object AfterReceiveRequest(ref Message request, System.ServiceModel.IClientChannel channel, System.ServiceModel.InstanceContext instanceContext)
        {
            return (Message) request;
        }

        public void BeforeSendReply(ref Message reply, object correlationState)
        {
        }
    }
 }

将行为添加为扩展元素的配置文件 -

  <system.serviceModel>
  <services>
  <service name="RouteToServiceA.Service1">
    <endpoint address="Service1" binding="basicHttpBinding" contract="RouteToServiceA.IService1" behaviorConfiguration="testEndPoint" />
  </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="true" />
    </behavior>
  </serviceBehaviors>
  <endpointBehaviors>
    <behavior name="testEndPoint">
      <testBehavior />
    </behavior>
  </endpointBehaviors>
</behaviors>
<extensions>
  <behaviorExtensions>
    <add name="testBehavior" type="RouteToServiceA.MyEndpointBehavior, RouteToServiceA" />
  </behaviorExtensions>
</extensions>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
 </system.serviceModel>

答案 1 :(得分:0)

使用ServiceHostFactory我们可以添加服务行为,而将行为配置添加到配置中的客户端端点似乎无法实现。所以我要进行配置更改