【问题标题】:Adding message header to request将消息头添加到请求
【发布时间】:2019-06-26 08:34:55
【问题描述】:

尝试将此标头添加到我在 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/

亲切的问候

/鲁迪

【问题讨论】:

    标签: c# wcf soap


    【解决方案1】:

    伙计,如上述链接所示,我们可以使用 OperationContext 来添加自定义消息头。请参考以下示例,希望对您有用。
    Server(Console application, 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();
        }
    

    结果。

    此外,我们还可以使用 IClientMessageInspector 接口来创建一个持久的 SOAP 消息头,因为上述添加肥皂消息头的操作仅在 USING 语句中有效。请参考我之前的回复。
    How to pass winform custom user credentials to WCF services in every requests?
    如果有什么可以帮助的,请随时告诉我。

    【讨论】:

      猜你喜欢
      • 2012-12-01
      • 1970-01-01
      • 2017-02-15
      • 1970-01-01
      • 1970-01-01
      • 2011-10-14
      • 1970-01-01
      • 1970-01-01
      • 2016-12-22
      相关资源
      最近更新 更多