The Open Data Protocol (OData) enables you to define data feeds that also make binary large object (BLOB) data, such as photos, videos, and documents, available to client applications that consume OData feeds. This sample is intended to demonstrate how to use the IDataServiceStreamProvider interface in WCF Data Services to implement an OData service that uses a streaming provider to store and retrieve image files, along with information about each photo. For more information about the scenario and design decisions made in implementing this application, see the Data Services Streaming Provider Series: Implementing a Streaming Provider (Part 1).

This sample streaming data service supports the following blog posts in the data services streaming provider series:

   
  Part 1: Implementing a Streaming Provider
  Part 2: Accessing a Media Resource Stream from the Client

Prerequisites

Before running this sample, make sure that the following software is installed:

  • Visual Studio 2010, including SQL Server Express (if SQL Server is not already installed).
  • Internet Information Services (IIS) on the local computer.

Building the Sample

Use the following procedure to build the sample.

Note:
Because the ASP.NET Development Server does not support a chunked HTTP Transfer-Encoding, you must instead use IIS to host the ASP.NET Web application that implements this streaming data service. For more information, see the Streaming Provider topic.

 To configure IIS

  1. Unzip the sample files on the local computer.

  2. On the Start menu, right-click Visual Studio 2010 and select Run as administrator.

    Note:
    Visual Studio must run with administrator privileges to be able to make changes to virtual directories and ASP.NET.

     

  3. In Visual Studio, open the PhotoService.sln solution file in the root directory of the sample.

  4. In the Solution Explorer window, expand the solution, right-click the PhotoService project, and click Properties.

  5. In the PhotoService properties page, click the Web tab, and then under Servers click Create Virtual Directory.

    This creates the \PhotoService virtual directory in the default Web site with the project directory as the physical directory.

  6. Navigate to the \PhotoService physical directory and make sure that the user under which IIS runs (usually NETWORK SERVICE) has at least modify permissions on the directory.

  7. (Optional) From the command prompt with administrator privileges, execute one of the following commands (depending on the operating system):

    • 32-bit systems:
         
      "%windir%\Microsoft.NET\Framework\v3.0\Windows Communication Foundation\ServiceModelReg.exe" -i
    • 64-bit systems:
         
      "%windir%\Microsoft.NET\Framework64\v3.0\Windows Communication Foundation\ServiceModelReg.exe" -i

    This makes sure that Windows Communication Foundation (WCF) is registered on the computer.

  8. (Optional) From the command prompt with administrator privileges, execute one of the following commands (depending on the operating system):

    • 32-bit systems:
         
      "%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe" -i -enable
    • 64-bit systems:
         
      "%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_regiis.exe" -i -enable

    This makes sure that IIS runs correctly after WCF has been installed on the computer. You might have to also restart IIS.

  9. When the ASP.NET application runs on IIS7, you must also perform the following steps:

    1. Open IIS Manager and navigate to the PhotoService application under Default Web Site.
    2. In Features View, double-click Authentication.
    3. On the Authentication page, select Anonymous Authentication.
    4. In the Actions pane, click Edit to set the security principal under which anonymous users will connect to the site.
    5. In the Edit Anonymous Authentication Credentials dialog box, select Application pool identity.
    Note:
    When you use the Network Service account, you grant anonymous users all the internal network access associated with that account.

     

To create the PhotoStorage database

  1. In Visual Studio, open the PhotoData.edmx.sql file in the PhotoService project.

  2. From the Data menu, expand Transact-SQL Editor and Connection and click Connect.

    This displays the Connect to Database Engine dialog box.

  3. Type localhost\SQLExpress or select another local instance of SQL Server, and then click Connect.

  4. From the Data menu, expand Transact-SQL Editor and Connection and click Execute SQL.

    This creates the PhotoStorage database. It also creates a login for the Network Service and grants this login access to the new PhotoStorage database, which gives the IIS process access to the data.

To build the sample

  1. Make sure that the connection string stored in the web.config file in the \PhotoService subdirectory is valid for the database instance being used.

  2. Build the solution.

Running the Sample

Use the following procedures to run the sample.

To test the data service

  1. On the local computer, open the following URI in a Web browser:

    Note:
    You may need to disable feed viewing in your browser. For more information, see Accessing the Service from a Web Browser (ADO.NET Data Services Quickstart).

    Verify that the data service returns the PhotoData entity set definition.

  2. On the local computer, open the following URI in a Web browser:

       
    http://localhost/PhotoService/PhotoData.svc/$metadata

    Notice that the HasStream attribute is applied to the EntitySet element that defines the PhotoInfo entity.

  3. On the local computer, open the following URI in a Web browser:

       
    http://localhost/PhotoService/PhotoData.svc/PhotoInfo(1)

    This returns the PhotoInfo entity that has a key value of 1.

  4. On the local computer, open the following URI in a Web browser:

       
    http://localhost/PhotoService/PhotoData.svc/PhotoInfo(1)/$value

    This returns the image file as a data stream for the entity with the specified key. The image is displayed in the Web browser.

To run the client application

  1. Make sure that PhotoStreamingClient is the startup project, and then press F5.

    PhotoInfo data is requested from the data service and loaded into a DataServiceCollection<T>, which is bound to the combo box. The image file associated with the selected entity is obtained as a stream from the data service.

    Note:
    The data service is configured to limit uploaded images to 500 KB.

     

  2. Click Photo Details.

    This displays data from the PhotoInfo entity for the selected image.

  3. Select another photo to display a different image file.

    Note:
    Images are not cached. Each selection results in a request to the data service.

     

  4. Click Add Photo, in the displayed dialog select an image to upload to the data service, and then click Open.

    Note:
    The data service is configured to limit uploaded images to 500 KB.

     

  5. In the Select a new photo to upload... dialog box, enter information about the selected photo and then click Save and Close.

    This sends a POST request to the data service that contains the image file as a stream. A subsequent MERGE request sends the rest of the data from the newly created PhotoInfo object.

  6. Click Refresh Images to reload PhotoInfo data from the data service.