Walkthrough: Create a simple WCF service in .NET Framework Windows Forms
This walkthrough demonstrates how to create a simple Windows Communication Foundation (WCF) service, test it, and then access it from a .NET Framework Windows Forms application.
Note
Your computer might show different names or locations for some of the Visual Studio user interface elements in this article. You might be using a different edition of Visual Studio or different environment settings. For more information, see Personalize the IDE.
Prerequisites
The WCF tools are not installed with the .NET workload; use the Visual Studio Installer to modify your installation. In the installer, choose Windows Communication Foundation under Individual Components. See Modify Visual Studio.
Create a service
Open Visual Studio.
On the start window, choose Create a new project.
Type wcf service library in the search box on the Create a new project page. Select either the C# or Visual Basic template for WCF Service Library, and then select Next.
Tip
If you don't see any templates, you might need to install the Windows Communication Foundation component of Visual Studio. Choose Install more tools and features to open Visual Studio Installer. Choose the Individual components tab, scroll down to Development activities, and then select Windows Communication Foundation. Click Modify.
On the Configure your new project page, click Create.
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.
Find the following line:
Change the type for the value
parameter to string:
In the above code, note the OperationContract
attribute. This attribute is required for any method exposed by the service.
In Solution Explorer, double-click Service1.vb or Service1.cs.
Find the following line:
Change the type for the value
parameter to string:
Test the service
Press F5 to run the service. A WCF Test Client form appears and loads the service.
In the WCF Test Client form, double-click the GetData() method under IService1. The GetData tab appears.
In the Request box, select the Value field and type
Hello
.Click the Invoke button. If a Security Warning dialog box appears, click OK. The result displays in the Response box.
On the File menu, click Exit to close the test form.
Access the Service
Reference the WCF service
On the File menu, point to Add > New Project. Choose Windows Forms App (.NET Framework) project.
Right-click on the project node, and click Add > Service Reference. The Add Service Reference dialog box appears.
In the Add Service Reference dialog box, click Discover.
Service1 displays in the Services pane.
Click OK to add the service reference.
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.
Open the Toolbox by clicking on View > Toolbox (or Ctrl+Alt+X on the keyboard).
From the Toolbox, drag a
TextBox
control, aLabel
control, and aButton
control onto the form.Double-click the
Button
, and add the following code in theClick
event handler:In Solution Explorer, right-click the project node (for example, WindowsFormsApp1), and click Set as StartUp Project.
Press F5 to run the project. Enter some text and click the button. The label displays "You entered:" and shows the text that you entered.