Reputation: 6428
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:)
Upvotes: 1
Views: 1363
Reputation: 10306
It could be one reason that you did not set ServiceHostFactory properly
<%@ ServiceHost Language="C#"
Service="WCFRestExample.HelloWorld"
CodeBehind="HelloWorld.svc.cs"
Factory="System.ServiceModel.Activation.WebServiceHostFactory"
%>
Upvotes: 1
Reputation: 754248
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 *.svc
file to use the WebServiceHostFactory
.
The simplest fix would be to fix the svc file to:
<%@ ServiceHost Language="C#" Debug="true"
Service="WCFRestExample.HelloWorld" CodeBehind="HelloWorld.svc.cs"
Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>
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.
Upvotes: 3
Reputation: 2549
try http://localhost:60503/HelloWorld
, and show us your Global.asax
Upvotes: 1
Reputation: 3772
Try changing your URI template to:
[WebGet(UriTemplate="/GetData", ResponseFormat=WebMessageFormat.Xml)]
Update
The bit in the uri template can be anything, so it can be:
/GetMySuperComplicatedData
and still be mapped to GetData()
For more information read here: http://msdn.microsoft.com/en-us/library/bb675245.aspx
Upvotes: 2