Create a Fixed Query Application
|
Use downloadable Service Classes in Visual Studio to programmatically consume Marketplace datasets that the data publisher has specified as fixed query datasets. |
Before you proceed make sure you have:
-
A valid Windows Live ID account. If you do not have a Live ID go to the Windows Live home page and sign up.
-
A valid Marketplace account. If you do not have a Marketplace go to the topic Create Your Marketplace Account and follow the instructions there.
-
A subscription to the Marketplace fixed query data set you want to use in your application. If you have not subscribed to a fixed query dataset go to Subscribe to a Data Offer and follow the instructions there.
| Section | Description |
|---|---|
|
Steps to take in Visual Studio to start a new console project to consume Marketplace data. |
|
|
Steps you need to take to add a Service Reference to consume Marketplace data. |
|
|
Add a reference to System.Data.Services.Client to your project. |
|
|
Code you write to consume a Marketplace fixed query dataset. |
|
|
Code for the completed program. |
|
|
Example XML metadata for this fixed query class. |
Start Visual Studio
-
Find Visual Studio in your Start Menu.
-
Right click Visual Studio.
-
In the dropdown click Run as administrator.
-
When asked if you want to allow this program (devenv.exe) to make changes to your computer click Yes.
Create a New Project
-
On the Visual Studio Start Page select New Project….
-
Select Visual C# and Console Application (Visual Basic and Console Application if you prefer Visual Basic) in the New Project dialog.
-
From the left drop-down list at the top of the New Project dialog box, select .NET Framework 4.
Visual Studio 2010 defaults to .NET Framework 4. If you use an earlier version of Visual Studio and do not have .NET Framework 4 installed go to the .NET Framework 4 download page to download and install it.
-
Give your project a meaningful name. For example, for this project you could name it CensusDemographics.
-
Click OK.
After starting Visual Studio as an administrator and creating a new console application (Step 1) download and add the service class for the data service you want to consume.
Download the Service Class
-
Go to the Marketplace.
-
Select the My Data tab.
-
Find the Alteryx – Census Demographic Data data service.
-
Click Use to the right of the service name.
This takes you to the “Details Page.” -
Click .NET C# Class Library on the right side of the page.
Figure 1 – Download the .NET C# Class Library -
Click the Save button on the dialog box.
-
Save the CensusDemographicDataContainer.cs file to your project folder.
Tip If you intend to use more than one data service from the same data publisher in this project, make sure each class library file has a unique and meaningful name so you don’t overwrite one service class file with another.
Add the Service Class to Your Project
-
Return to your Visual Studio project.
-
In Solution Explorer right click the project name (which is in bold).
-
Select Add Existing Item from the dropdown menu.
-
Select the service proxy class you just downloaded to your project folder.
-
Click Add.
-
Double click the file name in Solution Explorer.
-
Find the namespace for this service class.
For example,namespace Alteryx. -
Add the following code at the top of any code files that make use of Marketplace datasets.
If multiple datasets are being used in a single application, repeat steps 2 through 8 for each data service.
In addition to the service class for the dataset you need to add a reference to System.Data.Services.Client to your project.
Add System.Data.Services.Client to the Application
-
Open Solution Explorer.
-
Right-click References.
-
From the drop-down menu select Add Reference.
-
From the dialog click the .NET tab.
-
Locate
System.Data.Services.Client. -
Click OK.
After the service classes have been properly imported and a reference to System.Data.Services.Client has been added, you can invoke the methods of the container class to query the dataset.
The following code defines a simple Console Application that uses the CensusDemographicDataContainer.cs dataset. It is important to note which particular dataset is being used, as the service classes have pre-configured the methods that expose the service parameters as strongly-typed method parameters in the application code, and the symbolic names of the service class objects are unique to the dataset.
Create a class to do the work
-
Create a public class
CensusDemographicDatawithin your project namespace. -
Create two private variables within your class.
-
A Uri to the service.
private Uri serviceUri; -
A service container for your authentication credentials.
private CensusDemographicDataContainer context;
-
A Uri to the service.
-
Add a constructor for the CensusDemographicData class.
The constructor initializes both private variables and the user credentials.
TheROOT_SERVICE_URLis the Service Root URL. (See Get the Service Root URL for how to find the Service Root URL.)
Thecontextis the service container which is used for user credentials.
TheIgnoreMissingPropertiesproperty is set to true to make the client robust to properties being added to the type on the server. (See MSDN documentation for additional information.)
TheUSER_IDis your Live ID.
TheSECURE_ACCOUNT_KEYis the Marketplace account key you’re using for this application. (See Manage Your Marketplace Account.)class CensusDemographicData { private Uri serviceUri; private CensusDemographicDataContainer context; // class constructor public CensusDemographicData() { serviceUri = new Uri(ROOT_SERVICE_URL); context = new CensusDemographicDataContainer(serviceUri); context.IgnoreMissingProperties = true; context.Credentials = new NetworkCredential(USER_ID, SECURE_ACCOUNT_KEY); } }
-
Create a public method (Sub) in the CensusDemographicData class that returns a generic list.
For our program the generic type isCensusDemographicDataEntity.
This method queries the data service and if successful returns the result set as anIList<>(IList (Of )). If the query fails for any reason the method returns anull(Nothing).class CensusDemographicData { private Uri serviceUri; private CensusDemographicDataContainer context; // constructor public CensusDemographicData() { serviceUri = new Uri(ROOT_SERVICE_URL); context = new CensusDemographicDataContainer(serviceUri); context.IgnoreMissingProperties = true; context.Credentials = new NetworkCredential(USER_ID, SECURE_ACCOUNT_KEY); } // the method that queries the service and returns the IList<> of demographic data public IList<CensusDemographicDataEntity> GetCensusDemographicData() { IEnumerable<CensusDemographicDataEntity> query; query = context.GetCensusDemographicData(LONGITUDE,LATITUDE,RADIUS,null); // The function GetCensusDemographicData is the fixed web function that you can use to request data from this service // It is found in the service class you downloaded and added to your project. // Note: either of the last two parameters may be null but both cannot be null try { return query.ToList(); } catch (Exception ex) { Console.WriteLine("Census ERROR {0}",ex.Message); return null; } } }
Write Main()
-
Write your
Main()method (Sub Main()) with two private variables.
A genericIListthat receives the data list from your method.
A variable to instantiate your class. -
Add the code to main that exercises your class and its method.
-
Create an instance of the CensusDemographicdata class.
-
Call the public GetCensusDemographicData method.
-
If the method returns a list loop through the list and print out the contents.
static void Main(string[] args) { IList<CensusDemographicDataEntity> demographicList; CensusDemographicData dataClass; dataClass = new CensusDemographicData(); demographicsList = dataClass.GetCensusDemographicData(); if (demographicsList != null) { Console.WriteLine("Demographic Data"); Console.WriteLine("0,-15} {1,10} {2,6} {3,4}", "Name", "Population", "Female", "Male"); foreach (CensusDemographicDataEntity entity in demographicList) Console.WriteLine("{0,-15} {1,10} {2,6} {3,4}", entity.NAME, entity.POP00, entity.SEX00FEM, entity.SEX00MAL); } Console.Write("Tap any key to end. "); Console.ReadKey(); } }
-
using System; using System.Collections.Generic; using System.Linq; using System.Net; // needed for authentication using System.Text; using Alteryx; // service class for this data service namespace FixedDataQuery { //============================================ Program class class Program { static void Main(string[] args) { IList<CensusDemographicDataEntity> demographicList; CensusDemographicData censusData = new CensusDemographicData(); demographicList = censusData.GetCensusDemographicData(); //============================================ CENSUS DEMOGRAPHIC DATA OUTPUT // confirm data was returned from query if (demographicList != null) { // print column headings Console.WriteLine("Demographic Data"); Console.WriteLine("{0,-15} {1,10} {2,6} {3,4}", "Name", "Population", "Female", "Male"); // print query results foreach (CensusDemographicDataEntity entity in demographicList) Console.WriteLine("{0,-15} {1,10} {2,6} {3,4}", entity.NAME, entity.POP00, entity.SEX00FEM, entity.SEX00MAL); } // pause until a key is struck Console.Write("Tap any key to end. "); Console.ReadKey(); Console.WriteLine(); } } //============================================ CensusDemographicData class class CensusDemographicData { private const string USER_ID = "yourLiveId"; private const string SECURE_ACCOUNT_ID = "yourMarketplaceAccountKey"; // not your Live password private const string ROOT_SERVICE_URL = "https://api.datamarket.azure.com/Data.ashx/Alteryx/CensusDemographicData"; private const double LONGITUDE = 47.394; private const double LATITUDE = -122.392; private const double RADIUS = 10.0; private Uri serviceUri; private CensusDemographicDataContainer context; // ------ constructor public CensusDemographicData() { serviceUri = new Uri(ROOT_SERVICE_URL); context = new CensusDemographicDataContainer(serviceUri); context.IgnoreMissingProperties = true; context.Credentials = new NetworkCredential(USER_ID, SECURE_ACCOUNT_ID); } // ------ method that queries the dataset and returns the resultset (or null) public IList<CensusDemographicDataEntity> GetCensusDemographicData() { IEnumerable<CensusDemographicDataEntity> query; query = context.GetCensusDemographicData(LONGITUDE,LATITUDE,RADIUS,null); // The function GetCensusDemographicData is the fixed web function that you can use to request data from this service // Note: either of the last two parameters may be null but both cannot be null try { return query.ToList(); } catch (Exception ex) { Console.WriteLine("Census ERROR {0}",ex.Message); return null; } } } }
The dataset’s metadata informs intellisense as you write your code. If your IDE does not support intellisense or intellisense fails to work you can get the metadata from the service.
-
Get the dataset’s service root URI.
See the section Get the Service Root URL for instructions on how to get the service root URI. -
Add
/$metadatato the end of your service root URL.
For example, if your service root URL ishttps://datamarket.azure.com/Data.ashx/fabrikam.com/inventorythe metadata URL ishttps://datamarket.azure.com/Data.ashx/fabrikam.com/inventory/$metadata. -
Navigate your browser to the metadata URL.
-
Parse the metadata.
The following metadata is for the Alteryx – Census Demographic Data data service.
<?xml version="1.0" encoding="utf-8" ?>
<edmx:Edmx Version="1.0" xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx" xmlns:dr="http://schemas.microsoft.com/dallas/2010/04">
<edmx:DataServices xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" m:DataServiceVersion="1.0">
<Schema xmlns="http://schemas.microsoft.com/ado/2009/08/edm" Namespace="Alteryx" Alias="Alteryx">
<EntityContainer Name="CensusDemographicDataContainer">
<EntitySet Name="CensusDemographicDataEntitySet" EntityType="Alteryx.CensusDemographicDataEntity" />
<FunctionImport Name="GetCensusDemographicData" EntitySet="CensusDemographicDataEntitySet" ReturnType="Collection(Alteryx.CensusDemographicDataEntity)">
<Parameter Name="Latitude" Type="Edm.Double" Mode="In" Nullable="false" />
<Parameter Name="Longitude" Type="Edm.Double" Mode="In" Nullable="false" />
<Parameter Name="Radius" Type="Edm.Double" Mode="In" Nullable="true" />
<Parameter Name="Minutes" Type="Edm.Double" Mode="In" Nullable="true" />
</FunctionImport>
</EntityContainer>
<EntityType Name="CensusDemographicDataEntity">
<Property Name="NAME" Type="Edm.String" Nullable="true" dr:Queryable="false" dr:Returned="true" />
<Property Name="KEY" Type="Edm.String" Nullable="true" dr:Queryable="false" dr:Returned="true" />
<Property Name="POP00" Type="Edm.String" Nullable="true" dr:Queryable="false" dr:Returned="true" />
<Property Name="SEX00FEM" Type="Edm.String" Nullable="true" dr:Queryable="false" dr:Returned="true" />
<Property Name="XSEX00FEM" Type="Edm.String" Nullable="true" dr:Queryable="false" dr:Returned="true" />
<Property Name="SEX00MAL" Type="Edm.String" Nullable="true" dr:Queryable="false" dr:Returned="true" />
<Property Name="XSEX00MAL" Type="Edm.String" Nullable="true" dr:Queryable="false" dr:Returned="true" />
<Property Name="XAGE000004" Type="Edm.String" Nullable="true" dr:Queryable="false" dr:Returned="true" />
<Property Name="XAGE000509" Type="Edm.String" Nullable="true" dr:Queryable="false" dr:Returned="true" />
<Property Name="XAGE001013" Type="Edm.String" Nullable="true" dr:Queryable="false" dr:Returned="true" />
<Property Name="XAGE001014" Type="Edm.String" Nullable="true" dr:Queryable="false" dr:Returned="true" />
<Property Name="XAGE001417" Type="Edm.String" Nullable="true" dr:Queryable="false" dr:Returned="true" />
<Property Name="XAGE001820" Type="Edm.String" Nullable="true" dr:Queryable="false" dr:Returned="true" />
<Property Name="XAGE002024" Type="Edm.String" Nullable="true" dr:Queryable="false" dr:Returned="true" />
<Property Name="XAGE002124" Type="Edm.String" Nullable="true" dr:Queryable="false" dr:Returned="true" />
<Property Name="XAGE002529" Type="Edm.String" Nullable="true" dr:Queryable="false" dr:Returned="true" />
<Property Name="XAGE003034" Type="Edm.String" Nullable="true" dr:Queryable="false" dr:Returned="true" />
<Property Name="XAGE003539" Type="Edm.String" Nullable="true" dr:Queryable="false" dr:Returned="true" />
<Property Name="XAGE004044" Type="Edm.String" Nullable="true" dr:Queryable="false" dr:Returned="true" />
<Property Name="XAGE004549" Type="Edm.String" Nullable="true" dr:Queryable="false" dr:Returned="true" />
<Property Name="XAGE005054" Type="Edm.String" Nullable="true" dr:Queryable="false" dr:Returned="true" />
<Property Name="XAGE00GT55" Type="Edm.String" Nullable="true" dr:Queryable="false" dr:Returned="true" />
<Property Name="XAGE005559" Type="Edm.String" Nullable="true" dr:Queryable="false" dr:Returned="true" />
<Property Name="XAGE006064" Type="Edm.String" Nullable="true" dr:Queryable="false" dr:Returned="true" />
<Property Name="XAGE006569" Type="Edm.String" Nullable="true" dr:Queryable="false" dr:Returned="true" />
<Property Name="XAGE007074" Type="Edm.String" Nullable="true" dr:Queryable="false" dr:Returned="true" />
<Property Name="XAGE007579" Type="Edm.String" Nullable="true" dr:Queryable="false" dr:Returned="true" />
<Property Name="XAGE008084" Type="Edm.String" Nullable="true" dr:Queryable="false" dr:Returned="true" />
<Property Name="XAGE00G85" Type="Edm.String" Nullable="true" dr:Queryable="false" dr:Returned="true" />
<Property Name="AGE00MED" Type="Edm.String" Nullable="true" dr:Queryable="false" dr:Returned="true" />
<Property Name="XEDU00GR911" Type="Edm.String" Nullable="true" dr:Queryable="false" dr:Returned="true" />
<Property Name="XEDU00ASSOC" Type="Edm.String" Nullable="true" dr:Queryable="false" dr:Returned="true" />
<Property Name="XEDU00BACH" Type="Edm.String" Nullable="true" dr:Queryable="false" dr:Returned="true" />
<Property Name="XEDU00GRAD" Type="Edm.String" Nullable="true" dr:Queryable="false" dr:Returned="true" />
<Property Name="XEDU00HSCH" Type="Edm.String" Nullable="true" dr:Queryable="false" dr:Returned="true" />
<Property Name="XEDU00NSCH" Type="Edm.String" Nullable="true" dr:Queryable="false" dr:Returned="true" />
<Property Name="XEDU00LTGR9" Type="Edm.String" Nullable="true" dr:Queryable="false" dr:Returned="true" />
<Property Name="XEDU00SCOLL" Type="Edm.String" Nullable="true" dr:Queryable="false" dr:Returned="true" />
<Property Name="XRAC00AMIND" Type="Edm.String" Nullable="true" dr:Queryable="false" dr:Returned="true" />
<Property Name="XRAC00ASIAN" Type="Edm.String" Nullable="true" dr:Queryable="false" dr:Returned="true" />
<Property Name="XRAC00BLACK" Type="Edm.String" Nullable="true" dr:Queryable="false" dr:Returned="true" />
<Property Name="XHIS00HISP" Type="Edm.String" Nullable="true" dr:Queryable="false" dr:Returned="true" />
<Property Name="XRAC00HAWAI" Type="Edm.String" Nullable="true" dr:Queryable="false" dr:Returned="true" />
<Property Name="XHIS00NHISP" Type="Edm.String" Nullable="true" dr:Queryable="false" dr:Returned="true" />
<Property Name="XRAC00OTHER" Type="Edm.String" Nullable="true" dr:Queryable="false" dr:Returned="true" />
<Property Name="XRAC00MULT" Type="Edm.String" Nullable="true" dr:Queryable="false" dr:Returned="true" />
<Property Name="XRAC00WHITE" Type="Edm.String" Nullable="true" dr:Queryable="false" dr:Returned="true" />
<Property Name="XHIN00LT10" Type="Edm.String" Nullable="true" dr:Queryable="false" dr:Returned="true" />
<Property Name="XHIN001015" Type="Edm.String" Nullable="true" dr:Queryable="false" dr:Returned="true" />
<Property Name="XHIN001520" Type="Edm.String" Nullable="true" dr:Queryable="false" dr:Returned="true" />
<Property Name="XHIN002025" Type="Edm.String" Nullable="true" dr:Queryable="false" dr:Returned="true" />
<Property Name="XHIN002530" Type="Edm.String" Nullable="true" dr:Queryable="false" dr:Returned="true" />
<Property Name="XHIN003035" Type="Edm.String" Nullable="true" dr:Queryable="false" dr:Returned="true" />
<Property Name="XHIN003540" Type="Edm.String" Nullable="true" dr:Queryable="false" dr:Returned="true" />
<Property Name="XHIN004045" Type="Edm.String" Nullable="true" dr:Queryable="false" dr:Returned="true" />
<Property Name="XHIN004550" Type="Edm.String" Nullable="true" dr:Queryable="false" dr:Returned="true" />
<Property Name="XHIN005060" Type="Edm.String" Nullable="true" dr:Queryable="false" dr:Returned="true" />
<Property Name="XHIN006075" Type="Edm.String" Nullable="true" dr:Queryable="false" dr:Returned="true" />
<Property Name="XHIN0075100" Type="Edm.String" Nullable="true" dr:Queryable="false" dr:Returned="true" />
<Property Name="XHIN0010025" Type="Edm.String" Nullable="true" dr:Queryable="false" dr:Returned="true" />
<Property Name="XHIN0012550" Type="Edm.String" Nullable="true" dr:Queryable="false" dr:Returned="true" />
<Property Name="XHIN0015020" Type="Edm.String" Nullable="true" dr:Queryable="false" dr:Returned="true" />
<Property Name="XHIN00GT200" Type="Edm.String" Nullable="true" dr:Queryable="false" dr:Returned="true" />
<Property Name="HIN00MED" Type="Edm.String" Nullable="true" dr:Queryable="false" dr:Returned="true" />
<Property Name="INC00AVEHH" Type="Edm.String" Nullable="true" dr:Queryable="false" dr:Returned="true" />
<Property Name="INC00PCI" Type="Edm.String" Nullable="true" dr:Queryable="false" dr:Returned="true" />
<Property Name="HOO00MEDN" Type="Edm.String" Nullable="true" dr:Queryable="false" dr:Returned="true" />
<Property Name="XYMI00BF69" Type="Edm.String" Nullable="true" dr:Queryable="false" dr:Returned="true" />
<Property Name="XYMI007079" Type="Edm.String" Nullable="true" dr:Queryable="false" dr:Returned="true" />
<Property Name="XYMI008089" Type="Edm.String" Nullable="true" dr:Queryable="false" dr:Returned="true" />
<Property Name="XYMI009094" Type="Edm.String" Nullable="true" dr:Queryable="false" dr:Returned="true" />
<Property Name="XYMI009598" Type="Edm.String" Nullable="true" dr:Queryable="false" dr:Returned="true" />
<Property Name="XYMI009900" Type="Edm.String" Nullable="true" dr:Queryable="false" dr:Returned="true" />
<Property Name="HOU00STAB" Type="Edm.String" Nullable="true" dr:Queryable="false" dr:Returned="true" />
<Property Name="HOU00TURN" Type="Edm.String" Nullable="true" dr:Queryable="false" dr:Returned="true" />
</EntityType>
</Schema>
</edmx:DataServices>
</edmx:Edmx>
Important information found in the metadata includes:
-
The name of the fixed web function for this dataset that you call to request data and the type it returns.
<FunctionImport Name="GetCensusDemographicData" EntitySet="CensusDemographicDataEntitySet" ReturnType="Collection(Alteryx.CensusDemographicDataEntity)"> -
The order of the parameters you pass to the function as well as the name, type, mode and whether it is required or not for each parameter.
<Parameter Name="Latitude" Type="Edm.Double" Mode="In" Nullable="false" /><Parameter Name="Longitude" Type="Edm.Double" Mode="In" Nullable="false" /><Parameter Name="Radius" Type="Edm.Double" Mode="In" Nullable="true" /><Parameter Name="Minutes" Type="Edm.Double" Mode="In" Nullable="true" />
Note that two of the parameters require values (Nullable="false") and two where values are optional (Nullable="true"). -
The name, data type, whether a
null(Nothing) is a valid value, of the returned fields.<Property Name="HOU00TURN" Type="Edm.String" Nullable="true" dr:Queryable="false" dr:Returned="true" />
Note that in fixed query dataset the attributedr:Queryableis always“false”. -
The name of this dataset’s entity.
<EntityType Name="CensusDemographicDataEntity"> -
The container name for this dataset.
<EntityContainer Name="CensusDemographicDataContainer">