Objectives:: Topic: Windows Communication Foundation

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Topic: Windows Communication Foundation

Estimated Time: 30 min.

Objectives: At the end of the activity, the participant will be able to understand:
• Use of WCF.
• How to create WCF service.
• How to use WCF service.

Presentation:

• WCF provides a unified framework for rapidly building service-oriented applications


that makes it easy to build and consume secure, reliable and transacted web services.
• It unifies the capabilities in ASMX, WSE, Remoting, COM+ and MSMQ; that for
developers need to learn only one programming model.
• WCF simplifies development of connected applications through a new service-
oriented programming model. WCF supports many styles of distributed application
development by providing a layered architecture
• Windows Communication Foundation (WCF) is designed to offer a manageable
approach to distributed computing, broad interoperability, and direct support for
service orientation.

Windows Communication Foundation Architecture

Scenario:
This demonstrates how to create a simple Windows Communication Foundation (WCF)
service, test it, and then access it from a Windows Forms application.

Demonstration/Code Snippet:

Creating the Service

To create a WCF service


1. On the File menu, click New Project.
2. In the New Project dialog box, expand the Visual C# node and select WCF, and then
select WCF Service Library. Click OK to open the project.

This creates a working service that could actually be tested and accessed. The

following two steps demonstrate how you might modify the default method to use a
different data type. In a real application, you would also add your own functions to
the service.

1. In Solution Explorer, double-click IService1.vb and find the line Function


GetData(ByVal value As Integer) As String. Change the type for the value parameter
to String: Function GetData(ByVal value As String) As String.
2. In Solution Explorer, double-click Service1.vb and find the line Public Function
GetData(ByVal value As Integer) As String Implements IService1.GetData. Change
the type for the value parameter to String: Public Function GetData(ByVal value As
String) As String Implements IService1.GetData.

Testing the Service

To test a WCF service


1. Press F5 to run the service. A WCF Test Client form will be displayed and it will
load the service.
2. In the WCF Test Client form, double-click the GetData() method under IService1.
The GetData tab will be displayed.
3. In the Request box, select the Value field and type Hello.
4. Click the Invoke button. If a Security Warning dialog box is displayed, click OK.
The result will be displayed in the Response box.
5. On the File menu, click Exit to close the test form.

Accessing the Service

To reference a WCF service


1. On the File menu, point to Add and then click New Project.
2. In the New Project dialog box, expand the Visual C# node and select Windows, and
then select Windows Forms Application. Click OK to open the project.
3. Right-click WindowsApplication1 and click Add Service Reference. The Add
Service Reference dialog box will appear.
4. In the Add Service Reference dialog box, click Discover.
Service1 will be displayed in the Services pane.
5. Click OK to add the service reference.
To build a client application
1. In Solution Explorer, double-click Form1.vb to open the form designer if it is not
already open.
2. From the Toolbox, drag a TextBox control, a Label control, and a Button control
onto the form.
3. Double-click the Button, and add the following code in the Click event handler:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)


{
ServiceReference1.Service1Client client;
string returnString;
returnString = client.GetData(TextBox1.Text);
Label1.Text = returnString;
}
}
}

4. In Solution Explorer, right-click WindowsApplication1 and click Set as StartUp


Project.
5. Press F5 to run the project. Enter some text and click the button—the label will
display "You entered:" and the text that you entered.

Context:
• All communication with a Windows Communication Foundation (WCF) service
occurs through the endpoints of the service.
• The Windows Communication Foundation (WCF) can be thought of as a messaging
infrastructure. Service operations can receive messages, process them, and send them
messages. Messages are described using operation contracts.
• The run-time components of WCF can be divided into two major parts: the channel
stack and the service framework, with the Message class being the connection point.
• The channel stack is responsible for converting between a valid Message instance
and some action that corresponds to the sending or receiving of message data.
• The Web Programming Model provides the basic framework elements required to
build Web-style services with Windows Communication Foundation (WCF). Web-
style services are designed to be accessed by the widest range of possible clients
(including Web browsers with no additional client framework)

Practice Session:
• Explore the various methods, properties and events listed in the Message class and,
System.ServiceModel from MSDN.

Check list:
• Use of WCF.

Common Errors:

Exceptions:

Lessons Learnt:
☑ Performance of Windows Communication Foundation (WCF) application depends on
many different things.
☑ Application design, instancing mode, concurrency mode, transport, binding settings,
and throttling settings all have effects on the performance of your application.
☑ Because WCF can communicate using Web services, interoperability with other
platforms that also support SOAP, such as the leading J2EE-based application
servers, is straightforward.
☑ Managing object lifetimes, defining distributed transactions, and other aspects of
Enterprise Services are now provided by WCF.

Best Practices:
• Choosing the right size impacts how efficiently the network is put to use and the
optimal capacity the service is running at.
• Use WCF because it provides a single, unified and extendable programming object
model that can be used to interact with a number of previously diverse distributed
technologies.

You might also like