以编程方式启动和停止WCF服务

以编程方式启动和停止WCF服务

本文介绍了以编程方式启动和停止WCF服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我已经创建了WCF服务DLL,并在控制台应用程序中托管了该服务.如何以编程方式按需启动和停止WCF服务?我希望所有配置信息都保留在App.config文件中,但我只想以编程方式启动和停止服务.

任何示例代码和帮助都将不胜感激.

谢谢...

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...

推荐答案

// 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>


这篇关于以编程方式启动和停止WCF服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 03:48