The purpose of this tutorial is to show you how to make a call to an ADO.NET Data Service in the context of a WPF application. The following common tasks will be examined:
- Adding a reference.
- Creating a new instance of the exposed entity.
- Making call to the service and consuming the service result.
The process of developing an ADO.NET Data Service is beyond the scope of this tutorial. For more information read here.
This tutorial will use the following product and technology:
- Northwind database, which can be downloaded from here.
- ADO.NET Entity Framework - used to create an Entity Data Model representation of the Northwind database.
|The Telerik OpenAccess ORM also supports excellent integration with ADO.NET Data Services.|
The first step is to add a new service reference to your application. In order to do that you need to perform the following steps:
- Select the "References" folder, right click with your mouse button and choose "Add Service Reference..."
- A popup window appears, hit the Discover button to find the web service or enter the service location. Press OK to add it.
You have the ability to choose the type of the collections that the service returns. In order to do that you need to open the Service Reference Settings dialog from the Advanced button.
When a service reference is added to a project, any types defined in the service are generated in the local project. In many cases, this creates duplicate types when a service uses common .NET Framework types or when types are defined in a shared library. To avoid this problem, types in referenced assemblies are shared by default. If you want to disable type sharing for one or more assemblies, you can do so in the Service Reference Settings dialog.
Once the ADO.NET data service is added all needed assemblies will be included in your project automatically.
Creating New Instance of the Exposed Entity
NorthwindEntities northwindEntity = new NorthwindEntities( new Uri( "http://localhost:54248/Services/SampleAdoNetDataService.svc/" ) );
Dim northwindEntity As New NorthwindEntities(New Uri("http://localhost:54248/Services/SampleAdoNetDataService.svc/"))
NorthwindEntities represents the runtime context of a given data service. While data services themselves are stateless, the context is not, so state on the client is maintained between interactions in order to support features such as identity resolution and optimistic concurrency.
Making Call to the Service and Consuming the Result
Making a call to an ADO.NET Data Service is a simple process. Here is a sample code, demonstrating how to load all categories from the Categories table in the Northwind database:
foreach( Categories c in northwindEntity.Categories.Execute() )
this.Categories.Add( c );
For Each c As Categories In northwindEntity.Categories.Execute()