• June 29, 2022

HOW TO: Create WCF OData Service with U2 Toolkit for .NET

This quick start helps Rocket U2 Customers become familiar with WCF Data Services and the Open Data Protocol (OData) .

OData is a standardized protocol for creating and consuming data APIs. OData builds on core protocols like HTTP (GET, PUT, POST, and DELETE ) and commonly accepted methodologies like REST.  WCF Data Services  is a component of the .NET Framework that enables you to create services that use the Open Data Protocol (OData) to expose and consume data over the Web or intranet by using the semantics of representational state transfer (REST).  OData uses the entity-relationship conventions of the Entity Data Model to expose resources as sets of entities that are related by associations.

Overview

U2 Toolkit for .NET allows you to create WCF oData Service using Code First functionality. In addition to supporting a designer-based development workflow, Entity Framework also enables a more code-centric option which we call “code first development”. It enables you to:

  • Develop without ever having to open a designer or define an XML mapping file
  • Define your model objects by simply writing “plain old classes” with no base classes required
  • Use a “convention over configuration” approach that enables database persistence without explicitly configuring anything
  • Optionally override the convention-based persistence and use a fluent code API to fully customize the persistence mapping

Using U2 Toolkit for .NET, you can define your model with single value and multi-value U2 dictionary attributes. As you do not need to open a designer or dine an XML mapping file, you can use any U2 account flavor.

You can consume/use U2 WCF oData Service in the application such as

  • Windows Phone 8 application
  • Windows 8 Metro Style application
  • ASP.NET MVC 4 MVVM Pattern and application
  • iPhone/iPad application using ASP.NET MVC 4
  • Visual studio 2012 Light Switch application
  • WPF application

What is new in U2 Toolkit for .NET v1.2.1?

Recently we have released U2 Toolkit for .NET v1.2.1. It has the following new features.

  • It supports Visual Studio 2012, .NET Framework 4.5, Entity Framework 5.0. Now you can see U2 Server objects such Tables, Views and subroutines in Visual Studio 2012’s Server explorer and hence you can create Data Set and Entity Data Model (EDM) using VS2012’s designer.
  • U2 Parameter Class supports converting multi-value data into Data Set or Data Table
  • U2 Parameter Class’s Extension Methods allows converting multi-value data into .NET Simple Classes and collection of classes such as List
  • It supports Subroutine’s optimistic concurrency using U2 Parameter Class’s method such as DataTable_To_MV_OC() and SetOptimisticConcurrencyError()
  • It creates association on the fly between single value and multi-value in Data Set and EDM Designer

Testing Connection

You can test the connection using

  • Visual Studio 2012 Server Explorer
  • U2 Test Connection Tool

Visual Studio 2012 Server Explorer

Fig1

U2 Test Connection Tool

Fig2

Create Code First Entity Model from U2 Account/Dictionary

For this demo, we have used U2 UniVerse 11.1.10, XDEMO Account, PRODUCTS File.

Create ASP.NET Application

  1. In Visual Studio, on the File menu, select New, and then select Project.
  2. In the New Project dialog box, under either Visual Basic or Visual C# select the Web template, and then select ASP.NET Web Application.
  3.  Type WebApplication_U2_oData as the name of the project.

Create U2 Code First Model (Single Value)

  1. In Solution Explorer, right-click the name of the ASP.NET project, and then click Add New Item.
  2.  In the Add New Item dialog box, click the Class…
  3. Type ‘Product.cs’ the name of the file
  4. Cut and Paste the following C# Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
[Table("PRODUCTS")]
 
[DataServiceKey("ProductId")]
 
public class Product
 
{
 
public Product()
 
{
 
}
 
[Column("PRODUCT_ID")]
 
public decimal ProductId { get; set; }
 
[Column("TITLE")]
 
public string Title { get; set; } //Single Value
 
[Column("RATING")]
 
public string Rating { get; set; } //Single Value
 
[Column("MOVIE_IMAGE")]
 
public string Image { get; set; } //Single Value
 
}

 

Create U2 Code First Model (Multi Value)

  1. Open ‘Product.cs’ file
  2. Cut and Paste the following C# Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
[Table("PRODUCTS_AWARD_INFO")]
 
[DataServiceKey("ProductId", "ASSOC_ROW")]
 
public class Award
 
{
 
public Award()
 
{
 
}
 
[Column("@ASSOC_ROW")]
 
public decimal ASSOC_ROW { get; set; }
 
[Column("PRODUCT_ID")]
 
public decimal ProductId { get; set; }
 
[Column("AWARD_NAME")]
 
public string AwardName { get; set; } //multi-value
 
[Column("AWARD_TYPE")]
 
public string AwardType { get; set; } //multi-value
 
[ForeignKey("ProductId")]
 
public virtual Product Product { get; set; }
 
}

Create Association (between Single Value and Multi Value)

  1. Open ‘Product.cs’ file
  2. Cut and Paste the following C# Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[Table("PRODUCTS")]
 
[DataServiceKey("ProductId")]
 
public class Product
 
{
 
public Product() { Awards = new HashSet();}
 
}
 
// …. More code….
 
public virtual ICollection Awards { get; set; }
 
}
1
2
3
4
5
6
7
8
9
10
11
12
13
[Table("PRODUCTS_AWARD_INFO")]
 
[DataServiceKey("ProductId", "ASSOC_ROW")]
 
public class Award
 
{
 
// …. More code….
 
public virtual Product Product { get; set; }
 
}

Create a Context Class to Handle U2 Database Persistence

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
public class ProductContext : DbContext
 
{
 
public ProductContext()
 
{
 
this.Configuration.ProxyCreationEnabled = false;
 
}
 
public DbSet Products { get; set; }
 
public DbSet Awards { get; set; }
 
protected override void OnModelCreating(DbModelBuilder modelBuilder)
 
{
 
base.OnModelCreating(modelBuilder);
 
modelBuilder.Conventions.Remove();
 
modelBuilder.Entity().HasKey(u => new
 
{
 
u.ASSOC_ROW,
 
u.ProductId
 
});
 
modelBuilder.Entity().HasKey(u => new
 
{
 
u.ProductId
 
});
 
}
 
}

Create Connection String in ‘Web.Config’ file

<connectionStrings>

<add name=”ProductContext” connectionString=”Database=XDEMO;User ID=user;Password=pass;Server=129.91.89.87;Pooling=false;ServerType=universe;ConnectTimeout=360;PersistSecurityInfo=true;” providerName=”U2.Data.Client” />

</connectionStrings>

Completed Code For Domain Model

Create WCF oData Service using Code First

  1. In Solution Explorer, right-click the name of your ASP.NET project, and then click Add New Item.
  2. In the Add New Item dialog box, select WCF Data Service.
  3. For the name of the service, type U2_CodeFirst_oData_WcfDataService
  4. Visual Studio creates the XML markup and code files for the new service. By default, the code-editor window opens. In Solution Explorer, the service will have the name, U2_CodeFirst_oData_WcfDataService, with the extension .svc.cs or .svc.vb.
  5. Change the following:
1
public class U2_CodeFirst_oData_WcfDataService : DataService

Completed Code For Domain Context

Test WCF oData Service (Internet Explorer, ATOM/PUB XML Format and Fiddler JSON Format)

  1. In Solution Explorer right click ‘U2_CodeFirst_oData_WcfDataService.svc’ file and select Set as Start Page.
  2. In Internet Explorer, from the Tools menu, select Internet Options, click the Content tab, click Settings, and clear Turn on feed viewing.
  3. In Visual Studio, press the F5 key to start debugging the application.
  4. You will see the following

Fig3

  1. Navigate to Entity Set Resources such as Products and Awards.

Fig4

Fig5

Testing – Return JSON Format from oData using Fiddler2

You can return JSON format as well. See screen shot below.

Fig6

Deploy WCF oData Service in IIS Web Server

  1. Run IIS Manger (Control Panel\All Control Panel Items\Administrative Tools\ Internet Information Services (IIS) Manager
  2. Create Virtual Directory. Type ‘U2_oData_Service’ in Alias. For Physical Path, map with ‘U2_oData_WebApplication’ Project Directory.
  3. See screen shot below

Fig7

Source Code

Conclusion

U2 Toolkit for .NET allows creating WCF oData Service using Code First. You can use oData in various .NET applications such as Windows Phone 8 and Windows 8 Metro Style App. This feature allows any U2 Account Flavor to join main stream line of development such as Windows Azure Cloud, ASP.NET MVC 4, VS2012 Light Switch and Mobile.

Contact us to learn more about Rocket MultiValue Solutions.

Rocket Software 160 Posts

Rocket Software empowers organizations to create legendary impact in the world through innovation in legacy technologies. With deep expertise in IBM Z, IBM Power, and database and connectivity solutions, Rocket solutions power tens of thousands of global businesses, solving real problems and making real-world impact. With more than 70% of the world’s IT workload running on legacy platforms, Rocket helps companies and public-sector organizations innovate using the technology and data they already have, so they can always be ready for what comes next. Rocket customers include 44 of the Fortune 50, representing industries including Banking and Finance, Healthcare, Manufacturing, Transportation and Logistics, Retail and Insurance. A Bain Capital portfolio company, Rocket is headquartered in the Boston area with centers of excellence strategically located throughout North America, Europe, Asia, and Australia.

0 Comments

Leave a Comment

Your email address will not be published.