例: SOAPヘッダーを追加する
このシナリオ例では、利用中のSOAP WebサービスのすべてのリクエストのSOAPヘッダーに新しい要素を追加します。
ヒント: BeforeSendRequest
関数に対して必要な調整を行って新しいヘッダーを追加する代わりに、提供されたサンプルを調整してリクエストのSOAPヘッダーを削除または変更できます。
コードの概要
これを実行するには、以下の処理を実行するC# コードを作成する必要があります。
-
定義したアクションに対応してIntegration Studioによって作成された関数プレースホルダで、実装する必要があるカスタムエンドポイント動作を登録します。
-
次に、WCF(Windows Communication Foundation)から
IEndpointBehavior
インターフェイスを実装するエンドポイント動作クラスを作成します。
このクラスは、(次に実装する別のカスタムクラスに基づいて)メッセージインスペクタをクライアントのランタイムに追加します。これは新しいSOAPヘッダー要素を追加する役割を果たします。 -
SOAP WebサービスのすべてのリクエストにSOAPヘッダー要素を追加するメッセージインスペクタクラスを作成し、
BeforeSendRequest
関数にコードを追加します。
例
以下の手順の実行方法について具体例を見てみましょう。
1.Integration Studioで、エクステンションを作成してアクションを定義します。このアクションは、リクエストの送信前にSOAPリクエスト要素を追加するカスタムメッセージインスペクタを登録します。
この例では、入力パラメータを持たない「RegisterSoapHeaderCallback」というアクションをIntegration Studioで定義しています。
2.[Edit Source Code .NET]をクリックします。プロジェクトのターゲットフレームワークを設定し、System.ServiceModel
アセンブリへの参照を追加します。
以下のコードを入力し、Integration Studioで作成されたMssRegisterSoapHeaderCallback
関数プレースホルダを置き換えます。
// required 'using' statements at the beginning of the file using System.ServiceModel; using System.ServiceModel.Channels; using System.ServiceModel.Description; using System.ServiceModel.Dispatcher; using OutSystems.SOAP.API; /* ...*/ // replace the 'MssRegisterSoapHeaderCallback' function placeholder with the following code public void MssRegisterSoapHeaderCallback() { SoapRequest.RegisterEndpointBehavior(new AddSoapHeaderBehavior()); }
3.WCFのインターフェイスであるIEndpointBehavior
を実装するカスタムクラスAddSoapHeaderBehavior
を作成し、以下のコードを含めます。
class AddSoapHeaderBehavior : IEndpointBehavior { void IEndpointBehavior.ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime) { clientRuntime.ClientMessageInspectors.Add(new AddSoapHeaderMessageInspector()); // you can add more message inspectors here: // clientRuntime.ClientMessageInspectors.Add(new [...]); } void IEndpointBehavior.AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters) { // do nothing } void IEndpointBehavior.ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher) { // do nothing } void IEndpointBehavior.Validate(ServiceEndpoint endpoint) { // do nothing } }
注記: このユースケースの場合、コードを追加する必要があるのはApplyClientBehavior
関数のみです。
4.WCFのインターフェイスIClientMessageInspector
を実装するカスタムクラスAddSoapHeaderMessageInspector
を作成し、以下のコードを含めます。
class AddSoapHeaderMessageInspector : IClientMessageInspector { object IClientMessageInspector.BeforeSendRequest(ref Message request, IClientChannel channel) { // before sending a request, add a new SOAP header, specifying its name, namespace and value request.Headers.Add(MessageHeader.CreateHeader("Custom", "http://schemas.com", "header value")); return request; } void IClientMessageInspector.AfterReceiveReply(ref Message reply, object correlationState) { // here you would handle the web service response } }
このクラスでは、ユースケースに応じて、リクエストを処理する関数(BeforeSendRequest
)または応答を処理する関数(AfterReceiveReply
)のいずれか、あるいはその両方にコードを追加できます。
SOAPリクエストヘッダーに要素を追加するため、BeforeSendRequest
関数にコードを追加します。
5.Visual Studio .NETを停止し、Integration Studioに戻ります。Integration Studioで、[1-Click Publish]ツールバーアイコンをクリックするか、またはF5
キーを押して、エクステンションをパブリッシュします。
6.Service Studioで、アプリケーションモジュールにエクステンションの「RegisterSoapHeaderCallback」アクションへの参照を追加します。
7.SOAP WebサービスのSOAPコールバックフロー(OnBeforeRequestAdvanced)で、RegisterSoapHeaderCallbackアクションをフローにドラッグします。
8.アプリケーションモジュールをパブリッシュしてアプリケーションをテストし、以下のSOAPリクエストのサンプルのように、追加した要素「Custom」がWebサービスのリクエストのSOAPヘッダーに含まれていることを確認します。
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope"> <s:Header> <Custom xmlns="http://schemas.com">header value</Custom> </s:Header> <s:Body> ... </s:Body> </s:Envelope>