次の 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)));
}
どれだけ詳細を提供できるかわかりません。ご覧のとおり、提供されたソリューションを考慮して、提案されたすべてを実装しましたが、役に立ちませんでした。WebGet
Web レイヤー サービスの URL をブラウザーに配置するか、SOAPUI を使用してこのメソッドをヒットしようとしているか、サービス クライアントを使用して C# 単体テストでヒットしようとしても、WebGet
. ご協力いただきありがとうございます。
そして興味深いことに、App-layer URL は機能します。しかし、Web レイヤーはそうではありません。そう:
localhost/Utilities.AppService/API/GetFileInfo
動作しますが、
localhost/Utilities.WebService/API/GetFileInfo
ではない。