C# net内核中的统一框架

C# net内核中的统一框架,c#,.net-core,asp.net-core-mvc,unity-container,asp.net-core-webapi,C#,.net Core,Asp.net Core Mvc,Unity Container,Asp.net Core Webapi,我不熟悉.NETCore。需要帮助建立统一框架。这是我试过的 我添加了对System.Configuration.ConfigurationManager.net标准V2.0的引用 然后创建app.config <?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <!--In older version, Microsoft.Practice

我不熟悉.NETCore。需要帮助建立统一框架。这是我试过的

我添加了对System.Configuration.ConfigurationManager.net标准V2.0的引用

然后创建app.config

    <?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <!--In older version, Microsoft.Practices.Unity.Configuration.dll is part of older version (around 3.5.1404). In newer version,
    Microsoft.Practices.Unity.Configuration.UnityConfigurationSection class is moved to Unity.Configuration.dll.-->
    <!--<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/>-->
    <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Unity.Configuration"/>
  </configSections>
  <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
    <!--Old syntax-->
    <!--<typeAliases>
      <typeAlias alias="IDBAccess" type="Interfaces.IDataProvider, Interfaces" />
      <typeAlias alias="SQLDataAccess" type="SQLDataProvider.DataProvider, SQLDataProvider" />
    </typeAliases>-->
    <!--New syntax supported in newer versions. So if above syntax does not work then try below one-->
    <alias alias="IDBAccess" type="Interfaces.IDataProvider, Interfaces" />
    <alias alias="SQLDataAccess" type="SQLDataProvider.DataProvider, SQLDataProvider" />
    <alias alias="OracleDataAccess" type="OracleDataProvider.DataProvider, OracleDataProvider" />
    <containers>
      <container name="DataAccessProvider">
        <register type="IDBAccess" mapTo="SQLDataAccess"/>
        <register type="IDBAccess" mapTo="SQLDataAccess" name="SQLDataAccess" />
        <register type="IDBAccess" mapTo="OracleDataAccess" name="OracleDataAccess" />
      </container>
    </containers>
  </unity>
</configuration>

在app.config中,你能试试这个吗

<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection"/>

而不是这条线

<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Unity.Configuration"/>

如果您指的是Unity DI容器框架,您不需要设置它,因为dotnet core自带自己的IoC DI容器。dotnet核心还使用appSettings.json配置文件。 在Startup.cs中应该有以下方法:

public void ConfigureServices(IServiceCollection services) 
{

}
您可以使用服务对象配置依赖项,如下所示:

services.AddSingleton<IContract, Contract>();

然后检查Startup.cs文件,向其中添加接口和实现,然后将其注册到IServiceCollection实例中。

以下是我最终实现的方式。 在启动类中,我配置了依赖项

 public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
        services.AddScoped<IRepositoryFactory, RepositoryFactory>();
        services.AddScoped<IMapperFactory, MapperFactory>();
        services.AddScoped<ITestService, testtService>();


       // services.AddScoped<IMapperFactory, MapperFactory>();
    }
public void配置服务(IServiceCollection服务)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.addScope();
services.addScope();
services.addScope();
//services.addScope();
}
下面是解决这个问题的代码。使用DependencyFactory.Resolve()

public DependencyFactory(IServiceCollection服务)
{
_集装箱=服务;
}
公共静态T解析()
{
T ret=默认值(T);
var provider=_container.BuildServiceProvider();
if(provider.GetService()!=null)
{
ret=(T)provider.GetService();
}
返回ret;
}

为什么要使用app.config而不是普通的json配置?为什么需要Unity?我在Asp.Net core中使用Unity是因为嵌入式DI太简单了。出于好奇,Unity做了什么,嵌入式DI做不到,而您需要做什么?另外,您用于将unity引入项目的特定NuGet包和版本是什么?我问的原因是,您可以使用它来设置普通JSON,就像任何其他.net Core配置的对象一样。您不需要自己的DependencyResolver。dotnet core有自己的“解析器”,您可以使用它显式地获取服务,也可以使用IServiceProvider实例。您可以在这里找到更多信息:对于构造函数注入,在ConfigureServices方法中注册依赖项之后,就可以进行设置了。我需要动态生成类名,这需要解决。请参阅,通过使用单独的.config文件或json文件,我们可以不添加对实现库的引用。asp.net项目只能依赖Interfaces.csproj项目来执行具有依赖关系的功能。但是如果没有配置文件,我还需要添加对实现库的引用。这可能会在构建时导致循环依赖性问题。另一种方法是手动将这些实现DLL复制到asp.net应用程序的“debug”目录,以便unity容器能够从同一目录中的DLL自动加载依赖项。
dotnet new mvc -n testProj
 public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
        services.AddScoped<IRepositoryFactory, RepositoryFactory>();
        services.AddScoped<IMapperFactory, MapperFactory>();
        services.AddScoped<ITestService, testtService>();


       // services.AddScoped<IMapperFactory, MapperFactory>();
    }
public DependencyFactory(IServiceCollection services)
    {
        _container=services;
    }
 public static T Resolve<T>()
    {
        T ret = default(T);
        var provider = _container.BuildServiceProvider();

        if (provider.GetService<T>() !=null)
        {
            ret = (T)provider.GetService<T>();
        }

        return ret;
    }