2011-11-30 47 views
6

Tôi cố gắng để xây dựng một dịch vụ web REST cơ bản mà chỉ đơn giản trả về thời gian hiện tại và bất cứ khi nào tôi cố gắng tôi nhận được lỗi sau bất cứ khi nào tôi cố gắng gọi cho dịch vụ của tôi http://localhost:PORT/TimeService/CurrentTime:dịch vụ web Restful Endpoint không tìm thấy

Không tìm thấy điểm cuối. Vui lòng xem trang trợ giúp dịch vụ để xây dựng yêu cầu hợp lệ cho dịch vụ.

Tôi đang làm gì sai? Dưới đây bạn có thể tìm thấy tất cả mã tôi đang sử dụng. Cảm ơn

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

Tôi không biết tắt tay nếu điều đó làm cho execption nhưng không bạn thiếu '[OperationContract]' thuộc tính vào phương pháp CurrentTime –

+0

Kể từ .NET 4.0 bạn không cần [OperationContract ] nữa nếu bạn có [WebGet] hoặc [WebInvoke] cho các dịch vụ WCF HTTP. – carlosfigueira

+0

Trong đó vdir/application là global.asax nằm ở đâu? Nếu nó không trực tiếp tại thư mục gốc IIS, thì địa chỉ phải là http: // localhost: PORT/ApplicationName/TimeService/CurrentTime – carlosfigueira

Trả lời

4

Đây là một trong những điều tốt nhất mà tôi tìm thấy khi tôi lần đầu tiên bắt đầu học tập nghỉ ngơi; Tôi ước tôi đã làm việc ngay bây giờ tôi có ví dụ TimeService chính xác nhưng tôi chỉ google và không thể tìm thấy cùng một mã.

Ngày mai từ nơi làm việc, tôi sẽ đăng một ví dụ hoạt động nếu bạn vẫn cần.

http://geekswithblogs.net/michelotti/archive/2010/08/21/restful-wcf-services-with-no-svc-file-and-no-config.aspx

+1

Điều đó sẽ rất hữu ích nếu bạn có thể đăng bài đó. – atrljoe

Các vấn đề liên quan