问题描述
我想知道是否可以将自定义消息头注入传出请求以携带附加信息,而无需反序列化有效负载以完成身份验证、验证或请求相关性等功能,例如通过消息检查器提供的 wcf?
I am wondering it is possible to inject custom message header to outgoing request to carry additional information without deserialize the payload to fullfill the functionality like authentication, validation or correlation of request like wcf provided by means of messagesinspector?
推荐答案
更新
使用 SDK v2,您现在可以(相对)轻松地修改 Reliable Services 和 Actors 的标头.请注意,在下面的示例中,为简洁起见,省略了一些包装成员.
With SDK v2 you can now (relatively) easily modify the headers of both Reliable Services and Actors. Note in the examples below some wrapper members were omitted for brevity.
客户
我们使用 ServiceProxyFactory
来创建代理而不是静态的 ServiceProxy
.然后我们可以包装IServiceRemotingClientFactory
和IServiceRemotingClient
并拦截服务调用.使用 ActorProxyFactory
也可以做到这一点.请注意,这会覆盖诸如 WcfServiceRemotingProviderAttribute
之类的属性的行为,因为我们自己明确指定了客户端工厂.
We use ServiceProxyFactory
to create proxies instead of the static ServiceProxy
. Then we can wrap IServiceRemotingClientFactory
and IServiceRemotingClient
and intercept the service calls. The same can be done with ActorProxyFactory
. Note this overrides the behavior of attributes such as the WcfServiceRemotingProviderAttribute
, since we explicitly specify the client factory ourselves.
_proxyFactory = new ServiceProxyFactory(c => new ServiceRemotingClientFactoryWrapper(
// we can use any factory here
new WcfServiceRemotingClientFactory(callbackClient: c)));
private class ServiceRemotingClientFactoryWrapper : IServiceRemotingClientFactory
{
private readonly IServiceRemotingClientFactory _inner;
public ServiceRemotingClientFactoryWrapper(IServiceRemotingClientFactory inner)
{
_inner = inner;
}
public async Task<IServiceRemotingClient> GetClientAsync(Uri serviceUri, ServicePartitionKey partitionKey, TargetReplicaSelector targetReplicaSelector,
string listenerName, OperationRetrySettings retrySettings, CancellationToken cancellationToken)
{
var client = await _inner.GetClientAsync(serviceUri, partitionKey, targetReplicaSelector, listenerName, retrySettings, cancellationToken).ConfigureAwait(false);
return new ServiceRemotingClientWrapper(client);
}
}
private class ServiceRemotingClientWrapper : IServiceRemotingClient
{
private readonly IServiceRemotingClient _inner;
public ServiceRemotingClientWrapper(IServiceRemotingClient inner)
{
_inner = inner;
}
public Task<byte[]> RequestResponseAsync(ServiceRemotingMessageHeaders messageHeaders, byte[] requestBody)
{
// use messageHeaders.AddHeader() here
return _inner.RequestResponseAsync(messageHeaders, requestBody);
}
public void SendOneWay(ServiceRemotingMessageHeaders messageHeaders, byte[] requestBody)
{
// use messageHeaders.AddHeader() here
_inner.SendOneWay(messageHeaders, requestBody);
}
}
服务器
从 ServiceRemotingDispatcher
和 ActorServiceRemotingDispatcher
继承以检查标头.
Inherit from ServiceRemotingDispatcher
and ActorServiceRemotingDispatcher
to examine the headers.
class CustomServiceRemotingDispatcher : ServiceRemotingDispatcher
{
public override async Task<byte[]> RequestResponseAsync(IServiceRemotingRequestContext requestContext, ServiceRemotingMessageHeaders messageHeaders, byte[] requestBody)
{
// read messageHeaders here
// or alternatively put them in an AsyncLocal<T> scope
// so they can be accessed down the call chain
return base.RequestResponseAsync(requestContext, messageHeaders, requestBody);
}
}
要使用这个类,我们再次需要通过直接创建通信侦听器来覆盖ServiceRemotingProviderAttribute
:
To use this class, again we need to override the ServiceRemotingProviderAttribute
by directly creating the communication listener:
class MyService : StatelessService
{
protected override IEnumerable<ServiceInstanceListener> CreateServiceInstanceListeners()
{
yield return new ServiceInstanceListener(context => new WcfServiceRemotingListener(context, new CustomServiceRemotingDispatcher());
}
}
这篇关于使用 Azure 服务结构的默认客户端时如何向请求添加消息头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!