# Friday, September 11, 2009

When SQL Data Services (now SQL Azure) released its first CTP it did not look anything like SQL Server: there were no tables, stored procedures, views, etc. With the new CTP, SQL Azure embraces SQL Server in the sky and supports the relational model, including stored procedures. This is good since there are millions of lines of stored procedures out there in production today and migrating them to SQL Azure is pretty easy.

I decided to give a simple stored procedure a test drive. I opened up SQL Management Studio, started a new query, entered in my SQL:

   1:  CREATE Procedure sel_CustomerOrder   
   2:  @CustomerID char(5)   
   3:  AS
   4:  SELECT o.OrderID, o.OrderDate,  p.ProductName,   
   5:       (od.UnitPrice*od.Quantity)-((od.UnitPrice*od.Quantity)*od.Discount) as TotalCost   
   6:  FROM orders o   
   7:       inner join [order details] od   
   8:              on o.OrderID=od.OrderID  
   9:       inner join products p  
  10:              on od.ProductID=p.ProductId  
  11:  WHERE CustomerID=@CustomerID
  12:  ORDER BY o.OrderID

 

I hit F5 and all was right with the world. So I decided to try it out:

Exec sel_CustomerOrder @CustomerID='ALFKI'

And got the expected results.

image

You can now use this stored procedure in all the applications that you connect to since SQL Azure supports standard ADO.NET connection strings. Pretty cool.

posted on Friday, September 11, 2009 12:48:44 AM (Eastern Daylight Time, UTC-04:00)  #    Comments [0] Trackback
# Thursday, September 10, 2009

I like the idea of a database in the cloud. We have sort of been doing it for years, connecting to a SQL Server database over 1433. With SQL Azure, we take this one level further and let Azure worry about the hardware. So we don’t have to worry about scaling out with the bandwidth, RAIDs, etc. 

Last week I showed the basics on how to migrate data from a SQL Server 2008 database to SQL Azure. Yesterday I showed using SQL Azure with Telerik OpenAccess  and the WCF REST Toolkit. Today I will show how to build a simple REST based application using ADO.NET Data Services (aka Astoria.)

To get started we need a SQL Azure database. See my previous blog post about the CTP and getting data into your SQL Azure database. Once you have a SQL Azure database all set up let’s get to work.

The next thing we need to do is to create a new Web project and create our Entity Framework data model. I’ll go ahead and create an Entity Data Model against my local SQL Server 2008 Northwind database that has the same schema as my SQL Azure one. This is because SQL Azure and the Entity Framework chokes on the designer (or at least my version!) I will map:

  • Customers
  • Orders
  • Order Details

Now that my EDM is all set up, I will go in and change the connection string in my web.config to use SQL Azure. Here is my new connection string:

<add name="NorthwindEntities" 
connectionString="metadata=res://*/Northwind.csdl
|res://*/Northwind.ssdl|
res://*/Northwind.msl;
provider=System.Data.SqlClient;
provider connection string=&quot;Data Source=tcp:tpzlfbclx123.ctp.database.windows.net;
Initial Catalog=Northwind_Lite;
Integrated Security=False;UID=Stevef;PWD=GoMets!;
MultipleActiveResultSets=False&quot;"
providerName="System.Data.EntityClient"/>

You have to manipulate the EF connection string and put in the SQL Azure server name of your CTP in the “Data Source” and put in the database name in the Initial Catalog, turn off integrated security and put in the UID/PWD from the CTP. I set MARS set to false since SQL Azure does not support MARS.

Now let’s create the Astoria Service. Add a new “ADO.NET Data Service” to your project. I named mine NwindRestService.

image

Astoria can’t make it any easier for you to get the service up and running. All you need to do is set up the name of your EDM in line 2, in our case it was NorthwindEntities and also set the access permissions on line 8. I just uncommented the generated line and put in an “*” so all of my entities will inherit the AllRead access rule. With that we are good to go!

   1:  //Enter the name of your EDM (NorthwindEntities)
   2:  public class NwindRestService : DataService<NorthwindEntities>
   3:  {
   4:      public static void InitializeService(IDataServiceConfiguration config)
   5:      {
   6:          //Must set up the AccessRule, here I allow read only access
   7:          //to all entities. I can also do this one by one.
   8:          config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
   9:      }
  10:  }

 

