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’
- On the Project menu, click Add new item.
- In the Templates pane, click the Data category, and then select ADO.NET Entity Data Model.
Type the model name and then click Add.
In the Choose Model Contents dialog box, select Generate from database. Then click Next.
- Click the New Connection button.
- In the Connection Properties dialog box, type connection string parameters , and then click OK.
- Ensure that the Save entity connection settings in App.Config as: checkbox is checked. Then click Next.
- Change ‘Entities’ to ‘CustomerEntities’
- 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’.
- Click Finish to complete the wizard.
Create WCF oData Service (RESTful Service) using the new data model (Customer Model)
- In Visual Studio, open the Customer.edmx file that represents the data model.
- In the Model Browser, right-click the model, click Properties, and then note the name of the entity container.
- In Solution Explorer, right-click the name of your ASP.NET project, and then click Add New Item.
- In the Add New Item dialog box, select WCF Data Service.
- Supply a name for the service, and then click OK.
- 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 >
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);
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)
- Create WPF Project in the same solution. Rename the project ‘U2_Consumer_WpfApplication’
2. Add Service Reference
- Press Discover Button, Rename ‘U2_WCF_oData_ServiceReference’. Press OK.
- Go to Data->Show Data sources.
- Drag and Drop ‘CUSTOMERs’ into WPF Designer.
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/");
Add this line.
U2_WCF_oData_ServiceReference.CustomerEntities ctx = new U2_WCF_oData_ServiceReference.CustomerEntities(svcUri);
Add this line.
cUSTOMERsViewSource.Source = ctx.CUSTOMERs.ToList();
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(); }
}
Set WPF application as ‘Startup Project’. Run WPF Application.