简体   繁体   English

WCF请求中缺少消息标头值

[英]Message header values missing in WCF request

I have a WCF service which I am including a custom message header for vendor credentials in. I've created a class called 'AuthHeader' that inherits from SoapHeader, and added properties for a username and password. 我有我包括在供应商证书的自定义消息头的WCF服务。我已经创建了一个名为“AuthHeader”从SOAPHEADER继承类,并增加了一个用户名和密码的性质。

public class AuthHeader : SoapHeader
{
    public string Username;
    public string Password;
}

I'm adding the header using the following code on the client: 我在客户端上使用以下代码添加标头:

 OperationContext.Current.OutgoingMessageHeaders.Add(
    MessageHeader.CreateHeader("Identity", 
                               "http://www.my-website.com",
                               new AuthHeader
                                   {
                                       Username = "UserNameValue",
                                       Password = "PasswordValue"
                                   }
                              ));

I then attempt to retrieve the header in the service using the following: 然后,我尝试使用以下方法检索服务中的标头:

var result = OperationContext.Current.IncomingMessageHeaders.GetHeader<AuthHeader>("Identity", "http://www.my-website.com");

While this code finds the header, it does NOT deserialize the values correctly, rather, Username and Password on the result object are null. 尽管此代码找到了标头,但它不会正确地反序列化值,而是结果对象上的用户名和密码为空。 Any idea why it isn't setting these property values? 知道为什么不设置这些属性值吗?

用属性[DataContract]和字段[DataMember]标记您的类AuthHeader

This is the called traced by Fiddler. 这就是Fiddler所追踪的。 It might help you. 它可能会帮助您。

<s:Header><SecurityHeader xmlns="http://tempuri.org" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><Channel xmlns="http://schemas.datacontract.org/2004/07/CustomMessageHeader">ChannelName</Channel><Password xmlns="http://schemas.datacontract.org/2004/07/CustomMessageHeader">Password</Password><UserName xmlns="http://schemas.datacontract.org/2004/07/CustomMessageHeader">username</UserName></SecurityHeader></s:Header><s:Body><GetLookUp xmlns="http://tempuri.org/"><categoryType>GRIEVANCE_TYPE</categoryType></GetLookUp></s:Body>

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

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