Welcome to MSDN Blogs Sign in | Join | Help
Adding Silverlight into your blog

Luc Vo Van, one of my colleges has recently created the Windows Live Writer Silverlight App Plugin. Here is the link, really easy to use :

http://blogs.msdn.com/luc/archive/2009/11/19/windows-live-writer-silverlight-app-plugin.aspx

Many Thanks Luc :-)

ADO.NET Data Services : Tips & Tricks

Introduction

In this post, I’ll give you some tips interesting that helped me in my several projects.

For more information on Data Services, I suggest you to read my previous posts:

TIPS 1 : ADO.NET Data Services CTP 1.5 CTP 2

Accessible here, this new release targeting .net 3.5 & Silverlight 3 provides some great new features (some more details about this release):

  • Projections;
  • Better data binding;
  • Row count;
  • Server Page Driven;

-> One of the major feature of this version is to allow cross domain access to your Data Services from your Silverlight application.

Here you can access to a description of this feature written in the ADO.NET Data Services Team Blog.

TIPS 2 : Returning hierarchical data

A great advantage of returning hierarchical in advance to reduce the number of server calls especially when you know that the hierarchical data is needed.

In the following example, the request will return a specific product:

Code Snippet
  1. public DataServiceQuery<Product> ReturnHierarchicalData(int productId)
  2. {   // Create a hierachical data query
  3.     DataServiceQuery<Product> request = (from product in _adventureWorksProxy.Products
  4.          .Expand("ProductSubcategory")
  5.          .Expand("ProductSubcategory/ProductCategory")
  6.     where product.ProductID == productId
  7.     select product) as DataServiceQuery<Product>;
  8.  
  9.     return request;
  10. }

This request will returns the specific ProductSubCategory entity of the product and the ProductCategory of this ProductSubcategory.

TIPS 3 : Deferred loading

In opposition to hierarchical data return in a single call, when the size of data to return is too heavy, you should use deferred loading.

In this example, I will return the ProductSubcategory of the product only when needed.

Code Snippet
  1. void ReturnDeferredData(Product product)
  2. {   
  3.     // Deferred load
  4.     _adventureWorksProxy.BeginLoadProperty(product, "ProductSubcategory", ReturnDeferredDataCallBack, product);
  5. }
  6. void ReturnDeferredDataCallBack(IAsyncResult ar)
  7. {
  8.     _adventureWorksProxy.EndLoadProperty(ar);
  9.     Product product = ar.AsyncState as Product;
  10. }

 

BeginLoadProperty requires four parameters, the entity used for deferred loading, the name of the property used in the deferred mechanism, the callback ant the context.

The callback method will end the asynchronous system.

TIPS 4 : Batching requests

One other great feature of Data Services is batching. it allows you to request in a same time several requests to the server and to return the result only in one time.

This example shows how to execute two requests on the server with only one server access. You can check using fiddler to confirm :-)

Code Snippet
  1. // Creation of two requests
  2. var request1 = new DataServiceRequest<Product>(new Uri("Products",UriKind.Relative));
  3. var request2 = new DataServiceRequest<ProductSubcategory>(new Uri("ProductSubcategory", UriKind.Relative));
  4.  
  5. // Execution of those two requests on the server
  6. DataServiceResponse responses = adventureWorksProxy.ExecuteBatch(request1, request2);
  7. foreach (QueryOperationResponse response in responses)
  8. {
  9.     if(response.Query.ElementType == typeof(Product))
  10.     {
  11.         // ...
  12.     }
  13.     else if (response.Query.ElementType == typeof(Product))
  14.     {
  15.         // ...
  16.     }
  17. }

Here is an interesting post from ADO.NET Data Services Team Blog.

Visual Studio 2010 Beta 2 : How to add an external tool (MSBuild script) to a Visual Studio button

I’m a real fan of MSBuild Scripts, I always use a lot of MSBuild Tools in my projects (for example scripts that automatically get latest version of the code, compile, check-in, …).

The easier way I found to execute those scripts is to run them automatically while I am clicking on a button into my Visual Studio.

image

STEP 1: Creation of an external tool

In Visual Studio 2010, Click on “Tools” menu, “External Tools …”.

A pop-up appears, click Add button and enter the desired information.

image

“Title” textbox is used as the name of the button that will be created.

If you use MSBuild on a X64 operating System, you have to select x86 MSBuild version:

C:\Windows\Microsoft.NET\Framework\v4.0.21006\MSBuild.exe

Click on “Use Output window” checkbox to redirect the result of your MSBuild script into Visual Studio Output window.

Step2 : Creation of your Toolbar and affectation of the external tool

On the menu bar, right click and select “Customize …”.

image

A pop-up appear, create your desired toolbar. Name it MyToolbar and click OK.

image 

Click on the “Commands” tab and click on ToolBar radio button to select the newly created external tool.

image

Select MyToolBar and click on “Add Command …” button.

image

On the left lit item, select “Tools” and than select the “External Command x” item where X is the index of your tool that appears in the “Tool” menu (starting index => 1). In my example “External Command 6”.

image

Click “Ok” and than “Close”.

And here is the result :-)

image

ADO.NET Data Services : How to consume my ADO.NET Data Services in WPF

Introduction

In this post I will talk about how you can consume your ADO.NET Data Services. My examples will be based on WPF.

For more information on Data Services, I suggest you to read my previous posts:

Consuming in WPF

Creates a WPF application named MyWPFApplication.

image

Add a reference to your Data Service, right click on MyWPFApplication project, and select Add service reference. A windows appears, define the address, rename the namespace with MyDataServiceProxy and click on OK button.

image

In MainWindow.xaml.cs, write the following code:

Code Snippet
  1. public partial class MainWindow : Window
  2. {
  3.     public MainWindow()
  4.     {
  5.         InitializeComponent();
  6.         this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
  7.     }
  8.  
  9.     void MainWindow_Loaded(object sender, RoutedEventArgs e)
  10.     {
  11.  
  12.         // Define the URI of the Data Service Query
  13.         Uri uri = new Uri(@"http://localhost:34570/AdventureWorksService.svc/");
  14.         // Create an instance of the Data Service context
  15.         MyDataServiceProxy.AdventureWorksModel.AdventureWorksEntities adventureWorksProxy = new MyDataServiceProxy.AdventureWorksModel.AdventureWorksEntities(uri);
  16.         // Create your query
  17.         var query = (from product in adventureWorksProxy.Products
  18.                     select product).Take(10);
  19.         // Set the qury result into the DataContext
  20.         this.DataContext = query;
  21.     }
  22. }

In the loaded event, instantiate your DataService. Create a simple request using Linq syntax and put this query into the DataContext.

In MainWindow.xaml, bind the data grid with the DataContext :

Code Snippet
  1. <Window x:Class="MyWpfApplication.MainWindow"
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4.         Title="MainWindow" Height="350" Width="525">
  5.     <Grid>
  6.         <DataGrid AutoGenerateColumns="true" Height="287" HorizontalAlignment="Left" Margin="12,12,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="479"
  7.                   ItemsSource="{Binding}">
  8.         </DataGrid>
  9.     </Grid>
  10. </Window>

Press F5 and here is the result:

image

This post shows how easy it is to consume Data Services with WPF. Just few code lines are needed ;-)

ADO.NET Data Services : Operations and Interceptors

Introduction

In this post I will talk about Operations & Interceptors functionalities included into ADO.NET Data Services.

For more information on Data Services, I suggest you to read my previous posts:

Operations

If you need to aggregate an operation, instead of creating the rule on the client side (on each sort of client application), you can centralize it easily on the server side using Operators.

In the following example, GetProductsByColor returns all products of a specific color.

 

Code Snippet
  1. [WebGet]
  2. public IQueryable<Product> GetProductsByColor(string color)
  3. {
  4.     return from product in this.CurrentDataSource.Products
  5.            where string.Compare(color, product.Color) == 0
  6.            select product;
  7. }

