2011-12-08 33 views
8

Khi cố tạo một dịch vụ đơn giản để trả về một chuỗi JSON đơn giản bằng cách làm theo một số hướng dẫn. Tôi bị kẹt trên hai máy khác nhau với yêu cầu HTTP Statuscode 400. Ví dụ hướng dẫn Dịch vụ WCF RESTful với JSON pt.1 & pt.2 - http://www.youtube.com/watch?v=5BbDxB_5CZ8C# 4.0 WCF REST JSON - HTTP GET CODE 400 Yêu cầu không hợp lệ

Tôi có còn Google và tìm kiếm ở đây (StackOverflow) cho vấn đề tương tự nhưng không thành công.

Vấn đề là tôi nhận được 400 yêu cầu xấu khi cố gắng thực hiện kiểm tra sanity để duyệt đến dịch vụ WCF và thực thi phương thức. Bằng cách biên soạn dịch vụ và duyệt qua địa chỉ này: http://localhost:49510/Service1.svc/GetPerson Giống như hướng dẫn. Tôi đã thử tìm một giải pháp trong 3 ngày. Bất kỳ trợ giúp được đánh giá cao.

Đây là những gì tôi làm.

Đầu tiên tôi tạo một dự án mới một ứng dụng Dịch vụ WCF đơn giản. Tôi xóa mặc định Service1.svc và thêm một dịch vụ WCF mới, tạo ra một Service1.svc mới và một IService1.cs

Đây là mã cho giao diện (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; } 
    } 
} 

đây là mã cho 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" }; 
     } 
    } 
} 

và web.config là hoang sơ và tìm kiếm thích web.config này

<?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> 

Trả lời

11

để nghỉ ngơi WCF Bạn phải làm thiết lập ràng buộc và thiết bị đầu cuối trong web.config

Thay thế toàn bộ web của bạn.cấu hình bằng cách làm theo và nó sẽ làm việc

<?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> 

Bạn đã được còn lại với sau 2 điều

Sử dụng webHttpBinding (thay đổi cổng http mặc định bản đồ để webHttpBinding)

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

<system.serviceModel> 

Chỉ định webHttp End Point Hành vi

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

Tôi vừa sao chép và dán tệp như bạn đã nói! Bạn đã cứu ngày của tôi! Xuất sắc! Cảm ơn rất nhiều! – user1087261

4

Bạn không chỉ định bất kỳ điểm cuối ... Theo mặc định trên WCF 4, điểm cuối sử dụng basicHttpBinding sẽ được sử dụng. Nó sẽ không hoạt động ở đây vì nó là một ràng buộc dựa trên SOAP. Những gì bạn muốn sử dụng là webHttpBinding mà là dựa trên REST ...

Dưới đây là làm thế nào để ghi đè mặc định ràng buộc với WCF 4:

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

Bạn cũng phải kích hoạt webHttp bằng cách thêm hành vi đầu cuối này trong thư mục cấu hình:

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

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

0

Tôi không hoàn toàn chắc chắn lý do tại sao, nhưng khi tôi thêm thuộc tính 'Nhà máy' để tập .SVC của tôi (bạn cần phải kéo một cách rõ ràng để Visual Studio), tất cả mọi thứ chỉ làm việc - mà không có bất kỳ thay đổi mặc định cài đặt trong Web.config!

tôi thêm Factory = "System.ServiceModel.Activation.WebServiceHostFactory" nên tập .SVC của tôi đã đi từ này:

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

này:

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

Các chỉ có tác dụng phụ có vẻ là khi bạn nhấp vào tệp .SVC trong trình duyệt, bạn sẽ gặp lỗi 'Không tìm thấy điểm cuối', nhưng dịch vụ hoạt động tốt khi bạn tham gia voke nó một cách chính xác anyway. Như đã đề cập trước đó, tôi đang sử dụng Web.config mặc định với .NET 4.6 (Simplified WCF configuration), vì vậy tôi có thể chưa thêm chi tiết điểm cuối để làm việc lại.

Lưu ý người kiểm duyệt: lời xin lỗi của tôi về việc đăng câu trả lời này lên một vài câu hỏi. Sẽ không làm điều đó một lần nữa. Tuy nhiên, tôi không nghĩ rằng việc xóa nó từ BOTH câu hỏi là rất cân bằng. Đây là lý do tại sao tôi đã đăng lại câu trả lời này chỉ ở đây.

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