在WCF中添加传出邮件头无法在传入邮件头中检索 [英] Adding an outgoing message headers in WCF can't be retrieved in incoming message headers

查看:57
本文介绍了在WCF中添加传出邮件头无法在传入邮件头中检索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的应用程序使用WCF服务.我有三个功能-Add,GetList,GetSingle.

I am using WCF Services for my application. I've got three functions - Add, GetList, GetSingle.

要在客户端上创建服务,请使用以下代码:

To create the service on the client-side, I am using this code:

Public Shared Function GetService(ByRef oScope As OperationContextScope) As XService.XServiceClient
    Dim oService As New XService.XServiceClient
    oScope = New OperationContextScope(oService.InnerChannel)
    oService.Open()
    Dim oMessageHeader As System.ServiceModel.Channels.MessageHeader = MessageHeader.CreateHeader("SecurityContext", String.Empty, AuthenticationModule.GetAuthenticationTicketToService)
    OperationContext.Current.OutgoingMessageHeaders.Add(oMessageHeader)
    Return oService
End Function

AuthenticationModule.GetAuthenticationTicketToService 将返回包含GUID的字符串.

AuthenticationModule.GetAuthenticationTicketToService will return a string containing a GUID.

在服务器端,我使用以下方法检索数据:

On server-side, I am retrieving the data using this:

Public Function GetTokenValue() As String
    If OperationContext.Current.IncomingMessageHeaders.FindHeader("SecurityContext", "") <> -1 Then
        Return OperationContext.Current.IncomingMessageHeaders.GetHeader(Of String)("SecurityContext", "")
    End If
    Return ""
End Function

当我调用Add或GetList函数时,可以很好地检索到传入的标头.但是,当我调用GetSingle函数时,传入的标头始终为空.请注意,所有三种方法都使用相同的代码创建服务,以及检索所需的标头.

When I call the Add or the GetList function, the incoming header is being well retrieved. However, when I am calling the GetSingle function, the incoming header is always empty. Note that the same code is being used to create the service in all three methods, as well as to retrieve the wanted header.

由于执行相同的代码时,三个功能之一的行为不像其他功能,我感到迷惑.无法获取信息的原因可能是什么?

I am lost about the reason that one of the three functions is not behaving like the others, while the same code is being executed. What could possibly be the reason for not being able to retrieve the information?

推荐答案

我认为上面的客户端代码无效.OperationContext.Current将始终返回null.通常,我们设法仅在OperationContextScope内获取OperationContext.current实例,如下所示.

In my opinion, the above code on the client-side didn’t work. the OperationContext.Current will always return null. Ordinarily, we manage to get the OperationContext.current instance only within the OperationContextScope, like below.

  using (OperationContextScope ocs = new OperationContextScope(client.InnerChannel);)
            {
                MessageHeader header = MessageHeader.CreateHeader("myname", "mynamespace", "myvalue");
                OperationContext.Current.OutgoingMessageHeaders.Add(header);
                var result = client.GetData();
                Console.WriteLine(result);
            }
//this call would not add the custom header
            var result2 = client.GetData();
            Console.WriteLine(result2);

OperationContextScope的范围仅在using语句内有效.释放OperationContextScope实例后,将还原OperationContext,并且消息头不再有效.如果我们在using语句内调用该方法,则可以在服务器端找到自定义标头.
如果我们要向每个请求中永久添加消息头,则可以使用IClientMessageInspector接口.
https://putridparrot.com/blog/adding-data-to-wcf-message-headers-client-side/
随时让我知道是否有什么可以帮助您的.

The scope of OperationContextScope is only valid within the using statement. After the instance of OperationContextScope is released, the OperationContext is restored and the message header is no longer valid. If we call the method inside the using statement, we are capable of finding the custom header on the server-side.
We can use the IClientMessageInspector interface if we want to permanently add message headers to every requests.
https://putridparrot.com/blog/adding-data-to-wcf-message-headers-client-side/
Feel free to let me know if there is anything I can help with.

这篇关于在WCF中添加传出邮件头无法在传入邮件头中检索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