我目前正在为可以在控制台中运行的服务编写一些引导代码.它本质上归结为调用OnStart()方法而不是使用ServiceBase来启动和停止服务(因为如果它没有作为服务安装并且使调试成为一场噩梦,它就不会运行应用程序).
现在我使用Debugger.IsAttached来确定我是否应该使用ServiceBase.Run或[service] .OnStart,但我知道这不是最好的主意,因为有些时候最终用户想要在控制台中运行服务(看看输出等实时).
关于如何确定Windows服务控制器是否启动"我",或者用户是否在控制台中启动"我"的任何想法?Apparantly Environment.IsUserInteractive不是答案.我想过使用命令行args,但这看起来很"脏".
我总是可以看到围绕ServiceBase.Run的try-catch语句,但这看起来很脏.编辑:尝试捕获不起作用.
我有一个解决方案:把它放在这里给所有其他感兴趣的堆叠器:
public void Run()
{
if (Debugger.IsAttached || Environment.GetCommandLineArgs().Contains<string>("-console"))
{
RunAllServices();
}
else
{
try
{
string temp = Console.Title;
ServiceBase.Run((ServiceBase[])ComponentsToRun);
}
catch
{
RunAllServices();
}
}
} // void Run
private void RunAllServices()
{
foreach (ConsoleService component in ComponentsToRun)
{
component.Start();
}
WaitForCTRLC();
foreach (ConsoleService component in ComponentsToRun)
{
component.Stop();
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:在StackOverflow上有另一个问题,那个人有环境问题.CurrentDirectory是"C:\ Windows\System32"看起来可能是答案.我今天要考试.
已经问过这个问题的各种风格,但我还没有找到正确的答案.
假设我在文件服务器上有一个.jpg图像,我需要获得它的高度和宽度.我怎么在asp.net中做到这一点?
我已经看到一些答案,建议做这样的事情:
System.Drawing.Image image=System.Drawing.Image.FromFile(PicturePath);
int ActualWidth=image.Width;
int ActualHeight=image.Height;
image.Dispose();
Run Code Online (Sandbox Code Playgroud)
这可以正常工作,除了不支持在ASP.NET服务中使用System.Drawing命名空间中的类.
那么,如何在ASP.net中获得图像的实际高度和宽度?