Is WebGet functionally equivalent to WebInvoke(Method = “GET”)?

风格不统一 提交于 2019-12-06 05:25:08

问题


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?


回答1:


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";
}



回答2:


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



来源:https://stackoverflow.com/questions/27723584/is-webget-functionally-equivalent-to-webinvokemethod-get

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!