2011-11-30 9 views
6

Próbuję zbudować podstawową REST usługę internetową, która po prostu zwraca aktualny czas i gdy próbuję uzyskać następujący błąd, gdy próbuję zadzwonić mój serwis http://localhost:PORT/TimeService/CurrentTime:Spokojny serwis internetowy Endpoint Nie znaleziono

Nie znaleziono punktu końcowego. Proszę zobaczyć stronę pomocy serwisowej do konstruowania ważnych żądań do usługi .

Co robię źle? Poniżej znajdziesz cały kod, którego używam. Dziękuję

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

nie wiem od ręki, czy to powoduje execption ale nie brakuje w '[]' OperationContract atrybut metody CurrentTime –

+0

Od .NET 4.0 nie trzeba się [OperationContract ] jeśli masz już [WebGet] lub [WebInvoke] dla usług HTTP WCF. – carlosfigueira

+0

W którym vdir/application znajduje się plik global.asax? Jeśli nie jest bezpośrednio w katalogu głównym IIS, adres powinien być http: // localhost: PORT/ApplicationName/TimeService/CurrentTime – carlosfigueira

Odpowiedz

4

Jest to jeden z najlepszych artykułów znalazłem kiedy po raz pierwszy rozpoczął naukę odpoczynek; Chciałbym, abym był w pracy, teraz mam dokładny przykład usługi TimeService, ale po prostu szukałem go w Google i nie mogłem znaleźć tego samego kodu.

Jutro z pracy opublikuję działający przykład, jeśli nadal będzie potrzebny.

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

+1

Byłoby to bardzo pomocne, gdybyś mógł to opublikować. – atrljoe

Powiązane problemy