2011-11-30 88 views
6

我试图建立一个简单的返回当前时间的基本REST Web服务,每当我尝试,我得到以下错误,每当我尝试打电话给我服务http://localhost:PORT/TimeService/CurrentTimeRESTful Web服务端点没有发现

未找到端点。请参阅服务帮助页面,以构建 有效的服务请求。

我在做什么错?下面你可以找到我使用的所有代码。谢谢

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> 
     <!-- 
      Configure the WCF REST service base address via the global.asax.cs file and the default endpoint 
      via the attributes on the <standardEndpoint> element below 
     --> 
     <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true"/> 
     </webHttpEndpoint> 
    </standardEndpoints> 
    </system.serviceModel> 

</configuration> 
+2

我不知道了手,如果引起execption但你难道不缺少对CURRENTTIME方法 –

+0

的'[OperationContract的]'属性由于.NET 4.0,你不需要[OperationContract的],如果你有WCF HTTP服务的[WebGet]或[WebInvoke]。 – carlosfigueira

+0

其中vdir/application是global.asax所在位置?如果它不是直接在IIS根目录下,那么地址应该是http:// localhost:PORT/ApplicationName/TimeService/CurrentTime – carlosfigueira

回答