惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Jina AI
Jina AI
Forbes - Security
Forbes - Security
雷峰网
雷峰网
人人都是产品经理
人人都是产品经理
博客园 - 叶小钗
V
Visual Studio Blog
月光博客
月光博客
博客园 - Franky
有赞技术团队
有赞技术团队
宝玉的分享
宝玉的分享
博客园 - 三生石上(FineUI控件)
酷 壳 – CoolShell
酷 壳 – CoolShell
Apple Machine Learning Research
Apple Machine Learning Research
The Register - Security
The Register - Security
S
SegmentFault 最新的问题
博客园 - 司徒正美
P
Proofpoint News Feed
Know Your Adversary
Know Your Adversary
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
A
Arctic Wolf
Cyberwarzone
Cyberwarzone
Simon Willison's Weblog
Simon Willison's Weblog
U
Unit 42
P
Proofpoint News Feed
Scott Helme
Scott Helme
MyScale Blog
MyScale Blog
T
Tenable Blog
Hugging Face - Blog
Hugging Face - Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
小众软件
小众软件
C
CERT Recently Published Vulnerability Notes
P
Palo Alto Networks Blog
V
V2EX
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
T
Tailwind CSS Blog
V
Vulnerabilities – Threatpost
Latest news
Latest news
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
I
Intezer
Microsoft Azure Blog
Microsoft Azure Blog
爱范儿
爱范儿
博客园 - 【当耐特】
B
Blog RSS Feed
N
Netflix TechBlog - Medium
Recent Announcements
Recent Announcements
NISL@THU
NISL@THU
C
Cisco Blogs
C
CXSECURITY Database RSS Feed - CXSecurity.com
S
Schneier on Security

博客园 - Daniel

送一点GMail的邀请 运用PreviousPage简化提交流程 从CodeBehind访问页面上的控件(2.0) 一个用户故事的样例(极限编程) 生成Excel报表的最直观方法 给还未曾尝试过Microsoft Enterprise Library的朋友们 渴望XP-勇气篇 渴望XP-测试篇 全球6大广告集团 渴望XP-重构篇 Vista for developers 渴望XP-简单篇 LINQ-sql的终极疯狂 渴望XP-结对篇 真的需要Smart Client吗? Longhorn之三剑客 Oracle sqlplus语句编辑命令 在Oracle 9i isqlplus中使用替换变量 Oracle 9i的isqlplus你用过了吗?
Interop .NET and J2EE via WebService Contract-first approach
Daniel · 2005-11-18 · via 博客园 - Daniel

VisualStudio引进的ASMX的确大大简化了XML Web Services的开发过程,但也存在一个比较突出的弊端,那便是VisualStudio的设计使用的是Code-first approach,先编写代码,再由.NET Framework动态生成Web Services的WSDL,而不是实施SOA更加认同的Contract-first approach。近日实践了一把在开发ASMX Web Services时运用Contract-first approach,然后通过bea Weblogic Workshop来comsume。

Step 1:生成XSD,即Web Method传入和传出XML的Schema,比如我有个叫Test1()的Web Method, 那分别为它定义Test1Request和Test1Response,如下是一个例子:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="TestInterface" targetNamespace="http://groupm.com/TestInterface.xsd" elementFormDefault="qualified" 

xmlns
="http://groupm.com/TestInterface.xsd" xmlns:mstns="http://groupm.com/TestInterface.xsd" 

xmlns:xs
="http://www.w3.org/2001/XMLSchema">
    
<xs:complexType name="Test1Request">
        
<xs:sequence minOccurs="1" maxOccurs="1">
            
<xs:element name="keyword1" type="xs:string" />
            
<xs:element name="keyword2" type="xs:int" />
        
</xs:sequence>
    
</xs:complexType>
    
<xs:complexType name="Test1Response">
        
<xs:sequence>
            
<xs:element name="records" type="Record" maxOccurs="unbounded" minOccurs="0" />
        
</xs:sequence>
    
</xs:complexType>
    
<xs:element name="Test1Request" type="Test1Request">
    
