0

Is there a way in WCF to check some logic based on the type of Request it receives? Can this be done in the actual service endpoint code?

For example:

After Service Initialization my service receives a PUT Request. In myService.svc.cs I would like to have logic that looks like this:

if httpRequest.Type == PUT
{
    //Do Something
} 

Is this possible? I'm sure there is a better way to handle requests than adding logic for every Operation Contract that is of type PUT. Apologies if this question doesn't make sense I'm sort of new to WCF and am trying to learn. Please let me know if you need clarifiers.

EDIT:

This is what myService.svc.cs looks like currently:

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public partial class MyService: IMyService
{
    public async Task<someObject> GetMethod1 () // Some GET Method
    { 
        doSomethingForGetRequests();
        //method implementation
    }

    public async Task<someObject> GetMethod2 () // Some GET Method
    { 
        doSomethingForGetRequests();
        //method implementation
    }

    public async Task<someObject> PutMethod1 () // Some PUTMethod
    { 
        doSomethingForPutRequests();
        //method implementation
    }

    doSomethingForPutRequests()
    {
       if(config.IsReadOnly)
       {
           throw new WebFaultException(HttpStatusCode.BadRequest);
       }
    }
}


I am wondering if there is a place where i can place doSomethingForGetRequest() and doSomethingForPutRequest() in a central location before the request reaches these methods so I don't have to add these methods to each one of my Service Methods.

Would global.asax.cs Application_BeginRequest() be an appropriate place for this logic?

TeeseCaprice
  • 55
  • 1
  • 13
  • You can decorate your method with attributes that specify the request type. Do the examples given in this question help?: https://stackoverflow.com/questions/4197956/webinvoke-method-post-or-get-for-a-rest-service-on-wcf – Tawab Wakil Feb 05 '22 at 02:26
  • So knowing the HTTP Method is already defined in my interface. My question specifically is if there is a central location to add logic based on the request type? I made edit to above to see if that makes more clear what I am trying to do... – TeeseCaprice Feb 06 '22 at 19:21
  • Lan Huang's answer below seems reasonable. You can try this: `if (HttpContext.Current.Request.HttpMethod == "POST")` – Tawab Wakil Feb 07 '22 at 13:57

1 Answers1

3

Maybe a message inspector can help you, it's called on every request that arrives at the service.

IDispatchMessageInspector defines the methods that enable custom inspection or modification of inbound and outbound application messages in service applications.

You can check out these posts:
Detect if action is a POST or GET method
Call the method automatically for each and every request in the WCF REST

Lan Huang
  • 613
  • 2
  • 5
  • +1 You may want to include some code directly in your answer. This will make the answer more relevant to the OP while also maintaining its usefulness if the links go away at some point. – Tawab Wakil Feb 07 '22 at 14:06