Web Services使用out参数

      Web Services使用out参数,在SOAP协议中会跟返回值一样作为SOAP响应的内容返回。

      Web Services定义:

 1:  public class WebService1 : System.Web.Services.WebService
 2:  {
 3:      [WebMethod]
 4:      public string HelloWorld(out int outParamInt,out TestClass outParamObject)
 5:      {
 6:          outParamInt = 10;
 7:          outParamObject = new TestClass() { ID=1,Name="XX"};
 8:          return "Hello World";
 9:      }
10:  }
11:   
12:  public class TestClass
13:  {
14:      public int ID{get;set;}
15:      public string Name{get;set;}
16:  }
17:   

      定义两个out参数:一个int,一个复杂类型的。生成的SOAP:

HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <HelloWorldResponse xmlns="http://tempuri.org/">
      <HelloWorldResult>string</HelloWorldResult>
      <outParamInt>int</outParamInt>
      <outParamObject>
        <ID>int</ID>
        <Name>string</Name>
      </outParamObject>
    </HelloWorldResponse>
  </soap:Body>
</soap:Envelope>

 

       客户端的使用,生成的客户端代码:

1:  public string HelloWorld(out int outParamInt, out Client.localhost.TestClass outParamObject) {
2:      Client.localhost.HelloWorldRequest inValue = new Client.localhost.HelloWorldRequest();
3:      inValue.Body = new Client.localhost.HelloWorldRequestBody();
4:      Client.localhost.HelloWorldResponse retVal = ((Client.localhost.WebService1Soap)(this)).HelloWorld(inValue);
5:      outParamInt = retVal.Body.outParamInt;
6:      outParamObject = retVal.Body.outParamObject;
7:      return retVal.Body.HelloWorldResult;
8:  }
9:   

       测试代码:

1:  static void Main(string[] args)
2:  {
3:      localhost.WebService1SoapClient c = new localhost.WebService1SoapClient();
4:      localhost.TestClass testClass1;
5:      int i;
6:      c.HelloWorld(out i,out testClass1);
7:  }
8:   

 image

    

      对于其他的客户端,可以跟返回值一样获取out参数。如Flex:

     Flex测试代码:

 1:  <?xml version="1.0" encoding="utf-8"?>
 2:  <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
 3:                 xmlns:s="library://ns.adobe.com/flex/spark" 
 4:                 xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
 5:                 creationComplete="app_creationCompleteHandler(event)" >
 6:      <fx:Declarations>
 7:          <!-- 将非可视元素(例如服务、值对象)放在此处 -->
 8:          <s:WebService id="MyService" wsdl="http://localhost:1552/WebService1.asmx?WSDL">
 9:              <s:operation name="HelloWorld" result="onResult(event)"/>
10:          </s:WebService>
11:      </fx:Declarations>
12:      <fx:Script>
13:          <![CDATA[
14:              import mx.events.FlexEvent;
15:              import mx.rpc.events.ResultEvent;
16:   
17:              private function onResult(evnet:ResultEvent):void
18:              {
19:              
20:              }
21:              protected function app_creationCompleteHandler(event:FlexEvent):void
22:              {
23:                  MyService.HelloWorld();
24:              }
25:  
26:           ]]>
27:      </fx:Script>
28:  </s:Application>

        跟踪onResult的event的result:

image

转载于:https://www.cnblogs.com/zhaozhan/archive/2010/10/25/1860837.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值