Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
Silverlight
WCF Data Services
 How to: Create the Northwind Data S...
Collapse All/Expand All Collapse All
Silverlight
How to: Create the Northwind Data Service (WCF Data Services/Silverlight)

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:

    Visual Basic
    Public Class Northwind
        Inherits DataService(Of NorthwindEntities)
    
    C#
    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:

    Visual Basic
    ' 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)
    
    C#
    // 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:

    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.

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Set Visual Studio Development Server to a Specific Port      Cox, Ken ... markspansel   |   Edit   |   Show History

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.

 

 

Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2012 Microsoft. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker