2012-04-12 8 views
3

Je crée un algorithme squelette d'un service reposant montrant comment gérer les requêtes post et get. de mon exemple, ça marche bien, mais pas le post. Je suppose que je devrais ajouter des choses à web.config, mais je ne sais pas quoi et pourquoi. merci d'avance, Zoli.wcf erreur de configuration du service repos

[ServiceContract] 
public interface IRestfulService 
{ 
    [OperationContract] 
    [WebGet(UriTemplate = "/GetAStudent")] 
    Student GetExistingStudent(); 

    [OperationContract] 
    [WebInvoke(UriTemplate = "/GetTheGivenStudent/{studentName}", Method = "POST")] 
    Student GetGivenStudent(string studentName); 
} 



public class RestfulService : IRestfulService 
{ 
    public Student GetExistingStudent() 
    { 
     Student stdObj = new Student 
     { 
      StudentName = "Foo", 
      Age = 29, 
      Mark = 95 
     }; 
     return stdObj; 
    } 

    public Student GetGivenStudent(string studentName) 
    { 
     Student stdObj = new Student 
     { 
      StudentName = studentName, 
      Age = 29, 
      Mark = 95 
     }; 
     return stdObj; 
    } 
} 

[DataContract] 
public class Student 
{ 
    [DataMember] 
    public string StudentName { get; set; } 
    [DataMember] 
    public int Age { get; set; } 
    [DataMember] 
    public double Mark { get; set; } 
} 

web.config:

<system.web> 
    <compilation debug="true" targetFramework="4.0" /> 
</system.web> 
<system.serviceModel> 
    <protocolMapping> 
     <add scheme="http" binding="webHttpBinding"/> 
    </protocolMapping> 


    <behaviors> 
     <serviceBehaviors> 
      <behavior> 
       <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment --> 
       <serviceMetadata httpGetEnabled="true"/> 
       <!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information --> 
       <serviceDebug includeExceptionDetailInFaults="false"/> 
      </behavior> 

     </serviceBehaviors> 
     <endpointBehaviors> 
      <behavior> 
       <webHttp /> 
      </behavior > 
     </endpointBehaviors> 

    </behaviors> 


    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> 
</system.serviceModel> 
<system.webServer> 
    <modules runAllManagedModulesForAllRequests="true"/> 
</system.webServer> 

+2

lequel vous attendez-vous à travailler comme POST? le premier n'a pas de méthode déclarée, et l'autre seulement GET – Michel

+2

Quelle exception obtenez-vous? –

+0

J'ai modifié le post. maintenant c'est vrai, je m'attends à ce que la seconde fonctionne comme poste. L'erreur que je reçois est: le point de terminaison n'est pas trouvé –

Répondre

0

Vous n'avez pas besoin d'exposer le point de terminaison pour un service MEX REST. Votre web.config devrait ressembler à ceci:

<?xml version="1.0" encoding="UTF-8"?> 
<configuration> 
    <system.web> 
    <compilation debug="true" targetFramework="4.0" /> 
    </system.web> 
    <system.serviceModel> 
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" /> 
    <services> 
     <service name="BookService"> 

     <!-- Expose an XML endpoint: --> 
     <endpoint name="xml" 
       address="xml" 
       binding="webHttpBinding" 
       contract="BookStore.Contracts.IBookService" 
       behaviorConfiguration="poxBehavior" /> 

     <!-- Expose a JSON endpoint: --> 
     <endpoint name="json" 
       address="json" 
       binding="webHttpBinding" 
       contract="BookStore.Contracts.IBookService" 
       behaviorConfiguration="jsonBehavior" /> 
     </service> 
    </services> 
    <behaviors> 
     <endpointBehaviors> 
     <behavior name="poxBehavior"> 
      <webHttp /> 
     </behavior> 
     <endpointBehaviors> 
     <behavior name="jsonBehavior"> 
      <enableWebScript /> 
     </behavior> 
     </endpointBehaviors> 
    </behaviors> 
    </system.serviceModel> 
</configuration> 

Ce qui précède exposer deux points d'extrémité, qui utilise des données XML, et qui utilise JSON. Exposer deux points de terminaison comme celui-ci est complètement facultatif bien sûr; c'est juste un exemple de ce que vous pourriez faire. J'aime aussi utiliser le routage pour les services REST; quelque chose comme dans votre Global.asax.cs:

protected void Application_Start(object sender, EventArgs e) 
{ 
    RouteTable.Routes.Add(
     new System.ServiceModel.Activation.ServiceRoute("books", 
      new System.ServiceModel.Activation.WebServiceHostFactory(), 
      typeof(BookStore.Services.BookService) 
     ) 
    ); 
} 

qui, en utilisant les critères d'évaluation ci-dessus dans l'exemple web.config, permettrait au service à accéder à ceci:

http://yourdomain.com/books/xml 

et si vous choisissez d'utiliser ou d'ajouter le point de terminaison json, comme ceci:

http://yourdomain.com/books/json