What is use of message contract? [Resolved]

Posted by Rajeshatkiit under WCF on 12/19/2015 | Points: 10 | Views : 540 | Status : [Member] | Replies : 1
What is use of message contract? Is any body has used in their project?
Please explain with respect to project which you have handled?




Responses

Posted by: Rajnilari2015 on: 12/19/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 50

Up
0
Down

Resolved
If we need to intercept the SOAP message that is passed between service and client, we need Message Contract which provides complete control over the SOAP message. It is a structural contract.

e.g.

[MessageContract(IsWrapped = false)]

public class EmployeeInfoRequest
{
[MessageHeader()]
public string AuthCode;

[MessageBodyMember()]
public int EmployeeId;

[MessageBodyMember()]
public string EmployeeName;
}

[MessageContract(IsWrapped = false)]
public class EmployeeInfoResponse
{
[MessageBodyMember()]
public EmployeeInfo EmpInfo;
}

[ServiceContract]
public interface IEmployeeService
{
[OperationContract]
EmployeeInfoResponse GetAllEmployeeInfo(EmployeeInfoRequest empInfo);
}

public class EmployeeService : IEmployeeService
{
public EmployeeInfoResponse GetAllEmployeeInfo(EmployeeInfoRequest empInfo)
{
//Validation Check
if (empInfo.bearerToken != "hPOCxaXYeNJsIqG4PNYzNa9BvWy37dgvbtZQZ2qYE79R-7l3j-Skhwj0xDLgONd")
{
//unauthorised bad request

return BadRequest();
}
// authorized code
return valid EmployeeInfoResponse ;
}
}


In the above piece of code we are using the SOAP message header for storing the bearer token / authentication code while calling the service method. It is needed for a valid Employee authentication for accessing the other assets/methods/services of the Employees. In such a scenario, SOAP message header is the most secure place to do so.

Also DNF has a very nice article on the same available at
http://www.dotnetfunda.com/articles/show/2559/how-to-implement-message-contracts

Hope this will be helpful.

Thanks

--
Thanks & Regards,
RNA Team

Rajeshatkiit, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response