Skip to content

Instantly share code, notes, and snippets.

@JoanComasFdz
Created December 28, 2011 08:50
Show Gist options
  • Save JoanComasFdz/1527159 to your computer and use it in GitHub Desktop.
Save JoanComasFdz/1527159 to your computer and use it in GitHub Desktop.
Host wcf service in console app basic code (using dual tcp to allow callbacks)
using System;
using System.ServiceModel;
using <YOUR_SERVICE_NAMESPACE>;
namespace Host_Service_with_callbacks_via_tcp
{
class Program
{
static void Main(string[] args)
{
ServiceHost host = null;
try
{
// Create server, it will use the settings
// in App.config file.
host = new ServiceHost(typeof(Service1));
// Open it
host.Open();
// Wait to exit
Console.WriteLine(string.Format("Service running @ {0}", host.Description.Endpoints[0].Address));
Console.WriteLine("Press [ENTER] to exit.");
Console.ReadLine();
}
catch (Exception ex)
{
// Oups
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(string.Format("/!\\ Error:\r\n{0}:\r\n{1}", ex.GetType(), ex.Message));
Console.ReadLine();
}
finally
{
// Clean memory
if (host != null)
((IDisposable)host).Dispose();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment