Click here to Skip to main content
16,021,041 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi ,

I have created a WCF service DLL and I am hosting this service inside a console app. How do I programatically start and stop the WCF service on demand? I want all the configuration information to remain in the App.config file but I just want to start and stop the service programatically.

Any sample code and help is much appreciated.

Thanks...
Posted

1 solution

from Microsoft... http://msdn.microsoft.com/en-us/library/ms731758.aspx[^]
C#
// Create the ServiceHost.

using (ServiceHost host = new ServiceHost(typeof(HelloWorldService), baseAddress))
{
    // Enable metadata publishing.
    ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
    smb.HttpGetEnabled = true;
    smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
    host.Description.Behaviors.Add(smb);

    // Open the ServiceHost to start listening for messages. Since
    // no endpoints are explicitly configured, the runtime will create
    // one endpoint per base address for each service contract implemented
    // by the service.
    host.Open();

    Console.WriteLine("The service is ready at {0}", baseAddress);
    Console.WriteLine("Press <enter> to stop the service.");
    Console.ReadLine();

    // Close the ServiceHost.
    host.Close();
}
</enter>
 
Share this answer
 
Comments
Hellraiser123 15-May-12 12:10pm    
That example doesn't have an app.config file. It simply creates the service in a console app and uses default endpoint. My requirement is to have a app.config file with all the info present in a default app.config file and still be able to on demand start and stop the service programatically.
[no name] 15-May-12 12:49pm    
sorry but there is no relationship between having or not the app config and starting/stopping the service. the host.Open() starts it, the host.Close() shuts it down. How you set the service up either from app.config or manually from code is not relevant to that.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900