C# 向请求添加消息头

C# 向请求添加消息头,c#,wcf,soap,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/te

正在尝试在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>   
我尝试过,但没有成功:

问候


/Rudy

伙计,正如前面的链接所示,我们可以使用OperationContext添加自定义消息头。请参考下面的例子,希望对你有用。 服务器控制台应用程序,10.157.13.69:3336

服务器端的Appconfig

ClientConsole应用程序,使用ChannelFactory调用

结果。 此外,我们还可以使用IClientMessageInspector接口创建一个持久的SOAP消息头,因为上面的操作表明添加SOAP消息头只在USING语句中有效。请参阅我先前的答复。 如果有什么我能帮忙的,请随时告诉我

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));

            }
        }
}
<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>
  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();
    }