Hosting WCF service in Console Application

This video will explain , how to host a WCF Service in a Console Application

For your reference soure code is as below ,

IService1.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Runtime.Serialization;

using System.ServiceModel;

using System.ServiceModel.Web;

using System.Text;

 

namespace MultipleEndpoints

{

 

    [ServiceContract]

    public interface IService1

    {

 

        [OperationContract]

        string GreetingMessage(string Name);

    }

 

 

}

 

 

 
Service1.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Runtime.Serialization;

using System.ServiceModel;

using System.ServiceModel.Web;

using System.Text;

 

namespace MultipleEndpoints

{

 

    public class Service1 : IService1

    {

        public string GreetingMessage(string name)

        {

            return "Welcome to WCF " + name;

        }

 

    }

}

 

Hosting Console Application

Program.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.ServiceModel;

using MultipleEndpoints;

 

namespace ConsoleApplication1

{

    class Program

    {

        static void Main(string[] args)

        {

 

            ServiceHost host = new ServiceHost(typeof(Service1));

            host.Open();

            Console.Write("Service is up and running");

            Console.ReadKey();

            host.Close();

 

        }

    }

}

 

 

App.Config

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

  <system.serviceModel>

    <behaviors>

      <serviceBehaviors>

        <behavior name ="Mg">

          <!-- 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"/>

        </behavior>

      </serviceBehaviors>

    </behaviors>

    <!--<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />-->

    <services >

      <service name ="MultipleEndpoints.Service1" behaviorConfiguration ="Mg">

        <endpoint address ="/MyAddress" binding ="basicHttpBinding" contract ="MultipleEndpoints.IService1" />

        <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />

        <host>

          <baseAddresses >

            <add baseAddress ="http://localhost:8181/Service1.svc"/>

          </baseAddresses>

        </host>

      </service >

    </services>

  </system.serviceModel>

</configuration>

 


Thanks for watching this video. I hope this was useful. Happy Coding

8 responses to “Hosting WCF service in Console Application”

  1. […] To see the video tutorial of hosting WCF Service in Console Application Click Here […]

  2. If I had a nickel for every time I came to dhananjaykumar.net… Superb article!

  3. sir
    ur video is very good for self hosting but i want iis host have created one services examples of add,mul methods my of the client consume that services of add methods using tcp protocols and another client consume mul methods http protocols can u give me examples of this type of video sir

  4. somayeh khodadadi

    how can i host consol application for many wcf service?

  5. Thanks so much for this! Great tutorial, simple and easy to follow, works perfectly.

  6. it’s wery good thanks

  7. good one! thanks.

  8. how to debug a self hosted service?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a website or blog at WordPress.com