14

次の API を使用した RESTful WCF Web サービスがあります。

[WebGet(ResponseFormat = WebMessageFormat.Json)]
MyResponseContract GetFileInfo();

(SOAPUI を使用して) エンドポイントをヒットしようとすると、次のエラー メッセージが表示されます。

サーバーでリクエストの処理中にエラーが発生しました。サービスへの有効なリクエストの作成については、サービスのヘルプ ページを参照してください。

GET メソッド呼び出しでヒットするように SOAPUI を設定しました。本文のない POST に切り替えると、次のメッセージで失敗します。

メソッドは許可されていません。

これは完全に理にかなっています: POST で GET をヒットすることはできません。そこで、コードを次のように更新しました。

[WebInvoke(ResponseFormat = WebMessageFormat.Json)]
MyResponseContract GetFileInfo();

そして今、POST メソッドを使用して SOAPUI から呼び出すと、機能します。奇妙。したがって、コードを次のように変更します。

[WebInvoke(ResponseFormat = WebMessageFormat.Json, Method = "GET")]
MyResponseContract GetFileInfo();

いくつかの投稿で、これが本質的にWebGet属性と同等であることを見てきました。これも機能しません。

私の質問は、WebGetパラメーターを受け入れていないか、カスタム UriTemplate を使用していないにもかかわらず、なぜこれが機能しないのですか?

ヒットしようとしている URL (IIS でローカルにホストされている) は次のとおりです。

http://localhost/Utilities/API/GetFileInfo

更新 以下のコメントと与えられた回答を考えると、私はまだこの問題に直面しています。いくつかの追加の詳細。

私の Web レイヤー web.config

<?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
    <customErrors mode="Off" />
  </system.web>
  <system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
    <standardEndpoints>
      <webHttpEndpoint>
        <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true" maxReceivedMessageSize="10000000" />
      </webHttpEndpoint>
    </standardEndpoints>
    <behaviors>
      <endpointBehaviors>
        <behavior name="exampleBehavior">
          <callbackDebug includeExceptionDetailInFaults="true" />
          <enableWebScript />
          <webHttp helpEnabled="true" />
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <bindings>
      <webHttpBinding>
        <binding name="WebHttpBinding" maxReceivedMessageSize="10000000" />
      </webHttpBinding>
    </bindings>
    <client>    
      <endpoint address="http://LOCALHOST/Utilities.AppService/API"
                binding="webHttpBinding" bindingConfiguration="WebHttpBinding"
                contract="Utilities.Common.API.IMyApi"
                behaviorConfiguration="exampleBehavior" />
    </client>
  </system.serviceModel>
</configuration>

私のアプリレイヤーのweb.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
    <customErrors mode="Off" />
  </system.web>
  <system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
    <standardEndpoints>
      <webHttpEndpoint>
        <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true" maxReceivedMessageSize="10000000" />
      </webHttpEndpoint>
    </standardEndpoints>
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
    <handlers>
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE" type="System.Web.Handlers.TransferRequestHandler" resourceType="Unspecified" requireAccess="Script" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
  </system.webServer>
</configuration>

私のサービスインターフェース

[ServiceContract(Namespace = "API")]
public interface IMyApi
{    
    [WebGet]
    MyResponseContract GetFileInfo();
}

私の Web レイヤーの実装

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)]
public class MyApiWebService : ClientBase<IMyApi>, IMyApi
{
    public MyResponseContract GetFileInfo()
    {
        return Channel.GetFileInfo();
    }
}

私のアプリレイヤーの実装

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)]
public class MyApiAppService : IMyApi
{
    public MyResponseContract GetFileInfo()
    {
        return new MyResponseContract();
    }
}

私の Web レイヤー Global.asax:

    protected void Application_Start(object sender, EventArgs e)
    {
        RouteTable.Routes.Add(new ServiceRoute("API", new WebServiceHostFactory(), typeof(MyApiWebService)));
    }

私のアプリ層 Global.asax:

    protected void Application_Start(object sender, EventArgs e)
    {
        RouteTable.Routes.Add(new ServiceRoute("API", new WebServiceHostFactory(), typeof(MyApiAppService)));
    }

どれだけ詳細を提供できるかわかりません。ご覧のとおり、提供されたソリューションを考慮して、提案されたすべてを実装しましたが、役に立ちませんでした。WebGetWeb レイヤー サービスの URL をブラウザーに配置するか、SOAPUI を使用してこのメ​​ソッドをヒットしようとしているか、サービス クライアントを使用して C# 単体テストでヒットしようとしても、WebGet. ご協力いただきありがとうございます。

そして興味深いことに、App-layer URL は機能します。しかし、Web レイヤーはそうではありません。そう:

localhost/Utilities.AppService/API/GetFileInfo

動作しますが、

localhost/Utilities.WebService/API/GetFileInfo

ではない。

4

2 に答える 2

4

デフォルトでは、.NET WCF Web サービスは、テキスト エンコードされた SOAP メッセージを送信するように設定されています。これは、HTTP メソッドが POST であり、呼び出すメソッドをサービスに伝えるために必要なヘッダーがあることを意味します。

サービス エンドポイントを使用して簡単な例を作成しました。これは、そのエンドポイントと通信するためにフィドラーから生成された要求です。

POST http://localhost/Utilities/API/GetFileInfo/Service1.svc HTTP/1.1
Content-Type: text/xml; charset=utf-8
SOAPAction: "http://tempuri.org/IService1/GetFileInfo"
Host: localhost:8888
Content-Length: 136
Expect: 100-continue
Connection: Keep-Alive

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><GetFileInfo xmlns="http://tempuri.org/"/></s:Body></s:Envelope>

の応答を返す

HTTP/1.1 200 OK
Cache-Control: private
Content-Length: 398
Content-Type: text/xml; charset=utf-8
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?QzpcV29ya1xFSVAgV29ya1xRdWVzdGlvbnNcV0NGR2V0VGVzdFxTZXJ2aWNlMS5zdmM=?=
X-Powered-By: ASP.NET
Date: Thu, 21 May 2015 19:47:49 GMT

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><GetFileInfoResponse xmlns="http://tempuri.org/"><GetFileInfoResult xmlns:a="http://schemas.datacontract.org/2004/07/WCFGetTest" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><a:BoolValue>true</a:BoolValue><a:StringValue>MyResponseContract </a:StringValue></GetFileInfoResult></GetFileInfoResponse></s:Body></s:Envelope>

あなたにとって、SoapUI の何かが正しく設定されていないと思います。投稿データまたはヘッダーのいずれか。

于 2015-05-21T19:49:28.153 に答える