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
If I had a nickel for every time I came to dhananjaykumar.net… Superb article!
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
how can i host consol application for many wcf service?
Thanks so much for this! Great tutorial, simple and easy to follow, works perfectly.
it’s wery good thanks
good one! thanks.
how to debug a self hosted service?