i am creating the REST service with POST method and OBJECT as input param. while client request am unable to get the actual JSON data client have posted. Is there any way to dig the JSON code from the C# WCF service.
My code:
namespace ACTService
{
public class AssortmentService : IAssortmentService
{
public void DeleteColor(DeleteColorContarct objdelcolor)
{
new Methods.ColorUI().DeleteColorDetails(objdelcolor);
}
}
}
and my interface as
namespace ACTService
{
[ServiceContract]
public interface IAssortmentService
{
[OperationContract]
[WebInvoke(UriTemplate = "DeleteColor", Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json,BodyStyle=WebMessageBodyStyle.Wrapped)]
void DeleteColor(DeleteColorContarct objColor);
}
}
I need to access the JSON format in other class file ColorUI
WCF provides a lot of extensible points one of them is a feature called MessageInspector. You can create a custom message inspector to receive the request before it get de-serialized to C# object. And do what ever you can with Raw request data.
In order to implement it you would need to implement
System.ServiceModel.Dispatcher.IDispatchMessageInspector
interface like below:Here's the complete code snippet gist. Actual source.
Now you need to add this Message inspector to end point behavior. To achieve that you would be implementing
System.ServiceModel.Description.IEndpointBehavior
interface like below:Now if you are on selfhosting i.e. you are hosting your service programmatically you can directly attach this newly implemented behavior to your service end point. E.g.
endpoint.Behaviors.Add(new IncomingMessageLogger());
But If you have hosted the WCF Rest service in IIS then you would be injecting the new Behavior via configuration. In order to achieve that you have to create an additional class derived from
BehaviorExtensionElement
:Now in your configuration first register the behavior under
system.servicemodel
tag:Now add this behavior to the Endpoint behavior:
set the attribute
behaviorConfiguration="defaultWebHttpBehavior"
in your endpoint.That's it your service will now capture all the messages before deserializing them.