This article introduces the Windows Communication Foundation (WCF).
WCF Overview 1. What Is WCF? WCF stands for Windows Communication Foundations. WCF combines the functionality from ASP.NET Web Services, .NET Remoting, Message Queuing and Enterprise Services. WCF provides the following features:
2. Understanding WCF These days we are creating the software/application that should be capable of communication with other applications as well. Communication with other application simply means either sharing/exchanging the data or the sharing the logic. Now, this communication may be one of the following two kinds:
Suppose we are writing a .NET Software with n-tier architecture in which a Windows Forms client needs to communicate with the server in the same network. In such case we may go for .NET Remoting for communication between client and server. Suppose, once our software mentioned above is ready, we need to expose some business logic to another J2EE application. This J2EE application is supposed to use our .NET application's logic over the WWW. In such a case we will need to write a new ASP.NET Web Service to expose the logic. The following picture shows the limitation of .NET Remoting: WCF helps us to overcome this kind of scenario, since a WCF Service can be used as a .NET Remoting Component and ASP.NET Web Services as well. 3. WCF in Real-Time Scenario Suppose a 7 Star hotel has contacted you for software that will help the organization to managing the hotel's room booking for a "Room Booking System". Apart from its own features software should be capable of the following:
Of course we are supposed to implement the application using Microsoft .NET Technology. Now, since we know that in order to communicate with another .NET application within the same network .NET Remoting is the best option. But based on our requirements our application should be capable of interaction with another J2EE application over the WWW. So we can't use .NET Remoting. ASP.NET web services may work fine but the correct option for now would be a WCF Service.
4. Differences between WCF and ASP.NET Web ServicesAtomic Transactions
5. WCF Communication Model WCF follows a Client-Server Architecture. Communication between Client and Server are established using Endpoints exposed by the WCF Service. Endpoints are nothing but the locations defined by a service through which a message can be sent and received. The service may have multiple end points. 6. Know some terms/ Kew words
7. WCF Binding Comparisons
As we explained in Point 4, WCF supports multiple bindings, for example HTTP, TCP and and so on. The following table describes the various bindings supported by WCF. Bindings are configurable and can be achived by changing the web.config file Binding Class Name Transport Message Encoding Message Version Security Mode RM Tx Flow* BasicHttpBinding HTTP Text SOAP 1.1 None X X WSHttpBinding HTTP Text SOAP 1.2 WS-A 1.0 Message Disabled WS-AT WSDualHttpBinding HTTP Text SOAP 1.2 WS-A 1.0 Message Enabled WS-AT WSFederationHttpBinding HTTP Text SOAP 1.2 WS-A 1.0 Message Enabled WS-AT NetTcpBinding TCP Binary SOAP 1.2 Transport Disabled Ole Tx NetPeerTcpBinding P2P Binary SOAP 1.2 Transport X X NetNamedPipesBinding Named Pipes Binary SOAP 1.2 Transport X Ole Tx NetMsmqBinding MSMQ Binary SOAP 1.2 Message X X MsmqIntegrationBinding MSMQ X** X Transport X X CustomBinding You decide You decide You decide You decide You decide You decide
8. How to Create a Sample WCF application with VS2008?
8.1 Create/ Manage database Before proceeding toward the WCF Service Creation, we need to create a database say, "WCF" with one table having the following schema: Where, ReservationId is an auto-generated Primary Key column. Note: You may also restore the backup of the database given along with the sample application. 8.2 Create WCF Service
namespace Practice.WCF { [DataContract] publicclassRoomReservationRequest { [DataMember] public int ReservationId { get; set; } [DataMember] public int NoOfRooms { get; set; } [DataMember] public string TypeOfRoom { get; set; } [DataMember] publicDateTime FromDate { get; set; } [DataMember] publicDateTime ToDate { get; set; } [DataMember] public string ContactPersonName { get; set; } [DataMember] public string ContactPersonMail { get; set; } [DataMember] public string ContactPersonMob { get; set; } [DataMember] public string Comments { get; set;
} [DataMember] public string Status { get; set; } }}
internal bool ReserveRoom(RoomReservationRequest roomReservationReq) { SqlConnection connection = GetConnection(); string sqlCommand ="INSERT INTO RoomReservationRequest(NoOfRooms, TypeOfRoom, FromDate, ToDate, ContactPersonName, " + "ContactPersonMail, ContactPersonMob, Comments, Status) VALUES (" + "@NoOfRooms, @TypeOfRoom, @FromDate, @ToDate, @ContactPersonName, " + "@ContactPersonMail, @ContactPersonMob, @Comments, @Status)";
SqlCommand command = connection.CreateCommand(); command.CommandText = sqlCommand; command.Parameters.Add("@NoOfRooms", System.Data.SqlDbType.Int); command.Parameters.Add("@TypeOfRoom", System.Data.SqlDbType.NVarChar, 20); command.Parameters.Add("@FromDate", System.Data.SqlDbType.DateTime ); command.Parameters.Add("@ToDate", System.Data.SqlDbType.DateTime); command.Parameters.Add("@ContactPersonName", System.Data.SqlDbType.NVarChar, 50); command.Parameters.Add("@ContactPersonMail", System.Data.SqlDbType.NVarChar, 50); command.Parameters.Add("@ContactPersonMob", System.Data.SqlDbType.NVarChar, 20); command.Parameters.Add("@Comments", System.Data.SqlDbType.NVarChar, 200); command.Parameters.Add("@Status", System.Data.SqlDbType.NVarChar, 200);
command.Parameters["@NoOfRooms"].Value = roomReservationReq.NoOfRooms; command.Parameters["@TypeOfRoom"].Value = roomReservationReq.TypeOfRoom; command.Parameters["@FromDate"].Value = roomReservationReq.FromDate; command.Parameters["@ToDate"].Value = roomReservationReq.ToDate; command.Parameters["@ContactPersonName"].Value = roomReservationReq.ContactPersonName; command.Parameters["@ContactPersonMail"].Value = roomReservationReq.ContactPersonMail; command.Parameters["@ContactPersonMob"].Value = roomReservationReq.ContactPersonMob; command.Parameters["@Comments"].Value = roomReservationReq.Comments; command.Parameters["@Status"].Value = roomReservationReq.Status;
int rowsEffected =0; try { rowsEffected = command.ExecuteNonQuery(); } finally { if (connection !=null) { connection.Close(); connection.Dispose(); } }
return rowsEffected > 0; }
internalRoomReservationRequest[] GetReservations(DateTime fromDate, DateTime toDate) { List<RoomReservationRequest> reservedRooms = newList<RoomReservationRequest>(); SqlConnection connection = GetConnection(); SqlCommand command = connection.CreateCommand(); command.CommandText ="SELECT ReservationId, NoOfRooms, TypeOfRoom, FromDate" + ",ToDate, ContactPersonName, ContactPersonMail, ContactPersonMob, Comments, Status "+ "FROM RoomReservationRequest "+ "WHERE FromDate > @FromDate AND ToDate<@ToDate";
command.Parameters.Add("@FromDate", System.Data.SqlDbType.DateTime); command.Parameters.Add("@ToDate", System.Data.SqlDbType.DateTime);
command.Parameters["@FromDate"].Value = fromDate; command.Parameters["@ToDate"].Value = toDate;
SqlDataReader reader =null;
try { reader = command.ExecuteReader(CommandBehavior.CloseConnection); while (reader.Read()) { RoomReservationRequest roomReservationRequest = newRoomReservationRequest(); roomReservationRequest.ReservationId =Convert.ToInt16(reader[0]); roomReservationRequest.NoOfRooms =Convert.ToInt16(reader[1]); roomReservationRequest.TypeOfRoom = reader[2].ToString(); roomReservationRequest.FromDate =Convert.ToDateTime(reader[3]); roomReservationRequest.ToDate =Convert.ToDateTime(reader[4]); roomReservationRequest.ContactPersonName = reader[5].ToString(); roomReservationRequest.ContactPersonMail = reader[6].ToString(); roomReservationRequest.ContactPersonMob = reader[7].ToString(); roomReservationRequest.Comments = reader[8].ToString(); roomReservationRequest.Status = reader[9].ToString(); reservedRooms.Add(roomReservationRequest); } } finally { if (reader !=null) { reader.Close(); reader.Dispose(); }
if (connection != null) { connection.Close(); connection.Dispose(); } } return reservedRooms.ToArray();
privateSqlConnection GetConnection() { SqlConnection connection = newSqlConnection(connectionString); try { connection.Open(); } finally { } return connection; } } }
9. How to Consume the WCF Service? 9.1 Generate the Class and Config File using svcutil.exe You might have noticed the message displayed while browsing the service as in the following: We now need to generate the classes that our Client application will use to consume the service. We may generate the classes/configuration using the tool svcutil.exe. Use the following procedure to generate the Class/Config file.
9.2 Create Client to Consume Service
10. How to Use the Source Unzip the file WCF.zip, you may find the following files:
Restore / Create Database Unzip the file DBBackup.zip and restore the WCF.bak file to SQL Server Database or you may create the database as suggested in Step 8.1. Now, unzip the file Source.zip, you may find:
Publish/Host Client
Use WCF Client to consume the services
Note: You may need to modify the App.Config file if you have not published the WCF Service to the local machine.
Essential WCF Practical Implementation