サービスの作成
Web サービスの作成は、 サービス モデル API と WsUtil.exe ツールによって WWSAPI で大幅に簡略化されています。 サービス モデルは、サービスとクライアントが C メソッドの呼び出しとしてチャネル経由でメッセージを送受信できるようにする API を提供します。 WsUtil ツールは、サービスを実装するためのスタブとヘッダーを生成します。
WWSAPI を使用した電卓サービスの実装
Wsutil.exe ツールから生成されたソースを使用して、次の手順でサービスを実装します。
アプリケーション ソースにヘッダーを含めます。
#include "CalculatorProxyStub.h"
サービス操作を実装します。 この例では、サービス操作は電卓サービスの Add 関数と Subtract 関数です。
HRESULT CALLBACK Add (const WS_OPERATION_CONTEXT* context,
int a, int b, int* result,
const WS_ASYNC_CONTEXT* asyncContext,
WS_ERROR* error)
{
*result = a + b;
printf ("%d + %d = %d\n", a, b, *result);
return NOERROR;
}
HRESULT CALLBACK Subtract (const WS_OPERATION_CONTEXT* context,
int a, int b, int* result,
const WS_ASYNC_CONTEXT* asyncContext,
WS_ERROR* error)
{
*result = a - b;
printf ("%d - %d = %d\n", a, b, *result);
return NOERROR;
}
WS_SERVICE_CONTRACT構造体のフィールドを設定して、サービス コントラクトを定義します。
static const DefaultBinding_ICalculatorFunctionTable calculatorFunctions = {Add, Subtract};
static const WS_SERVICE_CONTRACT calculatorContract =
{
&DefaultBinding_ICalculatorContractDesc, // comes from the generated header.
NULL, // for not specifying the default contract
&calculatorFunctions // specified by the user
};
次に、サービス ホストを作成し、通信用に開きます。
WS_SERVICE_ENDPOINT serviceEndpoint = {0};
serviceEndpoint.uri.chars = L"https://+:80/example"; // address given as uri
serviceEndpoint.binding.channelBinding = WS_HTTP_CHANNEL_BINDING; // channel binding for the endpoint
serviceEndpoint.channelType = WS_CHANNEL_TYPE_REPLY; // the channel type
serviceEndpoint.uri.length = (ULONG)wcslen(serviceEndpoint.uri.chars);
serviceEndpoint.contract = (WS_SERVICE_CONTRACT*)&calculatorContract; // the contract
serviceEndpoint.properties = serviceProperties;
serviceEndpoint.propertyCount = WsCountOf(serviceProperties);
if (FAILED (hr = WsCreateServiceHost (&serviceEndpoint, 1, NULL, 0, &host, error)))
goto Error;
// WsOpenServiceHost to start the listeners in the service host
if (FAILED (hr = WsOpenServiceHost (host, NULL, error)))
goto Error;
電卓サービスの完全な実装については、 HttpCalculatorServiceExample のコード例を参照してください。