zxg58 发表于 2015-7-30 15:21:24

Silverlight访问 Apache服务器(Tomcat,Geronimo)中部署的Webservice

Silverlight 访问 Apache服务器中的Webservice
开发环境
  Vs2010 、 Silverlight4 、 Java Jdk1.6 U 21 、 Apache-tomcat-6.0.20 、 Myeclipse8.5 、 Apache-ant-1.8.1 、 Axis2 、 Geronimo-tomcat6-javaee5-2.2.
  下载地址:
  Apache-tomcat : http://apache.ziply.com/tomcat/
  Apache-ant   : http://apache.ziply.com/ant/
  Axis2 : http://apache.ziply.com/ws/axis2/
  Geronimo-tomcat : http://apache.ziply.com/geronimo/
编写 Webservice
  其实就是写个 java class ,步骤如下:
  1、 使用 Myeclipse 新建个 java project
  2、 在工程的 src 目录下新建个 java class (注:没有用 package ) SlWebServiceHelloWorld.java ,内容如下:
  3、 找到这个 SlWebServiceHelloWorld 的 class 文件 ,发布的时候有用。
SlWebServiceHelloWorld.java
  public class SlWebServiceHelloWorld {
  /**
  * Webservice 业务方法
  *
  * @return " XXX , Hello World!"
  */      
  public String sayHelloWorld(String name) {
  return name + ", Hello World!" ;
  }
  }
生成 Axis2
  Axis2 是一套崭新的 WebService 引擎,该版本是对 Axis1.x 重新设计的产物。 Axis2 不仅支持 SOAP1.1 和 SOAP1.2 ,还集成了非常流行的 REST WebService ,同时还支持 Spring 、 JSON 等技术。我们直接使用现成的 Axis2 War 文件。
  1、将 下载好的 axis2-1.5.1 和   apache-ant-1.8.1 解压。
  2、通过 运行 , 进入 axis2-1.5.1\webapp 文件夹下

  3、配置好 Ant!
  set ANT_HOME= D:\ apache-ant-1.8.1   (Ant 解压的路径 )
  set PATH=%ANT_HOME%\bin
  4、打开 axis2-1.5.1\webapp\WEB-INF , 在文件夹下新建个 名为 pojo 的文件夹,把
  SlWebServiceHelloWorld.class (上步编写好 java 类的 Class 文件) 粘帖到这

  5、运行 ant 命令   , 自动在上级添加 dist 文件夹 ,生成 axis2.war
  
  6、打开 axis2-1.5.1\dist 文件夹, 你会发现已经有 axis2.war 存在
  
发布 Axis2
  众所周知,若要允许 Silverlight 控件访问其他域上的服务,该服务必须明确选择允许跨域访问。 Silverlight 4 支持两种不同的机制供服务选择跨域访问:
  在承载服务的域的根目录中放置一个 clientaccesspolicy.xml 文件,以配置服务允许跨域访问。
  在承载服务的域的根目录中放置一个有效的 crossdomain.xml 文件。
  我们这边使用的是 clientaccesspolicy.xml 。内容如下:
clientaccesspolicy.xml
  
  
  
  
  
  
  
  
  
  
  
  
  
(1) 使用 Tomcat 发布
  1 、把clientaccesspolicy.xml 放在apache-tomcat-6.0.20\webapps\ROOT 下。
  2 、把 axis2.war 文件 复制 粘帖到 Tomcat 的 webapps   文件夹下
  apache-tomcat-6.0.20\webapps\axis2.war
  1、 启动 Tomcat ( apache-tomcat-6.0.20\bin\startup.bat )
  2、 在浏览器中输入: http://localhost:8080/axis2/services/listServices   结果如下:

  你会发现有你部署上的 SlWebServiceHelloWorld 。
  3、 点击页面中的 SlWebServiceHelloWorld ,你能看见 部署的 webservice结果如下:

  4、 测试你的 webservice 运行
  http://localhost:8080/axis2/services/SlWebServiceHelloWorld/sayHelloWorld?name=” Sweet ”
  结果如下:

  恭喜你, webservice 部署 ok !
