Walkthrough: Creating and Accessing WCF Services
This walkthrough demonstrates how to create a simple Windows Communication Foundation (WCF) service, test it, and then access it from a Windows Forms application.
Note
Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Customizing Development Settings in Visual Studio.
Creating the Service
To create a WCF service
On the File menu, point to New and then click Project.
In the New Project dialog box, expand the Visual Basic or Visual C# node and click WCF, followed by WCF Service Library. Click OK to open the project.
Note
This creates a working service that can 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.
-
In Solution Explorer, double-click IService1.vb or IService1.cs and find the following line:
<OperationContract()> Function GetData(ByVal value As Integer) As String
[OperationContract] string GetData(int value);
Change the type for the value parameter to String:
<OperationContract()> Function GetData(ByVal value As String) As String
[OperationContract] string GetData(string value);
In the above code, note the <OperationContract()> or [OperationContract] attributes. These attributes are required for any method exposed by the service.
-
In Solution Explorer, double-click Service1.vb or Service1.cs and find the following line:
Public Function GetData(ByVal value As Integer) As String Implements IService1.GetData Return String.Format("You entered: {0}", value) End Function
public string GetData(int value) { return string.Format("You entered: {0}", value); }
Change the type for the value parameter to String:
Public Function GetData(ByVal value As String) As String Implements IService1.GetData Return String.Format("You entered: {0}", value) End Function
public string GetData(string value) { return string.Format("You entered: {0}", value); }
Testing the Service
To test a WCF service
Press F5 to run the service. A WCF Test Client form will be displayed and it will load the service.
In the WCF Test Client form, double-click the GetData() method under IService1. The GetData tab will be displayed.
In the Request box, select the Value field and type Hello.
Click the Invoke button. If a Security Warning dialog box is displayed, click OK. The result will be displayed in the Response box.
On the File menu, click Exit to close the test form.
Accessing the Service
To reference a WCF service
On the File menu, point to Add and then click New Project.
In the New Project dialog box, expand the Visual Basic or Visual C# node and select Windows, and then select Windows Forms Application. Click OK to open the project.
Right-click WindowsApplication1 and click Add Service Reference. The Add Service Reference dialog box will appear.
In the Add Service Reference dialog box, click Discover.
Service1 will be displayed in the Services pane.
Click OK to add the service reference.
To build a client application
In Solution Explorer, double-click Form1.vb or Form1.cs to open the Windows Forms Designer if it is not already open.
From the Toolbox, drag a TextBox control, a Label control, and a Button control onto the form.
Double-click the Button, and add the following code in the Click event handler:
Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click Dim client As New ServiceReference1.Service1Client Dim returnString As String returnString = client.GetData(TextBox1.Text) Label1.Text = returnString End Sub
private void button1_Click(System.Object sender, System.EventArgs e) { ServiceReference1.Service1Client client = new ServiceReference1.Service1Client(); string returnString; returnString = client.GetData(textBox1.Text); label1.Text = returnString; }
In Solution Explorer, right-click WindowsApplication1 and click Set as StartUp Project.
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.