240 lines
5.7 KiB
C++
240 lines
5.7 KiB
C++
|
|
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
|
|
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
|
|
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
|
|
// PARTICULAR PURPOSE.
|
|
//
|
|
// Copyright (c) Microsoft Corporation. All rights reserved
|
|
|
|
#ifndef UNICODE
|
|
#define UNICODE
|
|
#endif
|
|
#include "WebServices.h"
|
|
#include "process.h"
|
|
#include "stdio.h"
|
|
#include "string.h"
|
|
#include "wincrypt.h"
|
|
#include "CalculatorServiceWithPolicy.wsdl.h"
|
|
|
|
// Print out rich error info
|
|
void PrintError(
|
|
_In_ HRESULT errorCode,
|
|
_In_opt_ WS_ERROR* error)
|
|
{
|
|
wprintf(L"Failure: errorCode=0x%lx\n", errorCode);
|
|
|
|
if (errorCode == E_INVALIDARG || errorCode == WS_E_INVALID_OPERATION)
|
|
{
|
|
// Correct use of the APIs should never generate these errors
|
|
wprintf(L"The error was due to an invalid use of an API. This is likely due to a bug in the program.\n");
|
|
}
|
|
|
|
HRESULT hr = S_OK;
|
|
if (error != NULL)
|
|
{
|
|
ULONG errorCount;
|
|
hr = WsGetErrorProperty(error, WS_ERROR_PROPERTY_STRING_COUNT, &errorCount, sizeof(errorCount));
|
|
if (FAILED(hr))
|
|
{
|
|
goto Exit;
|
|
}
|
|
for (ULONG i = 0; i < errorCount; i++)
|
|
{
|
|
WS_STRING string;
|
|
hr = WsGetErrorString(error, i, &string);
|
|
if (FAILED(hr))
|
|
{
|
|
goto Exit;
|
|
}
|
|
wprintf(L"%.*s\n", string.length, string.chars);
|
|
}
|
|
}
|
|
Exit:
|
|
if (FAILED(hr))
|
|
{
|
|
wprintf(L"Could not get error string (errorCode=0x%lx)\n", hr);
|
|
}
|
|
}
|
|
|
|
HANDLE closeServer = NULL;
|
|
|
|
|
|
HRESULT CALLBACK Add(
|
|
_In_ const WS_OPERATION_CONTEXT* context,
|
|
_In_ int a,
|
|
_In_ int b,
|
|
_Out_ int* result,
|
|
_In_ const WS_ASYNC_CONTEXT* asyncContext,
|
|
_In_ WS_ERROR* error)
|
|
{
|
|
UNREFERENCED_PARAMETER(context);
|
|
UNREFERENCED_PARAMETER(asyncContext);
|
|
UNREFERENCED_PARAMETER(error);
|
|
|
|
*result = a + b;
|
|
printf ("%d + %d = %d\n", a, b, *result);
|
|
fflush(stdout);
|
|
return S_OK;
|
|
}
|
|
|
|
HRESULT CALLBACK Subtract(
|
|
_In_ const WS_OPERATION_CONTEXT* context,
|
|
_In_ int a,
|
|
_In_ int b,
|
|
_Out_ int* result,
|
|
_In_ const WS_ASYNC_CONTEXT* asyncContext,
|
|
_In_ WS_ERROR* error)
|
|
{
|
|
UNREFERENCED_PARAMETER(context);
|
|
UNREFERENCED_PARAMETER(asyncContext);
|
|
UNREFERENCED_PARAMETER(error);
|
|
|
|
*result = a - b;
|
|
printf ("%d - %d = %d\n", a, b, *result);
|
|
fflush(stdout);
|
|
return S_OK;
|
|
}
|
|
|
|
HRESULT CALLBACK CloseChannelCallback(
|
|
_In_ const WS_OPERATION_CONTEXT* context,
|
|
_In_ const WS_ASYNC_CONTEXT* asyncContext)
|
|
{
|
|
UNREFERENCED_PARAMETER(context);
|
|
UNREFERENCED_PARAMETER(asyncContext);
|
|
|
|
SetEvent(closeServer);
|
|
return S_OK;
|
|
}
|
|
|
|
static const DefaultBinding_ICalculatorFunctionTable calculatorFunctions = {Add, Subtract};
|
|
|
|
// Method contract for the service
|
|
static const WS_SERVICE_CONTRACT calculatorContract =
|
|
{
|
|
&CalculatorServiceWithPolicy_wsdl.contracts.DefaultBinding_ICalculator, // comes from the generated header.
|
|
NULL, // for not specifying the default contract
|
|
&calculatorFunctions // specified by the user
|
|
};
|
|
|
|
|
|
// Main entry point
|
|
int __cdecl wmain()
|
|
{
|
|
|
|
HRESULT hr = S_OK;
|
|
WS_SERVICE_HOST* host = NULL;
|
|
WS_SERVICE_ENDPOINT* serviceEndpoint = NULL;
|
|
const WS_SERVICE_ENDPOINT* serviceEndpoints[1];
|
|
WS_ERROR* error = NULL;
|
|
WS_HEAP* heap = NULL;
|
|
const WS_STRING url = WS_STRING_VALUE(L"https://localhost:8443/example");
|
|
|
|
|
|
WS_SERVICE_PROPERTY_CLOSE_CALLBACK closeCallbackProperty = {CloseChannelCallback};
|
|
WS_SERVICE_ENDPOINT_PROPERTY serviceProperties[1];
|
|
serviceProperties[0].id = WS_SERVICE_ENDPOINT_PROPERTY_CLOSE_CHANNEL_CALLBACK;
|
|
serviceProperties[0].value = &closeCallbackProperty;
|
|
serviceProperties[0].valueSize = sizeof(closeCallbackProperty);
|
|
|
|
// Create an error object for storing rich error information
|
|
hr = WsCreateError(
|
|
NULL,
|
|
0,
|
|
&error);
|
|
if (FAILED(hr))
|
|
{
|
|
goto Exit;
|
|
}
|
|
|
|
// Create a heap to store deserialized data
|
|
hr = WsCreateHeap(
|
|
/*maxSize*/ 2048,
|
|
/*trimSize*/ 512,
|
|
NULL,
|
|
0,
|
|
&heap,
|
|
error);
|
|
if (FAILED(hr))
|
|
{
|
|
goto Exit;
|
|
}
|
|
|
|
hr = DefaultBinding_ICalculator_CreateServiceEndpoint(
|
|
NULL,
|
|
&url,
|
|
(DefaultBinding_ICalculatorFunctionTable*)&calculatorFunctions,
|
|
NULL,
|
|
serviceProperties,
|
|
WsCountOf(serviceProperties),
|
|
heap,
|
|
&serviceEndpoint,
|
|
error);
|
|
|
|
if (FAILED(hr))
|
|
{
|
|
goto Exit;
|
|
}
|
|
serviceEndpoints[0] = serviceEndpoint;
|
|
|
|
hr = WsCreateServiceHost(serviceEndpoints, 1, NULL, 0, &host, error);
|
|
if (FAILED(hr))
|
|
{
|
|
goto Exit;
|
|
}
|
|
|
|
// WsOpenServiceHost to start the listeners in the service host
|
|
hr = WsOpenServiceHost(host, NULL, error);
|
|
if (FAILED(hr))
|
|
{
|
|
goto Exit;
|
|
}
|
|
|
|
// Create Event object for closing the server
|
|
closeServer = CreateEvent(
|
|
NULL,
|
|
TRUE,
|
|
FALSE,
|
|
NULL);
|
|
if (closeServer == NULL)
|
|
{
|
|
hr = HRESULT_FROM_WIN32(GetLastError());
|
|
goto Exit;
|
|
}
|
|
WaitForSingleObject(closeServer, INFINITE);
|
|
|
|
// Close the service host
|
|
hr = WsCloseServiceHost(host, NULL, error);
|
|
if (FAILED(hr))
|
|
{
|
|
goto Exit;
|
|
}
|
|
|
|
Exit:
|
|
if (FAILED(hr))
|
|
{
|
|
// Print out the error
|
|
PrintError(hr, error);
|
|
}
|
|
if (host != NULL)
|
|
{
|
|
WsFreeServiceHost(host);
|
|
}
|
|
|
|
|
|
if (heap != NULL)
|
|
{
|
|
WsFreeHeap(heap);
|
|
}
|
|
if (error != NULL)
|
|
{
|
|
WsFreeError(error);
|
|
}
|
|
if (closeServer != NULL)
|
|
{
|
|
CloseHandle(closeServer);
|
|
}
|
|
fflush(stdout);
|
|
return SUCCEEDED(hr) ? 0 : -1;
|
|
}
|
|
|