使用 Azure 服务结构的默认客户端时如何向请求添加消息头?

编程入门 行业动态 更新时间:2024-10-28 16:25:24
本文介绍了使用 Azure 服务结构的默认客户端时如何向请求添加消息头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想知道是否可以将自定义消息头注入传出请求以携带附加信息,而无需反序列化有效负载以完成身份验证、验证或请求相关性等功能,例如通过消息检查器提供的 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 服务结构的默认客户端时如何向请求添加消息头?

本文发布于:2023-11-03 08:00:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1554631.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:客户端   消息   结构   Azure

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!