Hello - I'm trying to configure my first REST WCF service. I've implemented other types of WCF services in the past. I think I have the interface part figured out. Here's the method I have defined in the interface to get a customer by ID:
For WCF to know how to find this rest interface, you will need to add a service route. You need to implement (on the application_start global.asax) something similar to this:
RouteTable.Routes.Add(new ServiceRoute("Service1", new WebServiceHostFactory(), typeof(Service1)));
This allows for the routing to work. Keep in mind that for WCF to work as a restful service, it needs to behave like an HTTP application. This means that you also need to add the following to the service class:
[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class Service1
You should take a look at the WCF restful service application template for Visual Studio:
dotnetterAMG...
Member
234 Points
510 Posts
need help finishing this rest setup
Nov 23, 2012 07:08 AM|LINK
Hello - I'm trying to configure my first REST WCF service. I've implemented other types of WCF services in the past. I think I have the interface part figured out. Here's the method I have defined in the interface to get a customer by ID:
[OperationContract]
[WebGet(UriTemplate = "/Customers/{id}")]
Customer GetCustomer(string id);
I've defined my service endpoint in Web.config like this:
<service name="RestTest">
<endpoint
address="RestTest"
binding="webHttpBinding"
contract="IRestTest"
behaviorConfiguration="webHttp"/>
</service>
I'm able to view the svc in the browser without issue like this:
http://localhost/WcfTester/RestTest.svc
However, I want to get a customer by ID like this:
http://localhost/WcfTester/RestTest/Customers/5
When I try the url above I get a 404 error. What kind of changes do I need to make to be able to call my REST service in the way described above?
ozkary
Contributor
2034 Points
303 Posts
Re: need help finishing this rest setup
Nov 23, 2012 04:25 PM|LINK
For WCF to know how to find this rest interface, you will need to add a service route. You need to implement (on the application_start global.asax) something similar to this:
RouteTable.Routes.Add(new ServiceRoute("Service1", new WebServiceHostFactory(), typeof(Service1)));
This allows for the routing to work. Keep in mind that for WCF to work as a restful service, it needs to behave like an HTTP application. This means that you also need to add the following to the service class:
[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class Service1
You should take a look at the WCF restful service application template for Visual Studio:
http://visualstudiogallery.msdn.microsoft.com/fbc7e5c1-a0d2-41bd-9d7b-e54c845394cd
og-bit.com