(2)         使用 Geronimo 发布
  1、 把 clientaccesspolicy.xml 放在
  geronimo-tomcat6-javaee5-2.2\repository\org\apache\geronimo\configs\welcome-tomcat\2.2\welcome-tomcat-2.2.car 下
  2、 运行 Geronimo 服务,并登陆后台
  http://localhost:8080/console/portal/Welcome
  UserName:      system
  PassWord :      manager
  3、 点击“ 部署新应用 ” 部署 Axis2.war

  4、部署成功 点击“ web 应用程序”, 在浏览器中运行 http://localhost:8080/axis2/services/listServices
  效果如下:

  和 Tomcat 一样 ,你会发现有你部署上的 SlWebServiceHelloWorld 。
  5、 点击页面中的 SlWebServiceHelloWorld ,你能看见 部署的 webservice结果如下:

  和 Tomcat 部署一样
  6、 测试下运行的 webservice
  http://localhost:8080/axis2/services/SlWebServiceHelloWorld/sayHelloWorld?name="Geronimo "
  因为 Tomcat 和 Geronimo 部署的项目都一样 , 所以 效果都一样。
Silverlight 测试 Webservice
  1、新建个 silverlight 项目 ,命名为: SlApacheWebServiceTest
  2、AddService Reference ,填入
  http://localhost:8081/axis2/services/SlWebServiceHelloWorld?wsdl
  命名为: ApacheSR   如下:

  3、设计界面,编写方法。 因为用于测试,所以界面 比较随便。效果如下:
  左边文本框填入信息,右边文本框接收信息。

Xaml
  < UserControl x : Class ="SlApacheWebServiceTest.MainPage"
  xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns : x ="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns : d ="http://schemas.microsoft.com/expression/blend/2008"
  xmlns : mc ="http://schemas.openxmlformats.org/markup-compatibility/2006"
  mc : Ignorable ="d"
  d : DesignHeight ="300" d : DesignWidth ="800">
  < Grid >
  < TextBox x : Name ="tbName"
  Height ="79"
  Width ="200"
  Margin ="131,81,469,139" />
  < Button x : Name ="btnSend"
  Content =" 获 ? 取 &uml;? 信 ? 息 &iexcl;&eacute;"
  Height ="100"
  Width ="450"
  Click ="btnSend_Click"
  Margin ="131,167,205,33" />
  < TextBox x : Name ="tbMessage"
  Height ="79"
  Width ="200"
  Margin ="395,81,205,139" />
  
  
Cs
  namespace SlApacheWebServiceTest
  {
  public partial class MainPage : UserControl
  {
  public MainPage()
  {
  InitializeComponent();
  }
  private void btnSend_Click(object sender, RoutedEventArgs e)
  {
  ApacheSR.SlWebServiceHelloWorldPortTypeClient ws = new ApacheSR.SlWebServiceHelloWorldPortTypeClient ();
  ws.sayHelloWorldAsync(tbName.Text.Trim());
  ws.sayHelloWorldCompleted += new EventHandler (ws_sayHelloWorldCompleted);
  }
  void ws_sayHelloWorldCompleted(object sender, ApacheSR.sayHelloWorldCompletedEventArgs e) {
  tbMessage.Text = e.Result.ToString();
  }
  }
  }
  4、F5 运行,输入 Sweet , 得到结果 Sweet, Hello World! 。 效果如下:

  测试 Pass !
  (注意:我使用的 Tomcat 端口为 8081 ,大家注意自己的端口!
  跨域文件 clientaccesspolicy.xml 的路径:
  Tomcat : apache-tomcat-6.0.20\webapps\ROOT
  Geronimo :
  geronimo-tomcat6-javaee5-2.2\repository\org\apache\geronimo\configs\welcome-tomcat\2.2\welcome-tomcat-2.2.car )
页: [1]
查看完整版本: Silverlight访问 Apache服务器(Tomcat,Geronimo)中部署的Webservice