SoapHeaderAttribute: How to use a header parameter in ASP.Net web method
The most common way to pass parameters to a web method is through the SOAP body. However, sometimes you may want to pass parameters to web methods in the header. A common example for this is perhaps a “session” id or some other value used to create a stateful interface (like SSRS web services).
Here’s what you need to do (taken from MSDN)
- Create a class deriving from SoapHeader representing the data passed in the SOAP header.
- Add a member to the XML Web service class or XML Web service client proxy class of the type created in step 1.
- Apply a SoapHeaderAttribute to the XML Web service method or the corresponding method in the proxy class, specifying the member created in step 2 in the MemberName property.
- Within the XML Web service method or XML Web service client code, access the MemberName property to process the data sent in the SOAP header.
For example, in a web service I created that augments the SSRS web service:
Declared a property of type ExecutionHeader:
public ExecutionHeader ExecutionHeaderValue = new ExecutionHeader();
Apple the SoapHeaderAttribute to my web method:
In the method I use the property value as usual:
if(ExecutionHeaderValue.ExecutionID == null || ExecutionHeaderValue.ExecutionID == “null”)