2

.NET4.0でのWCFRESTサービスのセットアップに取り組んでいます。GETリクエストは機能していますが、サーバーへのデータのPOSTを伴うリクエストはすべて。で失敗しますHTTP 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"
4

2 に答える 2

6

Content-Typeリクエストでヘッダーを正しく設定していますか?XMLリクエストの場合はそうする必要がtext/xmlあり、JSONの場合はそうする必要がありますapplication/json。FiddlerでContent-Typeを設定すると、コードが機能します。

またAccept、GETのヘッダーを、応答の形式に応じtext/xmlて設定する必要がありapplication/jsonます。サーバーは、要求と同じ形式の応答が必要であると想定するため、POSTは問題ありません。automaticFormatSelectionEnabled="true"web.configに設定しました。WCF RESTでの形式の選択の詳細については、http://blogs.msdn.com/b/endpoint/archive/2010/01/18/automatic-and-explicit-format-selection-in-wcf-webhttp-servicesを参照してください。 .aspx

于 2011-12-18T18:27:28.487 に答える
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

public class Service1 : IService
{

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

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

Web.configファイルでもSystem.ServiceModelの下でサービスを構成する必要があります

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

これが主要な概念の一部であり、正しい方向に進むことができるはずです。優れたテストプロジェクトを開始する場合は、VS2010の「WCFアプリケーション」プロジェクトテンプレートを使用してください。必要な部品のほとんどが配線されています。お役に立てれば!

于 2011-12-19T20:21:39.843 に答える