C# 将头插入WCF出站消息

C# 将头插入WCF出站消息,c#,wcf,inject,C#,Wcf,Inject,我有一个基于从客户提供的WSDL创建的类的WCF服务。不幸的是,此WSDL不包含所需的消息头。客户端将不会提供包含标头的新WSDL。我确实有一个描述头的xsd文件 我还有一个示例标题,知道需要填充哪些字段 如何获取提供的头XML并将其注入到出站WCF方法调用中? 我希望像目前一样调用我的服务方法,但我也希望新的头结构构成出站消息的一部分 提前谢谢。 我们将非常感谢您的任何帮助 以下是消息结构的示例: 我需要添加整个标题结构。WSDL包含的所有内容都是主体 <s:Envelope xmlns

我有一个基于从客户提供的WSDL创建的类的WCF服务。不幸的是,此WSDL不包含所需的消息头。客户端将不会提供包含标头的新WSDL。我确实有一个描述头的xsd文件

我还有一个示例标题,知道需要填充哪些字段

如何获取提供的头XML并将其注入到出站WCF方法调用中? 我希望像目前一样调用我的服务方法,但我也希望新的头结构构成出站消息的一部分

提前谢谢。 我们将非常感谢您的任何帮助

以下是消息结构的示例: 我需要添加整个标题结构。WSDL包含的所有内容都是主体

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
   <s:Header>
      <glob:requestHeader xmlns:glob="http://....">
         <timestamp>2013-11-14T05:17:41.793+02:00</timestamp>
         <traceMessageId>GUID</traceMessageId>
         <enterpriseTraceUUId>GUID</enterpriseTraceUUId>
         <contentType>TEXT/XML</contentType>
         <sender>
            <senderId>SENDER</senderId>
            <sourceSystem>001</sourceSystem>
            <sourceApplication>001</sourceApplication>
            <applicationSessionId>ABC</applicationSessionId>
            <sourceLocation>100</sourceLocation>
         </sender>
         <interfaceName/>
         <version>1111</version>
      </glob:requestHeader>
   </s:Header>
   <s:Body xmlns:xsi="http://.../XMLSchema-instance" xmlns:xsd="http://.../XMLSchema">
      <UserData xmlns="http://.../Base">
         <IdField>1005687</IdField>
         <UserInfo>
            <UserType>1</UserType>
            <UserStatus>Y</UserStatus>
         </UserInfo>
      </UserData>
   </s:Body>
</s:Envelope>

2013-11-14T05:17:41.793+02:00
指南
指南
文本/XML
发件人
001
001
基础知识
100
1111
1005687
1.
Y

例如,我使用它将“用户代理”添加到我发出的消息的标题中,但我认为您可以根据自己的需要对其进行调整:

private void AddCustomHeader(System.ServiceModel.OperationContextScope scope)
{
    dynamic reqProp = new System.ServiceModel.Channels.HttpRequestMessageProperty();
    reqProp.Headers.Add("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT; blah; blah; blah)");
    System.ServiceModel.OperationContext.Current.OutgoingMessageProperties(System.ServiceModel.Channels.HttpRequestMessageProperty.Name) = reqProp;
}
我在上面从用于调用主机的客户端程序的构造函数调用此函数

 AddCustomHeader(new System.ServiceModel.OperationContextScope(base.InnerChannel));

可能需要注意的最重要的一点是,它将这个头变量添加到我的客户机使用的“当前”OperationContext的OutgoingMessageProperties中

你试过这个吗?也取自此处:

使用(OperationContextScope范围=新的OperationContextScope((IContextChannel)通道))
{
MessageHeader=newmessageheader(“秘密消息”);
var untyped=header.GetUntypedHeader(“标识”)http://www.my-website.com");
OperationContext.Current.OutgoingMessageHeaders.Add(非类型化);
//现在在这个using块中进行WCF调用
}

看看这里:这正是我想要的。但是在这个例子中,我在WCF之外定义了一个非常特殊的头结构,这将很难建模。我只想获取XML头块并将其插入到消息中。这可能会有所帮助。你有一个更完整的例子,我可以看看。我需要看看如何将它连接到我的应用程序中。实际上,你看到的就是我正在使用的。我只需要将它用于正在添加的用户代理元素。但我相信您可以推断出,以适应您认为在请求中缺少的任何数量的所需标题。
using (OperationContextScope scope = new OperationContextScope((IContextChannel)channel))
{
    MessageHeader<string> header = new MessageHeader<string>("secret message");
    var untyped = header.GetUntypedHeader("Identity", "http://www.my-website.com");
    OperationContext.Current.OutgoingMessageHeaders.Add(untyped);

    // now make the WCF call within this using block
}