For the reality check, let’s run the service in the browser, being a RESTful service, Astoria will allow you to browse all of the Customers by typing in this URL:

http://localhost:1075/NwindRestService.svc/Customers

We should see this:

image

I also edited my first row in Northwind (ALFKI) to say “SQL Azure” at the end of the customer name so I know I am working with the SQL Azure and did not mess up my connection strings. That is it, you now have a RESTful service that is hooked up to SQL Azure.

The hard part is over. Now let’s build a simple ASP.NET client to consume the RESTful data.

First you have to set a reference to your service. This will give you a proxy to write some LINQ (to Astoria) code against.

image

 

Next we will create a simple ASP.NET GridView control and bind some data to it on the page load event. (Sure we can do a lot more, but this is just to get our feet wet with SQL Azure.)

   1:  //the address of our service
   2:  Uri url = new Uri("http://localhost:1075/NwindRestService.svc/", UriKind.Absolute);
   3:  //a ref to our proxy 
   4:  ServiceReference1.NorthwindEntities dat = 
   5:          new ServiceReference1.NorthwindEntities(url);
   6:   
   7:  //link statement to get the data, can use WHERE, Orderby, etc
   8:  var customers =
   9:      from c in dat.Customers
  10:      where c.Country == "Germany"
  11:      orderby c.CustomerID
  12:      select c;
  13:   
  14:  //bind to the grid
  15:  GridView1.DataSource = customers;
  16:  GridView1.DataBind();
 

This is pretty basic code from here.  Line 2 is a Uri reference to our service (which is technically in the same project, but it could (and should) be in a different project.) Line 4-5 is setting up a reference to the proxy we created and this is also our data context, representing the Astoria service. Lines 8-12 is a simple LINQ to Astoria statement to filter by the German customers (look like LINQ to SQL? That is the point!) and Lines 15-16 is where we do the data binding to the ASP.NET GridView. Our gridview looks like this, notice the ALFKI record says it is coming from SQL Azure:

image

That is all there is too it. Enjoy.

posted on Thursday, September 10, 2009 5:39:59 AM (Eastern Daylight Time, UTC-04:00)  #    Comments [0] Trackback
# Wednesday, September 9, 2009

The following video shows how to use the Telerik OpenAccess WCF Wizard with ATOMPub services via the WCF REST Starter Kit. The video is done by .NET Ninja in training Peter Bahaa  and uses the same ATOMPub project I showed yesterday on my blog. Enjoy!

Telerik OpenAccess WCF Wizard: How-to Video #4- AtomPub from Stephen Forte on Vimeo.

posted on Wednesday, September 9, 2009 2:11:36 AM (Eastern Daylight Time, UTC-04:00)  #    Comments [0] Trackback
# Tuesday, September 8, 2009

Feeds are part of what power Web 2.0. You can use RSS or ATOM to syndicate your content and allow readers to subscribe to your content. It is what powers Twitter, Facebook, as well as CNN and the New York Times. ATOM is a popular alternative to RSS. Just about every blog and “RSS” reader will support ATOM. When you are talking about ATOM, you actually are talking about two things: Atom Syndication Format, an XML language for feed definitions, and the Atom Publishing Protocol, a very simple HTTP protocol for creating and updating feed based content.

The WCF REST Starter Kit allows you to create services that will produce an ATOM feed or expose your data as a service via the ATOM Publishing Protocol. The WCF REST Starter Kit gives you a Visual Studio template to get your started.

You can use Telerik OpenAccess as a data source for the collections of your ATOMPub service. To do so you have to wire up some code in your svc file back to your OpenAccess entities. This can be accomplished by using the OpenAccess WCF Wizard.

Creating the ATOMPub Service and a Silverlight Front End

To get started, let’s create four projects in Visual Studio, one data access layer using OpenAccess, one AtomPub service project using the new WCF REST Toolkit Visual Studio template, and a Silverlight Web/Client.

image

Then you can use the Telerik OpenAccess WCF Wizard to automatically create the SVC and CS files for an ATOMPub service (the arrow above.) Once you have that and allow the project to talk to the DAL with a reference to that project, view the service in the browser for a reality check. You can do the format, similar to REST of: http:// server name / service name / resource name, for example:

http://localhost:54669/NorthwindAtomPubService.svc/Customers will return a list of all the customers and if you add an /CustomerID to the end of the URL like this: http://localhost:54669/NorthwindAtomPubService.svc/Customers/ALFKI then you will bring up one individual customer.

image

Now let’s consume this from a Silverlight application. Pretty easy stuff, but first we have to create a XAML grid:

<data:DataGrid x:Name="dataGridCustomers" 
               AutoGenerateColumns="False" ItemsSource="{Binding}">
    <data:DataGrid.Columns>
        <data:DataGridTextColumn 
            Binding="{Binding Path=CompanyName}" Header="Company Name">
        </data:DataGridTextColumn>
        <data:DataGridTextColumn 
            Binding="{Binding Path=ContactName}" Header="Contact Name">
       </data:DataGridTextColumn>
    </data:DataGrid.Columns>
</data:DataGrid>

After you build your XAML grid, let’s set a service reference to the AtomPub service and then start to write some code. Just like before, I will have a LoadData() method to fill the grid. Of course being Silverlight it has to be asynchronous. Here is the code.

   1:  private void LoadData()
   2:  {
   3:  //the URL of our ATOM Pub service
   4:  string uri = "http://localhost:54669/NorthwindAtomPubService.svc/Customers";
   5:  //set up the web request
   6:  HttpWebRequest request = HttpWebRequest.Create(
   7:      new Uri(uri)) as HttpWebRequest;
   8:  //HTTP GET (REST uses the standard HTTP requests)
   9:  request.Method = "GET";
  10:  //begin the async call in a code block
  11:  request.BeginGetResponse(ar =>
  12:      {
  13:          Dispatcher.BeginInvoke(() =>
  14:              {
  15:                  //catch the HTTPWebRequest
  16:                  HttpWebResponse response = 
  17:                      request.EndGetResponse(ar) as HttpWebResponse;
  18:                  //get the customers back
  19:                  var result = 
  20:                      Customer.GetCustomersFromAtom20Stream(response.GetResponseStream());
  21:   
  22:                  //stuff the customers into a LIST
  23:                  List<Customer> customers = new List<Customer>();
  24:   
  25:                  foreach (var customer in result)
  26:                  {
  27:                      customers.Add(customer);
  28:                  }
  29:                  //bind the LIST to the Silverlight DataGrid
  30:                  dataGridCustomers.DataContext = customers;
  31:              });
  32:      }, null);
  33:  }

 

This code is easier than it looks. We start off with a HTTPWebRequest for the service (line 6-7) using an HTTP GET (line 9) and a code block to handle the async call (starting on line 11) . This code block works similar to a callback method. Inside the code block we catch the HTTPWebRequest asynchronously and get the results into the implicitly typed local variable var on line 19. After that is just basic LIST stuff, filling the list and adding it as the source of the dataGrid.

image

Pretty easy. Since that was so easy, let’s shake it up a little bit. How about we make the backend database be SQL Azure instead of SQL Server 2008.

Connecting Telerik OpenAccess to SQL Azure

Turns out that using SQL Azure and Telerik OpenAccess is pretty easy. If you have an Azure schema that is the same as your SQL Server 2008 schema, all you have to do it change the connection string in the OpenAccess DAL project’s app.config.  Let’s change our connection string in the DAL project to use SQL Azure like this:

   1:  <connection id="Connection.Azure">
   2:    <databasename>Northwind_Lite</databasename>
   3:    <servername>tcp:tpzlfbclx123.ctp.database.windows.net</servername>
   4:    <integratedSecurity>False</integratedSecurity>
   5:    <backendconfigurationname>mssqlConfiguration</backendconfigurationname>
   6:    <user>Stevef</user>
   7:    <password>gomets</password>
   8:  </connection>

 

Line 3 is the server you get from the SQL Azure CTP program and the user name and password is what you set up when you got the CTP. (Don’t have a SQL Azure CTP? Sign up here!)

That is it. When I run it I get the same results, except that the data is coming from SQL Azure and not from SQL Server 2008. I went into my Northwind_Lite database in SQL Azure and edited the first Customer row so I always know the data is coming from SQL Azure:

Update Customers 
Set CompanyName= 'Alfreds Futterkiste SQL Azure'
Where CustomerID='ALFKI'

Now when I run the project, I see the data from SQL Azure:

image

That is all there is too it!

You can get the WCF REST Starter Kit here, the Telerik OpenAccess WCF Wizard here, and the code from this blog post here.

Enjoy!

posted on Tuesday, September 8, 2009 5:09:58 AM (Eastern Daylight Time, UTC-04:00)  #    Comments [0] Trackback
# Sunday, September 6, 2009

The following video shows how to use the Telerik OpenAccess WCF Wizard with REST Collections via the WCF REST Starter Kit. The video is done by .NET Ninja in training Peter Bahaa and uses the same project I showed the other day on my blog. Enjoy!

Telerik OpenAccess WCF Wizard: How-to Video #3- REST Collection from Stephen Forte on Vimeo.

posted on Sunday, September 6, 2009 8:12:34 PM (Eastern Daylight Time, UTC-04:00)  #    Comments [0] Trackback
# Friday, September 4, 2009

SQL Data Services underwent a massive change with the last CTP, eliminating the entity bag container, whatever they called it thingie and moved to a fully cloud-based relational model. Another major change was that it was given a new and more appropriate name: SQL Azure. You can get the CTP and have access to what I call SQL Server lite in the cloud. Since SQL Azure supports the fully relational model along with stored procedures and views, you can connect to SQL Azure with a regular old ADO.NET connection string like the following one, allowing you to code against SQL Azure with .NET the same way you did with plain old SQL Server.

Server=tcp:tpzlfbclx1.ctp.database.windows.net;Database=Northwind_Lite;User ID=Stevef;Password=myPassword;Trusted_Connection=False;

Once you are all signed up for the CTP you can go into the web based admin tools and create a database. I created a database called Northwind and another one called Northwind_Lite for testing.

image

To be honest, I am not sure what else you can do in the web interface. So you have to connect via SQL Management Studio to create your database schema. There is the first problem. SQL Azure does not support the object explorer view that you get in SQL Management Studio, so you will have to hack a little bit.

Connecting to SQL Azure with SQL Server Management Studio

This is not as easy as it sounds. :) Since you can’t connect through the object explorer, you will have to open a new TSQL Query window.

image

In the log in dialog, enter in the server name from the CTP’s connection string and the user name and password that you choose to administer the CTP.

image

SQL Azure does not support the “Use” statement, or the ability to change databases on your connection. So you have to cheat and use some of the advanced options when logging in. You can do this by selecting the “Options >>” button on the log in dialog and then selecting “Connection Properties”. Under the Connect to database option, you have to select the database that you want to work with, since the default will be the Master database and most likely you will not be building any applications using the Master database.

image

After you connect you will get an error about the inability to apply connection settings, which you can ignore.

image

You will notice right away that there is nothing in your database as the following SQL statement will show:

select * from sys.objects

We now have to migrate some database objects from our SQL Server database to SQL Azure.

Migrating Existing SQL Server Objects to a SQL Azure Database

It would be cool if there were some easy way to migrate your databases to SQL Azure in this CTP. There is not. I suspect that in future CTPs this will not be a problem. But for now, you have to get creative. Some hacks and shortcuts are in order.

To get started, let’s just copy over one table. To do this, open your local SQL Server in the object explorer. Drill down to the Northwind database and drill down into the Customers table. Right click and select Script Table as|CREATE To|Clipboard and you will have a nice CREATE TABLE statement on your clipboard.

 

image

Then paste the TSQL into the Query Window that is connected to your SQL Azure database. Here is what my generated TSQL looks like:

   1:  USE [Northwind]
   2:  GO
   3:   
   4:  /****** Object:  Table [dbo].[Customers]    Script Date: 09/04/2009 03:35:38 ******/
   5:  SET ANSI_NULLS ON
   6:  GO
   7:   
   8:  SET QUOTED_IDENTIFIER ON
   9:  GO
  10:   
  11:  CREATE TABLE [dbo].[Customers](
  12:      [CustomerID] [nchar](5) NOT NULL,
  13:      [CompanyName] [nvarchar](40) NOT NULL,
  14:      [ContactName] [nvarchar](30) NULL,
  15:      [ContactTitle] [nvarchar](30) NULL,
  16:      [Address] [nvarchar](60) NULL,
  17:      [City] [nvarchar](15) NULL,
  18:      [Region] [nvarchar](15) NULL,
  19:      [PostalCode] [nvarchar](10) NULL,
  20:      [Country] [nvarchar](15) NULL,
  21:      [Phone] [nvarchar](24) NULL,
  22:      [Fax] [nvarchar](24) NULL,
  23:   CONSTRAINT [PK_Customers] PRIMARY KEY CLUSTERED 
  24:  (
  25:      [CustomerID] ASC
  26:  )
  27:  WITH
  28:   (
  29:  PAD_INDEX  = OFF, 
  30:  STATISTICS_NORECOMPUTE  = OFF, 
  31:  IGNORE_DUP_KEY = OFF, 
  32:  ALLOW_ROW_LOCKS  = ON, 
  33:  ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
  34:  ) 
  35:  ON [PRIMARY]
  36:   
  37:  GO
  38:   

We already know that SQL Azure does not support USE, so eliminate lines 1&2 and press F5. You will see that line 5 also is not supported, so eliminate that and keep going by pressing F5 again. You will see that ANSI_NULLs, PAD_INDEX, ALLOW_ROW_LOCKS, ALLOW_PAGE_LOCKS, and ON [PRIMARY] are not supported, so you will have to eliminate them as well. Your new trimmed down SQL Azure SQL script looks like this:

   1:  SET QUOTED_IDENTIFIER ON
   2:  GO
   3:  CREATE TABLE [dbo].[Customers](
   4:      [CustomerID] [nchar](5) NOT NULL,
   5:      [CompanyName] [nvarchar](40) NOT NULL,
   6:      [ContactName] [nvarchar](30) NULL,
   7:      [ContactTitle] [nvarchar](30) NULL,
   8:      [Address] [nvarchar](60) NULL,
   9:      [City] [nvarchar](15) NULL,
  10:      [Region] [nvarchar](15) NULL,
  11:      [PostalCode] [nvarchar](10) NULL,
  12:      [Country] [nvarchar](15) NULL,
  13:      [Phone] [nvarchar](24) NULL,
  14:      [Fax] [nvarchar](24) NULL,
  15:   CONSTRAINT [PK_Customers] PRIMARY KEY CLUSTERED 
  16:  (
  17:      [CustomerID] ASC
  18:  )WITH 
  19:      (STATISTICS_NORECOMPUTE  = OFF, 
  20:          IGNORE_DUP_KEY = OFF) 
  21:  ) 
  22:  GO
  23:   

Run this and you will have a new Customers table! Unfortunately there is no data in there, but we will get to that soon.

image

If you are moving a lot of tables and foreign key constraints, etc, you should use the SQL Azure Migration Wizard developed by George Huey. This tool,available on codeplex, will assist you in migrating your SQL Server schemas over to SQL Azure. Wade Wegner blogged about it here, including an instructional video.

Unfortunately there is no such tool for migrating data that I know of. Time for the next hack.

Migrating Data from SQL Server to SQL Azure

I thought that maybe I can cheat the same way I altered the connection settings and use SSIS to migrate the data. I choose the ADO.NET option and entered in all of the data, but it bombed. Then I tried my old reliable tool, Red Gate’s SQL Data Compare. No go. But it was worth a try, since it got me thinking. I created a new database locally called “Azure_Staging” and ran the same CREATE TABLE script there, creating a blank Customers table. I then ran SQL Data Compare using the full Customer table in Northwind as my source and my newly created blank Customer table in Azure_Staging as the destination.

Of course SQL Data Compare found 91 missing rows and I launched the Synchronization Wizard.

image

Click through it and on the 3rd page, click on the “View SQL Script…” button and copy and paste the generated SQL.

image

Copy and paste just the 91 INSERT INTO statements into your SQL Azure Query Window and run it. Now we have data in SQL Azure!

image

Unfortunately this is not the best situation, having to manually create some TSQL scripts, but this is an early CTP. I am sure that future CTPs will make this much easier.

posted on Friday, September 4, 2009 4:19:51 AM (Eastern Daylight Time, UTC-04:00)  #    Comments [0] Trackback
# Thursday, September 3, 2009

Developers have been using the REST specification for some time. If you are using Microsoft tools, ADO.NET Data Services aka Astoria is a very popular way to work with REST data. What you may not know is that Astoria works on top of WCF and you can write your own REST services outside of the Astoria model using WCF. WCF 3.5 SP1 gives us quite a few hooks to build our own RESTful services, however, it still takes a lot of manual wiring up by the developer. By now you all should know that I hate plumbing code.

Microsoft introduced the WCF REST Starter Kit, a set of WCF extensions and Visual Studio project templates to eliminate the plumbing code when building RESTful service with WCF. The five Visual Studio templates are:

  • REST Singleton Service
  • REST Collection Service
  • ATOM Feed Service
  • ATOMPub Service
  • HTTP Plain XML Service
  •  

    As explained in the developers guide to the WCF REST Starter Kit, the templates do the following:

    REST Singleton Service-Produces a service that defines a sample singleton resource (SampleItem) and the full HTTP interface for interacting with the singleton (GET, POST, PUT, and DELETE) with support for both XML and JSON representations.

    REST Collection Service-Similar to the REST Singleton Service only it also provides support for managing a collection of SampleItem resources.

    Atom Feed Service-Produces a service that exposes a sample Atom feed with dummy data.

    AtomPub Service-Produces a fully functional AtomPub service capable of managing collections of resources as well as media entries.

    HTTP Plain XML Service-Produces a service with simple GET and POST methods that you can build on for plain-old XML (POX) services that don’t fully conform to RESTful design principles, but instead rely only on GET and POST operations.

    While the REST Singleton is interesting, it is only useful if you are exposing one item, so the REST Collection is more suitable for interaction with a database driven dataset. The Atom Feed template is interesting but it is more useful if you are building feeds similar to RSS, so the AtomPub Service is more useful. The POX is a good option if you need to do something custom.

    While the REST WCF Starter Kit also provides some client libraries for easier interaction with your RESTful data, we will focus on the creation of the services.

    You can use Telerik OpenAccess as a data source of your REST Collection service. To do so you have to wire up some code in your svc file. Sound like a lot of work? Enter the OpenAccess WCF Wizard I wrote about before.

     

    If you create a project in Visual Studio to contain your data access layer and another to contain the REST Collection (using the new REST Collection template available from the WCF REST Starter Kit), you can point the Telerik WCF OpenAccess WCF Wizard at the data access layer project and then automatically generate the svc file and the corresponding CS file (shown by the arrow in our Visual Studio solution below.)

    image

    Just for a sanity check, let’s run our service by selecting the svc file and saying “view in browser”. You should see the RESTful XML representation as show below (make sure you turn off feed view in IE):

     image

    Now let’s consume this service from a Silverlight application. The WCF REST Starter Kit provides the developer with two classes, HttpClient and HttpMethodExtensions to help you consume the WCF RESTful service. Unfortunately they are not supported in Silverlight (or at least I can’t figure it out. :) )

    We’ll use plain old HTTPWebRequest instead.  But first I will create a grid in my XAML code like so:

    <data:DataGrid x:Name="dataGridCustomers" AutoGenerateColumns="False" ItemsSource="{Binding}">
        <data:DataGrid.Columns>
            <data:DataGridTextColumn Binding="{Binding Path=CompanyName}" Header="Company Name">
            </data:DataGridTextColumn>
            <data:DataGridTextColumn Binding="{Binding Path=ContactName}" Header="Contact Name">
            </data:DataGridTextColumn>
        </data:DataGrid.Columns>
    </data:DataGrid>

    I will create a LoadData() method to load the data on the page load or a “refresh” button event. Being Silverlight, of course we will use some asynchronous processing.

       1:  private void LoadData()
       2:  {
       3:      //address of your REST Collection service
       4:      string url= "http://localhost:60613/Customers.svc";
       5:      //set up the web resquest
       6:      HttpWebRequest rest = HttpWebRequest.Create(new Uri(url)) as HttpWebRequest;
       7:      //HTTP GET (REST uses the standard HTTP requests)
       8:      rest.Method = "GET";
       9:      //async callback
      10:      rest.BeginGetResponse(new AsyncCallback(ReadAsyncCallBack), rest);
      11:  }

     

    First we have to set a reference to our service in lines 4 & 6. Then we tell the HttpWebRequest to use an HTTP GET (line 8), this is the power of REST, it uses the standard HTTP requests (GET, POST, PUT, DELETE). On line 10 is where we begin our asynchronous call to ReadAsyncCallback() shown here.

       1:  private void ReadAsyncCallBack(IAsyncResult iar)
       2:  {
       3:      
       4:      //catch the HttpWebRequest
       5:      HttpWebRequest rest = (HttpWebRequest)iar.AsyncState;
       6:      HttpWebResponse response = rest.EndGetResponse(iar) as HttpWebResponse;
       7:      
       8:      var result = Customer.GetCustomersFromAtom10Stream(response.GetResponseStream());
       9:      //code block to handle the async call
      10:      this.Dispatcher.BeginInvoke( () =>
      11:          {
      12:              //build a collection (customers)
      13:              var customers = 
      14:                new System.Collections.ObjectModel.ObservableCollection<Customer>();
      15:              foreach (var customer in result)
      16:              {
      17:                  customers.Add(customer);
      18:              }
      19:              //bind to the grid when done
      20:              this.dataGridCustomers.DataContext = customers;
      21:          });
      22:  }

    ReadAsyncCallback() is the handler for the asynchronous call we did in LoadData(). We obtain a reference to the HttpWebRequest (lines 5-6) and then get the results back in line 8. Then we use a code block to build an ObservableCollection of Customers and fill them in a loop of the results (lines 15-18) and bind the data to the grid in line 20. The results are data binded to a grid as shown below.

    image

    Since Silverlight doesn’t support HTTP methods that do an update, we can’t do updates without a wrapper on the server. So we will stop the demo with just the read operation. Remember, if you are using a non-Silverlight client such as ASP.NET you can use the HttpClient and HttpMethodExtensions classes for an easier client coding experience.

    Grab the Telerik OpenAccess WCF Wizard here and the sample code from this blog post here.

    posted on Thursday, September 3, 2009 8:59:03 AM (Eastern Daylight Time, UTC-04:00)  #    Comments [0] Trackback
    # Wednesday, September 2, 2009

    I have written a white paper for the Microsoft Oslo team that is available on MSDN here. The paper is titled: “Using Oslo to Speed Up Database Development” and shows how you can use the new M language to model databases, browse that model in Quadrant, and tap into the power of the Oslo Repository.

    The paper shows how the model you make is mapped to TSQL and to SQL Server objects, including Tables and Views. It is pretty cool to see the following M type and its M values and how they will map to a TSQL script to create a People table and INSERT INTO statements for the Steve and Mike rows.

       1:  //Types
       2:  type Person
       3:  {
       4:      Id : Integer32;
       5:      Name : Text#50;
       6:      Age : Integer32;
       7:  }
       8:  //Values
       9:  People:Person*;
      10:  People
      11:  {
      12:      { Id=>1, Name=>"Steve", Age=>36},
      13:      { Id=>2, Name=>"Mike", Age=>29}
      14:  }

     

    The corresponding TSQL will look like this:

       1:  create table [OsloDemo].[People]
       2:  (
       3:    [Age] int not null,
       4:    [Id] int not null,
       5:    [Name] nvarchar(50) not null
       6:  );
       7:  go
       8:   
       9:  insert into [OsloDemo].[People] ([Id], [Name], [Age])
      10:   values (1, N'Steve', 36)
      11:  ;
      12:   
      13:  insert into [OsloDemo].[People] ([Id], [Name], [Age])
      14:   values (2, N'Mike', 29)
      15:  ;
      16:  go

     

    Developers will like to model in M since it will abstract them from the ins and outs of the database.

    In addition the paper shows how to use the repository and how modeled types will benefit from using the repository. In addition I discuss a little abut the DSL capabilities of Oslo using M Languages. What is cool is that I also talk about Telerik’s LINQ to M at the end, and how to use it with the DSL and M languages with Visual Studio and C#.

    You can get the paper here.

    posted on Wednesday, September 2, 2009 9:19:14 PM (Eastern Daylight Time, UTC-04:00)  #    Comments [0] Trackback
    # Friday, August 28, 2009

    Apparently iPhones explode. There is a report today of a 15 year old in Belgium who’s iPhone exploded. In addition there was a report about two weeks ago of an iPhone in France and there have been other reports in the UK and across the US. Luckily only minor injuries have been reported.

    The European Commission is starting to investigate. Apparently Apple has not issued a warning and it could be a fluke.

    posted on Friday, August 28, 2009 10:39:59 AM (Eastern Daylight Time, UTC-04:00)  #    Comments [0] Trackback