温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C#中怎么创建一个WebService服务

发布时间:2021-07-07 16:09:54 来源:亿速云 阅读:147 作者:Leah 栏目:编程语言

这篇文章将为大家详细讲解有关C#中怎么创建一个WebService服务,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

打开vs.Net,新建工程(asp.Net  web服务),在位置中键入http://localhost/webserver,其中webserver就是工程的名字。确定后,出现一个Service1.asmx.cx,双击,出现代码窗口,

using  System;   using  System.Collections;   using  System.ComponentModel;   using  System.Data;   using  System.Diagnostics;   using  System.Web;   using  System.Web.Services;   namespace  webserver   {   ///     ///  Service1  的摘要说明。   ///     (1)   public  class  Service1  :    System.Web.Services.WebService   {   public  Service1()   {   //CODEGEN:该调用是  ASP.Net  Web    服务设计器所必需的   InitializeComponent();   }   #region  Component  Designer  generated  code   //Web  服务设计器所必需的   private  IContainer  components  =  null;   ///     ///  设计器支持所需的方法  -    不要使用代码编辑器修改   ///  此方法的内容。   ///     private  void  InitializeComponent()   {   }   ///     ///  清理所有正在使用的资源。   ///     protected  override  void  Dispose  (  bool  disposing  )   {   if(disposing  &&  components  !=  null)   {   components.Dispose();   }   base.Dispose(disposing);   }   #endregion   //  WEB  服务示例   //  HelloWorld()  示例服务返回字符串  Hello  World   //  若要生成,请取消注释下列行,然后保存并生成项目   //  若要测试此  Web  服务,请按  F5  键   // [WebMethod]   // public  string  HelloWorld()   // {   // return  "Hello  World";   // }   }   }

下面在(1)处加入

[WebService(Namespace="http://localhost/webserver/")]

这是因为soap是基于http协议上的,客户端无法知道webservice位于那个服务器上。在实际应用中,比如http://www.ourfly.com上放置这个webservice,则Namespace改为http://www.ourfly.com/webserver.

下面我们给这个webservice添加一个方法。

// [WebMethod]   // public  string  HelloWorld()   // {   // return  "Hello  World";   // }   微软帮我们写好了一个,接着添加一个方法。  方法名称叫show.   [WebMethod]   public  string  show(string  yourname)   {   return  “http://www.ourfly.com”+”欢迎”+yourname;   }   现在,就可以运行了,按F5,点击show,输入你的名字,  然后点击invote 看到了吧。   〈 ?Xml  version="1.0"  encoding="utf-8"  ?〉     〈 string  Xmlns="http://tempuri.org/"〉  http://www.ourfly.com欢迎yyg〈 /string〉

成功了。打开bin目录,Vs.Net已经将proxy做好了.webserver.dll.

现在我们在不同的环境下测试:

1.打开vs.Net,新建”windows应用程序”工程,命名为Client,增加按钮,文本框。

现在要用到代理了,右键单击右边的reference(引用),选择”添加引用”,选择浏览,找到webserver目录下的bin目录下的webserver.dll
再加入一个system.web.webservices的引用,在列表中有。

在form1.cs里,加入

using System.Web.Services;

using webserver;

然后在

private  System.Windows.Forms.Button  button1;

private  System.Windows.Forms.TextBox  textBox1;

后面,插入

private  webserver.service1  Client

建立一个service1的实例。双击按钮,代码如下:

private  void  button1_Click  (object  sender,  System.EventArgs  e)   {   Client  =new  Service1();   string  name;   name=Client.show("龙卷风.Net");   textBox1.Text=name;   }

按F5,运行工程,点击按钮,文本框中显示 http://www.ourfly.com欢迎龙卷风.Net

2. Asp.Net  web窗口的测试

方法与上面的一模一样,添加引用,建立service1的实例 在此不在细说。

3.在VB中测试

这个就要相对来说复杂一些 ,首先在VB中建立一个”标准EXE”的工程。添加引用:Microsoft  Soap  Type  library。

注意:如果没有安装Microsoft  Soap  Toolkit,是没有这个类型库的。 可以在http://www.ourfly.com中下载。

添加一个text   Private  Sub  Form_Load()   Text1.Text  =  add()   End  Sub   Public  Function  Add()  As  String   Dim  objSoapClient  As  New    SoapClient objSoapClient.  ClientProperty("ServerHTTPRequest")  =  True   Call  objSoapClient.mssoapinit(  "http://localhost/webserver/service1.asmx?WSDL",    "Service1",  "Service1Soap")   这句也可以   objSoapClient.mssoapinit(  "http://localhost/webserver/service1.asmx?WSDL")   Add  =  objSoapClient.Show("龙卷风.Net")   End  Function

调试成功需要注意的:

运行服务端webservice的程序,出现下面时 支持下列操作。有关正式定义。

点击服务说明,会得到完整的wsdl文件 http://localhost/webserver/Service1.asmx?WSDL

我们就要使用这个文件,其中包含了我们定义的方法等等。Mssoapinit(bstrWSDLFile  as  string,[bStrServiceName  as  string  ],[bStrport  as  string  ]  ,[bstrWSMLDile  as  string])的用法: 其中第二个,第三个参数在wsdl文件中可以找到。也可以省略。

关于C#中怎么创建一个WebService服务就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI