我有一个问题,我不太确定它是如何开始的。我相当肯定它以前工作正常,但不记得做过任何更改。
首先,请不要过分关注设置,除非它直接影响它为什么不工作。我不是在寻找批评,而是在寻找导致它不起作用的原因。
我公开了一个使用 HTTP 标头身份验证的 API。我在我的解决方案中使用来自此 API 的操作。为了避免样板代码,我创建了一个要初始化服务的 ClientFactory,使用CustomClientMessageInspector和负责将标头添加到消息的CustomCredentialBehavior 。
这个想法是,当我需要使用服务时,代码看起来类似于:
IClientCredentials credentials = ClientCredentials.FromToken();
var service = ClientFactory.CreateClientInstance<API.Clients.ClientServiceClient>(credentials);
ClientFactory 如下图(请不要过多判断)。
public class ClientFactory
{
public static T CreateClientInstance<T>(IClientCredentials clientCredentials)
{
T t = Activator.CreateInstance<T>();
var factory = t.GetType().GetProperty("ChannelFactory");
using (var scope = new OperationContextScope((IContextChannel) t.GetType().GetProperty("InnerChannel").GetValue(t,null)))
{
var endpointBehavior = new CustomCredentialBehavior(clientCredentials);
if (((ChannelFactory) factory.GetValue((t),null)).Endpoint.Behaviors.Any(p => p.GetType() == typeof(CustomCredentialBehavior)))
{
var behavior =
((ChannelFactory) factory.GetValue((t),null)).Endpoint.Behaviors.FirstOrDefault(
p => p.GetType() == typeof (CustomCredentialBehavior));
((ChannelFactory) factory.GetValue((t),null)).Endpoint.Behaviors.Remove(behavior);
}
((ChannelFactory)factory.GetValue((t),null)).Endpoint.Behaviors.Add(endpointBehavior);
return t;
}
}
}
自定义端点行为:
public class CustomCredentialBehavior:IEndpointBehavior
{
private IClientCredentials _clientCredentials { get; set; }
public CustomCredentialBehavior(IClientCredentials clientCredentials)
{
_clientCredentials = clientCredentials;
}
public void Validate(ServiceEndpoint endpoint)
{
}
public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
{
}
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
{
}
public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
clientRuntime.MessageInspectors.Add(new ClientCredentialMessageInspector(_clientCredentials));
}
}
ClientMessageInspector:
public class ClientCredentialMessageInspector:IClientMessageInspector
{
private IClientCredentials _clientCredentials;
public ClientCredentialMessageInspector(IClientCredentials clientCredentials)
{
_clientCredentials = clientCredentials;
}
public object BeforeSendRequest(ref Message request, IClientChannel channel)
{
var buffer = request.CreateBufferedCopy(Int32.MaxValue);
request = buffer.CreateMessage();
HttpRequestMessageProperty messageProperty =
(HttpRequestMessageProperty) request.Properties["httpRequest"];
messageProperty.Headers.Add("AccessKey", _clientCredentials.AccessKey.ToString());
messageProperty.Headers.Add("ClientKey", _clientCredentials.ClientKey.ToString());
messageProperty.Headers.Add("AuthorizationKey", _clientCredentials.AuthorizationKey);
return null;
}
public void AfterReceiveReply(ref Message reply, object correlationState)
{
}
}
我面临的问题是,即使我可以看到从 ClientFactory 返回的服务具有正确的端点行为,但 ApplyClientBehavior() 永远不会被调用。出于某种原因,它似乎并没有像原来那样使用 WCF 管道。
如何让 ClientFactory 创建实例以便使用我添加的 EndpointBehavior?
编辑只是为了显示我正在查看的内容,这是调用服务操作之前监视窗口的屏幕截图。它显示了使用所有正确值初始化的我的 CustomCredentialBehavior。
编辑我接受了 Carlos 的建议(在评论中)并简化了调用,并使用了以下调用来代替它。我很高兴它可以工作,但是,我想找出问题出在我的原始代码中,这样我就不必每次需要使用服务引用时都调用样板代码。
以下代码有效:
API.Clients.Client client = new API.Clients.Client();
client.Active = true;
client.Enabled = true;
client.Name = model.ClientName;
API.Clients.ClientServiceClient service = new ClientServiceClient();
service.ChannelFactory.Endpoint.Behaviors.Add(new CustomCredentialBehavior(ClientCredentials.FromToken()));
var response = service.CreateClient(new CreateClientRequest() { Client = client });
知道为什么初始代码不起作用,而这个代码起作用吗?