none
How can I add attribute in header of a webservice? RRS feed

  • Question

  • I need to get context from a webservice which provide by our customer.

    I add the webservice sucessfully.

    Here is the code which to get the context:

    ServiceReference1.CxfWebServiceApiClient Client = new ServiceReference1.CxfWebServiceApiClient();                                
    ServiceReference1.sysUserOfWs SUOW=Client.getUserInfo("txhxl5h31111a", "123456");

    Well, it failed and threws an error which let me contact my customer.

    The customer told me that I need to add a attribute 'Code=123456' to the header or it must be failed.

    Meanwhile, I know less about the webservice and I found someone using the HttpPost/HttpWebRequest or any other method to call the webservice in some tutorial.

    I wanna know if my way is right and how to add attribute in header of a webservice? Thank you.


    Tuesday, June 25, 2019 8:08 AM

Answers

All replies

  • Your customer that has the Webs ervice should explain to you how to consume and use its Web service. Maybe you are afraid to ask on the howto, but you shouldn't be, and you need to get the information form the customer. 
    Tuesday, June 25, 2019 8:30 AM
  • Your customer that has the Webs ervice should explain to you how to consume and use its Web service. Maybe you are afraid to ask on the howto, but you shouldn't be, and you need to get the information form the customer. 

    They just showed me a tutorial about how to get the context via SoapUI. I did this sucessfully in the SoapUI, but I don't know how to do this in C# yet.

    Tuesday, June 25, 2019 8:39 AM
  • Your customer that has the Webs ervice should explain to you how to consume and use its Web service. Maybe you are afraid to ask on the howto, but you shouldn't be, and you need to get the information form the customer. 
    What's more, I tried the tutorial in Postman, it works also. Now it seems the problem is how to achieve this in C#.
    Tuesday, June 25, 2019 8:56 AM
  • It's where you can post for help.

    https://forums.asp.net/28.aspx/1?WCF+ASMX+and+other+Web+Services

    Tuesday, June 25, 2019 1:04 PM
  • It's where you can post for help.

    https://forums.asp.net/28.aspx/1?WCF+ASMX+and+other+Web+Services

    Let me try. However, I think my problem is to reference it but not to make it.
    Wednesday, June 26, 2019 1:00 AM
  • Hi mywatermelon,

    Thank you for posting here.

    Since this thread is related to wcf, I will move it to Windows Communication Foundation, Serialization, and Networking forum to get professional support.

    The Visual C# forum discusses and asks questions about the C# programming language, IDE, libraries, samples, and tools.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, June 26, 2019 2:18 AM
  • Hi mywatermelon,

    Thank you for posting here.

    Since this thread is related to wcf, I will move it to Windows Communication Foundation, Serialization, and Networking forum to get professional support.

    The Visual C# forum discusses and asks questions about the C# programming language, IDE, libraries, samples, and tools.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thank you so much.
    Wednesday, June 26, 2019 2:35 AM
  • Hi,
     We could use OperationContext to insert bespoke message header into the SOAP message, please refer to the below code.
    Server (10.157.13.69:3336, Console Application).

        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 on the server side.

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


    Client (Console application).

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

    Result.
    https://i.stack.imgur.com/LpvOw.png

    Besides, we could also use the IClientMessageInsepctor interface to create a persistent message header, please refer to the below link.

    https://stackoverflow.com/questions/55005505/how-to-pass-winform-custom-user-credentials-to-wcf-services-in-every-requests/55038793#55038793

    Feel free to let me know if there is anything I can help with.
    Best Regards
    Abraham

    Thursday, June 27, 2019 3:32 AM
    Moderator
  • Hi,
     We could use OperationContext to insert bespoke message header into the SOAP message, please refer to the below code.
    Server (10.157.13.69:3336, Console Application).

        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 on the server side.

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


    Client (Console application).

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

    Result.
    https://i.stack.imgur.com/LpvOw.png

    Besides, we could also use the IClientMessageInsepctor interface to create a persistent message header, please refer to the below link.

    https://stackoverflow.com/questions/55005505/how-to-pass-winform-custom-user-credentials-to-wcf-services-in-every-requests/55038793#55038793

    Feel free to let me know if there is anything I can help with.
    Best Regards
    Abraham

    I am sorry about that I can't change the code of the server side. It is made by our customer and they won't change any more.

    I use the Webservice to exchange data with them. I sent a post to him and he will reply data to me, just this.

    Thursday, June 27, 2019 2:54 PM
  • Hi mywatermelon,

    Glad that you have solved the problem.
    It seems that you want to add http request header instead of the SOAP message header,   thereby, you could directly add the http header by using httpclient/httpwebrequest.

    Besides, if you consume the service without the way of using Httpwebquest(Httpclient class),  we could use WebOperationContext class to add the Http request header. provided we use a client proxy class(adding service reference) or a channel factory to invoke the service. please refer to the link.

    https://stackoverflow.com/questions/13856362/adding-http-request-header-to-wcf-request

    Best Regards
    Abraham


    Friday, June 28, 2019 1:46 AM
    Moderator