示例#1
0
		private static void BuildUnityContainer(AWUnityContainer container)
		{
			//One DbContext per request
			container.RegisterInstancePerRequestType<IAWDbContext, AWDbContext>();

			//transiet lifetime manager, new UnitOfWork on every resolve
			container.RegisterType<IUnitOfWork, UnitOfWork>();

			//new controller on every resolve
			try
			{

				Assembly.Load("AdventureWorks.Web.App").GetTypes()
					.Where(x => CanBeInstanciatedAndAssignedTo(x, typeof(IController))).ToList()
					.ForEach(x => container.RegisterType(x));
			}
			catch (ReflectionTypeLoadException ex)
			{
				StringBuilder sb = new StringBuilder();
				foreach (Exception exSub in ex.LoaderExceptions)
				{
					sb.AppendLine(exSub.Message);
					if (exSub is FileNotFoundException)
					{
						FileNotFoundException exFileNotFound = exSub as FileNotFoundException;
						if (!string.IsNullOrEmpty(exFileNotFound.FusionLog))
						{
							sb.AppendLine("Fusion Log:");
							sb.AppendLine(exFileNotFound.FusionLog);
						}
					}
					sb.AppendLine();
				}
				string errorMessage = sb.ToString();
				//Display or log the error based on your application.
			}
		}
		public UnityDependencyResolver(AWUnityContainer container)
		{
			Container = container;
		}