1.WebService Project Structure
Simplemodel class:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;namespacedonetws{ Public classSimplemodel { PublicSystem.Int32 ID {Set; Get; } PublicSystem.String Str {Set; Get; } }}
Donetws.asmx Code:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Web.Services;namespacedonetws{/// <summary> ///Summary description of Donetws/// </summary>[WebService (Namespace ="http://tempuri.org/")] [WebServiceBinding (ConformsTo=Wsiprofiles.basicprofile1_1)] [System.ComponentModel.ToolboxItem (false)] //To allow this Web service to be called from a script using ASP. NET AJAX, uncomment the following line. //[System.Web.Script.Services.ScriptService] Public classDonetWS:System.Web.Services.WebService {//Test Pass[WebMethod] Public stringHelloWorld (stringname) { return "Hello World,"+name; } //test Get collection of objects[WebMethod] PublicList<simplemodel>getmodellist () {List<SimpleModel> Rsltdata =NewList<simplemodel>(); Simplemodel Model=NewSimplemodel (); Model.id=1; Model.str="str1"; Rsltdata.add (model); Model=NewSimplemodel (); Model.id=2; Model.str="str2"; Rsltdata.add (model); returnRsltdata; } }}
Run up:
2. Using the Apache CXF framework for client invocation
In the Java Project Pom.xml, add:
<Dependency> <groupId>Org.apache.cxf</groupId> <Artifactid>Cxf-rt-frontend-jaxws</Artifactid> <version>3.0.3</version></Dependency><Dependency> <groupId>Org.apache.cxf</groupId> <Artifactid>Cxf-rt-transports-http</Artifactid> <version>3.0.3</version></Dependency>
Jars
Open cmd, under the Cxf Directory Bin folder
Run: wsdl2java-p org.tempuri-d src-client http://localhost:26715/DonetWS.asmx?wsdl
The following classes are generated:
Leave only these 4 classes in the Java project's Org.tempuri package
Java Project structure
Cxfclient Class Code
Importjava.util.List;ImportJava.util.ResourceBundle;Importorg.apache.cxf.endpoint.Client;Importorg.apache.cxf.endpoint.dynamic.DynamicClientFactory;ImportOrg.tempuri.ArrayOfSimpleModel;ImportOrg.tempuri.SimpleModel; Public classcxfclient { Public StaticResourceBundle r = resourcebundle.getbundle ("config"); Public Staticdynamicclientfactory DCF =dynamicclientfactory.newinstance (); Public StaticClient client = dcf.createclient (r.getstring ("Wsdllocation"))); Public Static voidSynchelloworld () {Try{object[] replys= Client.invoke ("HelloWorld", "Darkdog"); for(Object reply:replys) {String rsltdata=(String) reply; System.out.println (Rsltdata); } } Catch(Exception e) {e.printstacktrace (); } } Public Static voidsyncgetmodellist () {Try{object[] replys= Client.invoke ("Getmodellist"); for(Object reply:replys) {Arrayofsimplemodel Arrayofsimplemodel=(Arrayofsimplemodel) reply; List<SimpleModel> Simplemodels =Arrayofsimplemodel.getsimplemodel (); for(Simplemodel simplemodel:simplemodels) {System.out.println (Simplemodel.getid ()+ " " +simplemodel.getstr ()); } } } Catch(Exception e) {e.printstacktrace (); } } Public Static voidMain (string[] args) {Synchelloworld (); Syncgetmodellist (); }}
Config.properties:
Wsdllocation=http\://localhost\:26715/donetws.asmx?wsdl
Complete!
Java code calls the Web service implemented by C #