ASP.NETWebServices与数据绑定技术解析
立即解锁
发布时间: 2025-08-26 01:35:14 阅读量: 13 订阅数: 45 AIGC 


Silverlight 4 in VB: 创建跨平台浏览器应用
### ASP.NET Web Services与数据绑定技术解析
#### 1. ASP.NET Web Services
在ASP.NET Web Services的应用中,我们会涉及到服务端和客户端的交互。下面详细介绍服务端和客户端的实现。
##### 1.1 服务端实现
服务端实现了`IAsyncTaskService`接口,并提供了`SubmitTask()`方法的代码。以下是服务端代码示例:
```vbnet
<AspNetCompatibilityRequirements( _
RequirementsMode:=AspNetCompatibilityRequirementsMode.Allowed)> _
Public Class AsyncTask
Implements IAsyncTaskService
Public Sub SubmitTask(ByVal taskDescription As TaskDescription) _
Implements IAsyncTaskService.SubmitTask
' Simulate some work with a delay.
Thread.Sleep(TimeSpan.FromSeconds(15))
' Reverse the letters in string.
Dim data As Char() = taskDescription.DataToProcess.ToCharArray()
Array.Reverse(data)
' Prepare the response.
Dim result As New TaskResult()
result.ProcessedData = New String(data)
' Send the response to the client.
Try
Dim client As IAsyncTaskClient = _
OperationContext.Current.GetCallbackChannel(Of IAsyncTaskClient)()
client.ReturnResult(result)
Catch
' The client could not be contacted.
' Clean up any resources here before the thread ends.
End Try
' Incidentally, you can call the client.ReturnResult() method mulitple times
' to return different pieces of data at different times.
' The connection remains available until the reference is released (when the
' method ends and the variable goes out of scope).
End Sub
End Class
```
在这个代码中,`SubmitTask()`方法首先模拟了一些耗时的工作,然后对传入的字符串进行反转处理,最后将处理结果通过`IAsyncTaskClient.ReturnResult()`方法发送给客户端。需要注意的是,服务端可以多次调用`client.ReturnResult()`方法,在方法结束和变量超出作用域之前,与客户端的连接一直保持可用。
##### 1.2 客户端实现
客户端代码相对简单。首先,需要引用`System.ServiceModel.PollingDuplex.dll`程序集,并且要使用Silverlight版本,该版本可以在`C:\Program Files\Microsoft SDKs\Silverlight\v4.0\Libraries\Client`文件夹中找到。以下是客户端代码示例:
```vbnet
Private client As AsyncTaskServiceClient
Public Sub New()
InitializeComponent()
Dim address As New EndpointAddress("http://localhost:" & _
HtmlPage.Document.DocumentUri.Port & "/DuplexService.Web/AsyncTask.svc")
Dim binding As New PollingDuplexHttpBinding()
client = New AsyncTaskServiceClient(binding, address)
...
AddHandler client.ReturnResultReceived, AddressOf client_ReturnResultReceived
End Sub
Private Sub cmdSubmit_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
Dim taskDescription As New TaskDescription()
taskDescription.DataToProcess = txtTextToProcess.Text
client.SubmitTaskAsync(taskDescription)
lblStatus.Text = "Asynchronous request sent to server."
End Sub
Private Sub client_ReturnResultReceived(ByVal sender As Object, _
ByVal e As ReturnResultReceivedEventArgs)
lblStatus.Text = "Response received: " & e.result.ProcessedData
End Sub
```
在这个代码中,客户端创建了一个代理对象,并设置了事件处理程序。当用户点击按钮时,客户端将文本发送给服务端进行异步处理。当服务端回调客户端时,客户端将处理结果显示在`TextBlock`中。
从客户端的角度来看,编程模型与普通的Web服务有相似之处,但也存在一些差异:
- 客户端不等待服务端的响应,而是定期轮询。
- 服务端可以长时间持有客户端引用,并在方法结束前多次调用客户端。
- 服务端可以从同一个Web服务方法调用不同的客户端方法。
需要注意的是,双工服务并不适合大量用户使用。默认情况下,双工服务最多支持10个同时连接,但可以通过代码进行修改。一般来说,双工服务在少量同时连接的客户端下性能最佳。
#### 2. 数据绑定
数据绑定是一种将对象中的信息提取并显示在应用程序用户界面中的技术,无需编写繁琐的代码。Rich客户端通常使用双向数据绑定,允许将用户界面中的信息推回到对象中。
##### 2.1 绑定到数据对象
数据绑定的基本过程是从源对象中提取属性值,并将其设置到目标对象的属性中。源对象可以是任何对象,而目标对象必须是`DependencyObject`类的实例,目标属性必须是依赖属性。
##### 2.2 构建数据对象
为了测试Silverlight的数据绑定功能,我们可以创建一个简单的数据对象。以下是一个封装产品信息的数据对象示例:
```vbnet
Public Class Product
Private _modelNumber As String
Public Property ModelNumber() As String
Get
Return _modelNumber
End Get
Set(ByVal value As String)
_modelNumber = value
End Set
End Property
Private _modelName As String
Public Property ModelName() As String
Get
Return _modelName
End Get
Set(ByVal value As String)
_modelName = value
End Set
End Property
Private _unitCost As Double
Public Property UnitCost() As Double
Get
Return _unitCost
```
0
0
复制全文
相关推荐








