WCF サービスの REST / SOAP エンドポイント 質問する

WCF サービスの REST / SOAP エンドポイント 質問する

私は WCF サービスを持っており、それを RESTfull サービスと SOAP サービスの両方として公開したいと考えています。これまでにこのようなことをしたことがある人はいますか?

ベストアンサー1

2つの異なるエンドポイントでサービスを公開できます。SOAPでは、basicHttpBindingなどのSOAPをサポートするバインディングを使用でき、RESTfulではwebHttpBindingを使用できます。RESTサービスはJSONであると想定していますが、その場合は、次の動作構成で2つのエンドポイントを構成する必要があります。

<endpointBehaviors>
  <behavior name="jsonBehavior">
    <enableWebScript/>
  </behavior>
</endpointBehaviors>

シナリオにおけるエンドポイント構成の例は次のとおりです。

<services>
  <service name="TestService">
    <endpoint address="soap" binding="basicHttpBinding" contract="ITestService"/>
    <endpoint address="json" binding="webHttpBinding"  behaviorConfiguration="jsonBehavior" contract="ITestService"/>
  </service>
</services>

そのため、このサービスは

RESTfulにするには、操作コントラクトに[WebGet]を適用します。例:

public interface ITestService
{
   [OperationContract]
   [WebGet]
   string HelloWorld(string text)
}

注意: REST サービスが JSON 形式でない場合、操作のパラメータに複合型を含めることはできません。

SOAP および RESTful POX(XML) の投稿に返信

戻り形式として単純な古い XML を使用する場合、これは SOAP と XML の両方で機能する例です。

[ServiceContract(Namespace = "http://test")]
public interface ITestService
{
    [OperationContract]
    [WebGet(UriTemplate = "accounts/{id}")]
    Account[] GetAccount(string id);
}

RESTプレーン オールド XMLの POX 動作

<behavior name="poxBehavior">
  <webHttp/>
</behavior>

エンドポイント

<services>
  <service name="TestService">
    <endpoint address="soap" binding="basicHttpBinding" contract="ITestService"/>
    <endpoint address="xml" binding="webHttpBinding"  behaviorConfiguration="poxBehavior" contract="ITestService"/>
  </service>
</services>

サービスは以下でご利用いただけます

RESTリクエストをブラウザで試してください。

http://www.example.com/xml/accounts/A123

サービス参照を追加した後のSOAPサービスのSOAPリクエストクライアントエンドポイント構成、

  <client>
    <endpoint address="http://www.example.com/soap" binding="basicHttpBinding"
      contract="ITestService" name="BasicHttpBinding_ITestService" />
  </client>

C#で

TestServiceClient client = new TestServiceClient();
client.GetAccount("A123");

別の方法としては、2 つの異なるサービス コントラクトを公開し、それぞれに特定の構成を持たせるという方法があります。これにより、コード レベルで重複が生成される場合がありますが、最終的には、それを機能させる必要があります。

おすすめ記事