【发布时间】:2020-01-28 13:20:15
【问题描述】:
我为我的服务创建了一个 Windows 服务和一个安装项目。 在我的 Windows 服务中,我可以让我的客户行为数据认为:
Context.Parameters["dbname"]
但我想在我的服务中访问我的客户操作数据的价值,以便在我的项目中使用它。
有人知道如何在 c# 中做到这一点吗?
更新
在我的 ProjectInstaller 中:
public ProjectInstaller()
{
InitializeComponent();
}
public override void Install(IDictionary stateSaver)
{
base.Install(stateSaver);
string dbName = Context.Parameters["dbname"];
AppHelper.Save(dbName+" "+ Context.Parameters["targetdir"].ToString());
string xml = Context.Parameters["targetdir"].ToString() + "App.config";
XmlDocument document = new XmlDocument();
document.Load(xml);
XPathNavigator navigator = document.CreateNavigator();
XmlNamespaceManager ns = new XmlNamespaceManager(navigator.NameTable);
foreach (XPathNavigator nav in navigator.Select(@"/configuration.appSettings/add[@key='dbName']"))
{
nav.SetValue(dbName);
}
document.Save(xml);
}
我的windows服务的app.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="dbName" value="totodb"/>
</appSettings>
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v13.0" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>
当我尝试安装我的项目时出现错误,提示我的 app.config 不存在。
【问题讨论】:
-
我认为您可以将所需的内容写入应用程序配置并从服务中获取它们。例如stackoverflow.com/questions/9208442/…
-
@NDJ 我看到了示例,但是如何在我的 ProjectInstaller 中定位我的 app.config 文件?
-
@NDJ 我看到了示例,但是如何在我的 ProjectInstaller 中定位我的 app.config 文件?
-
这不正是示例在做什么吗? Context.Parameters["TARGETDIR"].ToString() + "UpdateReportService.exe.config";
-
@NDJ 问题是我的 app.config 不在安装路径中
标签: c# windows-services setup-project custom-action