Click here to Skip to main content
Click here to Skip to main content
Go to top

How to create a WCF self hosted application

, 7 Jul 2014
Rate this:
Please Sign up or sign in to vote.
This article will show you how to create a WCF Service self hosted application.

Introduction

This article will show you how to create a WCF Service self hosted application. I have used Visual Studio 2012, but it should be very similar approach with 2010. I also use WPF (you should use that too, but you can probably do it with WF if you want). Hopefully this will give you a quick start.

Background

If you want to go further, it may be useful to know how WCF work, and it exists lots of articles about it. For this article, it's not necessary.

To create a service, you need something that defines the service, and something that implements it. The definition (also known as contract) is a interface that lists the methods the implementation (a class) must implement. These methods will be available for the clients.

How to do it

Make sure firewall isn't blocking the application. For a quick testing-solution, you may want to just disable it. Otherwise, allow TCP-connections on the port you're planning to use (or 8888 as I use).

First create a new blank application. I named mine "WcfSelfhostedService".

Then, add reference to System.ServiceModel.

Now, create a new file with name "IService.cs". This file shall contain one interface (the contract).

using System;
using System.ServiceModel;
namespace WcfSelfhostedService
{
    [ServiceContract]
    public interface IService
    {
        [OperationContract]
        String Hello(String name);
    }
}

Now, let's implement the definition. Create a file with name "Service.cs".

using System;
namespace WcfSelfhostedService
{
    class Service : IService
    {
        public String Hello(string name)
        {
            return String.Format("Hello {0}", name);
        }
    }
}

To start the service, some configuration of it must exist. Here, everything is done from code. I just put this in MainWindow.xaml

using System;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Windows;
namespace WcfSelfhostedService
{
    /// <summary>
    ///     Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
 
            var uri = new Uri("http://localhost:8888/WcfSelfhostedService");
 
            var host = new ServiceHost(typeof (Service), uri);
 
            //Enable realiable sessions
            var wsHttpBinding = new WSHttpBinding
                {
                    ReliableSession = {Enabled = true, Ordered = true}
                };
            
            //Disable security. Is's ok for now.
            wsHttpBinding.Security.Mode = SecurityMode.None;
            host.AddServiceEndpoint(typeof (IService), wsHttpBinding, "service");
            
            //Enable metadata publishing
            var smb = new ServiceMetadataBehavior
                {
                    HttpGetEnabled = true,
                    MetadataExporter = {PolicyVersion = PolicyVersion.Policy15}
                };
 
            //Generate policy information with the metadata that conforms to WS-Policy1.5
            host.Description.Behaviors.Add(smb);
 
            //Open host
            host.Open();
        }
    }
}

With this code, you're actually ready to start the service. Do it!

Time to create the client. Create a new console application.

You need to add Service Reference. Right click on References and choose "Add Service Reference".

Search for the URI that was specified earlier (http://localhost:8888/WcfSelfhostedService). If your firewall isn't causing trouble for you, the service should be visible.

In Program.cs, add some code to verify that the service is working.

using System;
using WcfSelfhostedServiceClient.SelfhostedService;
namespace WcfSelfhostedServiceClient
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            var client = new ServiceClient();
            string response = client.Hello("World");
            Console.WriteLine(response);
            //Just to make you can see the response
            Console.ReadLine();
        }
    }
}

Start the client, and you should see "Hello World". Yes, it's that easy!

History

  • 7.7.2013: First release.

License

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

Share

About the Author

Kim Syversen
Engineer
Norway Norway
Engineer-student.

Comments and Discussions

 
GeneralMy vote of 2 Pinprofessionaljrooks10178-Jul-14 7:31 
GeneralMy vote of 5 PinprofessionalAmir Mohammad Nasrollahi27-Jul-13 22:15 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 7 Jul 2014
Article Copyright 2013 by Kim Syversen
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid