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");
5
  • 1
    Why are you using app.config instead of the normal json configuration? Commented Dec 5, 2018 at 7:33
  • Why is Unity necessary? Commented Dec 6, 2018 at 3:15
  • I am using Unity in Asp.Net core because embedded DI is too simple. Commented Dec 9, 2018 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? Commented Dec 9, 2018 at 19:49
  • The reason I ask is you can use Unity.Microsoft.DependencyInjection which will allow you to use UnityConfigurationOptions to set up with the normal JSON like any other .net Core configured object. Commented Dec 9, 2018 at 19:57

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.
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"/>

Comments

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

2 Comments

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: learn.microsoft.com/en-us/dotnet/api/… As for constructor injection, it's all set up after you've registered the dependencies in the ConfigureServices method.
I have class names coming dynamically, which i need to resolve.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.