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


Discover more from Dhananjay Kumar

Subscribe to get the latest posts sent to your email.

Published by Dhananjay Kumar

Dhananjay Kumar is founder of NomadCoder and ng-India

8 thoughts on “Hosting WCF service in Console Application

  1. 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

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

Leave a comment

Discover more from Dhananjay Kumar

Subscribe now to keep reading and get access to the full archive.

Continue reading