我有一个.net服务在安装时刚刚开始失败(意味着它不会启动)。
注意
:这是64位服务
我最近所做的更改是检查.net框架版本,如Init函数调用中所示。
public Service1()
{
InitializeComponent();
CanHandleSessionChangeEvent = true; //can't be called after service start !
Init();
}
我在这里进行检查以查找.net框架版本:
private void Init()
{
try{
string version = GetVersion();
// check version here
}catch(Exception ex){
// log the error
}
}
GetVersion返回运行服务的系统的.net版本:
注意:此代码来自MS网站
private string GetVersion()
{
const string subkey = @"SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\";
string retval = "0.0";
using (RegistryKey ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey(subkey))
{
if (ndpKey != null)
{
object obj = ndpKey.GetValue("Release");
if( obj != null )
{
retval = CheckFor45PlusVersion((int)obj);
}
}
}
Console.WriteLine(retval);
return retval;
}
这个函数只是做一些数字检查
注意:这来自一个MS网站
private string CheckFor45PlusVersion(int releaseKey)
{
if (releaseKey >= 461808)
return "4.7.2";
if (releaseKey >= 461308)
return "4.7.1";
if (releaseKey >= 460798)
return "4.7";
if (releaseKey >= 394802)
return "4.6.2";
if (releaseKey >= 394254)
return "4.6.1";
if (releaseKey >= 393295)
return "4.6";
if (releaseKey >= 379893)
return "4.5.2";
if (releaseKey >= 378675)
return "4.5.1";
if (releaseKey >= 378389)
return "4.5";
// This code should never execute. A non-null release key should mean that 4.5 or later is installed.
return "4.0";
}
思考要点:
using (RegistryKey ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32)
事件查看器错误
这是我从EventViewer得到的唯一错误
Product: Service1 -- Error 1923. Service 'Service1' (Service1) could not be installed. Verify that you have sufficient privileges to install system services.