Share via


Walkthrough: Create a simple WCF service in Windows Forms

Applies to: yesVisual Studio noVisual Studio for Mac

Note

This article applies to Visual Studio 2017. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

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 may 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.
  1. On the File menu, choose New > Project.

  2. In the New Project dialog box, expand the Visual Basic or Visual C# node and choose WCF, followed by WCF Service Library.

  3. Click OK to create the project.

    The WCF Service Library 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.

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

    The IService1 file

    Find the following line:

    [OperationContract]
    string GetData(int value);
    
    <OperationContract()>
    Function GetData(ByVal value As Integer) As String
    

    Change the type for the value parameter to string:

    [OperationContract]
    string GetData(string value);
    
    <OperationContract()>
    Function GetData(ByVal value As String) As String
    

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

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

    The Service1 file

    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 string GetData(string value)
    {
        return string.Format("You entered: {0}", value);
    }
    
    Public Function GetData(ByVal value As String) As String Implements IService1.GetData
        Return String.Format("You entered: {0}", value)
    End Function
    

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 and then click New Project.

  2. In the New Project dialog box, expand the Visual Basic or Visual C# node, select Windows, and then select Windows Forms Application. Click OK to open the project.

    Windows Forms Application project

  3. Right-click WindowsApplication1 and click Add Service Reference. The Add Service Reference dialog box appears.

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

    The Add Service Reference dialog box

    Service1 displays in the Services pane.

  5. 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. From the Toolbox, drag a TextBox control, a Label control, and a Button control onto the form.

    Adding controls to the form

  3. 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;
    }
    
      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
    
  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 displays "You entered:" and shows the text that you entered.

    The form showing the result

See also