[RoR] 在RoR中调用.Net webservice

首先写一个简单的webservice:

using  System;
using  System.Web;
using  System.Web.Services;
using  System.Web.Services.Protocols;

[WebService(Namespace 
=   " http://tempuri.org/ " )]
[WebServiceBinding(ConformsTo 
=  WsiProfiles.BasicProfile1_1)]
public   class  Service : System.Web.Services.WebService
{
    [WebMethod]
    
public   string  HelloWorld( string  name) {
        
return   " Hello,  "   +  name;
    }
    
}

在RoR项目里,添加app/apis/test_api.rb:

class  TestApi  <  ActionWebService::API::Base
  api_method :HelloWorld, :expects 
=>  [{:name  =>  :string}], :returns  =>  [:string]
end

这是RoR里面通用的webservice元信息描述。

添加app/controllers/test_controller.rb:

class  TestController  <  ApplicationController
  web_client_api :test, :soap, 
" http://localhost/test/Service.asmx "
                 :namespace 
=>   " http://tempuri.org/ " ,
                 :soap_action_base 
=>   " http://tempuri.org " ,
                 :driver_options
=> {:default_encodingstyle  =>  SOAP::EncodingStyle::ASPDotNetHandler::Namespace }

  
def  hello
    render_text test.HelloWorld(
" Li Jie " )
  end
end

:soap_action_base选项是一个修补,不加这个选项会产生SOAPAction头错误。

运行服务器,在浏览器中访问/test/hello,发现名字为空。经过长时间调试,发现.Net在解析SOAP消息体时,不能处理这种命名空间:

     < n1:HelloWorld  xmlns:n1 ="http://tempuri.org/"
        soap:encodingStyle
="http://schemas.xmlsoap.org/soap/encoding/" >
      
<name   xsi:type ="xsd:string" >Li Jie </name >
    
</ n1:HelloWorld >

把n1去掉就行了。不过这部分实现在rubylib/soap/rpc/proxy.rb里面,实在不方便修改。为了让这个测试通过,暂时做了点小修改:

   def  route(req_header, req_body, reqopt, resopt)
    req_env 
=  ::SOAP::SOAPEnvelope.new(req_header, req_body)
    unless reqopt[:envelopenamespace].nil?
      set_envelopenamespace(req_env, reqopt[:envelopenamespace])
    end
    reqopt[:external_content] 
=  nil
    conn_data 
=  marshal(req_env, reqopt)
    
if  ext  =  reqopt[:external_content]
      mime 
=  MIMEMessage.new
      ext.each do 
| k, v |
          mime.add_attachment(v.data)
      end
      mime.add_part(conn_data.send_string 
+   " \r\n " )
      mime.close
      conn_data.send_string 
=  mime.content_str
      conn_data.send_contenttype 
=  mime.headers[ ' content-type ' ].str
    end

    conn_data.send_string.gsub!(
/:n1/'' )
    conn_data.send_string.gsub!(
/n1:/''
)

    conn_data 
=  @streamhandler.send(@endpoint_url, conn_data,
      reqopt[:soapaction])
    
if  conn_data.receive_string.empty?
      
return  nil
    end
    unmarshal(conn_data, resopt)
  end

加粗的2行是我添加的代码,勉强可以让它工作,不过显然不是正确的方法。

不知道是不是.Net库里面的BUG。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值