Integrating Azure in Office Solutionsaz12722.vo.msecnd.net/.../labs/azureinofficelab1-0-0/L… ·...

38
Hands-On Lab Integrating Azure in Office Solutions Lab version: 1.0.0 Last updated: 4/18/2022

Transcript of Integrating Azure in Office Solutionsaz12722.vo.msecnd.net/.../labs/azureinofficelab1-0-0/L… ·...

Page 1: Integrating Azure in Office Solutionsaz12722.vo.msecnd.net/.../labs/azureinofficelab1-0-0/L… · Web viewRun the sample Azure WCF service project. The project is configured to use

Hands-On LabIntegrating Azure in Office Solutions

Lab version: 1.0.0

Last updated: 5/19/2023

Page 2: Integrating Azure in Office Solutionsaz12722.vo.msecnd.net/.../labs/azureinofficelab1-0-0/L… · Web viewRun the sample Azure WCF service project. The project is configured to use

CONTENTS

OVERVIEW................................................................................................................................................. 3

EXERCISE 1: TESTING YOUR AZURE DEVELOPMENT ENVIRONMENT..............................................5Task 1 – Deploying the SaleObservationService Sample......................................................................5

Task 2 – Testing the SaleObservationService Sample with a Simple Test Client..................................7

Exercise 1 Verification.......................................................................................................................10

EXERCISE 2: CONSUMING AZURE TABLE SERVICE DATA IN EXCEL..............................................10Task 1 – Creating the Project.............................................................................................................10

Task 2 – Adding a Product Class.........................................................................................................13

Task 3 – Adding a ProductDataContext Class.....................................................................................14

Task 4 – Adding a ProductDataSource Class......................................................................................16

Task 5 – Adding Functionality to Excel Workbook to Use the Product Classes..................................18

Exercise 2 Verification.......................................................................................................................22

EXERCISE 3: CONSUMING AZURE WCF SERVICE DATA IN EXCEL..................................................23Task 1 – Creating the Project.............................................................................................................24

Task 2 – Modifying the Class so That it Can Interoperate With COM................................................26

Task 3 – Implementing the Call to the WCF Service...........................................................................28

Exercise 3 Verification.......................................................................................................................29

SUMMARY................................................................................................................................................ 34

Page 3: Integrating Azure in Office Solutionsaz12722.vo.msecnd.net/.../labs/azureinofficelab1-0-0/L… · Web viewRun the sample Azure WCF service project. The project is configured to use

Overview

The Windows Azure platform is a group of cloud technologies providing a specific set of services to application developers. There is Windows Azure, which provides an environment for running applications and storing data in the cloud, SQL Azure for providing relational data services in the cloud based on SQL Server, and the Windows Azure AppFabric that provides services for connecting applications running in the cloud or on premises.

Windows Azure is a general platform that businesses use in many wide-ranging scenarios from serving as a platform for a new Web site to providing an enterprise with a robust, scalable backbone for enterprise application development and deployment.

Increasingly, developers need to be aware of how to integrate applications with services exposed by Azure. In this Hands-On Lab, you will learn how to consume Azure services within an Office application.

Objectives

The objective of this Hands-On Lab is to provide you with a foundation for developing Office solutions that integrate with Windows Azure. In particular, you will

Learn how to setup your Azure development environment

Understand how to access the Azure Table Service for data storage

Learn how to call methods exposed by WFC Services on Azure

System Requirements

The steps in this Hands On Lab follow the requirements of the Windows Azure Tools for Microsoft Visual Studio 1.4:

Supported Operating Systems:Windows 7;Windows Server 2008;Windows Server 2008 R2;Windows Vista

IIS 7.0 (with ASP.NET, WCF HTTP Activation, Static Content, and optionally CGI).

Microsoft Visual Studio 2010

SQL Server 2005 Express Edition (or above – must be a local instance).

Additionally, this lab uses

Excel 2010

The steps in this lab were developed and tested using Windows Server 2008 R2 along with a 32-bit version of Microsoft Excel 2010.

Page 4: Integrating Azure in Office Solutionsaz12722.vo.msecnd.net/.../labs/azureinofficelab1-0-0/L… · Web viewRun the sample Azure WCF service project. The project is configured to use

Setup

