Annoying thing with WCF service is its ".svc" extension. Most of us , developers, think of having clean neat urls like in ASP.Net MVC, while there are lot of workarounds to create custom url, here is the simple one as described in the blog
http://geekswithblogs.net/michelotti/archive/2010/08/21/restful-wcf-services-with-no-svc-file-and-no-config.aspx
Here is a overview of the article published in the above link
Building a RESTful WCF service in this way is quite easy and you don’t need rely on any item templates. Just start with a blank asp.net web application project in Visual Studio. The web.config file is virtually empty:
Next just add a regular C# class - for this example, I call mine
PersonService and it will have basic CRUD operations. Typically we
create interfaces in WCF to define our ServiceContract and operations
like this:
Keep in mind, this step is *not* required. You *could* decorate your
service class directly with these attributes and not even have the
interface at all. However, in this case, it’s a nice convenience to
encapsulate all the WCF attributes on the interface rather than your
implementation class. The implementation of our PersonService class
looks like this.
This is just a normal C# class that implement an interface. It’s also
decorated with the typical WebGet/WebInvoke attributes (in the System.ServiceModel.Web
namespace) that we use for RESTful services. Also notice that 3 of the 4
methods have the same UriTemplate but they are differentiated by the
HTTP method (i.e., GET/PUT/DELETE). Also notice the
AspNetCompatibilityRequirements attribute – this is needed for RESTful
services that are processed in the ASP.NET pipeline as described here. You also have to add this to the config file (I know, I know – I said “no config” but I meant “no ugly WCF endpoint config”!):
So how do we take this regular C# class and make it into a service without an *.svc file? The System.Web.Routing infrastructure has now been incorporated into WCF 4 to make this possible. Just add the line of code (line #5) to your global.asax:
The WebServiceHostFactory
makes RESTful services possible in WCF. The first parameter of my
ServiceRoute constructor is an empty string which means that my URI of
my service will hang right off of the root – this get combined with the
UriTemplate defined in the WebGet/WebInvoke attributes. So to get a
person from my service, a URI would look like this: http://mydomain.com/Person(21).
If I had specified “foo” instead of an empty string in the first
parameters of the ServiceRoute constructor, then my URI would look like
this: http://mydomain.cmo/foo/Person(21).
That’s it! You now have a fully functioning RESTful WCF service that you can fully test with Fidder for all HTTP verbs.
One interesting aspect to all this is that you can do all this in MVC as well. In fact, I typically do use MVC to return JSON to views for AJAX calls in my MVC apps. However, if you were building stand-alone services for this, would MVC be easier than the example of above? Keep in mind, we could simplify the example above even further by eliminating the IPersonService interface all together. I daresay that setting up RESTful routes like the ones shown above is easier with WCF than MVC (this, coming from an “MVC guy”) because we can apply the UriTemplates directly to the methods. To accomplish the same in MVC, you have to create custom route constraints to avoid the name of the C# methods from showing up in the URL (and honoring the REST HTTP verbs). If you are going to do this, I really like the approach shown here. It’s a cool approach, but it’s *more* work than just doing it with RESTful WCF – no svc file and no configuration.
In fact, using the WCF infrastructure gets you even more features for free. For example, if we add 1 more line of configuration (check out line #5 below) we get a help page and automatic format selection for free! Now our entire configuration just looks like this (and still no WCF endpoint configuration needed):
Notice all we had to do to get the help page was the request “/help”:
http://geekswithblogs.net/michelotti/archive/2010/08/21/restful-wcf-services-with-no-svc-file-and-no-config.aspx
Here is a overview of the article published in the above link
Building a RESTful WCF service in this way is quite easy and you don’t need rely on any item templates. Just start with a blank asp.net web application project in Visual Studio. The web.config file is virtually empty:
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
</configuration>
[ServiceContract]
interface IPersonService
{
[OperationContract]
Person GetPerson(string id);
[OperationContract]
Person InsertPerson(Person person);
[OperationContract]
Person UpdatePerson(string id, Person person);
[OperationContract]
void DeletePerson(string id);
}
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class PersonService : IPersonService
{
[WebGet(UriTemplate = "Person({id})")]
public Person GetPerson(string id)
{
}
[WebInvoke(UriTemplate = "Person", Method = "POST")]
public Person InsertPerson(Person person)
{
}
[WebInvoke(UriTemplate = "Person({id})", Method = "PUT")]
public Person UpdatePerson(string id, Person person)
{
}
[WebInvoke(UriTemplate = "Person({id})", Method = "DELETE")]
public void DeletePerson(string id)
{
}
}
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
</system.serviceModel>
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
RouteTable.Routes.Add(new ServiceRoute("", new WebServiceHostFactory(), typeof(PersonService)));
}
}
That’s it! You now have a fully functioning RESTful WCF service that you can fully test with Fidder for all HTTP verbs.
One interesting aspect to all this is that you can do all this in MVC as well. In fact, I typically do use MVC to return JSON to views for AJAX calls in my MVC apps. However, if you were building stand-alone services for this, would MVC be easier than the example of above? Keep in mind, we could simplify the example above even further by eliminating the IPersonService interface all together. I daresay that setting up RESTful routes like the ones shown above is easier with WCF than MVC (this, coming from an “MVC guy”) because we can apply the UriTemplates directly to the methods. To accomplish the same in MVC, you have to create custom route constraints to avoid the name of the C# methods from showing up in the URL (and honoring the REST HTTP verbs). If you are going to do this, I really like the approach shown here. It’s a cool approach, but it’s *more* work than just doing it with RESTful WCF – no svc file and no configuration.
In fact, using the WCF infrastructure gets you even more features for free. For example, if we add 1 more line of configuration (check out line #5 below) we get a help page and automatic format selection for free! Now our entire configuration just looks like this (and still no WCF endpoint configuration needed):
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
<standardEndpoints>
<webHttpEndpoint>
<standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true"/>
</webHttpEndpoint>
</standardEndpoints>
</system.serviceModel>
No comments:
Post a Comment