Rails Rails 如何调用.NET 的 WCF 接口

mumu · 2015年10月08日 · 最后由 psvr 回复于 2015年10月14日 · 4204 次阅读

在 rails 中通过以下代码调用.net 的 WCF 接口方法时,总是提示

REXML::UndefinedNamespaceException (Undefined prefix s found)

请问如何能通过 rails 正常调用 WCF 方法

require 'soap/wsdlDriver'

WSDL_URL = "http://10.68.2.17/PacsService/PacsService.svc?wsdl"

factory = SOAP::WSDLDriverFactory.new(WSDL_URL)

driver = factory.create_rpc_driver

a=driver.HelloWorld(1)

puts a["HelloWorldResult"]

其中“http://10.68.2.17/PacsService/PacsService.svc?wsdl"格式如下:

<wsdl:definitions name="PacsService" targetNamespace="http://tempuri.org/">
 <wsdl:types>
      <xsd:schema targetNamespace="http://tempuri.org/Imports">
      <xsd:import schemaLocation="http://10.68.2.17/PacsService/PacsService.svc?xsd=xsd0" namespace="http://tempuri.org/"/>
      <xsd:import schemaLocation="http://10.68.2.17/PacsService/PacsService.svc?xsd=xsd1" namespace="http://schemas.microsoft.com/2003/10/Serialization/"/>
      <xsd:import schemaLocation="http://10.68.2.17/PacsService/PacsService.svc?xsd=xsd2" namespace="http://schemas.microsoft.com/Message"/>
      </xsd:schema>
 </wsdl:types>
  ……

若是通过上述代码调用.net 的 WebService 接口,例如 WSDL_URL=”http://localhost:2965/WebService1.asmx?WSDL“就能调用成功, 其中”http://localhost:2965/WebService1.asmx?WSDL“格式为

 ……
 <wsdl:types>
<s:schema elementFormDefault="qualified" targetNamespace="http://tempuri.org/">
<s:element name="HelloWorld">
  <s:complexType />  </s:element>
 ……
  </wsdl:types>
  ……

wcf 可以发布成 webservice 或者 restful 的 service

WIN32OLE?(我也就是随手一回复而已。。)

用 savon,别用内置的 soap/wsdlDriver,这个在 Ruby 新版本已经去除了

#1 楼 @huobazi 3Q,由于 wcf 为多个系统提供接口,发布成 webservice 或者 restful 的 service 其他的系统也得相应修改

#3 楼 @ericguo 已经测试成功,非常感谢!

#3 楼 @ericguo 您好,我还想再咨询一个问题,我现在想通过 rails 提供 web 服务,供.net 或 java 使用,我看了一些贴子 Acion Web Service 貌似从 rails3 就不用了,ActiveResource 据我理解是 rails 与 rails 之间提供 web 服务的一种方式,请问通过 rails 提供给.net 或 java 使用的 web 服务还有其他的方式吗

@mumu 用 grape 做 RESTful API 如何?

#6 楼 @mumu Google Poroto Buffer,Apache Avro,华顺提到的 Thrift 应该都可以夸语言调用。 Google Poroto Buffer 官方库不支持 Ruby,有第三方支持。 Facebook Thrift(现在叫 Apache Thrift 更合适)最新版支持 Ruby,支持的语言很广泛。你可以了解一下。

#6 楼 @mumu 最官方的是用 jbuilder,返回 json 对象给.NET/Java 使用。

using Newtonsoft.Json;

string strlot = jsonLotInfo("M1516M8177");
var lot = JsonConvert.DeserializeObject<dynamic>(strlot);
Console.WriteLine(string.Format("Product: {0}", lot.product.product_name));
Console.WriteLine(string.Format("POD: {0}", lot.product.package_of_drawing.name));
foreach (var wfs in lot.workflow.workflowsteps)
{
    Console.WriteLine(string.Format("  Step: {0}", wfs.workflowstepname));
}

private static string jsonLotInfo(string lot_no)
{
    String ret = String.Empty;

    HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(string.Format("http://mesview.sandisk.com/lots/{0}.json", lot_no)));
    webReq.ContentType = "application/json; charset=UTF-8";
    webReq.Accept = "application/json";
    webReq.Timeout = 30000;
    HttpWebResponse response = (HttpWebResponse)webReq.GetResponse();
    StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.Default);
    ret = sr.ReadToEnd();
    sr.Close();
    response.Close();

    return ret;
}

#9 楼 @diguage 谢谢!用 Thrift 的话是不是所有系统调用服务的方式都得更改呢,是用 ruby 重写一个老系统,如果用 wsdl 方式的 webservice,其他系统只需要配置一下服务路径就可以了,关于 thrift 查到一些资料和命令,貌似不是在 windows 系统运行,无从下手啊

#10 楼 @ericguo rails 里的 jbuilder gem 包是用来格式化 json 的,JBuilder 是用来开发 java 的,怎么用 jbuilder 提供 ruby 的 webService 呢

#8 楼 @psvr 用 grape 做完之后,.net 或 java 如何来调用呢

#8 楼 @psvr Rails::API 与 Grape::API 有什么不同啊

#13 楼 @mumu 用他们的 HTTP 库调用就行了,然后结合编解码库例如 JSON

标题是 Rails 如何调用.NET 的 WCF 接口,不过好像后来讨论的是反过来的

@mumu https://ruby-china.org/topics/27594#reply6

拼 request post xml,然后用 http client 发送,得到 response 后用 savon 解析

#16 楼 @psvr 这样其他的系统也得更改,我在想既然 soap4r 可以调用其他系统的 wsdl 服务,是不是也可以通过它来生成 wsdl 服务呢

@mumu 没用过,好像 ws-*在 rails 这边不太流行

#12 楼 @mumu jbuilder是 gem,Borland JBuilder 都不知道死了多少年了。。

#8 楼 @psvr grape好像有人说过单单 host 比和 rails 还慢,还有目前 intridea 又不维护,看来看去还是 AMS 靠谱点,不知道你怎么看。 #14 楼 @mumu 你也问得太 general 了吧,这样的问题感觉就是伸手党,ruby 社区不欢迎这样的提问方式。

@ericguo dblock 还是一直在维护 grape 吧

需要 登录 后方可回复, 如果你还没有账号请 注册新账号