2011-11-30 1 views
6

Je suis en train de construire un service Web REST de base qui retourne simplement l'heure et chaque fois que j'essaie de je reçois l'erreur suivante chaque fois que je tente d'appeler mon service http://localhost:PORT/TimeService/CurrentTime:service RESTful Web Endpoint introuvable

Point de terminaison non trouvé. Veuillez consulter la page d'aide du service pour construire les demandes valides au service.

Qu'est-ce que je fais mal? Ci-dessous vous pouvez trouver tout le code que j'utilise. Merci Vous

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

Je ne sais pas si de la main qui provoque la Execption mais ne sont pas vous manque le '[OperationContract]' attribut sur la méthode CurrentTime –

+0

Depuis .NET 4.0, vous n'avez pas besoin [OperationContract ] plus si vous avez [WebGet] ou [WebInvoke] pour les services HTTP WCF. – carlosfigueira

+0

Dans quelle vdir/application se trouve global.asax? Si ce n'est pas directement à la racine IIS, l'adresse doit être http: // localhost: PORT/NomApplication/TimeService/CurrentTime – carlosfigueira

Répondre

4

Ceci est l'un des meilleurs articles que j'ai trouvé quand j'ai commencé à apprendre du repos; Je voudrais être au travail maintenant, j'ai l'exemple exact TimeService mais je viens de googler et ne pouvait pas trouver le même code. Demain du travail Je posterai un exemple de travail si vous en avez encore besoin.

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

+1

Ce serait très utile si vous pouviez poster cela. – atrljoe