This Hands-On lab assumes that the Hands-On lab files are located in a folder named Student\Azure on the C:\ drive. If you haven’t already done so, perform the following steps

1. Open Windows Explorer

2. Right-click on the Local Disk (C:) item and choose New >> Folder

3. Name the new folder Student

4. Right-click on the Student folder and choose New >> Folder

5. Name the new folder Azure

6. Depending on where you installed the Training Kit, go to %Office2010DeveloperTrainingKitPath%\Labs\OfficeAzure\Source\Starter.

7. Press Ctrl key and click on SaleObservationServiceTestClient, SaleObservatonService, and ExcelAzure.xlsx

8. Drag the selected items into the Azure folder

Exercises

This Hands-On Lab comprises the following exercises:

1. Setting up your Development Environment

2. Consuming Azure Table Storage Server Data in Excel

3. Calling an Azure hosted WCF Service from an Office Add-In

Estimated time to complete this lab: 60 minutes.

Starting materials

This Hands-On Lab includes the following starting materials.

SaleObservationService – This is a Visual Studio project that implements a sample WCF Service for Azure. You will build and deploy this project in exercise 1.

SaleObservationServiceTestClient – This is a Visual Studio project for testing the SaleObservationService. You will use this project in exercise 1.

ExcelAzure.xlsx – This is an Excel file that you will base your Visual Studio project off of in exercise 2.

Page 5: Integrating Azure in Office Solutionsaz12722.vo.msecnd.net/.../labs/azureinofficelab1-0-0/L… · Web viewRun the sample Azure WCF service project. The project is configured to use

ExcelAzure – This is not required to complete the lab. It is a completed example of exercise 2.

NorthwindSalesFunctions – This is not required to complete the lab. It is a completed example of exercise 3.

Exercise 1: Testing your Azure development environment

In this exercise, you will deploy a sample Azure WCF service and then use a simple test client to access the WCF Service to create sample data and verify that your environment is setup correctly.

Task 1 – Deploying the SaleObservationService Sample

In this task, you will review the SaleObservationService solution files and then build/deploy the project to Azure’s Development Fabric.

1. If you have not already, setup the Azure development tools on your computer.

a. Download & install the Windows Azure Tools for Visual Studio 1.4 .

b. Setup Development Storage for local development

Note: Development Storage is a utility that simulates the Blob, Queue, and Table Services available on Windows Azure. By default, development storage is configured for SQL Server Express 2005 or 2008. You can also configure it to use a local instance of SQL Server using the dsInit tool. For more information see the following resources:

Windows Azure Tools for Visual Studio 1.4

Windows Azure Storage Emulator

2. Review the sample Azure WCF service project

a. Open Visual Studio 2010 (run as administrator)

b. Open C:\Student\Azure\SaleObservationService\SaleObservationService.sln

c. Double-click on the file named SaleObservationService.src in the Solution Explorer to view the code for this file. Observe there are four public methods:

i. LoadData – The purpose of this method is to create a table for storing sale observation data and then populating it with data contained in the SaleObservations.xml file that is bundled with the project.

Page 6: Integrating Azure in Office Solutionsaz12722.vo.msecnd.net/.../labs/azureinofficelab1-0-0/L… · Web viewRun the sample Azure WCF service project. The project is configured to use

ii. CategorySales – This function returns the total sales for a given category.

iii. ProductSales – This function returns the total sales for a given product.

iv. GetSaleObservation –This function returns a SaleObservation object given a sale observation ID.

d. Observe the three classes for accessing Azure’s table storage service

i. SaleObservation.cs – This is a class for modeling the data structure of an individual sale observation. It extends TableServiceEntity, an Azure class, to include a class attribute required by the Azure Table service along with two class additional class properties – PartitionKey and RowKey.

ii. SaleObservationDataContext.cs – This is a class for representing the runtime context of a data service.

iii. SaleObservationDataSource.cs – This class handles the details of connecting to the Azure table service and implements methods on the sales observation records.

3. Run the sample Azure WCF service project. The project is configured to use Azure’s Development Fabric and Development Storage. This allows you to test and develop Azure solutions locally before you deploy them to Azure.

a. Right-click on SaleObservationService.svc in the Solution Explorer and choose Set as Start Page

b. Press F5 to begin debugging the project. If everything is setup correctly, you should see a page similar to the screenshot below.

