使用 C# 使用 php Soap 服务

发布于 2024-09-28 10:57:24 字数 3353 浏览 3 评论 0原文

我做了一个简单的Web服务

wsdl:

<wsdl:definitions name='mysum' >

<wsdl:types>
 <xsd:schema 
  xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
 xmlns:tns="http://www.my-uni-project.info/joomla/components/com_jv_vm_soa/"
 xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
 xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="mysum"
   targetNamespace="http://www.my-uni-project.info/joomla/components/com_jv_vm_soa/"
   xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">

   <xsd:complexType name="mysumRequest">
    <xsd:all>
     <xsd:element minOccurs="0" name="n1" type="xsd:int"/>
     <xsd:element minOccurs="0" name="n2" type="xsd:int"/>
    </xsd:all>
   </xsd:complexType>   

   <xsd:element name="mysumResponse" type="xsd:int"/>
  </xsd:schema>
 </wsdl:types>

 <wsdl:message name="mysumRequest">
   <wsdl:part name="parameters" element="tns:mysumRequest" />
 </wsdl:message> 
 <wsdl:message name="mysumResponse">
   <wsdl:part name="result" element="tns:mysumResponse" />
 </wsdl:message> 


 <wsdl:portType name="mysum">
  <wsdl:operation name="mysum">
   <wsdl:input message="tns:mysumRequest"/>
   <wsdl:output message="tns:mysumResponse"/>
  </wsdl:operation>
 </wsdl:portType> 

 <wsdl:binding name="mysumSOAP" type="tns:mysum">
  <soap:binding transport="http://schemas.xmlsoap.org/soap/http" />
  <wsdl:operation name="mysum">
   <soap:operation soapAction="mysum" />
   <wsdl:input>
    <soap:body use="literal" />
   </wsdl:input>
   <wsdl:output>
    <soap:body use="literal" />
   </wsdl:output>
  </wsdl:operation>
 </wsdl:binding> 

 <wsdl:service name="mysum">
  <wsdl:port name="mysumSOAP" binding="tns:mysumSOAP">
    <soap:address location="http://www.my-uni-
    project.info/joomla/components/com_jv_vm_soa/mysum.php" />
  </wsdl:port>
 </wsdl:service>

</wsdl:definitions>

服务:

function mysum($parameters) {

$result = $parameters->item[0]->value + $parameters->item[1]->value; 返回$结果; }

ini_set("soap.wsdl_cache_enabled", "0"); // 禁用 WSDL 缓存 $server = new SoapServer("mysum.wsdl"); $server->addFunction("mysum"); $服务器->handle();

我可以从 php 客户端访问

$client = new SoapClient("http://www.my-uni- project.info/joomla/components/com_jv_vm_soa/mysum.wsdl"); $params = array('n1' => '4', 'n2' => '8');

try { 
  $result = $client->__soapCall('mysum', array('parameters' => $params));

echo $result; } catch (SoapFault $异常) { 回显$异常;
}

我尝试创建一个 C# 客户端,所以首先创建了一个服务引用“mysum”,然后在表单上添加了一个按钮和一个标签,并为按钮添加了以下代码

 private void button1_Click(object sender, EventArgs e)
    {
        mysum s = new mysum();
        label1.Text = "" + s.mysum(2, 3);                
    }

当我运行它时,出现此错误:

Error 5 The type or namespace name 'mysum' could not be found (are you 
missing a using directive or an assembly reference?) 

服务已上线

提前谢谢您 约翰

I have made a simple web service

wsdl:

<wsdl:definitions name='mysum' >

<wsdl:types>
 <xsd:schema 
  xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
 xmlns:tns="http://www.my-uni-project.info/joomla/components/com_jv_vm_soa/"
 xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
 xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="mysum"
   targetNamespace="http://www.my-uni-project.info/joomla/components/com_jv_vm_soa/"
   xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">

   <xsd:complexType name="mysumRequest">
    <xsd:all>
     <xsd:element minOccurs="0" name="n1" type="xsd:int"/>
     <xsd:element minOccurs="0" name="n2" type="xsd:int"/>
    </xsd:all>
   </xsd:complexType>   

   <xsd:element name="mysumResponse" type="xsd:int"/>
  </xsd:schema>
 </wsdl:types>

 <wsdl:message name="mysumRequest">
   <wsdl:part name="parameters" element="tns:mysumRequest" />
 </wsdl:message> 
 <wsdl:message name="mysumResponse">
   <wsdl:part name="result" element="tns:mysumResponse" />
 </wsdl:message> 


 <wsdl:portType name="mysum">
  <wsdl:operation name="mysum">
   <wsdl:input message="tns:mysumRequest"/>
   <wsdl:output message="tns:mysumResponse"/>
  </wsdl:operation>
 </wsdl:portType> 

 <wsdl:binding name="mysumSOAP" type="tns:mysum">
  <soap:binding transport="http://schemas.xmlsoap.org/soap/http" />
  <wsdl:operation name="mysum">
   <soap:operation soapAction="mysum" />
   <wsdl:input>
    <soap:body use="literal" />
   </wsdl:input>
   <wsdl:output>
    <soap:body use="literal" />
   </wsdl:output>
  </wsdl:operation>
 </wsdl:binding> 

 <wsdl:service name="mysum">
  <wsdl:port name="mysumSOAP" binding="tns:mysumSOAP">
    <soap:address location="http://www.my-uni-
    project.info/joomla/components/com_jv_vm_soa/mysum.php" />
  </wsdl:port>
 </wsdl:service>

</wsdl:definitions>

the service:

function mysum($parameters) {

$result = $parameters->item[0]->value + $parameters->item[1]->value;
return $result ;
}

ini_set("soap.wsdl_cache_enabled", "0"); // disabling WSDL cache
$server = new SoapServer("mysum.wsdl");
$server->addFunction("mysum");
$server->handle();

that I can access from a php client:


$client = new SoapClient("http://www.my-uni-
project.info/joomla/components/com_jv_vm_soa/mysum.wsdl");
$params = array('n1' => '4', 'n2' => '8');

try { 
  $result = $client->__soapCall('mysum', array('parameters' => $params));

echo $result;
} catch (SoapFault $exception) {
echo $exception;
}

I tried to create a C# client so first I created a service reference "mysum", then on the form I added a button and a label and I added the following code for the button

 private void button1_Click(object sender, EventArgs e)
    {
        mysum s = new mysum();
        label1.Text = "" + s.mysum(2, 3);                
    }

Whe I run it I get this error:

Error 5 The type or namespace name 'mysum' could not be found (are you 
missing a using directive or an assembly reference?) 

The service is online

Thank you in advanced
John

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

酒绊 2024-10-05 10:57:24

通常,您可以通过右键单击有问题的对象(在本例中为 mysum)并查看是否可以“使用指令解决问题”来确定是否可以使用指令解决问题 ',其中 是指令的名称。

Typically you can determine if you can resolve the problem using a directive by right-clicking on the object in question, in this case mysum, and seeing if you can 'Resolve Using ' where is the name of your directive.

你是年少的欢喜 2024-10-05 10:57:24

我认为您的问题是您将服务添加为服务引用而不是 Web 服务引用。

添加 Web 服务引用

  1. 添加服务引用
  2. 点击窗口上的高级按钮
  3. 点击添加 Web 引用
  4. 输入服务 URL

另外,

请确保您已添加 System.Web。项目中的服务命名空间引用。

希望有帮助。

I think your problem is that you are adding the service as a service reference instead of web service reference.

To add a web service reference

  1. Add service reference
  2. Hit the Advanced button on the window
  3. Hit Add Web Reference
  4. Enter the service url

Also,

Make sure you have added the System.Web.Services namespace reference in your project.

Hope it helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文