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

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
主站蜘蛛池模板: 南昌县| 沂水县| 平泉县| 香格里拉县| 桦甸市| 中牟县| 元氏县| 宜宾市| 舟山市| 襄汾县| 昭觉县| 崇文区| 广汉市| 靖远县| 保靖县| 平和县| 托克逊县| 方城县| 嘉义市| 巢湖市| 盐源县| 卓尼县| 澜沧| 肇源县| 麻城市| 拜城县| 溧水县| 鹤壁市| 日土县| 杭锦旗| 广丰县| 金堂县| 密云县| 前郭尔| 奎屯市| 从江县| 叙永县| 凌云县| 大渡口区| 合阳县| 兴义市|