Can anybody tell me why is this REST service not working?
namespace WCFRestExample
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
public class HelloWorld : IHelloWorld
{
[WebGet(UriTemplate="/", ResponseFormat=WebMessageFormat.Xml)]
public string GetData()
{
return "HIIII";
}
}
}
namespace WCFRestExample
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
[ServiceContract]
public interface IHelloWorld
{
[OperationContract]
string GetData();
}
}
This is my web.config:
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
I am able to browse the service .svc and I get the page correctly but when I browse:
http://localhost:60503/HelloWorld.svc/GetData
I get a 404 page. Can anybody tell me what is happening and where can I find tutorials for WCF REST? This is the simplest service one could create and even that is not working for me.
Thanks in advance:)
You’re not defining anywhere that this should be a REST service – neither do you have an endpoint in your web.config that uses the
webHttpBinding, nor did you specify in your*.svcfile to use theWebServiceHostFactory.The simplest fix would be to fix the svc file to:
With this, your svc file now defines it wants to use the
WebServiceHost(the host that understand REST) to host your service…Check out An Introduction To RESTful Services With WCF for a great intro to REST services with WCF.