2

我正在研究实现 IDispatchMessageInpector 和 IClientMessageInpector 以查看 AfterReceiveRequest 和 BeforeSendRequest 方法中的消息对象。我的要求是在 WCF 服务的代码级别进行更改。无配置更改。如何将此行为附加到调用此服务的所有端点以及服务本身。实施 IContractBehaviour 对我有帮助吗?

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

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

4

2 回答 2

4

是的,可以为 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>
于 2013-07-02T18:12:11.090 回答
0

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

于 2013-07-04T08:57:50.797 回答