C#WCF服务的C ++客户端

时间:2014-10-29 18:24:14

标签: c# c++ wcf visual-c++

我创建了简单的WCF服务:

namespace WcfServiceLibrary1
{
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        int GetData(int value);
    }
}

namespace WcfServiceLibrary1
{
    public class Service1 : IService1
    {
        public int  GetData(int value)
        {
            return 101;
        }


    }
}

现在我需要为它创建c ++客户端。 我已经生成了wsdl以及之后的.h.c个文件。整个c ++客户端:

#ifndef UNICODE
#define UNICODE
#endif
#include "WebServices.h"
#include "process.h"
#include "stdio.h"
#include "string.h"
//#include "CalculatorService.wsdl.h"
#include "WebServices.h"
#include "..\..\schemas.microsoft.com.2003.10.Serialization.xsd.h"
#include "..\..\tempuri.org.xsd.h"
#include "..\..\tempuri.org.wsdl.h"





// Print out rich error info
void PrintError(HRESULT errorCode, 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");
        DebugBreak();
    }

    HRESULT hr = NOERROR;
    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);
    }
}

// Main entry point
int __cdecl wmain(int argc, __in_ecount(argc) wchar_t **argv)
{
    UNREFERENCED_PARAMETER(argc);
    UNREFERENCED_PARAMETER(argv);

    WS_HTTP_BINDING_TEMPLATE templ = {};
    HRESULT hr = NOERROR;
    WS_ERROR* error = NULL;
    WS_HEAP* heap = NULL;
    WS_SERVICE_PROXY* proxy = NULL;

    int result = 0;
    WS_ENDPOINT_ADDRESS address = {};

    WS_STRING url= WS_STRING_VALUE(L"http://localhost:8733/Design_Time_Addresses/WcfServiceLibrary1/Service1/mex");
    address.url = url;

    // 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 = BasicHttpBinding_IService1_CreateServiceProxy(&templ, NULL, 0, &proxy, error);


    hr = WsOpenServiceProxy(
        proxy, 
        &address, 
        NULL, 
        error);
    if (FAILED(hr))
    {
        goto Exit;
    }



        hr = BasicHttpBinding_IService1_GetData(
        proxy, 
        4, 
        &result, 
        heap, 
        NULL, 
        0, 
        NULL, 
        error);
    if (FAILED(hr))
    {
        goto Exit;
    }

    wprintf(L"%d + %d = %d\n", 1, 2, result);

Exit:
    if (FAILED(hr))
    {
        // Print out the error
        PrintError(hr, error);
    }
    fflush(
        stdout);
    if (proxy != NULL)
    {
        WsCloseServiceProxy(
            proxy, 
            NULL, 
            NULL);

        WsFreeServiceProxy(
            proxy);
    }


    if (heap != NULL)
    {
        WsFreeHeap(heap);
    }
    if (error != NULL)
    {
        WsFreeError(error);
    }
    fflush(stdout);
    return SUCCEEDED(hr) ? 0 : -1;
}

在输出中我有错误:

Failure: errorCode=0x803d0000
There was an error communicating with the endpoint at 'http://localhost:8733/Design_Time_Addresses/WcfServiceLibrary1/Service1/mex'.
The server returned HTTP status code '415 (0x19F)' with text 'Cannot process the message because the content type 'text/xml; charset=utf-8' was not the expected type 'application/soap+xml; charset=utf-8'.'.
The format of the HTTP request was not supported by the server.

如何找到问题?

0 个答案:

没有答案