2012-05-26 14 views
6

Tôi đã làm việc trong .NET một thời gian rồi, nhưng tôi mới làm quen với WCF. Tôi đang cố gắng để tạo ra dịch vụ WCF đầu tiên của tôi bằng cách sử dụng JSON. Tôi nghĩ rằng tôi sẽ bắt đầu thực sự, thực sự đơn giản và sau đó xây dựng từ đó. Nhưng tôi bằng cách nào đó đã xoay xở để thậm chí là dịch vụ đơn giản nhất. Đây là những gì tôi đã có cho đến nay.WCF Json GET Service: Kiểm tra xem EndpointAddresses của người gửi và người nhận có đồng ý không

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> 

Sau đó, trong tập tin dịch vụ:

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

Và trong giao diện:

namespace MarathonInfo 
{ 
    [ServiceContract] 
    public interface IMarathonInfo 
    { 

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

Vì vậy, khi tôi đi đến url này:

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

tôi nhận được lỗi này:

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.

tôi có thể thực hiện các dịch vụ tốt thông qua Visual Studio trong chế độ gỡ lỗi. Nhưng trong trình duyệt, tôi chỉ nhận được lỗi đó.

Tôi đang làm gì sai?

Cảm ơn!

Casey

Trả lời

15

Nếu bạn muốn tạo một WCF WebHTTP Endpoint (ví dụ, một trong đó trả về JSON, và sử dụng [WebGet]/[WebInvoke] thuộc tính), các thiết bị đầu cuối cần phải có <webHttp/> hành vi liên kết với nó .

<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

Cảm ơn! Điều đó đã làm các trick !! – user1418704

+0

Nó thực sự hoạt động. Cảm ơn. –

Các vấn đề liên quan