The WebGetAttibute is used to request your service via REST.

Inside the method, you just have to write your Linq query on the CurrentDataSource to get the desired result.

You also need to define the Service Operation Access Control. You have to grant read access to the operation. Here is the code to add in the InitializeService method:

Code Snippet
  1.  
  2. config.SetServiceOperationAccessRule("GetProductsByColor", ServiceOperationRights.AllRead);

InitializeService is the method where you already have granted access to the Data Service.

And finally, you can execute the code with this URL:

http://localhost:34570/AdventureWorksService.svc/GetProductsByColor?color='Red' 

 

Interceptors

Interceptors is the Data Services mechanism to customize logic on the server side. With this mechanism, you can intercept queries and add specific restriction rules.

Example 1 : Interceptor on a query request

In the following example, you will create :

- a filter to check if the person requesting the DataService service on the Products entity is member of the Admin role (defined for example with ASP.NET role provider), else an exception is thrown;

- only Products where SellEndDate property is  not null;

Open you DataService class. Don't forget to add at the top of you file:

Code Snippet
  1.  
  2. using System.Linq.Expressions;

Create the filter method on the product entity. You need to create a function prefixed with QueryInterceptor attribute. This attribute expects the name of the entity on which the interceptor will be applied (as defined in the Data Service Workspace http://localhost:34570/AdventureWorksService.svc/). The method has to follow a specific signature.

Code Snippet
  1. [QueryInterceptor("Products")]
  2. public Expression<Func<Product, bool>> FilterProducts()
  3. {
  4.     if (HttpContext.Current.User.IsInRole("Admin"))
  5.         return (p => p.SellEndDate !=null);
  6.     else
  7.         throw new UnauthorizedAccessException("You don't have access to this information");
  8. }

Example 2 : Interceptor on a change query (Add, Change, Delete, … operation)

In this example, I’m just checking that only Admins have granted privileges for adding,updating or deleting Product entity set. The signature of the method is a little bit different from previous example.

Code Snippet
  1. [ChangeInterceptor("Products")]
  2. public void onChangeProducts(Product product, UpdateOperations operations)
  3. {
  4.     if (!HttpContext.Current.User.IsInRole("Admin"))
  5.         throw new UnauthorizedAccessException("You don't have access to this information");
  6. }

For more information, I suggest you to read MSDN documentation.

ADO.NET Data Services : How to request your ADO.NET Data Services

Introduction

In my previous post, I explained how to create a ADO.NET Data Service with Visual Studio 2010. In this post, I will explain how to request this Data Service using Internet Explorer (but this will also work in any other browser). The database used in previous post is AdventureWorks.

Listing and filtering entities

By default when accessing the service, the list of all entities is returned.

http://localhost:34570/AdventureWorksService.svc

image

http://localhost:34570/AdventureWorksService.svc/Products 
This request lists all products existing in the database.

image

Let’s go further, you would like to return articles 6 to 7 order by ProductNumber:

http://localhost:34570/AdventureWorksService.svc/Products?$orderby=ProductNumber&$skip=5&$top=2

REST Syntax Description
$orderby=ProductNumber order Products by ProductNumber
&$skip=5 and skips the first 5 rows
&$top=2 and returns the two first rows

You could also order the results by ProductNumber descending:

http://localhost:34570/AdventureWorksService.svc/Products?$orderby=ProductNumber%20desc&$skip=5&$top=2

REST Syntax Description
$orderby=ProductNumber desc order Products by ProductNumber descending
&$skip=5 and skips the first 5 rows
&$top=2 and returns the two first rows

Here is the syntax to filter a product in particular:

http://localhost:34570/AdventureWorksService.svc/Products?$filter=ProductID%20eq%201

REST Syntax Description
$filter=ProductID eq 1 where ProductID is equal to 1

http://localhost:34570/AdventureWorksService.svc/Products?$filter=ProductID%20ge%20900%20and%20ProductID%20lt%20950

REST Syntax Description
$filter=ProductID ge 900 where ProductID is greater or equal to 900
and ProductID lt 950 and ProductID less than 950

Other expression syntaxes are available here.

Expanding SubEntities

One of the great feature of Data Services is the ability to also return referenced entites.

image

For example, if you want to request a specific Product,  and also return the information defined in its ProductSubCategory property, the request is as follows:

http://localhost:34570/AdventureWorksService.svc/Products?$filter=ProductID%20eq%20771&$expand=ProductSubcategory

REST Syntax Description
$filter=ProductID eq 771 where ProductID equals to 771
&$expand=ProductSubcategory and expands ProductSubcategory for this specific Product. ProductSubcategory is the name of the navigation property  for ProductSubcategory in the Product entity defined in your emdx file

And if you also need to retrieve the Catagory entity, which is two levels above :

http://localhost:34570/AdventureWorksService.svc/Products?$filter=ProductID%20eq%20771&$expand=ProductSubcategory,ProductSubcategory/ProductCategory

REST Syntax Description
$filter=ProductID eq 771 where ProductID equals to 771
&$expand=ProductSubcategory,
ProductSubcategory/ProductCategory
expands ProductSubcategory for this specific Product and ProductSubcategory/ProductCategory

New features of Visual Studio 2010 beta 2

When your Data Service project is running with debugger inside VS2010, all the requests executed are listed into Visual Studio. Really great feature :-)

image

ADO.NET Data Services : How to create an ADO.NET Data Services with Visual Studio 2010 beta2

Introduction:

This post is the first of a series of articles based on Visual Studio 2010. In this one I will introduce you to publishing your data through ADO.NET Data Services. Here are the main interest for me into using Data Services:

  • Exposing your data as a Data Service allows clients to consume it easily from using the technology you want (WPF, Silverlight, ASP.NET, J2EE, …);
  • Addressing Scheme for Data with URIs;
  • Data Transport Format ubiquitous (AtomPub or JSON);
  • Data Storage independence;
  • Usage of REST semantic;

Pre-requisites:

Visual Studio 2010 beta2 + SQL Server 2005 Express or 2008 Express http://msdn.microsoft.com/en-us/vstudio/dd582936.aspx

AdventureWorks database http://www.codeplex.com/MSFTDBProdSamples/Release/ProjectReleases.aspx?ReleaseId=18407

Step 1 : Create your web application project

After installing all software pre-requisites, let’s begin.

Open Visual Studio 2010 beta 2 and create an empty ASP.NET Web application named MyDataServicesApplication, for example. This web project will contains the edmx (i.e. the Entity Data Model) file and the ADO.NET Data Services class.

image

Step 2 : Create an Entity Model of the AdventureWorks database

First of all, a edmx file has to be created.

Create a new Item, select ADO.NET Entity Data Model item template, name it AdventureWorksModel

image

Press the Add button and follow the configuration wizard step by step

image

Select Generate from database to generate the edmx file from the existing database structure.

image

Select the AdventureWorks database

image

Specify the name of the connection string used, the specified name is stored in the web.config file into the connectionString tags.

image

Click the checkbox next to the Tables element, in order to include all existing tables into your data model.

Check the Pluralize or singularize generated object names check box and specify AdventureWorksModel as the namespace. This will pluralize all collection property names, this is a new feature of Visual Studio 2010.

Step 3 : Create the ADO.NET Data Service

Create a new item, Select the ADO.NET Data Service template and name it AdventuWorksService.svc

Open AdventureWorksService.svc.cs and update the content to be like this:

Code Snippet
  1.  
  2. namespace MyDataServicesApplication
  3. {
  4.     public class AdventureWorksService : DataService<AdventureWorksEntities>
  5.     {
  6.         // This method is called only once to initialize service-wide policies.
  7.         public static void InitializeService(DataServiceConfiguration config)
  8.         {
  9.             // Grant full access to all exsisting entities
  10.             config.SetEntitySetAccessRule("*", EntitySetRights.All);
  11.             // Grant full access for all operations
  12.             config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);
  13.             config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
  14.         }
  15.     }
  16. }

Step 4 : Test

Press F5 to check the result, it should list all the available entities :

image

You can now request your database through REST, for example select all the products using this query (which is now just an URI!): http://localhost:34570/AdventureWorksService.svc/Products

image

The result is represented by default using the ATOM Publishing Protocol. Internet Explorer automatically applies a style on ATOM format to make it more readable. Here this is not the case :-). I suggest that you right click on the content of the page and select View Source, the result will be more user friendly.

image

Here is a link of the rest Services sementics, a usefull link : http://msdn.microsoft.com/en-us/library/cc668767.aspx

 

Conclusion

Here is a step by step guide for creating an ADO.NET Data Services with Visual Studio 2010 beta2.

Customer Care Framework 2009 SP1 RTM is now available

CCF 2009 SP1 RTM has been released. This new version contains some new great features like :

  • 64 bit client support;
  • Dynamic positionning in IAD;
  • Citric enhancement ;
  • Usage for a same application of several DDAs;
  • New platforms support;
  • ...

You can found more details on those two new links:

 

Enterprise Library & Unity - Some interesting links

Hands-On Labs for Enterprise Library 4.1 and Hands-On Labs for Unity Application Block 1.2 have been released few days ago; here is a the good opportunity to summarize some interesting links.

Enterprise Library: 

Unity Application Block:

Pattern & practice:

Blogs:

·         Gregori Melnik: http://blogs.msdn.com/agile

·         J.D. Meier: http://blogs.msdn.com/jmeier

Update the language of documentation created by Sandcastle

Here are the pre-requisits to use Sandcastle documentation:

Sandcastle is the tool for creating a code documentation, you can use the given examples to generate your documentation, everything works fine but all the documentation is in english.

How could I update that to have everything in french for example?

The solution is based on updating the xml files of the content folder of the choosen style.

Here are examples of steps for having your documentation in french:

  1. Duplicate all files xml files from <SandcastleInstallationDirectory>\Presentation\<Style>\Content\
    1. Duplicate shared_content.xml as shared_content_FR.xml (main file to translate, mostly everything is here)
    2. Duplicate reference_content.xml as reference_content_FR.xml
    3. Duplicate syntax_content.xml as syntax_content_FR.xml
  2. Translate all the xml content of the new created files;
  3. Update Sandcastle.config to point to those new files. Update this code:

<!-- Old code -->

<component type="Microsoft.Ddue.Tools.SharedContentComponent" assembly="%DXROOT%\ProductionTools\BuildComponents.dll">

<content file="%DXROOT%\Presentation\Prototype\content\shared_content.xml" />

<content file="%DXROOT%\Presentation\Prototype\content\reference_content.xml" />

<content file="%DXROOT%\Presentation\Shared\content\syntax_content.xml" />

</component>

with this code:

<!-- Updated code --> 

<component type="Microsoft.Ddue.Tools.SharedContentComponent" assembly="%DXROOT%\ProductionTools\BuildComponents.dll">

<content file="%DXROOT%\Presentation\Prototype\content\shared_content_FR.xml" />

<content file="%DXROOT%\Presentation\Prototype\content\reference_content_FR.xml" />

<content file="%DXROOT%\Presentation\Shared\content\syntax_content_FR.xml" />

</component>

Now you just have to try.

Benoît

Welcome to my blog

Hi there,

Here is a little introduction about myself.

I'm Benoît Sarie, I'm a MCS Consultant for Microsoft France, I'm more dedicated on .net development and on Customer Care Framework (CCF) development.

If you don't already know what is CCF, I suggest you to read http://msdn.microsoft.com/en-us/isg/bb421305.aspx.

In this blog I'll try to give you tips and tricks on Visual Studio, WPF, P&P and on CCF.

See you soon on this blog :-)

 Benoît

Page view tracker