2011-12-14 28 views
2

.NET 4.0でWCF RESTサービスを設定しています。 GET要求は機能していますが、サーバーへのデータのPOSTを含む要求は、HTTP 400 Bad Requestで失敗します。WCF REST 4.0 PUT/POST/DELETEが機能しない(400 Bad Request)

この私の単純なサービスされています

[ServiceContract] 
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)] 
public class Service1 
{ 
    [WebGet(UriTemplate = "")] 
    public string HelloWorld() 
    { 
     return "hello world"; 
    } 

    [WebInvoke(UriTemplate = "", Method = "POST")] 
    public string HelloWorldPost(string name) 
    { 
     return "hello " + name; 
    } 
} 

私のWeb.config:

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

    <system.web> 
    <compilation debug="true" targetFramework="4.0" /> 
    </system.web> 

    <system.webServer> 
    <modules runAllManagedModulesForAllRequests="true"> 
     <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /> 
    </modules> 
    </system.webServer> 

    <system.serviceModel> 
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/> 
    <protocolMapping> 
     <add scheme="http" binding="webHttpBinding" />  
    </protocolMapping> 
    <standardEndpoints> 
     <webHttpEndpoint> 
     <!-- 
      Configure the WCF REST service base address via the global.asax.cs file and the default endpoint 
      via the attributes on the <standardEndpoint> element below 
     --> 
     <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true"/> 
     </webHttpEndpoint> 
    </standardEndpoints> 
    </system.serviceModel> 

</configuration> 

そして、私のGlobal.asax:

public class Global : HttpApplication 
{ 
    void Application_Start(object sender, EventArgs e) 
    { 
     RegisterRoutes(); 
    } 

    private void RegisterRoutes() 
    { 
     RouteTable.Routes.Add(new ServiceRoute("Service1", new WebServiceHostFactory(), typeof(Service1))); 
    } 
} 

基本的に、すべてがテンプレートからデフォルトですただ、Service1を簡略化しました。私はデバッガを使って実行し、Fiddlerを介してリクエストを渡してIISで実行し、同じことをやっているだけでなく、POSTをフェイクするためのシンプルなコンソールアプリケーションを使ってみましたが、いつも400 Bad Requestエラーが発生し、 。私はインターネット上のすべてを見て、何かを把握することはできません。

私は、次の要求の例(いずれも作業)の両方を試してみた:

XML:

<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">String content</string> 

JSON:

"String content" 
+0

なぜ残りのタグが削除されたのですか?この投稿はREST Webサービスに関するものです。 – shuniar

+0

この投稿はHTTP、WCF、ASP、.NETなどについての質問です。 RESTアーキテクチャ実装上の問題であり、設計やアーキテクチャの問題ではありません。 –

+0

GET版のHelloWorldは機能しますか? –

答えて

6

あなたに正しくContent-Typeヘッダーを設定しています要求? XMLリクエストの場合はtext/xml、JSONの場合はapplication/jsonにする必要があります。 FiddlerでContent-Typeを設定すると、あなたのコードは私のために働きます。また、あなたが応答がしてなりたいどのような形式に応じて、text/xmlまたはapplication/jsonのいずれかにあなたのGETでAcceptヘッダーを設定する必要があり

。サーバーが同じで応答をしたいと仮定しますとそれは、POSTのために大丈夫ですweb.configにautomaticFormatSelectionEnabled="true"が設定されているため、リクエストとして使用できません。ここでWCF RESTのフォーマット選択に関する詳細があります:http://blogs.msdn.com/b/endpoint/archive/2010/01/18/automatic-and-explicit-format-selection-in-wcf-webhttp-services.aspx

+0

ありがとう、私はそれが私が行方不明だった些細なものであることを帽子を知っていたが、私はそれを理解することができませんでした。適切なヘッダーを追加した後で完璧に動作します。 – shuniar

1

属性は実装に含まれていてはならず、操作契約に含まれている必要があります。また、UriTemplateに名前付きパラメータが含まれていることを確認する必要があります。大文字と小文字が区別されるので、正確に一致させる必要があります。

IService.cs

[ServiceContract] 
public class IService1 
{ 
    [WebGet(UriTemplate = "")] 
    [OperationContract] 
    public string HelloWorld(); 

    [WebInvoke(UriTemplate = "/{name}", Method = "POST")] 
    [OperationContract] 
    public string HelloWorldPost(string name); 
} 

Service.csあなたは

<system.serviceModel> 
    <services> 
     <service name="Service1"> 
     <endpoint address="basic" binding="basicHttpBinding" contract="IService1" /> 
     </service> 
    <services> 
</system.serviceModel> 
System.ServiceModel

下だけでなく、あなたのweb.configファイルでサービスを設定する必要が

public class Service1 : IService 
{ 

    public string HelloWorld() 
    { 
     return "hello world"; 
    } 

    public string HelloWorldPost(string name) 
    { 
     return "hello " + name; 
    } 
} 

これは主なコンセプトの一部です。正しい方向に向かってスタートさせる必要があります。良いテストプロジェクトを始めるには、VS2010の "WCF Application"プロジェクトテンプレートを使用してください。それはあなたのために必要なもののほとんどを配線しています。お役に立てれば!

+0

私の実際のサービスはこのように実装されていますが、なぜGETが機能していたのかPOSTが理解できないのか分からないので、これは単なるテストプロジェクトです。また、リクエスト本文に渡す場合は、URIに必須のパラメータではありません。これは役立つ投稿ですが、ありがとうございます。 – shuniar

+0

BodyStyle属性を使用して、バインダーがパラメーターの入力方法を認識するようにしましたか?ラップされたWebMessageFormatを使用して、RequestFormatをJSONに設定します。次に、リクエスト本体に各パラメータのプロパティを持つシンプルなJSONオブジェクトを投稿できます。非常にきれいな!しかし、プロパティ名で大文字小文字が正しいことを確認してください。 – jerrylroberts

関連する問題