I am trying to create a basic REST web service that just returns the current time, and whenever I try to get the following error when I try to call my service http: // localhost: PORT / TimeService / CurrentTime :
Endpoint not found. See the service help page for valid service requests.
What am I doing wrong? Below you can find all the code I'm using. Thank you.
Service1.cs
using System; using System.Collections.Generic; using System.Linq; using System.ServiceModel; using System.ServiceModel.Activation; using System.ServiceModel.Web; using System.Text; namespace WcfRestService1 { [ServiceContract] [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)] public class TimeService { [WebGet(UriTemplate = "CurrentTime")] public string CurrentTime() { return DateTime.Now.ToString(); } } }
Global.asax
using System; using System.ServiceModel.Activation; using System.Web; using System.Web.Routing; namespace WcfRestService1 { public class Global : HttpApplication { void Application_Start(object sender, EventArgs e) { RegisterRoutes(); } private static void RegisterRoutes() { RouteTable.Routes.Add(new ServiceRoute("TimeService", new WebServiceHostFactory(), typeof(TimeService))); } } }
web.config
<?xml version="1.0"?> <configuration> <system.web> <compilation debug="true" targetFramework="4.0" /> </system.web> <system.webServer> <modules runAllManagedModulesForAllRequests="true"> <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /> </modules> </system.webServer> <system.serviceModel> <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/> <standardEndpoints> <webHttpEndpoint> <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true"/> </webHttpEndpoint> </standardEndpoints> </system.serviceModel> </configuration>
source share