简体   繁体   中英

Unity framework in .net core

I am new to .net core. Need help setting up unity framework. Here is what i tried.

I added reference to System.Configuration.ConfigurationManager .net standard V2.0

Then created 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>

In class i try reading the configuration , but getting NULL.

UnityConfigurationSection section =


(UnityConfigurationSection)ConfigurationManager.GetSection("unity");

If you mean the Unity DI Container Framework, you don't need to set it up because dotnet core comes with it's own IoC DI container. Also the dotnet core uses appSettings.json config files. In Startup.cs there should be this method:

public void ConfigureServices(IServiceCollection services) 
{

}

And you can configure your dependencies using the services object, like this:

services.AddSingleton<IContract, Contract>();

There are other options on how you can configure your dependencies, I've just presented the Singleton one, but you can go from here.

The easiest way to check this is to start a new project:

dotnet new mvc -n testProj

And check the Startup.cs file, add an interface, an implemenatation to it, then register it into the IServiceCollection instance.

in app.config can you try this

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

instead of this line

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

Here is how i finally implemented. In startup class i configured the dependencies

 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>();
    }

Here is code to resolve it. using DependencyFactory.Resolve();

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;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM