Microsoft Unity error when resolving repository from console app - NullReferenceException?

One option you could consider is creating two different Initialise classes (I'm guessing that is your Unity bootstrapper class).

Up vote 0 down vote favorite share g+ share fb share tw.

I'm having problems resolving a repository when calling it from a console app. Everything works fine when running the application (.NET 4, C#, Entity Framework, Unity) as normal, but I've created a standalone console app that will be run from the task scheduler to import feeds. I'm very close to giving up and do a dirty hack and write a script to call a webpage instead of using a console app, but I thought I'd at least try to understand why it isn't working first.

I'm new to both Entity Framework and Unity, so please bear with me and let me know if I've left out any important information. This is the error I'm getting: Resolution of the dependency failed, type = "MyNamespace.Domain.Template. IRepository`2MyNamespace.Domain.Employees.

OrganisationalUnit,System. Guid", name = "(none)". Exception occurred while: while resolving.

Exception is: NullReferenceException - Object reference not set to an instance of an object. At Microsoft.Practices.Unity.UnityContainer. DoBuildUp(Type t, Object existing, String name, IEnumerable`1 resolverOverrides) at Microsoft.Practices.Unity.UnityContainer.

Resolve(Type t, String name, ResolverOverride resolverOverrides) at Microsoft.Practices.Unity. UnityContainerExtensions. ResolveT(IUnityContainer container, ResolverOverride overrides) at MyNamespace.Infrastructure.FeedDefinition.

GetOrganisationalUnit(String name, OrganisationalUnit parent) in C:\FeedDefinition. Cs:line 552 This is the console app code: static void Main(string args) { if (args. Length > 0) { MyNamespace.Appliance.Configuration.Initialise.

InitialiseContainer(); ImportFeedProcessor importFeedProcessor = new ImportFeedProcessor(); importFeedProcessor. Run(args0); } } And this is where it fails: IRepository organisationalUnitRepository = Context.Instance.Container. Resolve>(); If anyone can help me understand what's going wrong I'd be very grateful!

UPDATE: Here's the (important) bits from the initialise class: public static void InitialiseContainer() { UnityContainer container = new UnityContainer(); // Use HttpContext for registering instances against for live IContext context = HttpContextWrapper. Instance; // Register the HttpContext as the default context to use container. RegisterInstance(HttpContextWrapper.

Instance); // repositories container. RegisterType, EmployeeRepository>( new UnityContextLifetimeManager(context), new InjectionConstructor(true, "OrganisationalUnit.Parent. Parent")); container.

RegisterType, EntityFrameworkRepository>( new UnityContextLifetimeManager(context), new InjectionConstructor("Parent. Parent")); // Create and populate a new Unity container from configuration Context.Instance. Container = container; } Is it perhaps the HttpContext that does it?

Thanks, Annelie c# .net . Net-4.0 unity link|improve this question edited Nov 24 '11 at 16:43 asked Nov 24 '11 at 16:00annelie372414 95% accept rate.

– John Allers Nov 24 '11 at 16:35 @John Allers - I'll update with the initialise code (cut down as it's a massive function but all things important should be there). – annelie Nov 24 '11 at 16:40.

One option you could consider is creating two different Initialise classes (I'm guessing that is your Unity bootstrapper class). The one you have can be used for your web application. The other one should be non-web specific.

Remove any reference to HttpContext (it won't be available in a Console app) and UnityContextLifetimeManager (assuming this is HttpContext specific as well).

Thanks, I should have realised this earlier as we have different initialise containers for the unit tests which work fine, but it wasn't until I copied in the code here that I thought about it! I'm now getting a Linq error but at least I'm closer so thanks! :) – annelie Nov 24 '11 at 17:28.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions