Created
December 27, 2011 16:50
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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