</xs:element>
    
<xs:element name="Test1Response" type="Test1Response">
    
</xs:element>
    
<xs:complexType name="Record">
        
<xs:sequence>
            
<xs:element name="id" type="xs:int" />
            
<xs:element name="name" type="xs:string" />
        
</xs:sequence>
    
</xs:complexType>
</xs:schema>


Step 2:生成此XSD对应的.NET类,命令为:xsd.exe /classes filename.xsd,有了这个类文件就可以在Web Method中方便地使用符合Schema的XML数据了(而不是手工去分析传入的XML数据)。

Step 3:生成WSDL,我用了能集成进VisualStudio的WSCF工具来完成这个工作,其间设定了Web Method及其参数和返回值使用的Schema,以下是一个例子:

<?xml version="1.0" encoding="utf-8"?>
<definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://groupm/TestService" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:import1="http://tempuri.org/XMLSchema1.xsd" targetNamespace="http://groupm/TestService" name="TestService" xmlns="http://schemas.xmlsoap.org/wsdl/">
  
<documentation xmlns="http://schemas.xmlsoap.org/wsdl/" />
  
<types>
    
<xsd:schema>
      
<xsd:import schemaLocation="D:\Daniel\My Documents\Visual Studio 2005\Projects\M21116-2\M2WebService\XMLSchema1.xsd" namespace="http://tempuri.org/XMLSchema1.xsd" />
    
</xsd:schema>
  
</types>
  
<message name="testIn">
    
<documentation xmlns="http://schemas.xmlsoap.org/wsdl/" />
    
<part name="messagePart" element="import1:TestRequest" />
  
</message>
  
<message name="testOut">
    
<documentation xmlns="http://schemas.xmlsoap.org/wsdl/" />
    
<part name="messagePart" element="import1:TestResponse" />
  
</message>
  
<portType name="TestServiceInterface">
    
<documentation xmlns="http://schemas.xmlsoap.org/wsdl/" />
    
<operation name="Test">
      
<documentation xmlns="http://schemas.xmlsoap.org/wsdl/" />
      
<input message="tns:testIn" />
      
<output message="tns:testOut" />
    
</operation>
  
</portType>
  
<binding name="TestService" type="tns:TestServiceInterface">
    
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" />
    
<operation name="Test">
      
<soap:operation soapAction="http://groupm/TestService:Test" style="document" />
      
<input>
        
<soap:body use="literal" />
      
</input>
      
<output>
        
<soap:body use="literal" />
      
</output>
    
</operation>
  
</binding>
  
<service name="WebService">
    
<port name="WebServiceSoap" binding="tns:WebServiceSoap">
      
<soap:address location="http://localhost/M2WebService/WebService.asmx" />
    
</port>
  
</service>
</definitions>

注意:最后的service标签,工具不会自动生成,需要手工加入。

Step 4:生成.NET代码,这一步我也使用WSCF来完成,生成的是一个Interface和一个ASMX,此ASMX已经可以通过浏览器查看,只不过还未实现其中的业务逻辑。

Step 5:编写ASMX的业务逻辑代码,使Web Service能够真正运作。
注意:每次使用WSCF都会重新生成接口和ASMX,所以如果WSDL发生变化而需要更新,须注意不要覆盖了原先的代码。

Step 6:在Weblogic Workshop中新建一个Application,在Schema中import Step 1中生成的XSD文件,此时Weblogic会编译此文件并生成相应的XML Bean。

Step 7:在默认的Project中新建一个Java Control,选择Web Service,设定WSDL为Step 2中生成的文件,完成后便可通过这个控件开始使用此Web Service。
注意:Weblogic提供了完备的测试机制,可以自动生成测试文件,并可在测试过程中通过XML来传入Web Method的参数,这点比VisualStudio方便很多。

总结:目前此种开发方式虽然达到了很高的互操作能力,但由于集成开发环境的缺憾,生产效率会被大大降低,所以是否采用Contract-first approach还是的根据业务的需求来确定。