本文介绍了如何使用 ServiceHostFactory 向 IIS 托管 WCF 服务中的客户端端点添加行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am looking at implementing IDispatchMessageInpector & IClientMessageInpector to look at the message objects in AfterReceiveRequest and BeforeSendRequest Methods.My requirement is to make changes at code level of WCF service. No Configuration Changes.How to attach this behaviour to all the endpoints which are calling this service and to service itself. Is implementing IContractBehaviour helps me?

WCF 服务托管在 IIS 上.是否可以通过代码添加行为?

Edit 1:WCF Service is hosted on IIS. Is it possible to add the behavior thru code?

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

Edit 2:Seems like using ServiceHostFactory we can achieve this.How can i add behavior to client endpoint which are defined in webconfig?

推荐答案

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

yes, it is possible to add behavior for services hosted in IIS. Behaviors are not concerned with the hosting environment of the service. Carlos Figueira's blog provides examples of all types of behaviors you could apply to Service, Endpoints, Contracts and Operations. A sample code that I tried for my service hosted in IIS (with endpoint defined in web.config file) - Config file here needs to add the behavior as 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)
        {
        }
    }
 }

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

Config file with behavior added as extension element -

  <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>

这篇关于如何使用 ServiceHostFactory 向 IIS 托管 WCF 服务中的客户端端点添加行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-25 01:23