示例#1
0
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);



            IUnityContainer container = UnityRegistration.Register(GlobalConfiguration.Configuration);

            DependencyHelper.container = container;
            UnityConfig.RegisterComponents();
        }
示例#2
0
        private static void RegisterDependencyResolver()
        {
            UnityContainer    unityContainer    = new UnityContainer();
            UnityRegistration unityRegistration = new UnityRegistration(unityContainer);

            unityRegistration.Register();

            IDependencyResolver unityDependencyResolver = new UnityDependencyResolver(unityContainer);

            DependencyResolver.SetResolver(unityDependencyResolver);

            IControllerFactory controllerFactory = new DependencyResolverControllerFactory(unityDependencyResolver);

            ControllerBuilder.Current.SetControllerFactory(controllerFactory);
        }
        static void Main(String[] args)
        {
            using (var container = new UnityContainer())
            {
                container.AddNewExtension <Interception>();

                // Illustrates how to put the ArgumentNotNullHandler on every method. You could of course change this policy or simply use the ArgumentNotNullAttribute explicitly.
                container.Configure <Interception>()
                .AddPolicy("ArgumentNotNull")
                .AddCallHandler <ArgumentNotNullHandler>()
                .AddMatchingRule(new MemberNameMatchingRule("*"));

                // Required to use Unity Call Handlers.
                UnityRegistration.Register(container);

                RegisterSampleLoggers(container);

                container.RegisterType <IMyService, MyService>(new InterceptionBehavior <PolicyInjectionBehavior>(), new Interceptor <InterfaceInterceptor>());
                var service = container.Resolve <IMyService>();

                ParameterlessTest(service);
                SingleArgumentTest(service);
                MultipleArgumentTest(service);
                ComplexTypeTest(service);

                try
                {
                    service.NullableArgument(null);
                    service.OptionalArgument();
                    service.GetDepartments(null);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Threw an exception: {0}", ex.ToString());
                }
            }
        }