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>
Ho appena copiato e incollato il file come hai detto tu! Mi hai salvato la giornata! Eccellente! Molte grazie! – user1087261