`
jackhuang
  • 浏览: 53417 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

webservice及在vs2005中的应用

    博客分类:
  • .NET
阅读更多

      今天有空闲时间来写了一个简单HELLO WORD在vs.net2005下开发的webService.

       原来做物流项目用的最多的是做接口.提供给别人调用.不过我们公司还用他做RF程序.感觉效果还是不错.运行起来比较稳定,没有让仓库人员感觉烦.

      接下来我就给那些需要初学WEBserivce的同志们写写HELLOWORD:

1.在VS.NET2005下建立ASP.NET WEB服务.建立好后可以看到如下有一个service.cs类.代码如下:

c# 代码
  1. using System;   
  2. using System.Web;   
  3. using System.Web.Services;   
  4. using System.Web.Services.Protocols;   
  5.   
  6. [WebService(Namespace = "http://localhost/webservice/")]   
  7. [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]   
  8. public class Service : System.Web.Services.WebService   
  9. {   
  10.     public Service () {   
  11.   
  12.         //如果使用设计的组件,请取消注释以下行    
  13.         //InitializeComponent();    
  14.     }   
  15.   
  16.     [WebMethod]   
  17.     public string HelloWorld() {   
  18.         return "Hello World";   
  19.     }   
  20.        
  21. }   

记得更改他默认的Namespace,我没有做影射就直接写上http://localhost/webservice/ .

2.在同一个解决方案下建立另一个WIN应用程序项目,你也可以建立WEB站点来.这里 我建立的是WIN项目.后添加web引用

在URL上输入http://localhost/webservice/Service.asmx,记得你的webservice一定要部署.我这里部署在IIS下.用.NETFramework2.0.有装VS2003的同志要记得别忘记让他在NETFramework2.0下跑哦

3.在WIN项目里头的Program.CS Main方法调用webService.代码如下:

c# 代码
  1. using System;   
  2. using System.Collections.Generic;   
  3. using System.Windows.Forms;   
  4.   
  5. namespace WindowsApplication1   
  6. {   
  7.     static class Program   
  8.     {   
  9.         /// <summary>   
  10.         /// 应用程序的主入口点。   
  11.         /// </summary>   
  12.         [STAThread]   
  13.         static void Main()   
  14.         {   
  15.             //Application.EnableVisualStyles();   
  16.             //Application.SetCompatibleTextRenderingDefault(false);   
  17.             //Application.Run(new Form1());   
  18.   
  19.             //调用web Service   
  20.             localhost.Service myService = new localhost.Service();   
  21.             String msg = myService.HelloWorld();   
  22.             Console.Write(msg);   
  23.         }   
  24.     }   
  25. }  

到此你运行代码,体验一下输入的hello WORD.

web Serivce 这个东西给企业信息集成带来很方便.所以大家有时间多学点好东西.

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics