Windows XP
Windows Vista
Windows 7
Windows Azure
Windows Server
Windows Phone
EPL Standings
Windows 7

Visual Basic 2010 : Implementing WCF Data Services

6/24/2011 6:05:09 PM
To implement an ADO.NET Data Service, you first create a Web application, add your data source, and finally add a service to your project. The goal of the next example is to expose data within a master-detail relationship from the Northwind database via an entity data model. Run Visual Studio 2010 and create a new Web application, naming the new project NorthwindDataService. Figure 1 shows the New project window to explain the selection.
Figure 1. Creating a new Web application for hosting a Data Service.

When the new project is ready, add a new entity data model to the project pointing to the Northwind database, ensuring that Customers, Orders, and Order_Details tables are selected and correctly mapped into the new EDM.In Solution Explorer right-click the project name and select Add New Item. In the Add new item dialog, search for the ADO.NET Data Service template and name the new service as NorthwindService.svc, as shown in Figure 2.

Figure 2. Adding a Data Service to the project.

After a few seconds the WCF service is added to the project. If you double-click the NorthwindService.svc file, the code editor lists the auto-generated code expressed in Listing 1.

Listing 1. Starting Code for a Data Service
Imports System.Data.Services
Imports System.Data.Services.Common
Imports System.Linq
Imports System.ServiceModel.Web

Public Class NorthwindService
' TODO: replace [[class name]] with your data class name
Inherits DataService(Of [[class name]])

' This method is called only once to initialize service-wide policies.
Public Shared Sub InitializeService(ByVal config As DataServiceConfiguration)
' TODO: set rules to indicate which entity sets and service operations are
'visible, updatable, etc.
' Examples:

'config.SetEntitySetAccessRule("MyEntitySet", EntitySetRights.All)
config.DataServiceBehavior.MaxProtocolVersion = _

End Sub
End Class

This is the point where we need to make some considerations. First, WCF Data Services are implemented by both the System.Data.Services.dll and System.Data.Services.Client.dll assemblies. The most important namespaces exposed by such assemblies are System.Data.Services, System.Data.Services.Common, and System.Data.Services.Client. On the server side, they need to work with the System.ServiceModel namespace that provides support for WCF. The entry point of a Data Service is the System.Data.Services.DataService(Of T) class that is the base class for each service. If you take a look at the code, you see that the NorthwindService class inherits from DataService(Of T). Comments suggest replacing the standard [[class name]] identifier with the appropriate one, which is NorthwindEntities in our case. With that said, the inheritance declaration becomes the following:

Inherits DataService(Of NorthwindEntities)

Notice how the InitializeService method (invoked to start the service) receives a config argument of type DataServiceConfiguration; with this class you can configure the service behavior, for example access authorizations for your data source. The SetEntitySetAccessRule enables establishing access authorizations on entities from the EDM. For example, if you want clients to gain full access on the Customers entity, you write the following line:

config.SetEntitySetAccessRule("Customers", EntitySetRights.All)

You need to provide an access rule for each entity. As an alternative, you can use an * character for providing the same access level to all entities. This is not the best approach, but it can be useful for demonstration purposes. With that said uncomment the line of code for the previously mentioned method and replace the default line with the following one:

'Allows clients performing complete C.R.U.D. operations on all entities
config.SetEntitySetAccessRule("*", EntitySetRights.All)

The access rule is set via one of the EntitySetRights enumeration’s values, which are summarized in Table 1

Table 1. EntitySetRights Enumeration’s Values
AllProvides full access to entities.
AllReadProvides reading access to both multiple and single entities.
AllWriteProvides writing access to both multiple and single entities.
NoneNo authorization offered.
OverrideEntitySetRightsIf entities have explicit access rules, these are overridden with the ones specified here.
ReadMultipleProvides reading access to multiple entities.
ReadSingleProvides reading access to a single entity.
WriteAppendAllows adding new entities.
WriteDeleteAllows deleting entities.
WriteMergeAllows merging entities with existing data.
WriteReplaceAllows replacing entities.

Just remember that if you want to perform classic insert/update/delete operations, you need to provide All access level. Basically you just completed the most basic steps for getting a Data Service up and running. If you now press F5 to start the application, your web browser shows the result of the XML serialization of your data, according to the REST model. This result is shown in Figure 3.

Figure 3. The Data Service running shows serialized data in the Web browser.

Turn Off RSS Reading View

If you do not get the result shown in Figure 3 and instead see an RSS feeds reading view, you need to turn off such view in your browser. If you run Internet Explorer, you can select Tools, Internet Options, Content and then click the Settings button, finally you unflag the Turn On Feed Reading View check box. You will need to restart Internet Explorer for the change to take effect.

Notice how the service tag stores the service address. This is important because you use such an address later when instantiating the service. Also notice how the three entitysets (Customers, Orders, and Order_Details) are serialized. Now type the following Uri in the browser address bar, replacing the port number with the one you see on your machine:


This line fetches the full customers list, as shown in Figure 4.

Figure 4. Fetching the customers list via Uri.

You can simply scroll the page to see how each customer is serialized in the query result. If you look at Figure 4, you can easily understand how each customer property is represented. You can also perform some more complex queries. For example, you might want to retrieve master-details data such as all orders from a specific customer, as in the following Uri:


