使用C ++客户端使用.NET WebService

时间:2013-12-21 09:54:13

标签: c# c++ web-services

我有一个查询数据库的Web服务,并返回这些查询的结果。 但是,webservice不能直接访问DB,而是使用用户创建的库查询它,并使用特定方法检索用户,名称等。 我通过选择add>将我的库包含在webservice项目中。引用,我的Web服务代码没有编译错误或警告。 我使用svcutil.exe和wsutil.exe创建了客户端库,一切正常,没有警告。 在我的客户端代码上,我得到一个"处理tempuri.org.xsd",#34;生成DataSet的代码时出错#''。"和& #34;无法将输入xml文件转换为DataSet。嵌套表' Utilizador'继承相应命名空间的不能在不同的命名空间中有多个表"。 最后一个错误被翻译成葡萄牙语,我尽力将其翻译成英语,所以YMMV ......' Utilizador'是我的数据库上的表名和访问数据库的库上的类名。 我使用此代码来使用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;
}

这是接口的WebService代码:

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();
}

这是实现接口的Class的代码:

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;
    }
}

我搜索了这个错误,似乎无法找到答案。我希望有人能帮助我,因为我已经在这个问题上被困了好几天了!

谢谢!

1 个答案:

答案 0 :(得分:0)

我昨晚实际上只是偶然解决了这个问题!事实证明,我将svcutil.exe和wsutil.exe生成的所有文件放在项目页面上(包括由svcutil.exe生成的.xsd和.wsdl)。我还将所有项目添加到visual studio项目中,但显然,我只包含.h文件。我认为编译器只会查看.h和相应的.c文件,但事实证明,它也在查看.xsd和.wsdl。当我删除这些文件时,编译器完美无缺! 那么,PEBKAC ...... Dunno,如果这是与Visual Studio相关的,或者如果这个行为可以用gc ++重现,例如。

相关问题