gpt4 book ai didi

c# - 如何从配置文件加载统一设置?

转载 作者:太空宇宙 更新时间:2023-11-03 22:42:54 27 4
gpt4 key购买 nike

我有一个 unity 配置文件 (App.config) 如下:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Unity.Configuration"/>
</configSections>
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
<alias alias="IProductService" type="UnityExample.Service.IProductService, UnityExample.Service" />
<containers>
<container name="Service">
<register type="IProductService" mapTo="ProductService"/>
</container>
</containers>
</unity>
</configuration>

现在我想从文件加载配置。

var container = new UnityContainer().LoadConfiguration("Service");

但我收到以下错误:

[ArgumentNullException: Value cannot be null. Parameter name: section] Microsoft.Practices.Unity.Configuration.UnityContainerExtensions.LoadConfiguration(IUnityContainer container, UnityConfigurationSection section, String containerName)

最佳答案

我能够让它发挥作用。我使用您正在使用的相同命名空间创建了一个演示控制台应用程序,如下所示:

Assembly information

安装的 Nuget 包:

<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Unity" version="5.8.6" targetFramework="net461" />
<package id="Unity.Abstractions" version="3.3.0" targetFramework="net461" />
<package id="Unity.Configuration" version="5.2.3" targetFramework="net461" />
</packages>

应用程序配置:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Unity.Configuration"/>
</configSections>

<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
<assembly name="UnityExample" />
<namespace name="UnityExample.Service" />

<containers>
<container name="Service">
<register type="IProductService" mapTo="ProductService"/>
</container>
</containers>
</unity>

<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Unity.Configuration" publicKeyToken="6d32ff45e0ccc69f" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>

代码:

namespace UnityExample.Service
{
public interface IProductService
{

}

public class ProductService : IProductService
{
}
}

主要内容:

using System.Configuration;
using Microsoft.Practices.Unity.Configuration;

using Unity;
using UnityExample.Service;

namespace UnityExample
{
class Program
{
static void Main(string[] args)
{
var container = new UnityContainer();
UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
section.Configure(container, "Service");

IProductService service = container.Resolve<IProductService>();
}
}
}

关于c# - 如何从配置文件加载统一设置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51399322/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com