
假设A是客户端,B是webservice服务端,用户通过http协议向服务器发送soap请求,webservice返回客户端Xml格式的数游袜据。
现在我们看一看创建一个C#创建WebService的大致过程:
服务端的webservice是必须要建的。中间的soap,Xml我们不用去关心,在客户端这边,比较重要的是如何从webservice取得对象?答案是用的是proxy对象。客户端由代理对象(proxy)负责与webservice的通信。所以在客户端使用webservice,完全和使用一个本地对象是一样的。
我们现在以一个简单的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欢迎g〈 /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,是没有这个类型库的。
为了改善调用阻碍线程的长期运行的方法的XML Web服务方法的性能,你应该考虑把它们作为异步的XML Web服务方法发布。实现一个异步XML Web服务方法允许线程在返回线程池的时候执行其他的代码。这允许增加一个线程池中的有限数目的线程,这样提高了整体性能和系统的可伸缩性。
通常,调用执行输入/输出 *** 作的方法的XML Web服务方法适于作为异步实现。这样的方法的例子包括和其他的XML Web服务通讯、访问远程数据库、执行网络输入/输出和读写大文件方法。这些方法都花费大量时间执行硬件级 *** 作,而把线程留着用来执行XML Web服务方法程序块。如果XML Web服务方法异步实现,那么线程可以被释放来执行其他的代码。
不管一个XML Web服务方法是否异步实现,客户端都可以与之异步通讯。使用Web服务描述语言工具(WSDL.EXE)生成的.net客户端中的代理类来实现异步通信,即使XML Web服务方法是同步实现。代理类包含用于与每个XML Web服务方法异步通信的Begin和End方法。因此,决定一个XML Web服务方法到底是异步还是同步要取决于性能。
注意:实现一个异步的XML Web服务方法对客户端和服务器上的XML Web服务之间的HTTP连接没有影响。HTTP连接既不不会关闭也不用连接池化。
实现一个异步的XML Web服务方法
实现一个异步的XML Web服务方法遵循NET Framework异步设计模式
把一个同步的XML Web服务方法分解为两个方法;其中每个都带有相同的基名--一个带有以Begin开头的名称,另一个带有以End开头的名称。
Begin方法的参数表包含方法的功能中的所有的in和by引用参数。
By引用参数是作为输入参数列出的。
陆唯倒数第二个参数必须是AsyncCallback。AsyncCallback参数允许客户端提供一个委托,在方法调用完成的时候调用。当一个异步XML Web服务方法调源缺用另一个异步方法,这个参数可以被传入那个方法的倒数第二个参数。最后一个参数是一个对象。对象参数允许一个调用者提供状态信息给方法。当一个异步XML Web服务方法调用另一个异步方法早裂培,这个参数可以被传入那个方法的最后一个参数。
返回值必须是IAsyncResult类型的。
下面的代码示例是一个Begin方法,有一个方法函数特定的String参数。
[C#]
[WebMethod]
public IAsyncResult BeginGetAuthorRoyalties(String Author,
AsyncCallback callback, object asyncState)
[Visual Basic]
<WebMethod()> _
Public Function BeginGetAuthorRoyalties(ByVal Author As String, _
ByVal callback As AsyncCallback, ByVal asyncState As Object) _
As IAsyncResult
End方法的参数表由一个IAsyncResult类型的out和by引用参数组成。
返回值与一个同步的XML Web服务方法的返回值类型相同。
By引用参数是作为输出参数列出的。
下面的代码示例是一个End方法,返回一个AuthorRoyalties用户定义的模式。
[C#]
[WebMethod]
public AuthorRoyalties EndGetAuthorRoyalties(IAsyncResult
asyncResult)
[Visual Basic]
<WebMethod()> _
Public Function EndGetAuthorRoyalties(ByVal asyncResult As _
IAsyncResult) As AuthorRoyalties
下面的代码示例是一个和另一个XML Web服务方法异步通讯的异步XML Web服务方法。
[C#]
using System
using System.Web.Services
[WebService(Namespace="http://www.contoso.com/")]
public class MyService : WebService {
public RemoteService remoteService
public MyService() {
// Create a new instance of proxy class for
// the XML Web Service to be called.
remoteService = new RemoteService()
}
// Define the Begin method.
[WebMethod]
public IAsyncResult BeginGetAuthorRoyalties(String Author,AsyncCallback callback, object asyncState) {
// Begin asynchronous communictation with a different XML Web
// service.
return remoteService.BeginReturnedStronglyTypedDS(Author,
callback,asyncState)
}
// Define the End method.
[WebMethod]
public AuthorRoyalties EndGetAuthorRoyalties(IAsyncResultasyncResult) {
// Return the asynchronous result from the other XML Web service.
return remoteService.EndReturnedStronglyTypedDS(asyncResult)
}
}
[Visual Basic]
Imports System.Web.Services
<WebService(Namespace:="http://www.contoso.com/")> _
Public Class MyService
Inherits WebService
Public remoteService As RemoteService
Public Sub New()
MyBase.New()
' Create a new instance of proxy class for
' the XML Web service to be called.
remoteService = New RemoteService()
End Sub
' Define the Begin method.
<WebMethod()> _
Public Function BeginGetAuthorRoyalties(ByVal Author As String, _
ByVal callback As AsyncCallback, ByVal asyncState As Object) _
As IAsyncResult
' Begin asynchronous communictation with a different XML Web
' service.
Return remoteService.BeginReturnedStronglyTypedDS(Author, _
callback, asyncState)
End Function
' Define the End method.
<WebMethod()> _
Public Function EndGetAuthorRoyalties(ByVal asyncResult As _
IAsyncResult) As AuthorRoyalties
' Return the asynchronous result from the other XML Web service.
Return remoteService.EndReturnedStronglyTypedDS(asyncResult)
End Function
End Class
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)