2012-05-26 11 views
6

Pracuję w .NET już od jakiegoś czasu, ale jestem nowy w WCF. Próbuję utworzyć moją pierwszą usługę WCF przy użyciu JSON. Pomyślałem, że zacznę naprawdę, naprawdę prosto, a potem zacznę od tego. Ale jakoś udało mi się zepsuć nawet najprostsze usługi. Oto, co mam do tej pory.WCF Json GET Usługa: Sprawdź, czy EndpointAdress nadawcy i odbiorcy zgadzają się na

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> 

Następnie w pliku usług:

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

I w interfejsie:

namespace MarathonInfo 
{ 
    [ServiceContract] 
    public interface IMarathonInfo 
    { 

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

Więc, kiedy idę do tego adresu URL:

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

otrzymuję ten błąd:

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.

Jestem w stanie wykonać usługę dobrze przez Visual Studio w trybie debugowania. Ale w przeglądarce dostaję tylko ten błąd.

Co robię źle?

Dzięki!

Casey

Odpowiedz

15

Jeśli chcesz utworzyć WCF WebHTTP punkt końcowy (czyli taki, który zwraca JSON, i wykorzystuje [WebGet]/[WebInvoke] atrybuty), punkt końcowy musi mieć zachowanie <webHttp/> z nim związane .

<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

Dzięki! To wystarczy! – user1418704

+0

To naprawdę działa. Dzięki. –