gSOAP和.NET兼容性

时间:2014-08-01 13:15:43

标签: .net web-services wsdl gsoap

我正在使用gSOAP来创建由基于Linux的系统托管的Web服务。

我从以下头文件(mytest.h)开始:

#ifndef MYTEST_H
#define MYTEST_H

//gsoap ns service name: mytest
//gsoap ns service namespace: urn:mytest
//gsoap ns service location: http://localhost/mytest

struct ns__testStruct {
    char *field1;
    char *field2;
};

int ns__setdata(struct ns__testStruct data, void);
int ns__getdata(struct ns__testStruct *data);

#endif // MYTEST_H

然后我运行gSOAP编译器以获得相应的框架/存根类和相应的WSDL。我使用soapcpp2 -e -j mytest.h运行编译器,并获得以下WSDL:

<?xml version="1.0" encoding="UTF-8"?>
<definitions name="mytest"
 targetNamespace="urn:mytest"
 xmlns:tns="urn:mytest"
 xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
 xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
 xmlns:ns="urn:mytest"
 xmlns:SOAP="http://schemas.xmlsoap.org/wsdl/soap/"
 xmlns:HTTP="http://schemas.xmlsoap.org/wsdl/http/"
 xmlns:MIME="http://schemas.xmlsoap.org/wsdl/mime/"
 xmlns:DIME="http://schemas.xmlsoap.org/ws/2002/04/dime/wsdl/"
 xmlns:WSDL="http://schemas.xmlsoap.org/wsdl/"
 xmlns="http://schemas.xmlsoap.org/wsdl/">

<types>

 <schema targetNamespace="urn:mytest"
  xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
  xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  xmlns:ns="urn:mytest"
  xmlns="http://www.w3.org/2001/XMLSchema"
  elementFormDefault="unqualified"
  attributeFormDefault="unqualified">
  <import namespace="http://schemas.xmlsoap.org/soap/encoding/"/>
 </schema>

</types>

<message name="setdata">
 <part name="data" type="ns:testStruct"/><!-- ns__setdata::data -->
</message>

<message name="getdata">
</message>

<message name="testStruct">
 <part name="field1" type="xsd:string"/><!-- ns__getdata::field1 -->
 <part name="field2" type="xsd:string"/><!-- ns__getdata::field2 -->
</message>

<portType name="mytestPortType">
 <operation name="setdata">
  <documentation>Service definition of function ns__setdata</documentation>
  <input message="tns:setdata"/>
 </operation>
 <operation name="getdata">
  <documentation>Service definition of function ns__getdata</documentation>
  <input message="tns:getdata"/>
  <output message="tns:testStruct"/>
 </operation>
</portType>

<binding name="mytest" type="tns:mytestPortType">
 <SOAP:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
 <operation name="setdata">
  <SOAP:operation style="rpc" soapAction=""/>
  <input>
     <SOAP:body use="encoded" namespace="urn:mytest" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
  </input>
 </operation>
 <operation name="getdata">
  <SOAP:operation style="rpc" soapAction=""/>
  <input>
     <SOAP:body use="encoded" namespace="urn:mytest" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
  </input>
  <output>
     <SOAP:body use="encoded" namespace="urn:mytest" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
  </output>
 </operation>
</binding>

<service name="mytest">
 <documentation>gSOAP 2.8.17r generated service definition</documentation>
 <port name="mytest" binding="tns:mytest">
  <SOAP:address location="http://localhost/mytest"/>
 </port>
</service>

</definitions>

然后,使用此WSDL我想在C#(VS2013)中创建一个简单的客户端,但是当我尝试导入WSDL时,Visual Studio失败并出现以下错误:

  

自定义工具错误:无法导入WebService / Schema。无法   从命名空间'urn:mytest'导入绑定'mytest'。无法导入   操作'setdata'。缺少数据类型'urn:mytest:testStruct'。

为什么呢?有关于此的任何想法吗?

1 个答案:

答案 0 :(得分:1)

好的,找到了解决方案here

我复制/粘贴链接页面的相关内容:

class test__X {
long id;
};

int test__f(test__X, bool &);
int test__g(test__X &);
  

这是缺少响应包装器结构的典型示例   test__g。问题是doc / lit样式需要test__X   定义为模式元素,因为它是响应消息   test__g。因为它也被定义为complexType,一个冲突   就产生了。

     

在这种情况下,您必须将响应参数包装在struct中,   喜欢:test__g(struct test__gResult {test__X;}&amp;);

     

由于gSOAP,不需要包含原始类型(如bool)   将为您生成一条响应消息test__fResponse。

所以,在我的情况下,我必须修改我的mytest.h,如下所示:

#ifndef MYTEST_H
#define MYTEST_H

//gsoap ns service name: mytest
//gsoap ns service namespace: urn:mytest
//gsoap ns service location: http://localhost/mytest

struct ns__testStruct {
    char *field1;
    char *field2;
};

int ns__setdata(struct ns__testStruct data, void);
int ns__getdata(struct ns__getDataResponse { struct ns__testStruct d; } *data);

#endif // MYTEST_H