WCF 3.5: WebInvoke Attribute

29. November 2007 05:47

A while back, I posted about the WebGet Attribute.  Unfortunately, I never got around to writing about its rather important sibling: WebInvoke Attribute.  While WebGet is completely dedicated to mapping a service operation to an HTTP GET, WebInvoke fills the void for all of the other HTTP methods.  It allows you to map a service operation to all the other HTTP verbs (PUT, POST, DELETE, etc).  Some people, particularly REST enthusiasts, have expressed their disapproval of the terminology because it tends to imply "remote procedure call".  However, WebInvoke simply indicates a service operation is logically an invoke operation. 

Here is a simple example:

[ServiceContract(Namespace = "http://JeffBarnes.Samples.WebInvokeDemo")]
public interface IExampleService
        Method = "POST",
        UriTemplate = "addPhoneNumber?customerID={customerID}&phoneNumber={phoneNumber}")]
    void InsertPhoneNumber(int customerID, string phoneNumber);

In this example, there is an operation named InsertPhoneNumber.  It has the usual OperationContract to expose it as an operation for consumers.  However, the WebInvoke attribute has been included as well.  Notice the UriTemplate that allows you do define how the query string parameters can be mapped into the method parameters.  The WCF runtime handles all of the heavy lifting for you.

It should be noted there are several parameters for WebInvoke that mirror WebGet: BodyStyle, RequestFormat, ResponseFormat, and UriTemplate.  I will refrain from going into the details of those parameters since I have already done so in my post on WebGet.  However, there is one parameter in WebInvoke that is not present in WebGet: Method. 

The Method parameter specifies the HTTP verb that corresponds to the operation.  By default, POST is the applied verb unless you specify otherwise.  I only specified POST in the example to be explicit and highlight the parameter does exist, but the behavior would be the same if I had omitted it completely.  The method that you specify in the parameter requires the corresponding HTTP verb in any request that is dispatched to the operation.  For example, if you specify PUT as the method parameter, but the request to the operation uses the POST verb, an exception will be thrown back to the client.

Remember, one of the great things about enabling the web programming model is that it doesn't require a proxy.  Here is a simple example of invoking the operation using System.Net.WebClient:

System.Net.WebClient client = new System.Net.WebClient();



1/3/2008 2:30:06 PM #


I am trying to create a WCF service which accept URL-encoded HTTP(s) POST containing a single name/value pair where the parameter name equals “xml”.

Please help me.

http:// |

4/11/2012 12:40:01 AM #


Pingback from impulsecoding.wordpress.com

WCF: Multiple Service Types on an app « Impulse Coding

impulsecoding.wordpress.com |

3/1/2015 1:37:46 PM #


Pingback from blog.kj-soft.ir

وبلاگ کیوان سافت ‫کار با اسکنر در برنامه های تحت وب (قسمت دوم و آخر)

blog.kj-soft.ir |

Comments are closed

About Me

I'm a passionate software developer and advocate of the Microsoft .NET platform.  In my opinion, software development is a craft that necessitates a conscious effort to continually improve your skills rather than falling into the trap of complacency.  I was also awarded as a Microsoft MVP in Connected Systems in 2008, 2009, and 2010.

Can’t code withoutThe best C# & VB.NET refactoring plugin for Visual Studio
Follow jeff_barnes on Twitter

View Jeff Barnes's profile on LinkedIn


Shared Items


Anything you read or see on this site is solely based on my own thoughts.  The material on this site does not necessarily reflect the views of my employer or anyone else.  In other words, I don't speak for anyone other than myself.  So, don't assume I am the official spokesperson for anyone.