4

This question already asks what I'm asking, but I want some clarification on the answer.

The answer states that WebGet and WebInvoke are similar, and that the primary difference is the Method parameter.

But if the Method parameter is set to "GET", is it actually functionally equivalent, or are there other differences?

Community
  • 1
  • 1
David says Reinstate Monica
  • 19,209
  • 22
  • 79
  • 122
  • 1
    The documentation for [WebInvoke](http://msdn.microsoft.com/en-us/library/system.servicemodel.web.webinvokeattribute(v=vs.110).aspx) says: "If you want a service operation to respond to GET, use the WebGetAttribute instead." So it seems that WebInvoke is only intended to be used with POST, PUT, or DELETE. – Michael Liu Dec 31 '14 at 16:45
  • One difference: The internal [System.Data.Services.Providers.BaseServiceProvider.AddOperationsFromType](http://referencesource.microsoft.com/#System.Data.Services/System/Data/Services/Providers/BaseServiceProvider.cs,d5f831be05db2b2a,references) method treats WebGet as GET and WebInvoke (even if its Method is GET) as POST. – Michael Liu Dec 31 '14 at 16:53
  • @MichaelLiu Yeah, I read that, but the code already contains `WebInvoke(Method="GET")`, so I want to make sure I'm not breaking anything before I change – David says Reinstate Monica Dec 31 '14 at 17:38

2 Answers2

2

They are simply marker attributes and end up being 100% functionally equivalent. The only thing that interprets these attributes is the WebHttpBehavior::GetWebMethod method and its functionality is simply:

internal static string GetWebMethod(OperationDescription od)
{
    WebGetAttribute webGetAttribute = od.Behaviors.Find<WebGetAttribute>();
    WebInvokeAttribute webInvokeAttribute = od.Behaviors.Find<WebInvokeAttribute>();
    WebHttpBehavior.EnsureOk(webGetAttribute, webInvokeAttribute, od);
    if (webGetAttribute != null)
    {
        return "GET";
    }
    if (webInvokeAttribute == null)
    {
        return "POST";
    }
    return webInvokeAttribute.Method ?? "POST";
}
Drew Marsh
  • 33,111
  • 3
  • 82
  • 100
0

It is not.

I just spent few hours trying to replace WCF DataContractJsonSerializer with Newtonsoft JsonSerializer using MessageFormatter based on this and this samples

found out (the hard way) there IS difference in using WebGet and WebInvoke(Method="GET").

With WebInvoke the request goes through different pipeline in WCF stack, trying to deserialize the expected message (method IDispatchMessageFormatter.DeserializeRequest() gets invoked) which is not the case with WebGet.

The lesson learned: use WebGet for GET operation

eXavier
  • 4,821
  • 4
  • 35
  • 57