With the simple Web service developed earlier only a simple string has been passed to the Web service. Now you are going to add a method where weather information is requested from the Web service. This information requires more complex data to be sent to and from the Web service.
Try It Out – Creating Passing Data with a Web Service
Open the previously created Web service project using Visual Studio. With this Web service, define the GetWeatherRequest and GetWeatherResponse classes that define the documents to be sent to and from the Web service. The enumerations TemperatureType and TemperatureCondition are used within these classes.
ASP.NET Web services use XML serialization to convert objects to an XML representation. You can use attributes from the namespace System.Xml.Serialization to influence how the generated XML format should look. (You can read more about XML serialization in Chapter 23.)
public enum TemperatureType { Fahrenheit, Celsius } public enum TemparatureCondition { Rainy, Sunny, Cloudy, Thunderstorm } public class GetWeatherRequest { public string City; public TemperatureType TemperatureType; } public class GetWeatherResponse { public TemparatureCondition Condition; public int Temperature; }
Add the Web service method GetWeather(). This method receives the data defined with GetWeatherRequest and returns data defined with GetWeatherResponse. Within the implementation a random weather condition is returned — with the exception of the home of Microsoft, Redmond, where it rains all week. For random weather generation, the class Random from the System namespace is used.
[WebMethod] public GetWeatherResponse GetWeather(GetWeatherRequest req) { GetWeatherResponse resp = new GetWeatherResponse(); Random r = new Random(); int celsius = r.Next(-20, 50); if (req.TemperatureType == TemperatureType.Celsius) resp.Temperature = celsius; else resp.Temperature = (212-32)/100 * celsius + 32; if (req.City == "Redmond") resp.Condition = TemparatureCondition.Rainy; else resp.Condition = (TemparatureCondition)r.Next(0, 3); return resp; }
After building the Web service, create a new project of type using the Windows Forms template and name the application WeatherClient.
Modify the main dialog like that shown in Figure 20-19. The control embeds two radio buttons where the temperature type (Celsius or Fahrenheit) can be selected, and the city can be entered. Clicking the Get Weather button will invoke the Web service, where the result of the Web service is shown in the Weather Condition and the Temperature text box controls.
Figure 20-19
The controls with their names and the value for the Text property are listed in this table:
Control | Name | Text Property |
---|---|---|
RadioButton | radioButtonCelsius | Celsius |
RadioButton | radioButtonFahrenheit | Fahrenheit |
Button | buttonGetWeather | Get Weather |
Label | labelWeatherCondition | |
TextBox | textCity | |
TextBox | textWeatherCondition | |
TextBox | textTemperature | |
Label | labelCity | City |
Label | labelWeatherCondition | Weather Condition |
Label | labelTemperature | Temperature |
GroupBox | groupBox1 |
Add a reference to the Web service, similar to how it was done with the earlier client application projects. Name the reference WeatherService.
Import the namespace WeatherClient.WeatherService with the client application.
Add a Click event handler to the button buttonGetWeather with the name OnGetWeather using the Property dialog of the button.
Add the implementation to the OnGetWeather() method as shown. First a GetWeatherRequest object is created that defines the request sent to the Web service. The Web service is invoked by calling the GetWeather() method. This method returns a GetWeatherResponse object with values that are read for display in the user interface.
private void OnGetWeather(object sender, EventArgs e) { GetWeatherRequest req = new GetWeatherRequest(); if (radioButtonCelsius.Checked) req.TemperatureType = TemperatureType.Celsius; else req.TemperatureType = TemperatureType.Fahrenheit; req.City = textCity.Text; Service ws = new Service(); GetWeatherResponse resp = ws.GetWeather(req); textWeatherCondition.Text = resp.Condition.ToString(); textTemperature.Text = resp.Temperature.ToString(); }
Start the client application. Enter a city and click the Get Weather button. If you are lucky, the real weather is shown (see Figure 20-20).
Figure 20-20