Moving a WCF Service and Database to the Cloud
Modern line-of-business applications have much to gain from Windows Azure.
This lab depends on the previous lab as well.
Migrating to the cloud
What the cloud has to offer
In addition to the increased scalability and reliability the cloud has to offer, cloud-based services are also easier to access from outside the corporate firewall.
This new accessibility opens a whole class of opportunities for developers to enhance their functionality and extend their user experiences to new devices and tablets.
Breaking through the firewall
In our scenario, we have a variety of resources and services that exist behind a firewall.
Now that we want to extend their reach and functionality, the time has come to move the experience into the cloud.
The easiest way for us to migrate the project to the cloud is piece-by-piece.
Moving the database
We’ll start off with our functional system, and then move just the database.
We’ll be able to confirm success by wiring the existing on-premises WCF service to the new in-cloud database, and everything else should continue to work as expected.
Moving the WCF Service to the cloud
After that, we’ll move the service itself out to the cloud.
By changing the WCF service URL configured in the WPF application, it should also immediately work.
In this hands-on lab, you will learn how to:
- Migrate an on-premises database to SQL Azure
- Migrate an on-premises WCF service to Windows Azure Web Sites
- Secure the WCF service using Azure Active Directory
- Integrate the WCF service with an on-premises service using Service Bus Relay
The following is required to complete this hands-on lab:
- Microsoft Visual Studio 2013
- The latest Windows Azure SDK for .NET
- A Windows Azure subscription
This lab picks up where module 2 left off.
This hands-on lab includes the following exercises:
- Deploying to SQL Azure
- Deploying to Windows Azure Web Sites
- Securing a WCF service using Windows Azure Active Directory
- Integrating a cloud service with on-premises resources using Service Bus Relay
Exercise 1: Deploying to SQL Azure
In this exercise, we’ll go through the process of setting up a SQL Azure database server. Then we’ll deploy our existing database, including its data, out to that server.
Task 1: Creating a SQL Azure database
Note: To learn more about database options in the cloud, see this article: https://msdn.microsoft.com/en-us/magazine/dn463783.aspx
In this task, we’ll create a SQL Database.
Log into your Azure administrative account at https://manage.windowsazure.com.
In the left menu bar, click the SQL Database button.
Selecting SQL Database
In the bottom right corner, click the New button.
Creating a New Data Service
Click Data Services | SQL Database | Quick Create and fill out the form to create your database.
- If you already have an existing SQL server configured in Azure you’ll see the option to reuse it.
- For our exercise, please select the option to create a new one.
Creating a New Data Service (continued)...
It will take a few moments for your database to get created.
Creating the database in progress
Once the empty database has been created, you’ll see a message like this one.
Click the SQL Databases tab along the left edge of the page and click on the newly created expenses database.
Note: You will need to click on the name specifically to navigate to its page.
Getting to database dashboard
Click the Dashboard link to see more options for the database.
Clicking on the dashboard
Note: Notice you have the server name, the database connection strings available also.
In the Quick Glance section along the right edge you can get some useful information about your server.
Take note of the Server Name, which we’ll need later on for deploying the database.
NOTE By default, only IP addresses inside the Azure datacenter have access to the database server.
Click the Manage allowed IP addresses link to allow your current connection to access the database as well.
- This is a protection mechanism to prevent access from outside the data center. We will expose this data database by allowing one or more IP addresses to connect.
_Managing IP Addresses)
If the public IP you connect from is a static IP address, you can add it by clicking the Add to the allowed IP addresses button.
- If you’re accessing via network with a range of public IPs, you can add a rule for that range.
- Note that you also have the option to disallow access to your server from services running inside Azure, which may be useful if all access comes from external sources, only.
- In our case, we will be ultimately connecting to this database via services running in Azure, so we’ll leave the option enabled.
After adding any necessary rules, click the Save button at the bottom of the page to commit them.
Save IP Address
Click the Databases link to view the databases on this server as seen in Step 11
Click the row of the expenses database, but not on the name itself. We want to select the row, not navigate to the database’s record.
Click the Delete button at the bottom of the page to delete this database.
Delete the Database
The management tool will ask you to confirm that you want to delete the database. Accept the confirmation. However, it will then ask you if you would like to delete the server because it no longer has any databases. Deny this request since we’ll be deploying a database in the next step.
Task 2: Deploy your existing database to SQL Azure
In this task, we’ll deploy your existing database to SQL Azure.
Verify that you can connect to the server, named [ YOUR SERVER ].database.windows.net
- Your server name will be different, except for the database.windows.net part.
Verifying allowed IP addresses to server
Add the current ip address.
Adding current IP Address
Click Save and continuel
Open SQL Server Management Studio.
Connect to the server containing your local Expenses database.
Expenses Database from previous blog post
In the Object Explorer, right-click the Expenses database and select Tasks | Deploy Database to SQL Azure… .
Deploying the Database
If presented with the Introduction step of the wizard, click Next to continue.
On the Deployment Settings page, click Connect to connect to the target server.
Notice the server name is [ YOUR SERVER ].database.windows.net.
- Yours will be different.
Connecting to the server for moving the Expenses database
Fill out the connection details for your SQL Azure server using the settings configured during creation earlier and click Connect.
Note that there are some options available for configuration. In the SQL azure database settings, we can decide if we want to use the Web or Business editions of SQL Azure. At this time, the only difference between the two is the maximum database sizes they allow. Web is limited to up to 5GB, whereas Business is intended for databases up to 150GB. For the purposes of our lab, we can use the smallest option available.
In the Other settings, you can decide where the temporary bacpac file is stored. If you have a very large database with limited space on your default drive, this gives you the option to select a different path.
Once the connection succeeds, click Next to continue.
Review the Summary page. Click Finish to begin the deployment.
It’ll take a minute or two for the database to fully deploy. During the operation you can see the steps it takes.
Export in progress
Click Close when complete.
In the Object Explorer, click Connect | Database Engine… .
Connecting to the exported database
Connect to your SQL Azure server.
Connecting to exported database
You will have a different name than [ YOUR SERVER ].database.windows.net
- Your server name will be different, except for the database.windows.net part.
Expand [your server] | Databases | Expenses | Tables.
Viewing the database in the data center
Right-click the Charges table. Note that the options when working with SQL Azure are somewhat limited versus those available for a local SQL instance.
SQL Azure still supports almost everything local SQL does, except that some of the features are not exposed via the tool and require scripting.
- For example, there is no option to edit records inline. Select Script Table as | SELECT To | New Query Editor Window.
Click Execute to run the query.
Viewing the table data
We can now see that all of our data is available in our SQL Azure database.
Task 3: Updating the Expenses WCF service to use the new database
In this task, we’ll update our WCF service to use the newly created SQL Azure database. 1. Log into your Azure administrative account at https://manage.windowsazure.com if it’s not already open.
Navigate to the newly deployed database by clicking the SQL Databases tab, followed by the Expenses database link.
Connection strings from the portal
Select View SQL Database connection strings.
Copying the connection string
In the Connection Strings dialog, highlight and copy the connection string from the ADO.NET box. Press Esc to close the dialog when done. See Step 2 above for details.
Download the Expenses.WCF project here:
Place the files into a folder and open the project with Visual Studio 2013 as administrator
Open the Web.config file in Visual Studio 2013.
In my case it looks like this:
(Code Snippet - Web.config)
<configuration> <connectionStrings> <add name="Expenses.WcfService.ServiceCore.Properties.Settings.ExpensesConnectionString" connectionString="Server=tcp:[ YOUR SERVER ].database.windows.net,1433; Database=Expenses;User ID=azureuser@[ YOUR SERVER ];Password=[PUT YOUR PASSWORD HERE]; Trusted_Connection=False;Encrypt=True;Connection Timeout=30;" providerName="System.Data.SqlClient"/> </connectionStrings>
In the ConnectionStrings section, change Expenses.WcfService.ServiceCore.Properties.Settings.ExpensesConnectionString to use the new connection string for your SQL Database. Be sure to put your password in where specified.
Run the Expenses client application to confirm that it works as expected, but is now using data from SQL Database.
Confirm it runs
Exercise 2: Deploying to Windows Azure Web Sites
In this exercise, we’ll go through the process of setting up a Windows Azure Web Site. Then we’ll deploy our existing WCF service out to it.
Task 1: Creating Windows Azure Web Site
In this task, we’ll create a Windows Azure Web Site.
Log into your Azure administrative account at https://manage.windowsazure.com if not already open.
Click the New button in the bottom left corner.
Creating a new website
Select Compute | Web Site | Quick Create and fill out the settings. You’ll need to come up with a unique name for the URL. Your eventual domain name will be [your URL name].azurewebsites.net. Click Create Web Site to create the Web site.
Defining a new website
Verifying your new website. On left menu pane, click Azure Web Sites.
Verifying your Azure website
Task 2: Deploy the Expenses WCF service to our new Windows Azure Web Site
In this task, we’ll update our WCF service to use the newly created SQL Azure database.
After the Web site has been created, open \Expenses WCF Service\Expenses WCF Service.sln in Visual Studio 2013.
Right-click Expenses.WcfService in Solution Explorer and select Publish Web Site.
Publishing a web site
In the Publish Web dialog, click Import… . Once we have this profile set up, we can use it in the future and jump right to the deployment step. For this lab, we’ll go through the whole process of downloading and importing the publish profile for our Windows Azure subscription.
Importing a profile
In the Import Publish Profile dialog, select the from the drop down and choose the Azure Web Site you've chosen.
Choosing the Azure Web Site
In the Import Windows Azure Subscriptions dialog, click the Download subscription file link. This will open a browser window to the Azure site. Save the file being automatically downloaded to disk somewhere convenient, such as the desktop.
Validating the connection
Click Next to continue to the next step
Validating the settings
Click publish to migrate the website to the Azure website. Notice that only a few files need to be migrated up to the cloud. Only show a few because only the built files that are necessary to run the service are published. Click Publish to publish.
Publishing the WCF service to the cloud
A confirmation screen will appear
Confirming the settings
Use the URL below to validate the proper behavior of the WCF service running in the Azure website.
Note: Your URL might differ.
Validating the WCF service
Task 3: Updating the Expenses WPF client to use the new service
In this task, we’ll update our WPF client to use the new cloud-hosted WCF service.
Open \Expenses\Expenses.sln in Visual Studio 2013. Use a new instance of Visual Studio so that both the service and client solutions are open.
From the Expenses.Wpf project, open App.config.
In the configuration/appSettings section, change the expenseServiceUrl to match the domain of your new hosted service. It should look like https://[site_name_from_earlier].azurewebsites.net/ExpenseService.svc. It may be easiest to copy the URL used in the previous task to confirm the service deployment.
Changing the URL to be used by the WPF client
Note that for any app already deployed across our enterprise, we could just update the config files they use locally to reach this new service endpoint.
Press F5 to launch the application. It should connect to the cloud-hosted service and work as expected.
Validating the proper behavior for the WPF client
This concludes this post. The goal of this post was to illustrate the migration of both a database and a WCF service to the cloud. In the next post we will incorporate security into the discussion and talk about leveraging the service bus for additional functionality.