2012-05-26 8 views
6

He estado trabajando en .NET desde hace un tiempo, pero soy nuevo en WCF. Intento crear mi primer servicio WCF usando JSON. Pensé que comenzaría muy, muy simple y luego construiría desde allí. Pero de alguna manera he logrado arruinar incluso los servicios más simples. Esto es lo que tengo hasta ahora.WCF Servicio JSON GET: compruebe que EndpointAddresses del emisor y el receptor estén de acuerdo

Web.Config:

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

    <system.web> 
    <compilation debug="true" targetFramework="4.0" /> 
    </system.web> 
    <system.serviceModel> 
    <services> 
     <service name="MarathonInfo.MarathonInfoService"> 
     <endpoint address="http://localhost:10298/MarathonInfoService.svc" binding="webHttpBinding" contract="MarathonInfo.IMarathonInfo" /> 
     </service> 
    </services> 
    <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="false" /> 
    </system.serviceModel> 
<system.webServer> 
    <modules runAllManagedModulesForAllRequests="true"/> 
    </system.webServer> 

</configuration> 

A continuación, en el archivo de servicio:

namespace MarathonInfo 
{ 
    public class MarathonInfoService : IMarathonInfo 
    { 
     public String GetData() 
     { 
      return "Hello World"; 
     } 
    } 
} 

Y en la interfaz:

namespace MarathonInfo 
{ 
    [ServiceContract] 
    public interface IMarathonInfo 
    { 

     [OperationContract] 
     [WebInvoke(Method = "GET", UriTemplate = "/GetData", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] 
     String GetData(); 
    } 
} 

Así, cuando voy a la siguiente dirección:

http://localhost:10298/MarathonInfoService.svc/GetData 

me sale este error:

The message with To 'http://localhost:10298/MarathonInfoService.svc/GetData' cannot be processed at the receiver, due to an AddressFilter mismatch at the EndpointDispatcher. Check that the sender and receiver's EndpointAddresses agree.

soy capaz de ejecutar el servicio muy bien a través de Visual Studio en modo de depuración. Pero en el navegador, solo obtengo ese error.

¿Qué estoy haciendo mal?

Gracias!

Casey

Respuesta

15

Si desea crear un WCF WebHTTP punto final (es decir, uno que devuelve JSON, y utiliza los [WebGet] atributos/[WebInvoke]), el punto final debe tener el comportamiento <webHttp/> asociado a él .

<system.serviceModel> 
    <services> 
    <service name="MarathonInfo.MarathonInfoService"> 
     <endpoint address="http://localhost:10298/MarathonInfoService.svc" 
       binding="webHttpBinding" 
       contract="MarathonInfo.IMarathonInfo" 
       behaviorConfiguration="Web"/> 
    </service> 
    </services> 
    <behaviors> 
    <serviceBehaviors> 
     <behavior> 
     <serviceMetadata httpGetEnabled="true"/> 
     <serviceDebug includeExceptionDetailInFaults="false"/> 
     </behavior> 
    </serviceBehaviors> 
    <endpointBehaviors> 
     <behavior name="Web"> 
     <webHttp/> 
     </behavior> 
    </endpointBehaviors> 
    </behaviors> 
    <serviceHostingEnvironment multipleSiteBindingsEnabled="false" /> 
</system.serviceModel> 
+2

Gracias! Eso hizo el truco! – user1418704

+0

Realmente funciona. Gracias. –

Cuestiones relacionadas