WCF Overview C# Help

WCF combines the functionality from ASP.NET Web services, .NET Remoting, Message Queuing, and Enterprise Services. What you get from WCF is:

Hosting for components and services – Just as you can use custom hosts with .NET Remoting and WSE, you can host a WCF service in the ASP.NET runtime, a Windows service, a COM process, just a Windows Forms application for peer-to-peer computing.

Declarative behavior – Instead of the requirement to derive from a base class (this requirement exists with .NET Remoting and Enterprise Services), attributes can be used to define the services. This is similar to Web services developed with ASP.NET.

Communication channels – Although NET Remoting is very flexible with changing the communication channel, WCF is a good alternative because it offers the same flexibility .WCF offers multiple channels to communicate using HTTP, TCP, or an IPC channel. Custom channels using different transport protocols can be created as well.

Security infrastructure – For implementing platform-independent Web services, a standardized security environment must be used. The proposed standards are implemented with WSE 3.0, and this continues with WCF.

Extensibility – .NET Remoting has a rich extensibility story. It is not only possible to create custom channels, formatters, and proxies, but also to inject functionality inside the message flow on the client and on the server. WCF offers similar extensibilities; however, here, the extensions are created by using SOAP headers.

Support of previous technologies – Instead of rewriting a distributed solution completely to use WCF, WCF can be integrated with existing technologies. WCF offers a channel that can communicate with serviced components using DCOM. Web services that have been developed with ASP.NET can be integrated with WCF as well.

The final goal is to end and receive messages from a client to a service either across processes or different systems, across a local network, or the Internet. This should be done if required in a platform independent way and as fast as possible. On a distant view, the service offers an endpoint that is described by a contract, binding, and an address. The contract defines the operations offered by the service, binding gives information about the protocol and encoding, and the address is the location of the service. The client needs a compatible end point to access the service.

Figure 42-1 shows the components that participate with a WCF communication.

Figure 42-1

Figure 42-1

The client invokes a method on the proxy. The proxy offers methods as defined by the service, but COM the method call to a message and transfers the message to the channel. The channel has a client side and a server-side part that communicate across a networking protocol. From the channel, the message is passed to the dispatcher, which converts the message to a method call that is invoked with the service.

WCF supports several communication protocols. For platform-independent communication, Web services standards are supported. For communication between .NET applications, faster communication protocols with less overhead can be used.

The following sections look at the functionality of core services used for platform-independent communication.

SOAP

.For platform-independent communication, the SOAP protocol can be used and is directly supported from WCF. SOAP originally was short hand for Simple Object Access Protocol, but since SOAP 1.2 this is no longer the case. SOAP no longer is an object access protocol, because, instead, messages are sent that can be defined by an XML schema.

A service receives a SOAP message from a client and returns a SOAP response message. A SOAP message consists of an envelope, which contains a header and a body:

Capture

The header is optional and can contain information about addressing, security, and transactions. The body contains the message data.

WSDL

A WSDL (Web Services Description Language) document describes the operations and messages of the service. WSDL defines metadata of the service that can be used to create a proxy for the client application. .

The WSDL contains this information:

  1. Types for the messages that are described using an XML schema.
  2. Messages that are sent to and from the service. Parts of the messages are the types that are defined with an XML schema.
  3. Port types map to service contracts and list operations that are defined with the service contract. Operations contain messages; for example, an input and an output message as used with a request and response sequence.
  4. Binding information that contains the operations listed with the port types and that defines the SOAP variant used.
  5. Service information that maps port types to endpoint addresses.

Service information that maps port types to endpoint addresses.

JSON

Instead of sending SOAP messages, accessing services from JavaScript can best be done using JSON JavaScript Object Notation) .. NET 3.5 includes a data contract serializer to create objects with the JSON notation.

JSON has less overhead than SOAP because it is not XML,but optimized for JavaScript clients. This makes it extremely useful from Ajax clients. Ajax is discussed in Chapter 39, ASP.NET AJAX. JSON does not offer reliability, security, and transaction features that can be sent with the SOAP header, but these are features usually not needed by JavaScript clients.

Simple Service and Client

Before going into the details of WCF, let’s start with a simple service. The service is used to reserve meeting rooms.

For a backing store of room reservations, a simple SQL Server database with the table Room Reservations is used. The table and its properties are shown in Figure 42-2. You can download the database together with the sample code of this chapter.

Figure 42-2

Figure 42-2

Create an empty solution with the name Room Reservation and add a new Component library project with the name Room Reservation Data to the solution. The first project that is implemented contains just the code to access the database. Because LINQ to SQL makes the database access code much easier, this .NET 3.5 technology is used here, .

Chapter 27 gives you the details of LINQ to SQL.

Add a new item, LINQ to SQL Classes, and name it Room Reservation , dbml. With the LINQ to SQL  designer, open the Server Explorer to drop the Room Reservation database table onto the designer as shown in Figure 42-3, This designer creates an entity class, Room Reservation, that contains properties fur every column of the table and the class Room Reservation Data Context. Room Reservation Data Context connects to the database .

Figure 42-3

Figure 42-3

Change the Serialization Mode property of the LINQ to SQL designer from None to Unidirectional. This way, the generated class Room Reservation gets a data contract that allows the entity classes to serialize across WCF.

To read and write data from the database using LINQ to SQL, add the class Room Reservation Data. The method Reserve Room ()writes a room reservation to the database. The method GetReservations() returns an array of room reservations from a specified date range.

Capture

Service Contract

Add a new project of type WCF Service Library to the solution and name the project Room Reservation Service. Rename the generated files  Iservice1 .cs to IRoomService. cs and Service. cs to Room Reservation Service. cs and change the namespace within the generated files to Wrox. Pro CSharp. WCF. Service. The assembly Room Reservation Data needs to be referenced to have the entity types and the Room Reservation Data class available.

The operations offered by the service can be defined by an interface. The interface IRoomService defines the methods Reserve Room and GetRoomReservations. The service contract is defined with the attribute (ServiceContract). The operations defined by the service have the attribute (OperationContract) applied.

Capture

Service Implementation

The service class Room Reservation service  implemented just by invoking the appropriate methods of the Room Reservation Data class:

Capture

Capture

WCF Service Host and WCF Test Client

The WCF Service Library project template creates an application configuration file named App. config that you need to adapt to the new class and interface names. The service element references the
service type Room Reservation Service including the namespace; the contract interface needs to be defined with the endpoint element.

Capture

The service address http://localhost :8731/Design_Time_Addresses has an access controllist (ACL) associated that allows the interactive user to create a listener port. By default, anon-administrative user is not allowed to open ports in listening mode. You can view the ACLs with the command-line utility netsh http show urlacl, and add new entries.

Starting this library from Visual Studio 2008 starts the WCF Service Host, which appears as an icon in the notification area of the taskbar. Clicking this icon opens the dialog (see Figure 42-4) of this
application where you can see the status of the service.The project properties have the command-Iine arguments client: ·WcfTestClient. exe defined. With this option, the WCF Service host starts the WCF Test Client (see Figure 42-5) that you can use to test the application. When you double-click an operation, input fields appear on the right side of the application that you can fill to send data to the service. When you click the XML tab, you can see the SOAP messages that have been sent and received .

Figure 42-4

Figure 42-4

Figure 42-5

Figure 42-5

Custom Service Host

WCF allows services to run in any host. You can create a Windows Forms or WPF application for peer -to- peer services, you can create a Windows service, or host the service with Windows Activation Services (WAS). A console application is also good to demonstrate a simple host.

With the service host, you must reference the library Room Reservation Service. The service is started y instantiating and opening an object of type Service Host. This class is defined in the namespace System. Service Model. The Room Reservation Service class that implements the service is defined in the constructor. Invoking the Open() method starts the listener channel of the service – the service is ready to listen for requests. The Close() method stops the channel.

Capture

For the WCF configuration, you need to copy the application configuration file that was created with the service library to the host application. You can edit this configuration file with the WCF Service
Configuration Editor (see Figure 42-6)

Figure 42-6

Figure 42-6

WCF Client

For the client, WCF is flexible again in what application type can be used. The client can be a simple console application as well. However, for reserving rooms, create a WPF application with controls, as shown in Figure 42-7.

Figure 42-7

Figure 42-7

Because the service offers a MEX end point with the binding mexHt tpBinding, and metadata access is enabled with the behavior configuration, you can add a service reference from Visual Studio. When you add a service reference, the dialog shown in Figure 42-8 pops up. When you click the Discover button, you can find services within the same solution.

Enter the link to the service and set the service reference name to Room Reservation Service. The service reference name defines the namespace of the generated proxy class.

Figure 42-8

Figure 42-8

Adding a service reference adds references to the assemblies System. Runtime. Serialization and System. Service Model and a configuration file containing the binding information and the endpoint address to the service.

From the data contract the class Room Reservation is generated. This class contains all (Data Member) elements of the contract. The class Room Service Client is the proxy for the client that contains methods that are defined by the service contract. Using this client you can send a room reservation-to the running service.

Capture

By running both the service and the client, you can add room reservations to the database.

Diagnostics

When running a client and service application, it can be very helpful to know what’s happening behind the scenes. For this, WCF makes use of a trace source that just needs to be configured. You can configure tracing using the Service Configuration Editor, selecting Diagnostics, and enabling Tracing and Message Logging. Setting the trace level of the trace sources to Verbose produces very detailed information. This configuration change adds trace sources and listeners to the application configuration file as shown here:

Capture

Capture

The implementation of the WCF classes uses the trace sources named System. Service Model and System. Service Model.Message Logging for writing trace messages. You can read more about tracing and configuring trace sources and listeners in Chapter 18, “Tracing and Events.”

When you start the application, the trace files soon get large with verbose trace settings. To analyze the information from the XML log file, the .NET SDK includes the Service Trace Viewer tool, .svctraceviewer.exe. Figure 42-9 shows the view from this tool after selecting the trace and message log files. With the default configuration you can see several messages exchanged; many of them are related to security. Depending on your security needs, you can choose other configuration options.

Figure 42-9

Figure 42-9

The following section discus~e details and different options of WCF.

 

Posted on October 31, 2015 in Windows Communication Foundation

Share the Story

Back to Top