向请求添加消息头

时间:2019-06-26 08:34:55

标签: c# wcf soap

尝试将此标头添加到我在c#中的请求中:

<soap:Header>
<UserCredentials soap:mustUnderstand="1" xmlns="http://test.credential.com/UserCredentials" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<CicsUsername xmlns="http://schemas.test.org/2004/07/test.Mainframe">ciscs</CicsUsername>
<TechnicalPassword "http://schemas.test.org/2004/07/test.Mainframe">password</TechnicalPassword>
<TechnicalUsername "http://schemas.test.org/2004/07/test.Mainframe">user</TechnicalUsername>
</UserCredentials>
</soap:Header>   

我尝试了这个但没有成功:

https://blogs.msdn.microsoft.com/wsdevsol/2014/02/07/adding-custom-messageheader-and-http-header-to-a-wcf-method-call-in-a-windows-store-app-or-windows-phone-app/

亲切的问候

/鲁迪

1 个答案:

答案 0 :(得分:0)

好友,如上述链接所示,我们可以使用OperationContext添加自定义消息头。请参考以下示例,希望它对您有用。
服务器(控制台应用程序,10.157.13.69:3336)

class Program
    {
        static void Main(string[] args)
        {

            using (ServiceHost sh=new ServiceHost(typeof(MyService)))
            {
                sh.Open();
                Console.WriteLine("service is ready....");

                Console.ReadLine();

                sh.Close();
            }
        }
    }
    [ServiceContract]
    interface IService
    {
        [OperationContract]
        void WriteMessageHeader();
    }
    public class MyService : IService
    {
        public void WriteMessageHeader()
        {
            OperationContext oc = OperationContext.Current;
            //output the SOAP Message Header.
            for (int i = 0; i < oc.IncomingMessageHeaders.Count; i++)
            {
                MessageHeaderInfo info = oc.IncomingMessageHeaders[i];
                Console.WriteLine("Name: "+info.Name);
                Console.WriteLine("Namespace: "+info.Namespace);
                Console.WriteLine("Content: "+oc.IncomingMessageHeaders.GetHeader<string>(i));

            }
        }
}

服务器端的Appconfig。

<system.serviceModel>
    <services>
      <service name="Server1.MyService">
        <endpoint address="" binding="basicHttpBinding" contract="Server1.IService" ></endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint>
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:3336"/>
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpsGetEnabled="true" httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

客户端(控制台应用程序,通过ChannelFactory调用)

  class Program
        {
            static void Main(string[] args)
            {
                BasicHttpBinding binding = new BasicHttpBinding();
                Uri uri = new Uri("http://10.157.13.69:3336");
                ChannelFactory<IService> factory = new ChannelFactory<IService>(binding, new EndpointAddress(uri));
                IService service = factory.CreateChannel();
                //without adding additional messsage header, generally invoke
                service.WriteMessageHeader();
                //add additional message header.
                using (OperationContextScope scope=new OperationContextScope((IContextChannel)service))
                {
                    //insert custom message header
                    OperationContext oc = OperationContext.Current;
                    MessageHeader mh = MessageHeader.CreateHeader("MyMessageHeaderName", "MyMessageHeaderNamespace", "myvaule");
                    oc.OutgoingMessageHeaders.Add(mh);
                    service.WriteMessageHeader();
                }
                Console.ReadLine();



            }
        }
        [ServiceContract]
        interface IService
        {
            [OperationContract]
            void WriteMessageHeader();
    }

结果。
enter image description here
此外,我们还可以使用IClientMessageInspector接口创建持久的SOAP消息头,因为上述添加soap消息头的操作仅在USING语句中有效。请参考我之前的回复。
How to pass winform custom user credentials to WCF services in every requests?
随时让我知道是否有什么可以帮助您的。