2011-12-08 17 views
8

Quando si tenta di creare un servizio semplice per restituire una semplice stringa JSON seguendo diversi tutorial. Mi blocco su due macchine diverse con una richiesta errata di Statuscode 400 HTTP. Esempio tutorial RESTful servizio WCF con JSON pt.1 & pt.2 - http://www.youtube.com/watch?v=5BbDxB_5CZ8C# 4.0 WCF REST JSON - HTTP GET CODE 400 Richiesta errata

ho anche cercato Google e qui (StackOverflow) per il problema simile senza successo.

Il problema è che ottengo la 400 cattiva richiesta quando provo a fare un controllo di integrità per cercare il servizio WCF ed eseguire il metodo. Compilando il servizio e sfoglia questo indirizzo: http://localhost:49510/Service1.svc/GetPerson Proprio come il tutorial. Ho provato a trovare una soluzione per come 3 giorni. Qualsiasi aiuto è apprezzato.

Questo è quello che faccio.

Per prima cosa, creo un nuovo progetto, una semplice applicazione di servizio WCF. A cancellare il default Service1.svc e aggiungere un nuovo servizio WCF, che generano una nuova Service1.svc e un IService1.cs

Ecco il codice per l'interfaccia (IService1.cs)

namespace WcfService1 
{ 
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together. 
    [ServiceContract] 
    public interface IService1 
    { 
     [OperationContract] 
     [WebInvoke(Method="GET", BodyStyle=WebMessageBodyStyle.Bare, ResponseFormat=WebMessageFormat.Json, RequestFormat=WebMessageFormat.Json, UriTemplate="GetPerson")] 
     Person GetPerson(); 
    } 

    [DataContract(Name="Person")] 
    public class Person 
    { 
     [DataMember(Name="name")] 
     public string Name { get; set; } 
    } 
} 

Ecco il codice per il Service1.svc

namespace WcfService1 
{ 
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together. 
    public class Service1 : IService1 
    { 
     public Person GetPerson() 
     { 
      return new Person() { Name = "Tobbe" }; 
     } 
    } 
} 

e il web.config è intatta e piace guardare questo web.config

<?xml version="1.0"?> 
<configuration> 

    <system.web> 
    <compilation debug="true" targetFramework="4.0" /> 
    </system.web> 
    <system.serviceModel> 
    <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> 
    </behaviors> 
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> 
    </system.serviceModel> 
<system.webServer> 
    <modules runAllManagedModulesForAllRequests="true"/> 
    </system.webServer> 

</configuration> 

risposta

11

per il riposo WCF Devi fare impostazione vincolante e degli endpoint in web.config

Sostituisci la tua intera rete.config seguendo e funzionerà

<?xml version="1.0"?> 
<configuration> 

    <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> 
</configuration> 

Eri rimasto con seguenti 2 cose

Usa webHttpBinding (porta HTTP predefinita mappatura modifica webHttpBinding)

<system.serviceModel> 
    <protocolMapping> 
     <add scheme="http" binding="webHttpBinding"/> 
    </protocolMapping> 
    <behaviors> 

<system.serviceModel> 

Specificare webHttp End Point Behaviors

<system.serviceModel> 
    ----- 
    </protocolMapping> 
    <behaviors> 
     <endpointBehaviors> 
      <behavior> 
       <webHttp /> 
      </behavior > 
     </endpointBehaviors> 
    <behaviors> 
    ------ 
<system.serviceModel> 
+0

Ho appena copiato e incollato il file come hai detto tu! Mi hai salvato la giornata! Eccellente! Molte grazie! – user1087261

4

Non è stato specificato alcun endpoint ... Per impostazione predefinita su WCF 4, verrà utilizzato un endpoint che utilizza basicHttpBinding. Non funzionerà qui perché è un'associazione basata su SOAP. Ciò che si vuole utilizzare è webHttpBinding che è basato su REST ...

Ecco come eseguire l'override di legame con WCF 4 di default:

<system.serviceModel> 
    <protocolMapping> 
    <add scheme="http" binding="webHttpBinding"/> 
    </protocolMapping> 
</system.serviceModel> 

È inoltre necessario abilitare webHttp aggiungendo questo comportamento endpoint nella tua config:

<behaviors> 
    <endpointBehaviors> 
     <behavior> 
      <webHttp /> 
     </behavior > 
    </endpointBehaviors> 
<behaviors> 

http://msdn.microsoft.com/en-us/library/bb924425.aspx

0

io non sono del tutto sicuro perché, ma quando ho aggiunto l'attributo 'fabbrica' per il mio file .SVC (è necessario trascinare esplicitamente a Visual Studio), tutto solo funziona - senza alcuna modifica ai valori predefiniti impostazioni in Web.config!

ho aggiunto fabbrica = "System.ServiceModel.Activation.WebServiceHostFactory" così il mio file .SVC è andato da questo:

<%@ ServiceHost Language="C#" Debug="true" Service="ServiceNameSpace.ServiceName" CodeBehind="ServiceName.svc.cs" %>

a questo:

<%@ ServiceHost Language="C#" Debug="true" Service="ServiceNameSpace.ServiceName" CodeBehind="ServiceName.svc.cs" Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>

Il l'unico effetto collaterale sembra essere che quando si fa clic sul file .SVC nel browser, si ottiene un errore "Endpoint non trovato", ma il servizio funziona correttamente quando si chiamarlo correttamente comunque. Come accennato in precedenza, sto usando un Web.config predefinito con .NET 4.6 (Simplified WCF configuration), quindi potrei aver bisogno di aggiungere i dettagli dell'endpoint affinché funzionino di nuovo.

Nota per il moderatore: mi scuso per aver postato questa risposta su un paio di domande. Non lo farò più. Tuttavia, non penso che eliminarlo da entrambe le domande fosse molto equilibrato. Questo è il motivo per cui ho ripubblicato questa risposta solo qui.

Problemi correlati