What i want to achieve:
I want to controll all calls to WCF methods and add some validation. If the validation fails then i want the method to return earlier with a custom response of the same type with the method return type.
How can i achieve this in central place without having to add the validation check in every method?
Here is the pseudocode:
using System;
using System.Collections.Generic;
using System.ServiceModel;
using System.Text;
using System.Threading;
namespace Microsoft.WCF.Documentation
{
[ServiceContractAttribute(Namespace="http://microsoft.wcf.documentation")]
public interface ISampleService
{
[OperationContractAttribute(AsyncPattern=true)]
IAsyncResult BeginGetCustomerInfo(GetCustomerInfoRequest request, AsyncCallback callback, object asyncState);
GetCustomerInfoResponse EndGetCustomerInfo(IAsyncResult result);
[OperationContractAttribute(AsyncPattern=true)]
IAsyncResult BeginGetProductDetails(GetProductDetailsRequest request, AsyncCallback callback, object asyncState);
GetProductDetailsResponse EndGetProductDetails(IAsyncResult result);
}
public class SampleService : ISampleService
{
public IAsyncResult BeginGetCustomerInfo(GetCustomerInfoRequest request, AsyncCallback callback, object asyncState)
{
Console.WriteLine("BeginGetCustomerInfo called with: \"{0}\"", request);
//...
}
public GetCustomerInfoResponse EndGetCustomerInfo(IAsyncResult r)
{
//...
return new GetCustomerInfoResponse();
}
public IAsyncResult BeginGetProductDetails(GetProductDetailsRequest request, AsyncCallback callback, object asyncState)
{
Console.WriteLine("BeginGetProductDetails called with: \"{0}\"", request);
//...
}
public GetProductDetailsResponse EndGetProductDetails(IAsyncResult r)
{
//...
return new GetProductDetailsResponse();
}
}
}
You can use a WCF message interceptor. A message interceptor allows you to intercept incoming and outgoing messages in a WCF service.
You need to add a class which implements the IDispatchMessageInspector interface, let's say
ValidationInterceptor
. your validation logic should be implemented inAfterReceiveRequest
method.And a class which implements
Attribute, IServiceBehavior
, you can write your logic in ApplyDispatchBehavior method like the below.And use like this