我在consol应用程序中托管了一个WCF服务(也充当Windows服务安装程序),请在此处查看更多信息:http://msdn.microsoft.com/en-us/library/ms733069.aspx
这是consol应用程序中的类的样子:
public class MyAppWindowsService : ServiceBase
{
public ServiceHost _MyAppClientServiceHost = null;
public ServiceHost _MyAppIntegrationServiceHost = null;
public ServiceHost _MyAppserviceHost = null;
public MyAppWindowsService()
{
// Name the Windows Service
ServiceName = "MyApp Service";
}
public static void Main()
{
ServiceBase.Run(new MyAppWindowsService());
}
private void StopService(ServiceHost serviceHost)
{
if (serviceHost != null)
{
serviceHost.Close();
serviceHost = null;
}
}
private ServiceHost StartService(Type serviceType)
{
ServiceHost serviceHost = null;
// Create a ServiceHost for the CalculatorService type and
// provide the base address.
serviceHost = new ServiceHost(serviceType);
// Open the ServiceHostBase to create listeners and start
// listening for messages.
serviceHost.Open();
return serviceHost;
}
private void StartServices()
{
StopService(_MyAppClientServiceHost);
StopService(_MyAppIntegrationServiceHost);
StopService(_MyAppServiceHost);
_MyAppClientServiceHost = StartService(typeof(MyApp.ServiceImplementation.MyAppClientService));
_MyAppIntegrationServiceHost = StartService(typeof(MyApp.ServiceImplementation.MyAppIntegration));
_MyAppServiceHost = StartService(typeof(MyApp.ServiceImplementation.HL7Service));
}
private void StopServices()
{
StopService(_MyAppClientServiceHost);
StopService(_MyAppIntegrationServiceHost);
StopService(_MyAppHl7ServiceHost);
}
// Start the Windows service.
protected override void OnStart(string[] args)
{
StartServices();
}
protected override void OnStop()
{
StopServices();
}
}
这是为了在Windows服务中运行,我该怎么做才能在调试模式下(在开发期间)将其作为常规自主机运行?或者我是否真的必须启动一个特殊项目才能在运行时调试此servuce?
修改
我决定使用现有的Windows服务项目,但将主要内容更改为:
public static void Main()
{
if (Debugger.IsAttached)
{
Console.WriteLine("--- MyApp Services ---");
Console.WriteLine("Starting services...");
Instance.StartServices();
Console.WriteLine("--Finished--");
Console.WriteLine("Press any key to exit");
Console.ReadKey();
Instance.StopServices();
}
else
ServiceBase.Run(new MyAppWindowsService());
}
答案 0 :(得分:6)
这就是我的工作
解决方案A
InstallUtil
安装Windows服务sc start
或sc stop
解决方案B
在Debugger.Break方法的第一行进行OnStart调用。
解决方案C
添加一个与您的服务完成相同工作的临时单独控制台应用程序。