Skip to content

Instantly share code, notes, and snippets.

@JoanComasFdz
Created December 27, 2011 16:50
Show Gist options
  • Save JoanComasFdz/1524327 to your computer and use it in GitHub Desktop.
Save JoanComasFdz/1524327 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 <YORU_SERVICE_NAMESPACE>;
namespace <YORU_APP_NAMESPACE>
{
class Program
{
static void Main(string[] args)
{
Uri base_address = new Uri("http://localhost:8080/<YOUR_DESIRED_SERVICE_NAME>");
ServiceHost host = null;
try
{
// Create the server
host = new ServiceHost(typeof(<YOUR_SERVICE>), base_address);
// Add a way to connect
host.AddServiceEndpoint(typeof(<YOUR_SERVICE_INTERFACE>), new WSDualHttpBinding(), base_address);
// Start the server
host.Open();
// Notify it
Console.WriteLine("The service is ready at {0}", base_address);
// Allow close the server
Console.WriteLine("Press <Enter> to stop the service.");
Console.ReadLine();
// Close it
host.Close();
}
catch (Exception ex)
{
// Ouch an error occurred
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(string.Format("Host error:\r\n{0}:\r\n{1}", ex.GetType(), ex.Message));
Console.ReadLine();
}finally
{
// Correct memory clean
if(host != null)
((IDisposable)host).Dispose();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment