Creating the Northwind Data Service (ADO.NET Data Services/Silverlight)
This page is specific to:Microsoft Version:Silverlight 3
Silverlight
Creating the Northwind Data Service (ADO.NET Data Services/Silverlight)

Applications for Silverlight can display and update data that is provided by ADO.NET Data Services. The System.Data.Services.Client library manages interactions between applications for Silverlight and ADO.NET Data Services, so applications do not have to create a database connection or use relational query syntax to access the data.

In this task, you will create a new Web application to host the Northwind data service, define the Northwind data model by using the Entity Data Model tools, create the data service, and then test the data service by using a Web browser.

NoteNote:

The Northwind data service is based on the Northwind sample database. However, data sources other than relational databases are supported by ADO.NET Data Services. For more information, see ADO.NET Data Services.

To create the ASP.NET Web application

  1. In Visual Studio, on the File menu, select New, and then select Project.

  2. In the New Project dialog box, select either Visual Basic or Visual C# as the programming language.

  3. In the Templates pane, select ASP.NET Web Application.

NoteNote:

If you use Visual Studio Web Developer, you must create a new Web site instead of a new Web application.

  1. Type NorthwindDataService as the name of the project.

  2. Click OK.

To define the data model

  1. In Solution Explorer, right-click the name of the ASP.NET project that you just created, and then click Add New Item.

  2. In the Add New Item dialog box, select ADO.NET Entity Data Model.

  3. For the name of the data model, type Northwind.edmx, and then click Add.

  4. In the Entity Data Model Wizard, select Generate from Database, and then click Next.

  5. Connect the data model to the database by doing one of the following steps, and then click Next:

    • If you do not have a database connection already configured, click New Connection and create a new connection. For more information, see How to: Create Connections to SQL Server Databases.

      - or -

    • If you have a database connection already configured to connect to the Northwind database, select that connection from the list of connections.

  6. On the final page of the wizard, select the check boxes for all tables in the database, and clear the check boxes for views and stored procedures.

  7. Click Finish to exit the wizard.

To create the data service

  1. In Solution Explorer, right-click the name of your ASP.NET project, and then click Add New Item.

  2. In the Add New Item dialog box, select ADO.NET Data Service.

  3. For the name of the service, type Northwind.svc.

    Visual Studio creates the XML markup and code files for the new service. By default, the code-editor window opens. In Solution Explorer, the service will have the name Northwind with the extension .svc.cs or .svc.vb.

  4. In the code for the data service, replace the comment /* TODO: put your data source class name here */ in the definition of the class that defines the data service with the type that is the entity container of the data model, which in this case is NorthwindEntities. The class definition should resemble the following:

    Public Class Northwind
        Inherits DataService(Of NorthwindEntities)
    
    
    

To enable access to the data service

  • In the code for the data service, replace the placeholder code in the InitializeService function with the following:

    config.SetEntitySetAccessRule("Customers", EntitySetRights.AllRead)
    config.SetEntitySetAccessRule("Orders", EntitySetRights.All)
    config.SetEntitySetAccessRule("Order_Details", EntitySetRights.All)
    config.SetEntitySetAccessRule("Products", EntitySetRights.All)
    
    
    

    This enables authorized clients to access resources for the three specified entity sets.

    NoteNote:

    Any client that can access the ASP.NET application can also access the resources that the data service exposes. In a production data service, to prevent unauthorized access to resources, you should also secure the application itself. For more information, see Securing ASP.NET Web Sites.

To test the data service in a Web browser

  1. In Internet Explorer, on the Tools menu, select Internet Options, click the Content tab, click Settings in the Feed section, and clear Turn on feed viewing.

    This ensures that feed reading is disabled. If you do not disable this functionality, then the Web browser will treat the returned AtomPub encoded document as an XML feed instead of displaying the raw XML data.

  2. In Visual Studio, press the F5 key to start the application.

  3. In the notification area of the taskbar, double-click the ASP.NET Development Server icon.

    This opens the ASP.NET Development Server window.

  4. Note the value of the Port: field.

  5. Open a Web browser on the local computer, and then in the address field enter the following URL, where <PortNumber> is the number noted in step 3:

    http://localhost:<PortNumber>/northwind.svc
    

    This returns the default service document, which lists entity sets that are exposed by this data service. Only the entity sets that were explicitly enabled for read access are returned.

  6. In the address field of the Web browser, enter the following URI:

    http://localhost:<PortNumber>/northwind.svc/Customers('ALFKI')/Orders
    

    This traverses the relationship between customers and orders to return a set of all orders for the specific customer ALFKI.

Next Steps

You have successfully created a new data service that is based on the Northwind sample database, you have enabled access to the service for clients that have permissions on the ASP.NET Web application, and you have tested the service in a Web browser. Next, you will create a project for the client application for Silverlight and to it add a reference to this data service:

Creating the Client Application

See Also

Other Resources

Community Content

Set Visual Studio Development Server to a Specific Port
Added by:Ken Cox - MVP

By default, the built-in VS development Web server assigns a random port. This causes problems when developing Data Services because the Silverlight application has no way of knowing that the port used by the Data Services has changed and can no longer connect.

After creating the initial project - and before adding Data Models and Data Services - assign a specific port (e.g., 5000) to the development Web server.

  1. In Solution Explorer, right click on the Web application name, and from the context menu, click Properties.
  2. In the Properties pane, click the Web item.
  3. In the Servers area of the page (around the middle), click Specific Port.
  4. Set the port to 5000 or another valid value.
  5. Leave the Virtual Path value as a slash (/).
  6. Exit and save the changes.

© 2009 Microsoft Corporation. All rights reserved.   Terms of Use | Trademarks | Privacy Statement
Page view tracker
Rate the Lightweight library
x
Lightweight builds on ScriptFree (loband) by adding features you've requested: a SearchBox and default code language selection.
Do you like the SearchBox?
Do you like the tabbed code blocks?
How useful is this topic?
Tell us more.
Thanks
x
You're helping to improve MSDN Online.
Feedback
Switch View
Classic
Lightweight Beta
ScriptFree
Switch View