【问题标题】:Consuming a .NET WebService using a C++ client使用 C++ 客户端使用 .NET WebService
【发布时间】:2013-12-21 09:54:13
【问题描述】:

我有一个查询数据库并返回这些查询结果的网络服务。 然而,webservice 不能直接访问数据库,而是使用用户创建的库查询它,并使用特定的方法来检索用户、名称等。 我通过选择 add > reference 将库包含在 webservice 项目中,并且我的 webservice 代码没有编译错误或警告。 我已经使用 svcutil.exe 和 wsutil.exe 创建了客户端库,一切正常,没有警告。 在我的客户端代码上,我收到“处理 tempuri.org.xsd 时出错”、“为 DataSet ''生成代码时出错。”和“无法将输入 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();
}

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

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

我已经用谷歌搜索了这个错误,但似乎找不到答案。我希望有人可以帮助我解决这个问题,因为我在这个问题上被困了好几天!

谢谢!

【问题讨论】:

    标签: c# c++ web-services


    【解决方案1】:

    我实际上只是偶然解决了这个问题!原来我将svcutil.exe和wsutil.exe生成的所有文件都放在了项目页面上(包括svcutil.exe生成的.xsd和.wsdl)。我还在 Visual Studio 项目中添加了所有项目,但很明显,我只包括了 .h 文件。我以为编译器只会查看 .h 和相应的 .c 文件,但事实证明,它也在查看 .xsd 和 .wsdl。当我删除这些文件时,编译器完美运行! 所以,PEBKAC... 不知道这是否与 Visual Studio 相关,或者是否可以使用 gc++ 重现此行为。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-04-04
      • 2011-04-06
      • 2010-09-16
      • 1970-01-01
      • 2019-01-04
      • 2017-07-28
      • 1970-01-01
      相关资源
      最近更新 更多