This Uri will retrieve the result shown in Figure 5.

Figure 5. Retrieving master-details data via Uri.

You can perform complex queries via Uri, and this is one of the allowed modes for querying data from client applications, so you need to understand how query strings are composed. For this, read the following document from the MSDN Library for a full list of supported operators: http://msdn.microsoft.com/en-us/library/cc668784(VS.100).aspx. Generally you cannot query your service this way, whereas you will instead do it from a client application. This is what the next section begins to show.

Deploying WCF Data Services to Internet Information Services

In real-world applications you will probably host your Data Services on web servers such as Internet Information Services. Because they are WCF services, you will deploy them with related techniques described in this page on the MSDN Library: http://msdn.microsoft.com/en-us/library/ms730158(VS.100).aspx.

Other -----------------
- Microsoft Visio 2010 : Adding Sophistication to Your Drawings - Orienting Shape Text
- Microsoft Visio 2010 : Adding Sophistication to Your Drawings - Orienting Shapes on the Page
- Microsoft Visio 2010 : Adding Text to Shapes & Creating and Formatting Text Boxes
- Monitoring and Maintaining Windows 7 : Using System Configuration
- Using Windows 7 Tools to Discover System Information
- Optimizing Windows 7 with Performance Monitor (part 3)
- Optimizing Windows 7 with Performance Monitor (part 2) - Utilizing Customized Counters in Performance Monitor & Managing Performance Monitor Properties
- Optimizing Windows 7 with Performance Monitor (part 1) - Using Resource Monitor
- Visual Basic 2010 : Reflection - Generating Code at Runtime with Reflection.Emit
- Visual Basic 2010 : Reflection - Invoking Code Dynamically
- Visual Basic 2010 : Reflection - Reflecting Types
- Administering Internet Explorer : Troubleshooting Internet Explorer Issues
- Administering Internet Explorer : Understanding Advanced Settings (part 2) - Branding Internet Explorer & Group Policy Settings
- Administering Internet Explorer : Understanding Advanced Settings (part 1) - Certificate Settings
- Administering Internet Explorer : Managing Windows Internet Explorer Settings (part 2)
- Administering Internet Explorer : Managing Windows Internet Explorer Settings (part 1) - Managing Cache
- Visual Basic 2010 : Reflection - Understanding Assemblies Metadata & Getting Assembly Information
- Visual Basic 2010 : Hosting WCF Services in Internet Information Services & Configuring Services with the Configuration Editor
- Supporting Mobile Windows 7 Users : Understanding DirectAccess & Using BranchCache
- Microsoft Excel 2010 - Creating and Modifying Charts - Changing Chart Titles
Most view of day
- Adobe Dreamweaver CS5 : Using Library Items and Server-side Includes (part 4) - Editing a Library Item
- Windows Server 2012 : Configuring IPv6/IPv4 interoperability (part 2) - Default IPv6 functionality
- Maintaining Desktop Health : Monitoring Reliability and Performance (part 6) - Using Reliability Monitor
- Windows Phone 8 : Configuring Basic Device Settings - Controlling the Keyboard’s Behavior (part 3) - Customizing the Keyboard’s Behavior
- System Center Configuration Manager 2007 : Network Design - Troubleshooting Configuration Manager Network Issues (part 2) - Identifying Network Issues Affecting Configuration Manager
- Printing from a Program, Printing a Document - Print a Document Using the Default Printer, Print a Document Using a Specific Printer
- Troubleshooting Hardware, Driver, and Disk Issues : How to Diagnose Hardware Problems
- Exploring the Vista Task Scheduler
- Windows Server 2008 : Designing the Active Directory Administrative Model (part 3) - Planning to Audit AD DS and Group Policy Compliance, Planning Organizational Structure
- Sharepoint 2013 : Backup and Restore (part 3) - Unattached Content Database Data Recovery
Top 10
- Microsoft Exchange Server 2007 : Implementing Client Access and Hub Transport Servers - Installing the Hub Transport Server
- Microsoft Exchange Server 2007 : Implementing Client Access and Hub Transport Servers - Transport Pipeline
- Microsoft Exchange Server 2007 : Hub Transport Server Policy Compliance Features (part 4) - Message Classification , Rights Management and the Hub Transport Server
- Microsoft Exchange Server 2007 : Hub Transport Server Policy Compliance Features (part 3) - Journaling
- Microsoft Exchange Server 2007 : Hub Transport Server Policy Compliance Features (part 2) - Disclaimers
- Microsoft Exchange Server 2007 : Hub Transport Server Policy Compliance Features (part 1) - Transport Rules
- Microsoft Exchange Server 2007 : Implementing Client Access and Hub Transport Servers - Understanding the Hub Transport Server
- Conducting Research in OneNote 2010 : Translating Text
- Conducting Research in OneNote 2010 : Researching a Topic, Customizing the Research Task Pane
- Conducting Research in OneNote 2010 : Handling the Research Task Pane
Windows XP
Windows Vista
Windows 7
Windows Azure
Windows Server
Windows Phone
2015 Camaro