简体   繁体   中英

Consuming a .NET WebService using a C++ client

I have a webservice that queries a DB and returns the results of those queries. However the webservice does not have direct access to the DB, instead, it queries it using a user created library, with specific methods for retrieving Users, names, etc. I've included the library in the webservice project by selecting add > reference and I get no compiling errors or warnings on my webservice code. I have created the client libraries using svcutil.exe and wsutil.exe and everything went OK, without warnings. On my client code I get a "There was an error processing tempuri.org.xsd","Error generating code for DataSet ''.", and "Unable to convert input xml file to DataSet. The nested table 'Utilizador' which inherits the respective namespace cannot have multiple tables in different namespaces". The last error was translated to Portuguese, and I did my best to translate it back to English, so YMMV... 'Utilizador' is a table name on my DB and a class name on the library that accesses the DB. I'm using this code to consume the WebService:

#include "stdafx.h" 
#include "WebServices.h" 
#include "schemas.microsoft.com.2003.10.Serialization.xsd.h" 
#include "tempuri.org.xsd.h" 
#include "tempuri.org.wsdl.h"
#include <string> 
#include <iostream> 
#include <stdlib.h> 

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    unsigned int *listSize;
    Utilizador **arrayUser;
    HRESULT hr = ERROR_SUCCESS;
    WS_ERROR* error = NULL;
    WS_HEAP* heap = NULL;
    WS_SERVICE_PROXY* proxy = NULL;
    WS_ENDPOINT_ADDRESS address = {};
    WS_STRING url= WS_STRING_VALUE(L"http://localhost:51765/Graph4Social.svc");
    address.url = url;
    hr = WsCreateHeap(2048, 512, NULL, 0, &heap, error);
    WS_HTTP_BINDING_TEMPLATE templ = {};
    //criação do proxy para o serviço 
    //hr = BasicHttpBinding_IGraph4WebService_CreateServiceProxy(&templ, NULL, 0, &proxy, error);
    //WsCreateServiceProxy(&templ, NULL, 0, &proxy, error);
    hr = BasicHttpBinding_IGraph4Social_CreateServiceProxy(&templ, NULL, 0, &proxy, error);
    hr = WsCreateServiceProxy(WS_CHANNEL_TYPE_REQUEST,WS_HTTP_CHANNEL_BINDING,NULL,NULL,0,NULL,0,&proxy,error);
    hr = WsOpenServiceProxy(proxy,&address,NULL,error);

    hr = BasicHttpBinding_IGraph4Social_getUserList(proxy,listSize,&arrayUser,heap, NULL,0, NULL,error);

    for (unsigned int i = 0; i < *listSize; i++)
        cout << "ID Utilizador: " + arrayUser[i]->idUtilizador;
    return 0;
}

This is the WebService code for the Interface:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using TM_TDG.WithDataSets.BLL;
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IGraph4Social" in both code and config file together.
[ServiceContract]
public interface IGraph4Social
{
    [OperationContract]
    IList<Utilizador> getUserList();
}

And this is the code for the Class that implements the interface:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using TM_TDG.WithDataSets.BLL;
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Graph4Social" in code, svc and config file together.
public class Graph4Social : IGraph4Social
{
    public IList<Utilizador> getUserList()
    {
        RedeSocial rede = RedeSocial.getRedeSocial();
        IList<Utilizador> userList = rede.getUserList(true); //so queremos os utilizadores activos
        return userList;
    }
}

I have googled this error and can't seem to find an answer. I hope someone can help me with this, as I've been stuck for days on this issue!

Thanks!

I actually solved this last night by mere chance! It turns out I was placing all files generated by svcutil.exe and wsutil.exe on the project page(including the .xsd and .wsdl generated by svcutil.exe). I was also adding all items to the visual studio project, although, obviously, I was only including the .h files. I thought that the compiler was only going to look at the .h and the corresponding .c files, but it turns out, it was looking at the .xsd and .wsdl too. When I deleted those files, the compiler worked flawlessly! So, PEBKAC... Dunno if this is Visual Studio related or if this behaviour can be reproduced with gc++, for example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM