Rediger

Del via


Walkthrough: Create a WCF service in .NET Framework Windows Forms

This walkthrough demonstrates how to create a 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

Visual Studio with the Windows Communication Foundation component installed. To install it:

  1. Open the Visual Studio Installer app, or select Tools > Get Tools and Features from the Visual Studio menu.
  2. In Visual Studio Installer, choose Modify next to the version of Visual Studio you want to modify.
  3. Select the Individual components tab, and then choose Windows Communication Foundation under Development activities.
  4. Select Modify.

For more information, see Modify Visual Studio workloads, components, and language packs.

Create a WCF service

To create a WCF service in Visual Studio, follow these steps:

  1. In Visual Studio, select File > New > Project from the menu.

  2. On the Create a new project page, enter wcf service library in the search box. Select either the C# or Visual Basic template for WCF Service Library, and then select Next.

    Screenshot that shows how to create a 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. For more information, see Prerequisites

  3. On the Configure your new project page, verify the settings, and then select Create.

    This step creates a working WCF service that you can test and access. The following steps demonstrate how to modify the default method to use a different data type. In a real application, you'd also add your own functions to the service.

  4. In Solution Explorer, double-click IService1.cs or IService1.vb in your WCF service library project, depending on which type of project you created.

    Screenshot that shows the IService1 file in Solution Explorer.

  5. Find the following line:

    [OperationContract]
    string GetData(int value);
    

  1. Change the type for the value parameter to string.

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

    [OperationContract]
    string GetData(string value);
    

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

    Screenshot that shows the Service1 file in Solution Explorer.

  2. In the editor, find the following line:

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

  1. Change the type for the value parameter to string, as follows:

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

Test the WCF service

To test the WCF service you created, follow these steps:

  1. Press F5 to run the service.

    The WCF Test Client form appears and loads the service.

  2. Under IService1, double-click the GetData() method.

    The GetData tab appears in the WCF Test Client form.

    Screenshot that shows the GetData method in the WCF Test Client form.

  3. In the Request box of the GetData tab, select the Value field and enter Hello.

    Screenshot that shows the Value field in the GetData tab.

  4. Select the Invoke button. If a Security Warning dialog box appears, select OK.

    The result displays in the Response box.

    Screenshot that shows the result displayed in the Response box in the GetData tab.

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

Access the WCF service

After you create and test the WCF service, you can reference it from a project and use it to build a client application.

Reference the WCF service

To reference the WCF service from a project, follow these steps:

  1. Select File > Add > New Project.

  2. In the Add a New Project window, choose either a C# or Visual Basic Windows Forms App (.NET Framework) project. Select Next, and then Create to create the project.

  3. In Solution Explorer, right-click the project node of the new project, and select Add > Service Reference.

    The Add Service Reference dialog box appears.

  4. Select Discover.

    Screenshot that shows the Add Service Reference dialog box.

    Service1 displays in the Services pane.

  5. Select OK to add the service reference.

Build a client application

To use a WCF service reference to build a client application, follow these steps.

  1. In Solution Explorer, double-click Form1.cs or Form1.vb in the Windows Forms app.

    The form opens in Windows Forms Designer.

  2. Open the Toolbox by selecting View > Toolbox from the menu, or press Ctrl+Alt+X from the keyboard.

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

    Screenshot that shows how to add controls to the client application form.

  4. Double-click the Button control, 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 select Set as StartUp Project.

  6. Press F5 to run the project. Enter some text and select the button. The label displays You entered:, followed by your text.

    Screenshot that shows the result displayed in the running client application form.