Code the ws_SaveSignatureCompleted
event handler as follows:
void ws_SaveSignatureCompleted( object sender,
Signature.ServiceReference1.SaveSignatureCompletedEventArgs e) {
txtStatus.Text = "Signature sent to WS!";
}
In Page.xaml.cs
, code the Load
button as follows:
//---Load button---
void btnLoad_MouseLeftButtonDown(
object sender, MouseButtonEventArgs e) {
try {
ServiceReference1.WebServiceSoapClient ws = new
Signature.ServiceReference1.WebServiceSoapClient();
//---wire up the event handler when the web service
// returns---
ws.GetSignatureCompleted +=
new EventHandler(ws_GetSignatureCompleted);
//---calls the web service method---
ws.GetSignatureAsync();
} catch (Exception ex) {
txtStatus.Text = ex.ToString();
}
}
Here, you call the Web service to retrieve the saved signature. When the Web Service call returns, the ws_GetSignatureCompleted
event handler will be called.
Code the ws_GetSignatureCompleted
event handler as follows:
void ws_GetSignatureCompleted( object sender,
Signature.ServiceReference1.GetSignatureCompletedEventArgs e) {
txtStatus.Text = "Signature loaded from WS!";
DrawSignature(e.Result);
}
Save the Signature
project. In Solution Explorer, right-click on the SignatureWebSite
project and select Add Silverlight Link (see Figure 19-77).
Figure 19-77
This causes Visual Studio 2008 to copy the relevant files from the Silverlight project onto the current project. Use the default values populated and click Add (see Figure 19-78).
Figure 19-78
Notice that a new folder named ClientBin, containing the Signature.xap
file, is added to the project (see Figure 19-79).
Figure 19-79
In Solution Explorer, right-click the SignatureWebSite
project and select Set as Startup Project (see Figure 19-80).
Figure 19-80
Select SignatureTestPage.aspx
, and press F5 to test the project. You can now save the signature to the Web Service as well as load the saved signature from the Web Service (see Figure 19-81).
Figure 19-81
This chapter has demonstrated how you can use Silverlight to build Rich Interactive Applications (RIAs). At the time of writing, there are two versions of Silverlight — 1.0 and 2 — the key difference being the integration of the .NET Framework in Silverlight 2. To build the user interface of a Silverlight application, you can use the Microsoft Expression suite of applications, while the coding can be done using Visual Studio 2008.
Chapter 20
Windows Communication Foundation
Windows Communication Foundation (WCF) is Microsoft's unified programming model for building service oriented applications (SOA). Parts of a service-oriented application can be exposed as a service that other applications can access.
WCF is a big topic, and it cannot be fully covered in a single chapter. However, this chapter provides a quick introduction to this new technology and shows how it addresses some of the limitations of today's technology. While most books and conference focused heavily on the theory behind WCF, this chapter shows you how to build WCF services and then explains the theory behind them.
In short, this chapter explores:
□ How traditional ASMX Web Services differ from WCF
□ The ABCs of WCF
□ Building different types of WCF services
To understand the rationale behind WCF, it is important to understand the offerings that are available today. In previous versions of Visual Studio (Visual Studio 2005 and Visual Studio .NET 2003), you use the ASP.NET application model to you create ASMX XML Web Services that expose functionalities to clients who want to use them.
ASMX Web Services are still supported in Visual Studio 2008 for backward compatibility, but going forward Microsoft recommends that developers use WCF when building services.
To compare WCF and ASMX Web Services, let's first use Visual Studio 2008 to create a new ASP.NET Web Service Application project. Name the project StocksWS
.
Populate the default Service1.asmx.cs
file as follows:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
namespace StocksWS {
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
public class Service1 : System.Web.Services.WebService {
[WebMethod]
public float GetStockPrice(string symbol) {
switch (symbol) {
case "MSFT":
return 29.91f;
case "AAPL":
return 180.21f;
case "YHOO":
return 23.93f;
default:
return 0;
}
}
}
}
This Web Service contains a web method to let users query the price of a stock. For simplicity, you will hardcode the stock prices of a few stocks.
To host this Web Service, you need to publish this project to a web server (IIS, for instance), or use the ASP.NET Web Development server that ships with Visual Studio. Figure 20-1 shows the ASP.NET Web Development Server hosting the service after you press F5.
Figure 20-1
For a client to use this Web Service, you need to add a web reference. So add a Windows Forms Application project to the current solution to consume this service. Name the project StockPriceChecker
.
Populate the default Form1
with the controls shown in Figure 20-2.
Figure 20-2
Читать дальше