Page 7: Integrating Azure in Office Solutionsaz12722.vo.msecnd.net/.../labs/azureinofficelab1-0-0/L… · Web viewRun the sample Azure WCF service project. The project is configured to use

Figure 1SaleObservation Service

c. Take note of the URL of the SaleObservationService as you will need to know this in later steps.

Task 2 – Testing the SaleObservationService Sample with a Simple Test Client

In task 2, you will use a basic test client application to call methods exposed by the SaleObservationService.

1. Without closing the browser window containing the SaleObservationService service page, open another instance of Visual Studio (run as administrator).

2. Open the C:\Student\Azure\SaleObservationTestClient \SaleObservationServiceTestClient.sln

3. In the Solution Explorer, double-click on app.config

4. Verify that the endpoint address in the configuration file matches the endpoint of your service (as noted above in Task 1, step 3, item c).

Page 8: Integrating Azure in Office Solutionsaz12722.vo.msecnd.net/.../labs/azureinofficelab1-0-0/L… · Web viewRun the sample Azure WCF service project. The project is configured to use

Figure 2Endpoint Address

5. Press F5 to debug the test client

6. Click the button Load Data. The SaleObservationService exposes a method to create a table using the Azure Table Storage service and populate it with sample sales data. The test client merely calls this method when you click Load Data. After a brief moment, the test client should indicate that 58 items were loaded.

Figure 3Test Sale Observation Client Dialog

Page 9: Integrating Azure in Office Solutionsaz12722.vo.msecnd.net/.../labs/azureinofficelab1-0-0/L… · Web viewRun the sample Azure WCF service project. The project is configured to use

7. Enter Beverages in the Total by Category text box

8. Click Total. When you click Total, the test client calls the CategorySales function exposed by the SaleObservationService.

Figure 4CategorySales function result

9. Enter Northwind Traders Beer in the Total by Product text box

10. Click Total. When you click Total, the test client calls the ProductSales function exposed by the SaleObservationService.

Figure 5ProductSales function result

11. Close the test client

Page 10: Integrating Azure in Office Solutionsaz12722.vo.msecnd.net/.../labs/azureinofficelab1-0-0/L… · Web viewRun the sample Azure WCF service project. The project is configured to use

12. Close the SaleObservationServiceTestClient solution in Visual Studio.

Exercise 1 Verification

If you completed steps 6-10 in task 2 above, you have verified that the SaleObservationService is running in the Azure Development Fabric and that your Azure Development Storage is working correctly. Another way to verify the existence of objects within Azure storage is to use Visual Studio. To do this, perform the following steps.

1. In Visual Studio, select ViewServer Explorer

2. Expand the Windows Azure Storage node and click through to Windows Azure Storage(Development)TablesSaleObservation

3. Double-click on SaleObservation to see a read-only view of the items in the table.

Figure 6Read-only view of the items

Exercise 2: Consuming Azure Table Service Data in Excel

In this exercise, you will learn how to interact with Azure’s table service to read/write records to Excel.

Task 1 – Creating the Project

One of the initial challenges you may experience with Windows Azure is how to get data in and out of it. In this exercise, you will create a project that demonstrates how you can create an Azure Table service table, load the table based on data found in Excel, and then retrieve data from the Azure Table service into Excel.

Page 11: Integrating Azure in Office Solutionsaz12722.vo.msecnd.net/.../labs/azureinofficelab1-0-0/L… · Web viewRun the sample Azure WCF service project. The project is configured to use

1. Open Visual Studio 2010 (run as Administrator)

2. Choose File>>New Project

3. Select the C#>>Office>>Excel 2010 Workbook project template

4. Name the project ExcelAzure and save it to your student directory at C:\Student\Azure\

Figure 7New Project dialog

5. Choose Copy an existing document and select workbook C:\Student\Azure\ExcelAzure.xlsx.

Page 12: Integrating Azure in Office Solutionsaz12722.vo.msecnd.net/.../labs/azureinofficelab1-0-0/L… · Web viewRun the sample Azure WCF service project. The project is configured to use

Figure 8Use the existing ExcelAzure.xlsx file

6. Click OK. Visual Studio creates the project and displays the ExcelAzure workbook.

7. Observe that Sheet2 contains a list of Northwind products. You will use this list to populate a table on Azure using Azure’s Table service.

Page 13: Integrating Azure in Office Solutionsaz12722.vo.msecnd.net/.../labs/azureinofficelab1-0-0/L… · Web viewRun the sample Azure WCF service project. The project is configured to use

Figure 9ExcelAzure.xlsx data

8. Right-click on the ExcelAzure project and choose Properties

9. On the Application tab, set the Target framework to .NET Framework 4 (not .NET Framework 4 Client Profile).

10. Click Yes to close and reopen the project

Task 2 – Adding a Product Class

To use Windows Azure table storage in .NET, you construct a class that models the desired schema. In addition to the properties required by your model, the class must include a Timestamp, a PartitionKey and a RowKey property and it must be decorated with a DataServiceKey(“PartitionKey”, “RowKey”) custom attribute. To simplify this, the Microsoft.WindowsAzure.StorageClient namespace includes a TableServiceEntity class that already defines the mandatory properties and required attribute.

1. Right-click on the ExcelAzure project and choose Add>>Class

2. Name the class Product.cs

3. Add a Reference to Microsoft.WindowsAzure.StorageClient.

a. Right-click on References in the Solution Explorer and choose Add Reference

b. On the Browse tab, select the Microsoft.WindowsAzure.StorageClient.dll that is located by default at C:\Program Files\Windows Azure SDK\v1.2\ref\ Microsoft.WindowsAzure.StorageClient.dll

Page 14: Integrating Azure in Office Solutionsaz12722.vo.msecnd.net/.../labs/azureinofficelab1-0-0/L… · Web viewRun the sample Azure WCF service project. The project is configured to use

4. Add the StorageClient namespace to the Product class in addition to the namespaces added by default

C#

using Microsoft.WindowsAzure.StorageClient;

5. Mark the class as public and have it derive from TableServiceEntity

C#

public class Product:TableServiceEntity{}

6. Add public fields to the Product class

C#

public string Supplier { get; set; }public string ID { get; set; }public string ProductCode { get; set; }public string ProductName { get; set; }public string Description { get; set; }public double StandardCost { get; set; }public double ListPrice { get; set; }public string QuantityPerUnit { get; set; }public string Category { get; set; }

7. Add two constructors to the Product class

C#

public Product(string ID){ PartitionKey = "Products"; RowKey = ID;}

public Product(){ PartitionKey = "Products";}

Task 3 – Adding a ProductDataContext Class

The next step is to implement a TableServiceContext (an Azure specific DataServiceContext) that will represent the runtime context for our data service. This is necessary since while data services themselves are stateless; the context in which a developer interacts with them is not.

Page 15: Integrating Azure in Office Solutionsaz12722.vo.msecnd.net/.../labs/azureinofficelab1-0-0/L… · Web viewRun the sample Azure WCF service project. The project is configured to use

1. Right-click on the ExcelAzure project and choose Add>>Class

2. Name the class ProductDataContext.cs

3. Add a reference to System.Data.Services.Client

a. Right-click on References in the Solution Explorer and select Add Reference

b. On the .NET tab, select System.Data.Services.Client

c. Click OK

4. Add the WindowsAzure and WindowsAzure.StorageClient namespaces to the class in addition to the namespaces added by default

C#

using Microsoft.WindowsAzure;using Microsoft.WindowsAzure.StorageClient;

5. Mark the class as public and have it derive from TableServiceContext

C#

public class ProductDataContext:TableServiceContext{}

6. Add a constructor to the class

C#

public ProductDataContext(string baseAddress, StorageCredentials credentials) : base(baseAddress, credentials){

}

7. Add a Product property to the class

C#

public IQueryable<Product> Product{ get { return this.CreateQuery<Product>("Product"); }}

Page 16: Integrating Azure in Office Solutionsaz12722.vo.msecnd.net/.../labs/azureinofficelab1-0-0/L… · Web viewRun the sample Azure WCF service project. The project is configured to use

8. Press F6 and verify that the project builds

Task 4 – Adding a ProductDataSource Class

Now that you have a data model (Product) and a data context (ProductDataContext), the final step is to create a data source. The data source class handles the details of connecting to the Azure Table Storage service using REST in addition to the methods for interacting with the data.

1. Right-click on the ExcelAzure project and choose Add >> Class

2. Name the class ProductDataSource.cs

3. Add the WindowsAzure and WindowsAzure.StorageClient namespaces to the class in addition to the namespaces added by default

C#

using Microsoft.WindowsAzure;using Microsoft.WindowsAzure.StorageClient;

4. Mark the class as public

C#

public class ProductDataSource{}

5. Add two class variables at the top of the class – storageAccount and context

C#

public class ProductDataSource{ private static CloudStorageAccount storageAccount; private ProductDataContext context;}

6. Add a static constructor for the class. Note that the constructor configures the storage account so that it uses the DevelopmentStorageAccount. Additionally, the constructor creates the Product table if it does not already exist.

C#

static ProductDataSource(){ try { storageAccount = CloudStorageAccount.DevelopmentStorageAccount;

Page 17: Integrating Azure in Office Solutionsaz12722.vo.msecnd.net/.../labs/azureinofficelab1-0-0/L… · Web viewRun the sample Azure WCF service project. The project is configured to use

CloudTableClient c = new CloudTableClient( storageAccount.TableEndpoint.AbsoluteUri, storageAccount.Credentials);

if (!c.DoesTableExist("Product")) { CloudTableClient.CreateTablesFromModel(typeof(ProductDataContext), storageAccount.TableEndpoint.AbsoluteUri, storageAccount.Credentials); } } catch (Exception e) { System.Windows.Forms.MessageBox.Show(e.Message); }}

7. Add an instance constructor for the class

C#

public ProductDataSource(){ this.context = new ProductDataContext( storageAccount.TableEndpoint.AbsoluteUri, storageAccount.Credentials);

this.context.RetryPolicy = RetryPolicies.Retry(3, TimeSpan.FromSeconds(1));}

8. Add the following data related methods

C#

public IEnumerable<Product> Select(){ var results = from g in this.context.Product where g.PartitionKey == "Products" select g;

return results;}

public void AddProduct(Product newItem){ this.context.AddObject("Product", newItem); this.context.SaveChanges();

Page 18: Integrating Azure in Office Solutionsaz12722.vo.msecnd.net/.../labs/azureinofficelab1-0-0/L… · Web viewRun the sample Azure WCF service project. The project is configured to use

}

// use to add multiple products in a batch// call Save() after adding the productspublic void AddProducts(Product newItem){ this.context.AddObject("Product", newItem);}

public void Save(){ this.context.SaveChanges(System.Data.Services.Client.SaveChangesOptions.Batch);}

public void DeleteProduct(Product item){ this.context.DeleteObject(item); this.context.SaveChanges();}

public void DeleteProducts(){ foreach (var p in this.Select()) { this.context.DeleteObject(p); } this.context.SaveChanges(System.Data.Services.Client.SaveChangesOptions.Batch);}

9. Press F6 to verify the project builds

Task 5 – Adding Functionality to Excel Workbook to Use the Product Classes

At this point, you have completed all of the steps required to interact with Azure’s Table Storage service. The final step is to implement functionality in the Excel workbook to use the Product related classes to interact with Azure.

1. Double-click on Sheet2.cs in the Solution Explorer to open the worksheet in design view

2. Scroll to the right until to reach an empty column

3. Drag a Button control from the Toolbox to an unused portion of the worksheet (i.e. column P)

a. Name the button buttonLoad

Page 19: Integrating Azure in Office Solutionsaz12722.vo.msecnd.net/.../labs/azureinofficelab1-0-0/L… · Web viewRun the sample Azure WCF service project. The project is configured to use

b. Set the Text property to Load Data

Figure 10Text property set to Load Data

4. Double-click on the buttonLoad to automatically generate and navigate to an event handler for the button

5. Enter the following code inside the buttonLoad_Click event handler. Note that you will implement ReadStringand ReadDouble in the next step.

C#

// Load starting in row 2 (row 1 contains column headings)Excel.Range rg = this.Cells[2, 1];

ProductDataSource ds = new ProductDataSource();

