Chapter 1 – Introducing Windows Communication Foundation

MS WCF 4 Step by Step

Introduction to WCF; how to create, deploy and access a simple WCF service.

Web Services: XML + XML Schema
Web Service ßà Web Service Client using SOAP. A web service can publish a Web Service Description language (WSDL) which is also XML, that describe the messages the web service can accept and the structure of the response. A client application can use this information to determine how to communicate with the web service.

JSON – Javascript Object Notation. Completely language-independent. It organizes data into name/value pairs, or collections of values.

SOA – service oriented architecture

WCF provides a model with which you can implment services that conforms to many commonly-accepted styles and standards, such as SOAP, XML, JSON. Additionally, WCF supports many MS-specific technologies for building components, such as Enterprises and MSMQ.

Building a WCF service (P. 9)

VS 2010 has several WCF project templates that you can use to build WCF service.
In this exercise, we’re building a WVF service that provides the following information:
(1) products sold by AdvWorks
(2) obtain details of a specific product
(3) Query the current stock level of a product
(4) Modify the stock level of a product

We will make use of .NET 4.0 Entity Framework. Purpose: provide an object mapping between tables in a database and a set of objects that you can use in your applications and services. Generate an object model that you can use to query tables, and inset/update/delete data. A major adv: you can build your applications that are independent of the underlying database management system.

Create a new C# project using the Class Library template, name “ProductEntityModel”, solution name: ProductService

Dele Class1.cs file. Add a new item – ADO.NET Entity Data Model template to the project, name: ProductModel.edmx. You see the wizard.

Select generate from Database:

Add a new connection to the AdvWorks database

In the next screen, select the tables; click Finish. VS 2010 generates the entity model.

On the next scrren, it dispay the tables and the table fields <-> Coding property relationship:

Build the project, next, add a new web site to the solution. Right click solution à Add à Add new web site.
Add a WCF service webs ite

Note: you can create WCF service as part of a web site; or library; or a standalone application, or WF.

The structure of WCF service enables you to adopt a “contract-first” approach to develop. First you define the “interface”, or contract (that the actual service will implement), and then build a service that conforms to these contracts. Adv: you can concentrate on designing your service. Separate definition from its implementation.

The Interface file includes: [ServiceContract] and [DataContract].

— ServiceContract
— OperationContract
— OperationContract
— OperationContract
— DataContract
— DataMember
— DataMember
— DataMember

Update with your own DataContract

Define ServiceContract (method interfaces)

Implementing the Service

You must implement every method defined b the service contract in the WCF service. If you define additional methods that are not in the service contract, those methods will not be visible to the client app.

In the WCF web site, add a reference to the ProductsEntityModel (Project) and System.Data.Entity (.NET) – required from fetching data from entity model.

Rename Service.cs to ProductsService.cs

Add a namespace and an implementation class:

ListProducts method:

getProduct method:

Get current stock number of a specific product

Change stock level:

Build it.

Open Service.svc, update to the correct class and file name:

(Now you can “Browse to” service.svc and see the WCF service in IE)

*.SVC file is a apecial file that IIS use to recognize WCF service. If oyu don’t want the *.SVC file, you can add a serviceActivation tag in web.congif. But you still need to use *.svc in your URL. Can’t really see the benefits. Just add more confusion!

Creating a WCF Client application

VS 2010 can use the WCF metadata (WSDL) to create a proxy class
that the client can use to connect and consume the service.

Add a new project – Console Application to your solution, name: ProductsClient.

For this project, add a reference to System.ServiceModel.

Add the WCF service as a Service Reference to the ProductsClient project. Click Discover and it will display the WCF service.

*** The Discover button is to display all WCF services that are in the same solution. Give the namespace “ProductsService”. Now the WCF Service is a service reference in the console application:

Reference.cs is the Proxy Class:

Open console app’s app.config and you see the WCF info (ABC etc.) has been added when the service ref is added.

Add these two usings in the Program.cs, in console app.

First, create a proxy object:

To configure startup project(s) in a solution:

Set the WCF service and the client console app to start:

Right click the WCF service, “Start Options”.

Set to “Don’t open a page” so VS 2010 don’t open an IE window.


Full client code:

Deploying WCF Service to IIS

We want to deploy the WCE service to an IIS, and then reconfigure the client to connect to IIS.

Right click WCF service, Publish web site:

Point to local IIS; default web site, create new web application.

Name is “ProductsService” and then Open. Click OK:

*** Configure the new WCF web app to use an application pool that uses .NET 4.0, or else it won’t work.
*** If still not working, make sure the WCF service’s application pool’s identity can access the Database!!!!!

end of chaper 1

Advertisements
Post a comment or leave a trackback: Trackback URL.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: