5

I have the following code :

using System.Web.Http;
using Coben.DataAccess.Tests.Constructiv.CentralDB.Contracts.ServiceContracts;
using Coben.Person.DataAccess;
using Constructiv.CentralDB.Contracts.ServiceContracts;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;
using Unity.WebApi;

namespace CoBen.UI
{
    public static class UnityConfig
    {
        public static void RegisterComponents()
        {
            var container = new UnityContainer();

            container.RegisterInstance<IPersonService>(new PersonServiceMock().Mock);
            container.RegisterType<PersonRepository>();
            GlobalConfiguration.Configuration.DependencyResolver = new UnityDependencyResolver(container);
        }
    }
}

This configures my dependency injection. But I would like to do the same from my web.config.

i have the following :

<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
<alias alias="IPersonService" type="Constructiv.CentralDB.Contracts.ServiceContracts.IPersonSercice, Constructiv.CentralDB.Contracts.ServiceContracts" />
<namespace name="Coben.DataAccess.Tests.Constructiv.CentralDB.Contracts.DataContracts.Persons" />
<assembly name="Coben.DataAccess.Tests.Constructiv.CentralDB.Contracts.DataContracts.Persons" />
<container>
  <register type="IPersonService" name="special" mapTo="PersonServiceMock().Mock" />    
</container>

But when i do it like this I get an error message :

The type name or alias IPersonService could not be resolved. Please check your configuration file and verify this type name.

What am I doing wrong or what am I not seeing ?

Update: I know get the following error : The type name or alias Constructiv.CentralDB.Contracts.ServiceContracts.IPersonService, Constructiv.CentralDB.Contracts.ServiceContracts could not be resolved. Please check your configuration file and verify this type name.

My config looks as follows:

  <configSections>
    <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration" />
</configSections>
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
<container>        
    <register type="Constructiv.CentralDB.Contracts.ServiceContracts.IPersonService, Constructiv.CentralDB.Contracts.ServiceContracts"                  
        mapTo="Coben.DataAccess.Tests.Constructiv.CentralDB.Contracts.DataContracts.Persons.PersonServiceMock().Mock, Coben.DataAccess.Tests.Constructiv.CentralDB.Contracts.DataContracts.Persons" />    
</container>

1 Answer 1

5

You need to configure your instance of the UnityContainer with the settings from the web.config.

UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
section.Configure(_container);
Sign up to request clarification or add additional context in comments.

Comments

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.