简体   繁体   English

使域数据可用于 WCF 行为

[英]Making domain data available to WCF behavior

In my application, I have a value ("BusinessUnit") which I want to add into every request to a web-service.在我的应用程序中,我有一个值(“BusinessUnit”),我想将它添加到对 Web 服务的每个请求中。 One way of doing this would be to write a WCF behaviour, which would insert the value for me.这样做的一种方法是编写 WCF 行为,这将为我插入值。

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?有任何想法吗?

If this value is the same for all calls, then you can consider using a static variable for it.如果此值对于所有调用都相同,那么您可以考虑为其使用 static 变量。 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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM