Partager via


How to: Create the Northwind Data Service (WCF Data Services/Silverlight)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Applications for Silverlight can display and update data that is provided by WCF Data Services. The System.Data.Services.Client library manages interactions between applications for Silverlight and WCF Data Services, so applications do not have to create a database connection or use relational query syntax to access the data. The Northwind data service is based on the Northwind sample database, which is available from this Microsoft Web site. However, data sources other than relational databases are supported by WCF Data Services. For more information, see WCF Data Services.  

This topic shows how to 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.

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, clear the check boxes for views and stored procedures, and clear the Pluralize or singularize generated object names check box.

  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 WCF Data Services.

  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)
    
    public class Northwind : DataService<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:

    ' Grant only the rights needed to support the client application.
    config.SetEntitySetAccessRule("Orders", EntitySetRights.AllRead _
         Or EntitySetRights.WriteMerge _
         Or EntitySetRights.WriteReplace)
    config.SetEntitySetAccessRule("Order_Details", EntitySetRights.AllRead _
        Or EntitySetRights.AllWrite)
    config.SetEntitySetAccessRule("Customers", EntitySetRights.AllRead)
    config.SetEntitySetAccessRule("Products", EntitySetRights.AllRead _
            Or EntitySetRights.WriteReplace)
    
    // Grant only the rights needed to support the client applications.
    config.SetEntitySetAccessRule("Orders", EntitySetRights.AllRead
         | EntitySetRights.WriteMerge
         | EntitySetRights.WriteReplace);
    config.SetEntitySetAccessRule("Order_Details", EntitySetRights.AllRead
        | EntitySetRights.AllWrite);
    config.SetEntitySetAccessRule("Customers", EntitySetRights.AllRead);
    config.SetEntitySetAccessRule("Products", EntitySetRights.AllRead 
        | EntitySetRights.WriteReplace);
    

    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 Sites04b37532-18d9-40b4-8e5f-ee09a70b311d.

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:

    https://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:

    https://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.