R
rraagg
Guest
Hi all,
Trying to use unity as an IOC want to inject an implementation MasterCard in for an interface ICreditCard. For some reason I keep getting an error (see below). Ive followed the MSDN instructions line by line. What am I doing wrong :-(
The app is very simple here is the code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;
namespace myUnityIOCContainerExample
{
class Program
{
static void Main(string[] args)
{
//Example: Using Unity as the IOC container
var container = new UnityContainer();
container.LoadConfiguration();
var shopper = container.Resolve<Shopper>();
shopper.Charge();
Console.ReadLine();
}
public class VisaCard : ICreditCard
{
public string Charge()
{
return "Swiping the VisaCard";
}
}
public class MasterCard : ICreditCard
{
public string Charge()
{
return "Swiping the masterCard";
}
}
//Main Class that is dependent on something
public class Shopper
{
private readonly ICreditCard creditCard;
//Constructor Injector: i.e takes in the object it is dependant on via the constructor
public Shopper(ICreditCard creditCard)
{
this.creditCard = creditCard;
}
public void Charge()
{
var chargeMessage = creditCard.Charge();
Console.WriteLine(chargeMessage);
}
}
//The interface for the objects that we depend on
public interface ICreditCard
{
string Charge();
}
}
}
Here is the default app.config file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/>
</configSections>
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity
<alias alias="ICreditCard" type="myUnityIOCContainerExample.ICreditCard, myUnityIOCContainerExample" />
<namespace name="myUnityIOCContainerExample" />
<assembly name="myUnityIOCContainerExample" />
<container>
<register type="ICreditCard" name="special" mapTo="MasterCard" />
</container>
</unity>
</configuration>
And here is the error that happens on the .loadConfiguration line:
System.InvalidOperationException was unhandled
Message=The type name or alias ICreditCard could not be resolved. Please check your configuration file and verify this type name.
Source=Microsoft.Practices.Unity.Configuration
StackTrace:
at Microsoft.Practices.Unity.Configuration.ConfigurationHelpers.TypeResolverImpl.ResolveType(String typeNameOrAlias, Boolean throwIfResolveFails) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity.Configuration\Src\ConfigurationHelpers\TypeResolverImpl.cs:line 110
at Microsoft.Practices.Unity.Configuration.ConfigurationHelpers.TypeResolver.ResolveType(String typeNameOrAlias) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity.Configuration\Src\ConfigurationHelpers\TypeResolver.cs:line 47
at Microsoft.Practices.Unity.Configuration.RegisterElement.GetRegisteringType() in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity.Configuration\Src\RegisterElement.cs:line 121
at Microsoft.Practices.Unity.Configuration.RegisterElement.ConfigureContainer(IUnityContainer container) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity.Configuration\Src\RegisterElement.cs:line 88
at Microsoft.Practices.Unity.Configuration.ContainerConfiguringElement.ConfigureContainerInternal(IUnityContainer container) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity.Configuration\Src\ContainerConfiguringElement.cs:line 43
at Microsoft.Practices.Unity.Configuration.ContainerElement.<>c__DisplayClass1.<ConfigureContainer>b__0(ContainerConfiguringElement element) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity.Configuration\Src\ContainerElement.cs:line 114
at Microsoft.Practices.ObjectBuilder2.EnumerableExtensions.ForEach[TItem](IEnumerable`1 sequence, Action`1 action) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity\Src\ObjectBuilder\Utility\EnumerableExtensions.cs:line 36
at Microsoft.Practices.Unity.Configuration.ContainerElement.ConfigureContainer(IUnityContainer container) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity.Configuration\Src\ContainerElement.cs:line 110
at Microsoft.Practices.Unity.Configuration.UnityConfigurationSection.Configure(IUnityContainer container, String configuredContainerName) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity.Configuration\Src\UnityConfigurationSection.cs:line 151
at Microsoft.Practices.Unity.Configuration.UnityContainerExtensions.LoadConfiguration(IUnityContainer container, UnityConfigurationSection section, String containerName) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity.Configuration\Src\UnityContainerExtensions.cs:line 37
at Microsoft.Practices.Unity.Configuration.UnityContainerExtensions.LoadConfiguration(IUnityContainer container) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity.Configuration\Src\UnityContainerExtensions.cs:line 64
at test.Program.Main(String[] args) in S:\Dropbox\IOC\myUnityIOCContainerExample - Simple\myUnityIOCContainerExample\Program.cs:line 19
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
What am I doing wrong :-s
Continue reading...
Trying to use unity as an IOC want to inject an implementation MasterCard in for an interface ICreditCard. For some reason I keep getting an error (see below). Ive followed the MSDN instructions line by line. What am I doing wrong :-(
The app is very simple here is the code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;
namespace myUnityIOCContainerExample
{
class Program
{
static void Main(string[] args)
{
//Example: Using Unity as the IOC container
var container = new UnityContainer();
container.LoadConfiguration();
var shopper = container.Resolve<Shopper>();
shopper.Charge();
Console.ReadLine();
}
public class VisaCard : ICreditCard
{
public string Charge()
{
return "Swiping the VisaCard";
}
}
public class MasterCard : ICreditCard
{
public string Charge()
{
return "Swiping the masterCard";
}
}
//Main Class that is dependent on something
public class Shopper
{
private readonly ICreditCard creditCard;
//Constructor Injector: i.e takes in the object it is dependant on via the constructor
public Shopper(ICreditCard creditCard)
{
this.creditCard = creditCard;
}
public void Charge()
{
var chargeMessage = creditCard.Charge();
Console.WriteLine(chargeMessage);
}
}
//The interface for the objects that we depend on
public interface ICreditCard
{
string Charge();
}
}
}
Here is the default app.config file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/>
</configSections>
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity
<alias alias="ICreditCard" type="myUnityIOCContainerExample.ICreditCard, myUnityIOCContainerExample" />
<namespace name="myUnityIOCContainerExample" />
<assembly name="myUnityIOCContainerExample" />
<container>
<register type="ICreditCard" name="special" mapTo="MasterCard" />
</container>
</unity>
</configuration>
And here is the error that happens on the .loadConfiguration line:
System.InvalidOperationException was unhandled
Message=The type name or alias ICreditCard could not be resolved. Please check your configuration file and verify this type name.
Source=Microsoft.Practices.Unity.Configuration
StackTrace:
at Microsoft.Practices.Unity.Configuration.ConfigurationHelpers.TypeResolverImpl.ResolveType(String typeNameOrAlias, Boolean throwIfResolveFails) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity.Configuration\Src\ConfigurationHelpers\TypeResolverImpl.cs:line 110
at Microsoft.Practices.Unity.Configuration.ConfigurationHelpers.TypeResolver.ResolveType(String typeNameOrAlias) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity.Configuration\Src\ConfigurationHelpers\TypeResolver.cs:line 47
at Microsoft.Practices.Unity.Configuration.RegisterElement.GetRegisteringType() in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity.Configuration\Src\RegisterElement.cs:line 121
at Microsoft.Practices.Unity.Configuration.RegisterElement.ConfigureContainer(IUnityContainer container) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity.Configuration\Src\RegisterElement.cs:line 88
at Microsoft.Practices.Unity.Configuration.ContainerConfiguringElement.ConfigureContainerInternal(IUnityContainer container) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity.Configuration\Src\ContainerConfiguringElement.cs:line 43
at Microsoft.Practices.Unity.Configuration.ContainerElement.<>c__DisplayClass1.<ConfigureContainer>b__0(ContainerConfiguringElement element) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity.Configuration\Src\ContainerElement.cs:line 114
at Microsoft.Practices.ObjectBuilder2.EnumerableExtensions.ForEach[TItem](IEnumerable`1 sequence, Action`1 action) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity\Src\ObjectBuilder\Utility\EnumerableExtensions.cs:line 36
at Microsoft.Practices.Unity.Configuration.ContainerElement.ConfigureContainer(IUnityContainer container) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity.Configuration\Src\ContainerElement.cs:line 110
at Microsoft.Practices.Unity.Configuration.UnityConfigurationSection.Configure(IUnityContainer container, String configuredContainerName) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity.Configuration\Src\UnityConfigurationSection.cs:line 151
at Microsoft.Practices.Unity.Configuration.UnityContainerExtensions.LoadConfiguration(IUnityContainer container, UnityConfigurationSection section, String containerName) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity.Configuration\Src\UnityContainerExtensions.cs:line 37
at Microsoft.Practices.Unity.Configuration.UnityContainerExtensions.LoadConfiguration(IUnityContainer container) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity.Configuration\Src\UnityContainerExtensions.cs:line 64
at test.Program.Main(String[] args) in S:\Dropbox\IOC\myUnityIOCContainerExample - Simple\myUnityIOCContainerExample\Program.cs:line 19
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
What am I doing wrong :-s
Continue reading...