How to create/consume WCF oData Service (RESTful Service) using U2 toolkit for .NET? [closed]

徘徊边缘 提交于 2019-12-13 09:02:52

问题


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:


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

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’

  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.

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

  5. Click the New Connection button.

  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’

  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’.

  10. Click Finish to complete the wizard.

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.

  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.

  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

Consume WCF oData Service (RESTful Service)

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

2. Add Service Reference

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

  1. Go to Data->Show Data sources.

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

  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.



来源:https://stackoverflow.com/questions/14312892/how-to-create-consume-wcf-odata-service-restful-service-using-u2-toolkit-for

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!