Exercise 4: Creating a WCF Service in Windows Azure
In this exercise, you will create a Windows Azure Service. In this scenario, the Windows Phone 7 application calls the Windows Azure Service and passes in the product make and model. The Windows Azure Service returns the lead-time to order and receive the corresponding replacement part from a parts vendor.
Task 1 – Beginning the Exercise
In this task, you will open the lab solution in Visual Studio 2010.
- Make sure that you have downloaded and installed the items listed in System Requirements above prior to beginning this exercise.
- Launch Visual Studio 2010 as administrator and open the lab project by selecting File » Open » Project.
- Browse to the WP7.Cloud.PartsServiceEnhanced.sln file located at %TrainingKitPath%\Labs\IntegratingAzureCloudServicesandData\Source\Before and select it.
- Click Open to open the solution.
Task 2 – Implementing and Configuring the WCF Service
In this task, you will implement a service operation in the PartServiceEnhanced WCF service to return the ordering lead-time for a given part.
- In the WCFServiceWebRoleEnhanced project, open the file PartService.svc.cs.
Add the following code under the //TODO: 8.3.1 comment to define the UpdateOrderStatus method:
public int UpdateOrderStatus(int ID, bool orderPending) { string orderPendingValue = string.Empty; if (orderPending) { orderPendingValue = "Yes"; } else { orderPendingValue = "No"; } using (TransactionScope scope = new TransactionScope()) { using (SqlConnection conn = new SqlConnection(RoleEnvironment. GetConfigurationSettingValue("DatabaseConnectionString"))) { string updateOrderPending = "UPDATE Parts " + "SET OrderPending = '" + orderPendingValue + "'" + "WHERE ID = " + ID.ToString(); SqlCommand cmd = new SqlCommand(updateOrderPending, conn); cmd.CommandType = CommandType.Text; conn.Open(); cmd.ExecuteNonQuery(); } scope.Complete(); } return 1; }
The above code uses ADO.NET to connect to the PartsInventoryEnhanced database and update the OrderPending column for the corresponding replacement part and warehouse.
- Open the ServiceConfiguration.cscfg file.
- Modify the DatabaseConnectionString elements value attribute to match the SQL Azure database server you created in exercise 2.
Change the Data Source, ID, and Password values.
The following code snippet shows the DatabaseConnectionString modified to work with a SQL Azure database server named snvnv617ib. You database name will be different.
<Setting name="DatabaseConnectionString" value="Data Source=[SQL Azure Database Name].database.windows.net;Initial Catalog=PartsInventoryEnhanced;User ID=admin;Password=pass@word1" />
The Server Name is the Fully Qualified DNS Name for the SQL Azure database server you created in task 2.
- Save the ServiceConfiguration.cscfg file.
- In the Solution Explorer, right click the PartService.svc file and select Set As Start Page.
- Press the F5 key to run the Azure Web Role in the local Azure AppFabric simulation environment. This causes the web roles to run on your local machine.
- Verify the PartService.svc service is available when it opens in Internet Explorer.
Do not stop the WP7.Cloud.PartsService project. You will use the running instance of the service in upcoming steps.
Figure 13
PartsService page