我已经创建了一个dll,它有一些功能,可以在项目之间共享。不过,我可能需要从引用它的项目中调整dll的设置。在另一个完全不同的解决方案中公开配置设置以供其他项目使用的最佳方式是什么?创建可以通过使用应用程序进行配置的实用程序dll的最佳方法是什么?
方面,我曾尝试:
的appSettings导入
的applicationSettings导入
当前设置,消费解决方案设定值被忽略/不设置,为什么呢?
DLL设置
public class ApplicationSettingsRetriever : IApplicationSettingsRetriever
{
public string LogEnvironmentSetting => ConfigurationManager.AppSettings.Get("LogEnvironment");
}
消费者解决方案的app.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-9.0.0.0" newVersion="9.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<appSettings>
<add key="LogEnvironment" value="local"/>
</appSettings>
</configuration>
如果您想获得最大的灵活性,您应该接受具有属于您的“配置”属性的注入接口。然后,客户端可以实现他们想要的 - 数据库,配置文件,无论如何。 – Crowcoder
为客户端和dll使用案例添加了设置 – user1732364