官术网_书友最值得收藏!

Exposing OData endpoints from WCF RIA Service

WCF RIA Service is one of the great extension components based on the standard WCF service. WCF RIA Service is designed for building data access services (for n-tier solutions), which will not only expose data sets to clients but also encapsulate most of the business/application logics at service layer. With the latest WCF RIA Service version, we can make a WCF RIA Service expose data through various kinds of endpoints such as SOAP, OData, and JSON.

In this recipe, we will show you how to open an OData endpoint from an existing WCF RIA Service.

Getting ready

To play with WCF RIA Service, we need to install Visual Studio 2010 Service Pack 1, which includes the runtime and development tools for WCF RIA Service V1 SP1.

Visual Studio 2010 Service Pack 1 is available at http://support.microsoft.com/kb/983509.

The source code for this recipe can be found in the \ch01\ODataRIAServiceSln\ directory.

How to do it...

  1. Create a new ASP.NET Empty Web Application.
  2. Create the ADO.NET Entity Framework data model from the sample database.

    The following screenshot shows the class diagram of the data model created from the Northwind sample database (four tables are included):

    How to do it...
  3. Create a new WCF RIA Service by using the Domain Service Class item template in Visual Studio (see the following screenshot).
    How to do it...
  4. Specify the service options (especially the one for enabling an OData endpoint) in the Add New Domain Service Class dialog (see the following screenshot).
    How to do it...

    The following are all the options we need to set for a new WCF RIA Service:

    • Domain Service Class name: This is the type name of our RIA service class.
    • Available DataContext/ObjectContext classes: This is the data model class we will use for providing the underlying data objects. Make sure we have saved all items in the project so that the ADO.NET Entity Framework data model class will appear in the drop-down list.
    • Enable client access and Expose OData endpoint options: As the name explains, these two options will enable the RIA service to be accessed from client applications and also add an additional endpoint on it so as to expose data entities in an OData compatible format.
  5. Create a .svc file as the service access endpoint for the WCF RIA Service.

    In the .svc file, we need to specify the ServiceHostFactory and Service types through the @ServiceHost directive (see the following code snippet).

    <%@ ServiceHost Language="C#" Debug="true" Service="ODataRIAService.NWDomainService" Factory="System.ServiceModel.DomainServices.Hosting.DomainServiceHostFactory, System.ServiceModel.DomainServices.Hosting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
    

    As shown in the previous @ServiceHost directive, we need to supply the full name (including namespace and assembly name) of the ServiceHostFactory type in the Factory attribute.

    Tip

    If you use the WCF service item template to create a new .svc file, Visual Studio will generate the ServiceContract and Service implementation code files automatically. To prevent this, you can create a Text or XML file instead and manually change the file extension to .svc (and adjust the file content correspondingly).

  6. Launch the WCF RIA Service and access its OData endpoint by adding the odata/ suffix to the URL.

    By adding the odata/ suffix to the URL over the base service address, we can reach the OData endpoint exposed by the WCF RIA Service. The default output of the OData endpoint is just the same as a standard WCF Data Service (see the following screenshot).

How to do it...

How it works...

When creating the sample WCF RIA Service, we enable the OData endpoint on it by selecting the Expose OData endpoint option in the Add New Domain Service Class dialog. Actually, we can find the magic behind the dialog within the web.config file (see the following configuration fragment).

How it works...

The dialog adds a domainServices/endpoints/add element in the<system.serviceModel> section. This element tells the runtime to add a new endpoint for each WCF RIA Service and this endpoint will generate an OData format response (by using the System.ServiceModel.DomainServices.Hosting.ODataEndpointFactory type).

Likewise, if you have some existing WCF RIA Services, which were created without the OData endpoints enabled, we can simply make them OData enabled by adding the previous configuration settings manually in the web.config file.

See also

  • Building an OData service via WCF Data Service and ADO.NET Entity Framework recipe
主站蜘蛛池模板: 淮安市| 监利县| 惠来县| 商水县| 青浦区| 淳化县| 鹰潭市| 睢宁县| 高雄市| 荆州市| 隆化县| 中宁县| 沙洋县| 肥东县| 旬阳县| 肥东县| 浦县| 井研县| 织金县| 于田县| 海晏县| 林州市| 宁远县| 洱源县| 馆陶县| 邵武市| 大渡口区| 大安市| 邢台市| 柏乡县| 牙克石市| 漳州市| 涟水县| 祁阳县| 洞头县| 固安县| 湛江市| 丹江口市| 苍南县| 高雄县| 历史|