使域数据可用于 WCF 行为

发布于 2024-11-10 05:28:17 字数 554 浏览 0 评论 0原文

在我的应用程序中,我有一个值(“BusinessUnit”),我想将其添加到对 Web 服务的每个请求中。一种方法是编写一个 WCF 行为,它会为我插入该值。

然而,我不清楚的一部分是如何从我的应用程序中获取这个值并融入到行为中。

为了说明我的问题,我将如何实现它。

public object BeforeSendRequest(ref Message request, IClientChannel channel)
{
    string businessUnit = //How do I set this to a value known by the client?
    MessageHeader<string> header = 
        new MessageHeader<string>(businessUnit);
    request.Headers.Add(
        header.GetUntypedHeader("Business Unit", "http://mywebsite.com"));
}

有什么想法吗?

In my application, I have a value ("BusinessUnit") which I want to add into every request to a web-service. One way of doing this would be to write a WCF behaviour, which would insert the value for me.

However, the one part I am not clear on is how I can get this value from my application and into the behaviour.

To illustrate my question, here is how I might implement it.

public object BeforeSendRequest(ref Message request, IClientChannel channel)
{
    string businessUnit = //How do I set this to a value known by the client?
    MessageHeader<string> header = 
        new MessageHeader<string>(businessUnit);
    request.Headers.Add(
        header.GetUntypedHeader("Business Unit", "http://mywebsite.com"));
}

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉城凉梦凉人心 2024-11-17 05:28:17

如果所有调用的该值都相同,那么您可以考虑为其使用静态变量。如果每次调用都有所不同,您可以使用操作上下文来添加它(甚至跳过该行为),如下所示。

ServiceClient client = new ServiceClient(...);
using (new OperationContextScope(client.InnerChannel))
{
    MessageHeader<string> header = new MessageHeader<string>(businessUnit);
    OperationContext.Current.OutgoingMessageHeaders.Add(header.GetUntypedHeader("Business Unit", "http://mywebsite.com"));
    client.MakeServiceCall();
}

如果每个调用都有所不同,您可以考虑将其传递给创建客户端时的行为,然后该行为可以将其传递给它创建的检查器:

ServiceClient client = new ServiceClient(...);
client.Endpoint.Behaviors.Add(new MyBehavior(businessUnit));
client.MakeServiceCall1();
client.MakeServiceCall2();
client.MakeServiceCall3();

If this value is the same for all calls, then you can consider using a static variable for it. If it varies per call, you can use the operation context to add it (and even skipping the behavior), as shown below

ServiceClient client = new ServiceClient(...);
using (new OperationContextScope(client.InnerChannel))
{
    MessageHeader<string> header = new MessageHeader<string>(businessUnit);
    OperationContext.Current.OutgoingMessageHeaders.Add(header.GetUntypedHeader("Business Unit", "http://mywebsite.com"));
    client.MakeServiceCall();
}

If it's something that varies per group of calls, you can consider passing it to the behavior when you create the client, and then the behavior can pass it to the inspector it creates:

ServiceClient client = new ServiceClient(...);
client.Endpoint.Behaviors.Add(new MyBehavior(businessUnit));
client.MakeServiceCall1();
client.MakeServiceCall2();
client.MakeServiceCall3();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文