while (rg.Value != null){

Product p = new Product(); p.PartitionKey = "Products"; p.RowKey = ReadString(rg.Offset[0, 1]); p.Supplier = ReadString(rg); p.ID = ReadString(rg.Offset[0, 1]); p.ProductCode = ReadString(rg.Offset[0, 2]); p.ProductName = ReadString(rg.Offset[0, 3]);

Page 20: Integrating Azure in Office Solutionsaz12722.vo.msecnd.net/.../labs/azureinofficelab1-0-0/L… · Web viewRun the sample Azure WCF service project. The project is configured to use

p.Description = ReadString(rg.Offset[0, 4]); p.StandardCost = ReadDouble(rg.Offset[0, 5]); p.ListPrice = ReadDouble(rg.Offset[0, 6]); p.QuantityPerUnit = ReadString(rg.Offset[0, 9]); p.Category = ReadString(rg.Offset[0, 12]);

ds.AddProducts(p);

rg = rg.Offset[1, 0];}

ds.Save();

6. In order to ensure that the proper data types are assigned to the product objects properties, add two additional methods to Sheet2

C#

private string ReadString(Excel.Range rg){ if (rg.Value == null) { return ""; } else { return rg.Value.ToString(); }}

private double ReadDouble(Excel.Range rg){ double result = 0.0; try { result = Convert.ToDouble(rg.Value); } catch (Exception) { } return result;}

7. Double-click on Sheet1 in the Solution Explorer to view the designer for Sheet1.

8. Drag two Button controls from the Toolbox on to the surface of Sheet1

a. Name the buttons buttonList and buttonDelete

b. Set the Caption properties for the buttons to List Products and Delete All respectively

Page 21: Integrating Azure in Office Solutionsaz12722.vo.msecnd.net/.../labs/azureinofficelab1-0-0/L… · Web viewRun the sample Azure WCF service project. The project is configured to use

Figure 11List Products and Delete All buttons

9. Double-click on the List Products button to generate and navigate to an event handler for the button

10. Add the following code to the buttonList_Click event handler

C#

this.Cells.ClearContents();

Excel.Range rg = this.Cells[1, 1];ProductDataSource ds = new ProductDataSource();foreach (var p in ds.Select()){ rg.Value = p.ID; rg.Offset[0, 1].Value = p.ProductName; rg = rg.Offset[1, 0];}

11. Right-click on Sheet1.cs in the Solution Explorer and choose View Designer

12. Double-click on the Delete All button to generate and navigate to an event handler for the button

Page 22: Integrating Azure in Office Solutionsaz12722.vo.msecnd.net/.../labs/azureinofficelab1-0-0/L… · Web viewRun the sample Azure WCF service project. The project is configured to use

13. Add the following code to the buttonDelete_Click event handler

C#

ProductDataSource ds = new ProductDataSource();ds.DeleteProducts();buttonList_Click(null, null);

Exercise 2 Verification

To verify your work, perform the following actions.

Confirm that the SaleObservationService from Exercise 1 is running in debug mode, then

1. Press F5 to debug your project

2. After Excel starts, select Sheet2

3. Click Load Data

4. Switch to Sheet1 and click List Products. Observe that your solution retrieves a list of product ID’s and product names from Azure’s table storage.

Page 23: Integrating Azure in Office Solutionsaz12722.vo.msecnd.net/.../labs/azureinofficelab1-0-0/L… · Web viewRun the sample Azure WCF service project. The project is configured to use

Figure 12List Products button results

5. Click Delete All. Observe that your solution deletes all of the items from the Products table.

6. Switch to Sheet2 and click Load Data.

7. Close Excel, terminating your debugging session.

Exercise 3: Consuming Azure WCF Service Data in Excel

In the previous exercise you saw how you could use WCF Data Services to easily work with the RESTful data exposed by Azure’s Table Storage service. Another way that you can interact with Azure is by creating WCF Services that are hosted on the Azure platform. WCF Services can expose a wide range of functionality from abstracting a data tier to implementing business logic and more. In this exercise, you will create an Excel User-Defined Function that integrates with a WCF Service on Azure to retrieve summarized sales totals.

Although it is outside of the scope of this exercise, UDF’s can be an interesting way to expose external data in Excel, particularly if Excel Services is part of your solution as user-defined functions are one of the primary ways you can extend workbooks destined for Excel Services. See http://msdn.microsoft.com/en-us/library/ms493934.aspx for more information. For this exercise you will create a managed code UDF for use on the client (Excel rather than Excel Services).

Task 1 – Creating the Project

