4

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");
Taran
  • 2,895
  • 25
  • 22
  • 1
    Why are you using app.config instead of the normal json configuration? – cl0ud Dec 05 '18 at 07:33
  • Why is Unity necessary? – mvermef Dec 06 '18 at 03:15
  • I am using Unity in Asp.Net core because embedded DI is too simple. – Karel Kral Dec 09 '18 at 19:47
  • Out of curiosity, what does unity do that the embedded DI can't that you need to do? Also, what is the specific NuGet package and version you are using to pull unity in to your project? – Scott Chamberlain Dec 09 '18 at 19:49
  • The reason I ask is you can use [Unity.Microsoft.DependencyInjection](https://www.nuget.org/packages/Unity.Microsoft.DependencyInjection/) which will allow you to use [`UnityConfigurationOptions`](https://github.com/unitycontainer/microsoft-dependency-injection/blob/master/src/UnityConfigurationOptions.cs) to set up with the normal JSON like any other .net Core configured object. – Scott Chamberlain Dec 09 '18 at 19:57

3 Answers3

1

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.

Ovy.Istrate
  • 474
  • 3
  • 15
  • See, by having a separate .config file or json file enables us to not to add reference to the implementation libraries. the asp.net project can only depend on Interfaces.csproj project to carry on functions with dependencies. But without configuration file, I need to add reference to the Implementation library as well. That may cause cyclic dependency problem while building. The another approach could be to manually copy those implementation dlls to "debug" directory of asp.net app in order to allow unity container to autoload dependencies from dlls from same directory. – Gokul E Jun 08 '21 at 18:23
0

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"/>
Omar Hamdan
  • 172
  • 1
  • 12
0

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;
    }
Taran
  • 2,895
  • 25
  • 22
  • 1
    You don't need your own DependencyResolver. The dotnet core has it's own "resolver" you can use to get your services explicitly, you can use a IServiceProvider instance. You can find here more info: https://learn.microsoft.com/en-us/dotnet/api/system.iserviceprovider?view=netframework-4.7.2 As for constructor injection, it's all set up after you've registered the dependencies in the ConfigureServices method. – Ovy.Istrate Dec 24 '18 at 16:00
  • I have class names coming dynamically, which i need to resolve. – Taran Jan 02 '19 at 18:31