How to create/consume WCF oData Service (RESTful S

2019-09-20 03:01发布

I want to create WCF oData Service (RESTful Service) using U2 Toolkit for .NET and U2 Database. Then I want to consume oData Service in any .NET Client Application.

1条回答
别忘想泡老子
2楼-- · 2019-09-20 03:46

Please see my answer below:

Overview

WCF Data Services exposes entity data as a data service. This entity data can be created from U2 Database using U2 Toolkit for .NET. This topic shows you how to create an Entity Framework-based data model in a Visual Studio Web application that is based on an existing database and use this data model to create a new WCF oData service (RESTful Service). You can consume WCF oData Service in different .NET application such as:

  • WPF application
  • Windows 8 Metro Style application
  • Office EXCEL

Installation

You need to install U2 toolkit for .NET v 1.2.0. It contains U2 ADO.NET Provider and U2 Database Add-ins for Visual Studio

enter image description here

Create Entity Data Model with existing U2 Account

We will use U2 UniVerse ‘s sample database called “HS.SALES”. 1. Create ASP.NET Web Application called ‘U2_WCF_oData_WebApplication’

enter image description here

  1. On the Project menu, click Add new item.
  2. In the Templates pane, click the Data category, and then select ADO.NET Entity Data Model.
  3. Type the model name and then click Add. enter image description here

  4. In the Choose Model Contents dialog box, select Generate from database. Then click Next.

  5. Click the New Connection button. enter image description here
  6. In the Connection Properties dialog box, type connection string parameters , and then click OK.
  7. Ensure that the Save entity connection settings in App.Config as: checkbox is checked. Then click Next.
  8. Change ‘Entities’ to ‘CustomerEntities’ enter image description here
  9. In the Choose Your Database Objects dialog box, select CUSTOMERand CUSTOMER_ORDERS that you plan to expose in the data service. Modify ‘HS.SALESModel’ for ‘CustomerModel’. enter image description here
  10. Click Finish to complete the wizard. enter image description here

Create WCF oData Service (RESTful Service) using the new data model (Customer Model)

  1. In Visual Studio, open the Customer.edmx file that represents the data model.
  2. In the Model Browser, right-click the model, click Properties, and then note the name of the entity container. enter image description here
  3. In Solution Explorer, right-click the name of your ASP.NET project, and then click Add New Item.
  4. In the Add New Item dialog box, select WCF Data Service.
  5. Supply a name for the service, and then click OK. enter image description here
  6. 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 inherits from the ObjectContext class and that is the entity container of the data model, which was noted in step 2.

public class U2_Customer_WcfDataService : DataService< /* TODO: put your data source class name here */ >

public class U2_Customer_WcfDataService : DataService< CustomerEntities >
  1. In the code for the data service, enable authorized clients to access the entity sets that the data service exposes. For more information, see Creating the Data Service.

    // config.SetEntitySetAccessRule("MyEntityset", EntitySetRights.AllRead);

     config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
    
  2. To test the ‘U2_Customer_WcfDataService.svc ‘ data service by using a Web browser, press Visual Studio ->Debug->StartWithoutDebugging

enter image description here

enter image description here

enter image description here

Consume WCF oData Service (RESTful Service)

  1. Create WPF Project in the same solution. Rename the project ‘U2_Consumer_WpfApplication’

enter image description here 2. Add Service Reference

enter image description here

  1. Press Discover Button, Rename ‘U2_WCF_oData_ServiceReference’. Press OK.

enter image description here

  1. Go to Data->Show Data sources.

enter image description here

  1. Drag and Drop ‘CUSTOMERs’ into WPF Designer.

enter image description here

  1. Open ‘MainWindow.xaml.cs’ file. Add this line ( yours uri will be different).

    private Uri svcUri = new Uri("http://localhost:38346/U2_Customer_WcfDataService.svc/");

  2. Add this line.

    U2_WCF_oData_ServiceReference.CustomerEntities ctx = new U2_WCF_oData_ServiceReference.CustomerEntities(svcUri);

  3. Add this line.

    cUSTOMERsViewSource.Source = ctx.CUSTOMERs.ToList();

  4. Your competed code will look as below. public partial class MainWindow : Window { private Uri svcUri = new Uri("http://localhost:38346/U2_Customer_WcfDataService.svc/");

    public MainWindow()
    {
        InitializeComponent();
    }
    
    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        U2_WCF_oData_ServiceReference.CustomerEntities ctx = new U2_WCF_oData_ServiceReference.CustomerEntities(svcUri);
        System.Windows.Data.CollectionViewSource cUSTOMERsViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("cUSTOMERsViewSource")));
        // Load data by setting the CollectionViewSource.Source property:
        // cUSTOMERsViewSource.Source = [generic data source]
        cUSTOMERsViewSource.Source = ctx.CUSTOMERs.ToList();
    }
    

    }

  5. Set WPF application as ‘Startup Project’. Run WPF Application.

enter image description here

查看更多
登录 后发表回答