Creating a UDF is slightly different from a traditional Office document or application add-in solution created using Visual Studio. Rather than starting with one of the Visual Studio Office templates, you start using a standard class library template.

1. Open Visual Studio 2010 (run as administrator).

2. Select File>>New Project

3. Select the Class Library template found under Visual C#

a. Name: SalesFunctions

b. Location: C:\Student\Azure

c. Solution Name: NorthwindSalesFunctions

d. Click OK

Page 24: Integrating Azure in Office Solutionsaz12722.vo.msecnd.net/.../labs/azureinofficelab1-0-0/L… · Web viewRun the sample Azure WCF service project. The project is configured to use

Figure 13Creating a Class Library

4. Add necessary references

a. Right-click on References in the Solution Explorer and choose Add Reference

b. On the .NET tab locate and select the following components (hold the CTRL key to select multiple non-contiguous items)

i. System.Runtime.Serialization

ii. System.ServiceModel

c. Click OK to add the references

5. Right-click on the SalesFunctions project in the Solution Explorer and choose Add Existing Item

6. Locate the SaleObservationService.cs file in the folder C:\Student\Azure\SaleObservationServiceTestClient\SaleObservationServiceTestClient and click Add

7. Right-click on the project and choose Properties

8. On the Build tab, put a check next to Register for COM interop

9. Press CTRL+S to save your changes

10. Close the Properties window

11. Click on Class1.cs in the Solution Explorer

Page 25: Integrating Azure in Office Solutionsaz12722.vo.msecnd.net/.../labs/azureinofficelab1-0-0/L… · Web viewRun the sample Azure WCF service project. The project is configured to use

12. Change the File Name to SalesFunctions.cs

13. Choose Yes to perform a rename in the project of all references to the code element ‘Class1’

Task 2 – Modifying the Class so That it Can Interoperate With COM

Since Excel is a COM-based application, in order to use the class library within Excel you need to be sure that class is COM callable.

1. Import the necessary namespaces in the SalesFunctions class

C#

using System.Runtime.InteropServices;using Microsoft.Win32;

2. Decorate the SalesFunctions class with the following attributes

C#

