Custom SOAP Header for WCF Service
From this article I'm going to explain how to create a custom SOAP header ( actually add custom attributes to the SOAP Header). So I hope you have some kind of knowledge regarding WCF. so I'm not going to explain fundamental thing in WCF.
3. Now It's time to create MyMessage class and MyMessageResponseClass. so below you can find the Implementation of both classes.
Note: As you can see I added Message header attribute to Id variable to make it available to SOAP Header. likewise you can add any number of attributes to SOAP header. for example you can send client credential details, consumer's identification number anything else you want to get each and every message header. by using this approach you can reduce no of parameters in each and every service method
4. Okay now It's time to Implement method in Service1 class. So go to that class and write down these code segment or whatever you like.
5. Great now your service is complete. It's time to run and see the outcome. Press F5 or Debug then you can see the WCF Test Client. go to XML tab see the SOAP message. now you can see ID attribute is added to the SOAP Header.
6. See, adding custom attribute for WCF SOAP header is pretty easy right? so Hope this will help you guys somehow. Happy coding guys and have a nice day..!
1. Go to Visual Studio and create new WCF Service application
2. Then you can find Iservice1.cs Interface and Service1.svc file in your WCF project. so go to Iservice interface and add a method call Method or even you can add whatever name you like. so here is my one.
2. Then you can find Iservice1.cs Interface and Service1.svc file in your WCF project. so go to Iservice interface and add a method call Method or even you can add whatever name you like. so here is my one.
[ServiceContract]
public interface IService1
{
[OperationContract]
MyMessageResponse Method(MyMessage message);
}
3. Now It's time to create MyMessage class and MyMessageResponseClass. so below you can find the Implementation of both classes.
[MessageContract]
public class MyMessage
{
[MessageHeader]
public int Id;
[MessageBodyMember]
public string Name;
}
[MessageContract]
public class MyMessageResponse
{
[MessageBodyMember]
public string Result;
}
Note: As you can see I added Message header attribute to Id variable to make it available to SOAP Header. likewise you can add any number of attributes to SOAP header. for example you can send client credential details, consumer's identification number anything else you want to get each and every message header. by using this approach you can reduce no of parameters in each and every service method
4. Okay now It's time to Implement method in Service1 class. So go to that class and write down these code segment or whatever you like.
public class Service1 : IService1
{
public MyMessageResponse Method(MyMessage message)
{
var response = new MyMessageResponse {Result = "Test"};
return response;
}
}
5. Great now your service is complete. It's time to run and see the outcome. Press F5 or Debug then you can see the WCF Test Client. go to XML tab see the SOAP message. now you can see ID attribute is added to the SOAP Header.
6. See, adding custom attribute for WCF SOAP header is pretty easy right? so Hope this will help you guys somehow. Happy coding guys and have a nice day..!
Comments
Post a Comment