Поделиться через


Hosting the Data Service (ADO.NET Data Services)

ADO.NET Data Services are not standalone network services. An ADO.NET Data Service does not bind to and listen on a network socket for incoming requests to its representational state transfer (REST) entry points. Instead, an ADO.NET Data Service is a component hosted in an environment such as the Windows Communication Foundation (WCF) that provides core server networking facilities. The host handles direct interactions with the network and support caching, scalability, and authentication modules.

ADO.NET Data Services defines a generic hosting interface IDataServiceHost that abstracts its implementation from a specific host. This allows ADO.NET Data Services to run in a range of host environments, from custom HTTP server-side implementations such as WCF, ASP.NET, and IIS.

Note   When using an ASP.NET host the characters (:) and (/) are blocked and will cause a 400 Bad Request response code if used. Installing Fix HttpException (0x80004005) solves the issue for the colon (:).

The examples in the following sections assume that an ADO.NET Data Service is defined as follows:

namespace TestDataService 
{
    public class SampleDataService :
              DataService<NorthwindModel.NorthwindEntities> 
    { // service implementation } 
}

Hosting an ADO.NET Data Service using Windows Communication Foundation (WCF) and ASP.NET

ADO.NET Data Services can be hosted as a WCF endpoint inside an ASP.NET application. In this implementation, WCF and ASP.NET handle the network interaction for an ADO.NET Data Service.

To host an ADO.NET Data Service in WCF within an ASP.NET application, define a new WCF endpoint as follows:

  1. Create a new WCF service endpoint; this is the .svc file.

  2. In the ServiceHost declaration:

    • The value of the Factory attribute must be System.Data.Services.DataServiceHostFactory.

    • The value of the Service attribute must be the fully-qualified namespace class name of the data service to be exposed.

TestDataService.svc:
<%@ ServiceHost Language="C#"
 Factory="System.Data.Services.DataServiceHostFactory"
 Service="TestDataService.SampleDataService"%> 

Hosting an ADO.NET Data Service Using Windows Communication Foundation (WCF)

ADO.NET Data Services can be hosted using the WCF ServiceHost or WebServiceHost classes. The example below shows a data service using the WebServiceHost class.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel.Web;
using System.Data.Services;

namespace SelfHost
{
    class Program
    {
        static void Main(string[] args)
        {
            Uri baseAddress =
                  new Uri("https://localhost:8000/dataservice");
            HostWithWebServiceHost(baseAddress);

            Console.WriteLine(
                 "Host running - https://localhost:8000/dataservice");
            Console.WriteLine("Press any key to shut down service...");

            Console.ReadKey();
            host.Close();
        }

        private static void HostWithWebServiceHost(Uri baseAddress)
        {

            WebServiceHost host = new WebServiceHost(typeof(SampleDataService), 
                                      baseAddress);
            WebHttpBinding binding = new WebHttpBinding();
            host.AddServiceEndpoint(
               typeof(System.Data.Services.IRequestHandler), binding, 
               "WebServiceHost");
            host.Open();
        }
    }
}

See Also

Other Resources

Data Model (ADO.NET Data Services Framework)

Quickstart (ADO.NET Data Services)