Click here to Skip to main content
13,357,438 members (68,251 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


14 bookmarked
Posted 10 Dec 2012

Create a WCF Service using wsHttpBinding and use in a Windows Forms application

, 10 Dec 2012
Rate this:
Please Sign up or sign in to vote.
Create a WCF service using wsHttpBinding and use in a Windows Forms application.


In this article, I would like to create a WCF service application with wshttpbinding and use it in a Windows Forms application.

Creating a WCF Service Application

  1. Open Visual Studio, choose File => New => Project
  2. .

  3. Choose WCF Service Application and enter the name:
  4. Add a new class to the project for the custom validator:
  5. Choose class, enter the name, and click the Add button.
  6. Here is the sample code for the custom validator:
  7. using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.ServiceModel;
    using System.IdentityModel.Selectors;
    namespace TestService
        public class CustomValidator : UserNamePasswordValidator
            public override void Validate(string userName, string password)
                if (userName == null || password == null)
                    throw new ArgumentNullException();
                if (!(userName == "a" && password == "a"))
                    throw new FaultException("Password or name is wrong");
  8. Change the web config in system service model as below:
  9. <system.serviceModel>
          <serviceBehaviors >
            <behavior name="ServiceBehaviors"  >
              <!-- To avoid disclosing metadata information, 
              set the value below to false and remove the metadata endpoint above before deployment -->
              <serviceMetadata httpGetEnabled="True"/>
              <!-- To receive exception details in faults for debugging purposes, 
              set the value below to true.  Set to false before deployment 
              to avoid disclosing exception information -->
              <serviceDebug includeExceptionDetailInFaults="False" />
                <serviceCertificate findValue="localhost" x509FindType="FindBySubjectName"
                                 storeLocation="LocalMachine" storeName="My" />
                <userNameAuthentication userNamePasswordValidationMode="Custom"
                 customUserNamePasswordValidatorType="TestService.CustomValidator, TestService" />
              <security mode="Message">
                <message clientCredentialType="UserName"/>
          <service name="TestService.Service1" behaviorConfiguration="ServiceBehaviors" >
            <endpoint contract="TestService.IService1" binding="wsHttpBinding" />
            <!-- This endpoint does not use a secure binding and should be secured or removed before deployment -->
            <endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange"/>
        <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  10. When trying to run the service, it will show an error like below, so we need to configure the project:
  11. Right click, add the project, and choose Use IIS Express...

    Choose Yes

    Choose OK.

    Choose the project and at the properties window, set SSL Enabled to true.

    The service will be ready to use.

Use the service in a Windows Forms application

  1. Right click, add the solution, and choose Add New Project.
  2. Choose Windows Forms Application, enter the name, and click the OK button.
  3. Right click, add the project, and choose Add Service Reference.
  4. Enter the service address and click the OK button.
  5. Add controls to the form as in the below picture.
  6. Here is the sample code of the form. Add an event for button click:
  7. using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using WindowsFormsApplication1.ServiceReference1;
    using System.ServiceModel.Security;
    namespace WindowsFormsApplication1
        public partial class Form1 : Form
            public Form1()
            private void button1_Click(object sender, EventArgs e)
                    Service1Client c = new Service1Client();
                    c.ClientCredentials.UserName.UserName = textBox1.Text;
                    c.ClientCredentials.UserName.Password = textBox2.Text;
                    c.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = 
                catch (Exception ex)
  8. Change the solution properties to run two projects at the same time.
  9. Try to submit the data with a random username and password.
  10. Here is the sample if the username and the password is correct.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Indonesia Indonesia
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralGood Example Pin
zizou200015-Jun-15 8:32
memberzizou200015-Jun-15 8:32 
GeneralMy vote of 3 Pin
manshitu10-Oct-13 1:05
membermanshitu10-Oct-13 1:05 
QuestionI get the following error message Pin
bborac8-Aug-13 14:37
memberbborac8-Aug-13 14:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180111.1 | Last Updated 10 Dec 2012
Article Copyright 2012 by Luhuiya
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid