无法在从 WPF 应用程序使用的 WCF 应用程序中调用 ApplyClientBehavior 方法

问题描述 投票:0回答:1

无法点击ApplyClientBehavior方法

使用 System.ServiceModel.Description;

namespace Utils.CMSAuth
{
    public class AuthorizationHeaderEndpointBehavior : IEndpointBehavior
    {
        public void ApplyClientBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime)
        {
            clientRuntime.ClientMessageInspectors.Add(new AuthorizationHeaderMessageInspector(TokenGenerator.GetToken()));
        }
        public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
        {
        }

        public void ApplyDispatchBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher)
        {
        }

        public void Validate(ServiceEndpoint endpoint)
        {
        }
    }
}

未点击ApplyClientBehavior方法。

在 web.config 端添加了行为。

<extensions>
    <behaviorExtensions>
        <add name="AuthorizationHeaderMessageInspector" type="Utils.CMSAuth.CustomBehaviorExtensionElement, Utils, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
    </behaviorExtensions>
</extensions>
c# wpf wcf
1个回答
0
投票

您可以通过如下代码向客户端添加行为:

client.Endpoint.Behaviors.Add(new AuthorizationHeaderEndpointBehavior(null, true, true));

您还可以尝试修改客户端的 app.config 配置文件。这篇文章解释得更好:

ApplyClientBehavior 在 IOperationBehavior 中(有时)未调用

© www.soinside.com 2019 - 2024. All rights reserved.