namespace SalesFunctions{ [Guid("7C69C8CF-747C-4855-B897-AF0710109816")] [ClassInterface(ClassInterfaceType.AutoDual)] [ComVisible(true)] public class SalesFunctions

3. Replace the supplied Guid above with a new Guid

a. Select Tools>>Create GUID

b. Select the 5th GUID Format and choose Copy

Page 26: Integrating Azure in Office Solutionsaz12722.vo.msecnd.net/.../labs/azureinofficelab1-0-0/L… · Web viewRun the sample Azure WCF service project. The project is configured to use

Figure 14Create GUID dialog

c. Select the [Guid("7C69C8CF-747C-4855-B897-AF0710109816")] statement

d. Press CTRL+V to replace it with the new GUID

4. Add the following functions to the SalesFunction class to handle COM registration and un-registration and the class constructor

C#

public SalesFunctions(){}[ComRegisterFunctionAttribute]public static void RegisterFunction(Type type){ Registry.ClassesRoot.CreateSubKey( GetSubKeyName(type, "Programmable")); RegistryKey key = Registry.ClassesRoot.OpenSubKey( GetSubKeyName(type, "InprocServer32"), true); key.SetValue("", System.Environment.SystemDirectory + @"\mscoree.dll", RegistryValueKind.String);}

Page 27: Integrating Azure in Office Solutionsaz12722.vo.msecnd.net/.../labs/azureinofficelab1-0-0/L… · Web viewRun the sample Azure WCF service project. The project is configured to use

[ComUnregisterFunctionAttribute]public static void UnregisterFunction(Type type){ Registry.ClassesRoot.DeleteSubKey( GetSubKeyName(type, "Programmable"), false);}

private static string GetSubKeyName(Type type,string subKeyName){ System.Text.StringBuilder s = new System.Text.StringBuilder(); s.Append(@"CLSID\{"); s.Append(type.GUID.ToString().ToUpper()); s.Append(@"}\"); s.Append(subKeyName); return s.ToString();}

Task 3 – Implementing the Call to the WCF Service

The final task is to make the call to the WCF Service.

1. Add the necessary namespaces to SalesFunctions.cs

C#

using System.ServiceModel;using System.Xml;

2. Add a method to the SalesFunctions class to create a new instance of the SaleObservationService

C#

private SaleObservationServiceClient GetClient(){ //Specify the binding to be used for the client. BasicHttpBinding binding = new BasicHttpBinding();

//Specify the address to be used for the client. EndpointAddress address = new EndpointAddress( "http://127.0.0.1:81/SaleObservationService.svc");

return new SaleObservationServiceClient(binding, address);}

Page 28: Integrating Azure in Office Solutionsaz12722.vo.msecnd.net/.../labs/azureinofficelab1-0-0/L… · Web viewRun the sample Azure WCF service project. The project is configured to use

3. Implement the GetSales function in the SalesFunctions class

C#

public double GetSales(string category){ double result = 0.0; SaleObservationServiceClient client = GetClient(); result = client.CategorySales(category); client.Close(); return result;}

4. Press F6 to build the project

Exercise 3 Verification

To verify your work, perform the following actions.

1. Verify your WCF SaleObservationService from Exercise 1 is running.

a. Open the Development Fabric UI by right-clicking on the Windows Azure icon in the system tray

b. Choose Show Development Fabric UI

Figure 15Windows Azure context menu

c. Verify that the WCFSaleObservationService is running

Page 29: Integrating Azure in Office Solutionsaz12722.vo.msecnd.net/.../labs/azureinofficelab1-0-0/L… · Web viewRun the sample Azure WCF service project. The project is configured to use

Figure 16Running WCFSaleObservationService service

d. If it is not running, restart it by debugging the SaleObservationService project (from Exercise 1) in Visual Studio

2. Open up Microsoft Excel

3. Activate the SalesFunctions add-in in Excel

a. In Excel, select File>>Options

b. In the Excel Options window click on Add-Ins

c. Down near the bottom, select Excel Add-ins in the Manage drop-down box and

d. Click Go

Page 30: Integrating Azure in Office Solutionsaz12722.vo.msecnd.net/.../labs/azureinofficelab1-0-0/L… · Web viewRun the sample Azure WCF service project. The project is configured to use

Figure 17Activating SalesFunction add-in

e. In the Add-Ins window, click Automation or click the Automation… button

Page 31: Integrating Azure in Office Solutionsaz12722.vo.msecnd.net/.../labs/azureinofficelab1-0-0/L… · Web viewRun the sample Azure WCF service project. The project is configured to use

Figure 18AutomationAddin.MyFunctions selected for activation

f. Locate and select the SalesFunctions.SalesFunctions Automation server and click OK

Page 32: Integrating Azure in Office Solutionsaz12722.vo.msecnd.net/.../labs/azureinofficelab1-0-0/L… · Web viewRun the sample Azure WCF service project. The project is configured to use

Figure 19SalesFunctions.SalesFunctions automation server

g. Click OK to close the Add-Ins window

4. Create a function using the user-defined function

a. In a blank worksheet, enter the values as shown in the screenshot below

Figure 20

Page 33: Integrating Azure in Office Solutionsaz12722.vo.msecnd.net/.../labs/azureinofficelab1-0-0/L… · Web viewRun the sample Azure WCF service project. The project is configured to use

Product Category values

b. In cell C4, enter the formula =GetSales(B4)

c. Fill the formula down to cell C9

d. (Optional) Format the cells using the comma style

Figure 21Using the formula to get Sales Totals over Product Category values

Summary

The Windows Azure technology platform provides on-demand, cloud-based computing, where the cloud is a set of interconnected computing resources located in one or more data centers. Developers can use the cloud to deploy and run applications and to store data. On-premises applications can still use cloud–based resources. For example, an application located on an on-premises server, a rich client that runs on a desktop computer, or one that runs on a mobile device can use storage that is located on the cloud.

In this Hands-On Lab you learned how you can access data exposed by the Azure Table Storage service using ADO.NET Data Services within an Office add-in. Additionally, you also learned how to call a method on an Azure WCF Service.

For more information on the Windows Azure platform start with the following resources:

Windows Azure - http://www.microsoft.com/windowsazure/windowsazure/

Windows Azure Developer Portal - http://msdn.microsoft.com/en-us/windowsazure/default.aspx

Windows Azure Platform Training Course - http://channel9.msdn.com/Learn/Courses/Azure