Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用ServiceHostFactory在IIS托管的WCF服务中向客户端端点添加行为_Wcf_Wcf Extensions - Fatal编程技术网

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

如何使用ServiceHostFactory在IIS托管的WCF服务中向客户端端点添加行为,wcf,wcf-extensions,Wcf,Wcf Extensions,我正在研究实现IDispatchMessageInputer和IClientMessageInputer,以查看AfterReceiveRequest和BeforeSendRequest方法中的消息对象。 我的要求是在WCF服务的代码级别进行更改。没有配置更改。 如何将此行为附加到调用此服务的所有端点和服务本身。实施iContractBehavior对我有帮助吗 编辑1: WCF服务托管在IIS上。是否可以通过代码添加行为 编辑2: 似乎使用ServiceHostFactory我们可以实现这一点

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

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

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

是的,可以为IIS中承载的服务添加行为。行为与服务的宿主环境无关。Carlos Figueira的博客提供了可以应用于服务、端点、契约和操作的所有类型行为的示例。我为托管在IIS中的服务(在web.config文件中定义了端点)尝试的示例代码-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>

是的,可以为IIS中承载的服务添加行为。行为与服务的宿主环境无关。Carlos Figueira的博客提供了可以应用于服务、端点、契约和操作的所有类型行为的示例。我为托管在IIS中的服务(在web.config文件中定义了端点)尝试的示例代码-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>


使用ServiceHostFactory,我们可以添加服务行为,而向配置中的客户端端点添加行为配置似乎是不可能实现的。因此,我将使用ServiceHostFactory进行配置更改,我们可以添加服务行为,而向配置中的客户端端点添加行为配置似乎是不可能实现的。因此,我将进行配置更改

看看Carlos Figueira的博客-他有一个关于WCF可扩展性的系列文章和代码示例-我正在看这个博客,作为WCF的新手,我只是想看看我是否走上了正确的道路:)看看Carlos Figueira的博客-他有一个关于WCF扩展性的系列文章,有代码示例-我在看博客,作为WCF的新手,我只是想看看我是否走上了正确的道路:)你可以类似地向客户端元素添加相关的行为(IEndpointBehavior、IContractBehavior、IOperationBehavior)并将它们添加到配置文件的behaviorextensions部分,如我上面提到的示例所示。我正在寻找不进行任何配置更改的方法您可以类似地将相关行为添加到客户端元素(IEndpointBehavior、IContractBehavior、IOperationBehavior)并将它们添加到配置文件的behaviorextensions部分,如我上面提到的示例所示