Category Archives: WCF

Using WCF Web API in .NET 4.5 and WCF 4.5


In previous posts we have seen some of the new features coming in the next version of .NET 4.5 and WCF 4.5. Today we are going to talk about new tool which simplifies the process of exposing data, services and application through the HTTP. This project is Codeplex project named as WCF Web API. Developing as an open source project, Microsoft want that developer and potential user can access and be part of the development process from the early stage to the final releases and integration to next version of .NET and WCF. Detailed information about WCF Web API you can find on http://wcf.codeplex.com site. As we mentioned with WCF Web API you dramatically simplifies the process of exposing data through service to consumers.

This blog post will describe how to use WCF Web API and to show how you can simply convert you existing WCF Data Service in to the service based on the WCF Web API, in just a few lines of code.

The most important features WCF Web API contains are:

-First-class programming model for HTTP in WCF
-Access HTTP from top to bottom
-Content negotiation
-HttpClient
-Out of the box support for XML, JSON and OData
-Typeless JSON support – Allows using a dynamic object on the server for accessing the body. Useful for jQuery clients.
-Plug in any format / media type
-IQueryable support – Supports OData’s URI format for services
-Designed for better separation of concerns and testability
-Validation, URI model binding, caching, etags, etc
-Easily integrate your IoC container
-Simple code-based configuration model
-Support for HTTP file upload and MIME-based batching
-Integrated Web API test client

Note that WCF Web API is working on the .NET 4.0 and Visual Studio 2010 as well, but with this post we want to show how such a technology works on the next version of .NET 4.5 and WCF 4.5. In fact, we will see how simply we can implement data service and expose it to ASP.NET application, or any client which can make request to the service in just few lines of code. To see simplicity of the WCF Web API, we will use existing WCF Data Service and convert it to the new version based on the WCF Web API. The existing project is demo developed for the last DevDays conference held in Sarajevo. The demo sample we are going to use here, you can download from this link. After you download the zip file, open it and you will get the content as next picture show.

The BugTracked Demo sample contains three folders:

-SQL Database –data tier and sql database for the project
-BugTrackerWCFClients – folder contains clients using the service
-BugTrackerService – folder contains WCF Service and EF DBModel project.

&For this demo we will use only DBModel. This is EntityFramework project which maps the tables from SQL database.

Now Open the Visual Studio 11 DP and create new ASP.NET empty application. Name it WCFWebAPISample, and click OK, similar as picture below.

Now that we have project we are going to use NuGet to install WCFWebAPI directly in to the project. Right Click on project and choose: Manage NuGet Package.

After WCF Web API installation, we need to copy existing EF project in to WcfWebAPISample solution folder and import it to the current WcfWebAPI Solution.

First Copy the folder in to Current solution folder:

If you haven’t installed NuGet you can install it from the following link. It also supports VS 11 beta, when the beta appear. Type WCF Web API in to search box and click on Install button of the WebApi.All.

After WCF Web API installation, we need to copy existing EF project in to WcfWebAPISample solution folder and import it to the current WcfWebAPI Solution.

First Copy the folder in to Current solution folder:

Return to VS 11 and Add existing project to current solution. After importing the project add the following reference to WCFWebAPISample project:
BugTracked.DBModel.dll
System.Data.Entity.dll


Note: Be sure that recently imported project is set to use .NET 4.5 instead of old .NET 4.0.

After we complete the importing BugTracker.DBModel project we need to setup database. Copy btdb database to you database folder and attach it to the SQL Server, or you can use Model First feature of the Entity Framework and create database to your SQL Server.
If you want to use Model First Feature connect to SQL Server as picture shows.

Create database and name it as BTDB, like the folowing picture:

Open BugTrackerEFDBmodel.edmx, right click and choose: Generate Database From Model…

Click Finish on the Wizard and complete SQL Script is generated for run.

Execute the script and the BTDB is created on SQL Server. Before we use it, wee need to fill some data. So that open every table and put at least one record in it, or just copy the data from the downloaded database.

Now the we have the Database and DBModel we can build our service to get manipulate data. If you look the existing project , we have created the WCF service project, implement Service interface, and service implementation. We have also implement Data Contract and define which data type can be exchanged thorough the service. We have also implement REST API for each Service operation, to be able to retrieve data by REST API. This was pretty big job, in comparison to what we are going to implement. To use our DBModel with WCF Web API, we need to implement one class, with BugTrackerWebAPI name.

The following code represent implementation for the class:

The class is decorated with [ServiceContract], and operation with [WebGet], attribute and this is all we need to implement. This two attribute replace whole implementation of WCF Service. The last thing wee need to implement before we runt the app is route registration in to Global.asax.

Add new item, choose Global.asax, and write just only one line of code, showed in the following picture:

We only register Api/bug route to Service implementation. This rout we need to put in to project property to be able to run the service immediately when browser appears. The picture below show this step.

After we run the application browser shows our data in XML format:

As we have seen this is very easy implementation of the Service data, without any configuration setting, DataContract and other WCF specific feature. On this way WCF becomes very hady tool in lighter application architecture, which was not the case in previous versions.

The complete implementation of this Demo you can download from here.

Advertisements

Sastanak MSCommunity BiH UG sa temom Introduction to WCF


U Sarajevu se 15. septembra se održava sastanak svih User Grupa u BiH, na kojem će se govoriti o stanju u User grupama u BiH, te kako pomoći sami sebi (UG u BiH) razmjenjujući iskustva, predavače i slične stvari  međusobno. Prije samog sastanka, noć prije održaće se sastanak MSCommunity BiH na kojem ću održati predavanje o Windows Comunication Foundation (WCF).

WCF predstavlja jedinstevnu platformu za distribuirane servisno orjentisane aplikacije. U ovom predavanju biće prezentirano šta je WCF ili Windows Comunication Foundation, te se upoznati sa osnovnim komponentama i pojmovima ove tehnologije. Na predavanju će većim dijelom biti predstavljeni Demo primjeri upotrebe WCF u raznim situacijama prilagođenim tehničkom nivou 200 i 300.

Tokom predavanje biće prikazani demo primjeri deploymenta, consuminga odnosno WCF sa raznih kiljentskih platformi te pokazati WCF u punom kapacitetu kao jednu od Microsoftovih najuspješnijih tehnologija danas.

Predavanje i sastanak će se održati 14. sep. 2011. god.  u Microsoft BiH  Sarajevo sa početkom u 18:30 sati. Prijave za sastanak, kao i ostale informacije o cjelokupnom sastanku možete pogledati na oficijelnoj stranici MSCommunity BiH http://www.mscommunity.ba

Vidimo se na sastanku.

WCF in N Tier Architecture – TechDay 2011


image

Kao što vam je već poznato 26. maja održava se redovna godišnja Microsoft jednodnevna konferencija pod nazivom DevDays, a koja je ove godine promjenila ime u TechDays. Konferencija će se održati u hotelu Sarajevo u Sarajevu 26. maja  sa početkom u 9:00. Više informacija o konferenciji, kao i o rasporedu predavanja, te registraciji za učešće možete vidjeti ovdje.

Na konferenciji ću držati, po meni interesantno predavanje, o Windows Communication Foundation u N Tier arhitekturi.

Nešto više o predavanju…

WCF je jedna od Microsoftovih tehnologija koja postaje sve popularnija, posebno u razvoju Enterprise aplikacija u kompanijama u kojima se želi postići maksimalna sigurnost skalabilnost i portabilnost. Smještena u srednji sloj WCF obezbjeđuje da višeslojne aplikacije zadovoje i najrigoroznije zahtjeve.  Iz tog razloga ova tehnologija se sve više koristi u današnjim modernih poslovnim klijent-server i distribuiranim  aplikacijama, kao što su banke, osiguravajuće kuće, internet portali, velike kompanije i td. REST, HTTP, TCP, Named Pipe neki su od protokola  koji su sastavni ugradbeni dijelovi WCF, te ovoj tehnologiji daju neograničenu primjenu u svim servisno orjentisanim aplikacijama, bilo da se radi o komunikaciji između procesa na jednoj PC mašini, u LAN-u ili na internetu neovisno o tome da li je klijent na PC-u ili nekoj drugoj ne-Windows baziranom klijentu.

Prezentacija će poslije 5 minutnog uvoda, biti full 40-to minuta coding sesija sa live Demo primjerima koje će publika u toku predavanja moći i sama isprobati na svojim mobitelima ili laptopima. To će biti omogućeno iz razloga što će se demo deployati na hostirani Windows Web Server. Demo će se sastojati u implementaciji jednog DB-modela pomoću Entity Frameworka, nakon čega će se formirati WCF REST Web servis. Isti će se deployati na pomenuti Web Server na internetu, poslije čega će se izgraditi nekoliko klijenata koji će konzumirati ovaj web servis. Prvo će se demonstrirati kao radi REST protokol u samom Internet Exploreru, a zatim će se implementirati Silverlight aplikacija koja će prikazivati, editovati i unositi nove podatke preko deployanog WCF servisa. Zatim će se implementirati drugi klijent koji će biti Windows Phone 7 aplikacija koja će također, konzumirati deployani  WCF servis.

Nadam se da sam donekle zainteresirao publiku za ovu prezentaciju, što vas ovom prilikom i pozivam.

Dobro došli i vidimo se na TechDays 2011.

7th Bihac .NET User Group Meeting


After great presentation of SharePoint in March, Bihac.NET User group is organizing the 7th meeting with cool topics of MVC 3.0 and WCF.

Details of the meeting:
Location: Edukativni Centar Laser DOO, Pritoka, 77000 Bihać
DateTime: 27. April. 2011, 17:00
Guest: Dragan Panjkov Microsoft BiH

Description of presentations:

1. Whats New in ASP.NET MVC 3.0, Dragan Panjkov, Microsoft BiH

The first topic will present What’s new in MVC3.0, so anyone wants to start this  technology is welcome on presentation.

2. Introduction of Windows Comunication Foundation WCF, Bahrudin Hrnjica MVP, DataSoft Bihać

WCF is unified platform for building Service oriented applications, that comunicate across web and enterprise. The presentation will cover the programming model of the WCF, how to implement basic Service, hosting of the service, messaging, security and many more features. During the presentation it will be presented  practice examples of covered topics. As previous presentation, this presentation is for the beginers so if you want to learn how to build web services on Microsoft plaftorm you are welcome.

You can register for meeting via facebook fun page http://www.facebook.com/bihac.net then events,  or directly  to event on the following link: https://www.facebook.com/event.php?eid=146941645371595

See you there!