Logo
programming4us
programming4us
programming4us
programming4us
Home
programming4us
XP
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server
programming4us
Windows Phone
 
programming4us
Windows 7

Visual Basic 2010 : Implementing WCF Data Services

- Windows 10 Product Activation Keys Free 2019
- How to active Windows 8 without product key
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
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.SetServiceOperationAccessRule("MyServiceOperation",
ServiceOperationRights.AllRead)
config.DataServiceBehavior.MaxProtocolVersion = _

DataServiceProtocolVersion.V2
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
ValueDescription
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:

http://localhost:1443/NorthwindService.svc/Customers

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:

http://localhost:1443/NorthwindService.svc/Customers('ANATR')/Orders?orderby=OrderDate

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
 
 
Top 10
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
Popular tags
Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Biztalk Exchange Server Microsoft LynC Server Microsoft Dynamic Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 windows Phone 7 windows Phone 8
programming4us programming4us
Celebrity Style, Fashion Trends, Beauty and Makeup Tips.
 
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server