using System.Web.Script.Services;

/// <summary>

/// Summary description for WebService /// </summary>

[WebService(Namespace = 'http://tempuri.org/')]

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

[System.Web.Script.Services.ScriptService]

public class WebService : System.Web.Services.WebService {

 ...

 ...

}

Define the following two web methods:

[WebMethod]

public bool SaveSignature(string value) {

 try {

  File.WriteAllText(Server.MapPath('.') +

   @'Signature.txt', value);

  return true;

 } catch (Exception ex) {

  return false;

 }

}

[WebMethod]

public string GetSignature() {

 string fileContents;

 fileContents = File.ReadAllText(

  Server.MapPath('.') + @'Signature.txt');

 return fileContents;

}

The SaveSignature() function saves the values of the signature into a text file. The GetSignature() function reads the content of the text file and returns the content to the caller.

In the Signature project, add a service reference (see Figure 19-75).

Figure 19-75

Click the Discover button and then OK (see Figure 19-76).

Figure 19-76

In Page.xaml.cs, modify the Save button as follows:

//---Save button---

void btnSave_MouseLeftButtonDown(

 object sender, MouseButtonEventArgs e) {

 try {

  ServiceReference1.WebServiceSoapClient ws = new

   Signature.ServiceReference1.WebServiceSoapClient();

  //---wire up the event handler when the web service returns---

  ws.SaveSignatureCompleted += new

   EventHandler<Signature.ServiceReference1.SaveSignatureCompletedEventArgs> (ws_SaveSignatureCompleted);

  //---calls the web service method---

  ws.SaveSignatureAsync(GetSignatureLines());

 } catch (Exception ex) {

  txtStatus.Text = ex.ToString();

 }

}

Here, you send the signature to the Web service asynchronously. When the Web Service call returns, the ws_SaveSignatureCompleted event handler will be called.

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<Signature.ServiceReference1.GetSignatureCompletedEventArgs> (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

Вы читаете C# 2008 Programmer's Reference
Добавить отзыв
ВСЕ ОТЗЫВЫ О КНИГЕ В ИЗБРАННОЕ

0

Вы можете отметить интересные вам фрагменты текста, которые будут доступны по уникальной ссылке в адресной строке браузера.

Отметить Добавить цитату