Edit

Share via


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

  1. Open Visual Studio.

  2. On the start window, choose Create a new project.

  3. 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.

    Create new WCF Service Library project in Visual Studio

    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.

  4. 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.

  5. In Solution Explorer, double-click IService1.vb or IService1.cs.

    The IService1 file

    Find the following line:

    [OperationContract]
    string GetData(int value);
    

Change the type for the value parameter to string:

[OperationContract]
string GetData(string value);

In the above code, note the OperationContract attribute. This attribute is required for any method exposed by the service.

  1. In Solution Explorer, double-click Service1.vb or Service1.cs.

    The Service1 file

    Find the following line:

    public string GetData(int value)
    {
        return string.Format("You entered: {0}", value);
    }
    

Change the type for the value parameter to string:

public string GetData(string value)
{
    return string.Format("You entered: {0}", value);
}

Test the service

  1. Press F5 to run the service. A WCF Test Client form appears and loads the service.

  2. In the WCF Test Client form, double-click the GetData() method under IService1. The GetData tab appears.

    The GetData() method

  3. In the Request box, select the Value field and type Hello.

    The Value field

  4. Click the Invoke button. If a Security Warning dialog box appears, click OK. The result displays in the Response box.

    The result in the Response box

  5. On the File menu, click Exit to close the test form.

Access the Service

Reference the WCF service

  1. On the File menu, point to Add > New Project. Choose Windows Forms App (.NET Framework) project.

  2. Right-click on the project node, and click Add > Service Reference. The Add Service Reference dialog box appears.

  3. In the Add Service Reference dialog box, click Discover.

    Screenshot showing the Add Service Reference dialog box.

    Service1 displays in the Services pane.

  4. Click OK to add the service reference.

Build a client application

  1. In Solution Explorer, double-click Form1.vb or Form1.cs to open the Windows Forms Designer if it is not already open.

  2. Open the Toolbox by clicking on View > Toolbox (or Ctrl+Alt+X on the keyboard).

  3. From the Toolbox, drag a TextBox control, a Label control, and a Button control onto the form.

    Screenshot showing adding controls to the form.

  4. Double-click the Button, and add the following code in the Click event handler:

    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;
    }
    
  5. In Solution Explorer, right-click the project node (for example, WindowsFormsApp1), and click Set as StartUp Project.

  6. 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.

    Screenshot of the running form showing the result.