Freigeben über


Moving a WCF Service and Database to the Cloud

Overview

Modern line-of-business applications have much to gain from Windows Azure.

This lab depends on the previous lab as well.

https://blogs.msdn.com/b/brunoterkaly/archive/2014/03/13/how-to-implement-a-service-architecture-on-windows-azure-using-wcf-wpf-and-unit-testing.aspx

overview

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.

Objectives

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

Prerequisites

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

Setup

This lab picks up where module 2 left off.

Exercises

This hands-on lab includes the following exercises:

  1. Deploying to SQL Azure
  2. Deploying to Windows Azure Web Sites
  3. Securing a WCF service using Windows Azure Active Directory
  4. 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.

  1. Log into your Azure administrative account at https://manage.windowsazure.com.

  2. In the left menu bar, click the SQL Database button.

    sqldb

    Selecting SQL Database

  3. In the bottom right corner, click the New button.

    newdataservice

    Creating a New Data Service

  4. 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.

    newdataservice2

    Creating a New Data Service (continued)...

  5. It will take a few moments for your database to get created.

    creatingdb

    Creating the database in progress

  6. Once the empty database has been created, you’ll see a message like this one.

    creatingdbdone

  7. 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.

    gotodash

    Getting to database dashboard

  8. Click the Dashboard link to see more options for the database.

    sqldashboard

    Clicking on the dashboard

    Note: Notice you have the server name, the database connection strings available also.

  9. 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.

  10. 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.

    manageip

    _Managing IP Addresses)

  11. 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.

    allowedips Allowed IP Addresses

  12. After adding any necessary rules, click the Save button at the bottom of the page to commit them.

    saveip

    Save IP Address

  13. Click the Databases link to view the databases on this server as seen in Step 11

  14. 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.

  15. Click the Delete button at the bottom of the page to delete this database.

    deletedb

    Delete the Database

  16. 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.

  1. 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.

    iptoserver

    Verifying allowed IP addresses to server

  2. Add the current ip address.

    addip

    Adding current IP Address

  3. Click Save and continuel

    addip2

    Clicking Save

  4. Open SQL Server Management Studio.

  5. Connect to the server containing your local Expenses database.

    dbexpenses

    Expenses Database from previous blog post

  6. In the Object Explorer, right-click the Expenses database and select Tasks | Deploy Database to SQL Azure… .

    exportdb

    Deploying the Database

  7. If presented with the Introduction step of the wizard, click Next to continue.

  8. 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.

    addip3

    Connecting to the server for moving the Expenses database

  9. 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.

  1. Once the connection succeeds, click Next to continue.

  2. Review the Summary page. Click Finish to begin the deployment.

  3. It’ll take a minute or two for the database to fully deploy. During the operation you can see the steps it takes.

    exportdb3

    Export in progress

  4. Click Close when complete.

  5. In the Object Explorer, click Connect | Database Engine… .

    connectodb

    Connecting to the exported database

  6. Connect to your SQL Azure server.

    connectodb2

    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.
  7. Expand [your server] | Databases | Expenses | Tables.

    exportdb4b

    Viewing the database in the data center

  8. 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.
  9. Click Execute to run the query.

    exportdb5

    Viewing the table data

  10. 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.

  1. Navigate to the newly deployed database by clicking the SQL Databases tab, followed by the Expenses database link.

    dbatportal1

    Connection strings from the portal

  2. Select View SQL Database connection strings.

    dbatportal2

    Copying the connection string

  3. 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.

  4. Download the Expenses.WCF project here:

    Place the files into a folder and open the project with Visual Studio 2013 as administrator

  5. Open the Web.config file in Visual Studio 2013.

    webconfig

    Opening Web.config

    In my case it looks like this:

    (Code Snippet - Web.config)

    XML

     <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>
    
  6. 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.

  7. Save Web.config.

  8. Run the Expenses client application to confirm that it works as expected, but is now using data from SQL Database.

    confirm

    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.

  1. Log into your Azure administrative account at https://manage.windowsazure.com if not already open.

  2. Click the New button in the bottom left corner.

    newwebsite

    Creating a new website

  3. 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.

    newwebsite2

    Defining a new website

  4. Verifying your new website. On left menu pane, click Azure Web Sites.

    verifyazurewebsite

    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.

  1. After the Web site has been created, open \Expenses WCF Service\Expenses WCF Service.sln in Visual Studio 2013.

  2. Right-click Expenses.WcfService in Solution Explorer and select Publish Web Site.

    ex2task2_a

    Publishing a web site

  3. 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.

    ex2task2_b

    Importing a profile

  4. In the Import Publish Profile dialog, select the from the drop down and choose the Azure Web Site you've chosen.

    ex2task2_c

    Choosing the Azure Web Site

  5. 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.

  6. Click next to validate the connection to the Azure website ex2task2_d

    Validating the connection

  7. Click Next to continue to the next step

    ex2task2_e

    Validating the settings

  8. 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.

    ex2task2_f

    Publishing the WCF service to the cloud

  9. A confirmation screen will appear

    ex2task2_g

    Confirming the settings

  10. Use the URL below to validate the proper behavior of the WCF service running in the Azure website.

    Note: Your URL might differ.

    ex2task2_h

    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.

  1. 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.

  2. From the Expenses.Wpf project, open App.config.

    ex3task1_a

    Opening App.config

  3. 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.

    ex3task1_b

    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.

  4. Press F5 to launch the application. It should connect to the cloud-hosted service and work as expected.

    ex3task1_c

    Validating the proper behavior for the WPF client

Summary

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.