public void When_resolving_type_with_container_controlled_lifetime_then_returns_same_instance_every_time() { using (var container = new UnityContainer()) { container.RegisterType <IFoo, Foo>(new ContainerControlledLifetimeManager()); var resolver = new UnityDependencyResolver(container); IFoo resolve1 = (IFoo)resolver.GetService(typeof(IFoo)); IFoo resolve2 = (IFoo)resolver.GetService(typeof(IFoo)); Assert.IsNotNull(resolve1); Assert.AreSame(resolve1, resolve2); } }
protected void Application_Start() { AreaRegistration.RegisterAllAreas(); WebApiConfig.Register(GlobalConfiguration.Configuration); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); UnityContainer container = new UnityContainer(); CreateDataMapper.InitMapper(); IList <Assembly> assList = new List <Assembly>(); assList.Add(Assembly.Load("Service, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null")); assList.Add(Assembly.Load("IService, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null")); assList.Add(Assembly.Load("DAL, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null")); assList.Add(Assembly.Load("IDAL, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null")); UnityRegister.RegisterAssembly ( ref container, assList.ToArray() //AppDomain.CurrentDomain.GetAssemblies() //.Where( // m => m.FullName.IndexOf("Service") >= 0 // || m.FullName.IndexOf("DAL") >= 0 //).ToArray() ); IDependencyResolver resolver = new UnityDependencyResolver(container); GlobalFilters.Filters.Add((MvcActionFilterAttribute)resolver.GetService <MvcActionFilterAttribute>()); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); DependencyResolver.SetResolver(resolver); }
public void When_resolving_type_with_hierarchical_lifetime_then_returns_same_instance_every_time() { using (var container = new UnityContainer()) { container.RegisterType <IFoo, Foo>(new HierarchicalLifetimeManager()); using (var resolver = new UnityDependencyResolver(container)) { IFoo parentResolve = (IFoo)resolver.GetService(typeof(IFoo)); IFoo scope1Resolve; IFoo scope2Resolve; using (var scope = resolver.BeginScope()) { scope1Resolve = (IFoo)scope.GetService(typeof(IFoo)); } using (var scope = resolver.BeginScope()) { scope2Resolve = (IFoo)scope.GetService(typeof(IFoo)); } Assert.IsNotNull(parentResolve); Assert.AreSame(parentResolve, scope1Resolve); Assert.AreSame(parentResolve, scope2Resolve); } } }
private void OnEndRequest(object src, EventArgs args) { var resolver = new UnityDependencyResolver("MyDefaultConnection"); var photoService = (IPhotoService)resolver.GetService(typeof(IPhotoService)); IEnumerable <Photo> imgs = photoService.GetAll(); int pngCount = 0; int jpegCount = 0; foreach (var item in imgs) { if (item.MimeType.Equals("image/png")) { pngCount++; } else if (item.MimeType.Equals("image/jpeg")) { jpegCount++; } } string result = $" Total images: {pngCount + jpegCount}; PNG images: {pngCount}; JPEG images: {jpegCount};"; string path = (src as HttpApplication).Server.MapPath("/App_Data/info.txt"); using (StreamWriter sw = new StreamWriter(path, false)) { sw.WriteLine(DateTime.Now.ToString() + result); } }
public void When_resolving_controller_with_unregistered_dependencies_then_throws() { using (var container = new UnityContainer()) { var resolver = new UnityDependencyResolver(container); AssertThrows <ResolutionFailedException>(() => resolver.GetService(typeof(TestController))); } }
public void When_resolving_unregistered_type_then_returns_null() { using (var container = new UnityContainer()) { var resolver = new UnityDependencyResolver(container); Assert.IsNull(resolver.GetService(typeof(IFoo))); } }
public void TestGetServiceFromDependencyResolver() { //arrange IUnityContainer container = InitialUnityContainer(); IDependencyResolver dependencyResolver = new UnityDependencyResolver(container); //act var controllerActivator = dependencyResolver.GetService <IControllerActivator>(); //assert Assert.NotNull(controllerActivator); }
public void When_resolving_concrete_controller_then_returns_injected_instance() { using (var container = new UnityContainer()) { container.RegisterInstance <IFoo>(new Foo { TestProperty = "value" }); var resolver = new UnityDependencyResolver(container); var actual = (TestController)resolver.GetService(typeof(TestController)); Assert.AreEqual("value", actual.Foo.TestProperty); } }
public void When_resolving_then_returns_registered_instance() { using (var container = new UnityContainer()) { container.RegisterInstance <IFoo>(new Foo { TestProperty = "value" }); using (var resolver = new UnityDependencyResolver(container)) { var actual = (IFoo)resolver.GetService(typeof(IFoo)); Assert.AreEqual("value", actual.TestProperty); } } }
public void ProcessRequest(HttpContext context) { var resolver = new UnityDependencyResolver("MyDefaultConnection"); var photoService = (IPhotoService)resolver.GetService(typeof(IPhotoService)); IEnumerable <Photo> imgs = photoService.GetAll(); string result = "<div>"; foreach (var item in imgs) { result += $"<img src={item.ImagePath} />"; } result += "</div>"; context.Response.ContentType = "text/html"; context.Response.Write(result); }
public void GetService_should_Throw_on_null_serviceType() { IUnityContainer container = new Mock <IUnityContainer>().Object; UnityDependencyResolver resolver = new UnityDependencyResolver(container); Executing.This( () => resolver.GetService(null) ). Should(). Throw <ArgumentNullException>(). And. ValueOf. ParamName. Should(). Be("serviceType"); }
public void TestCreateContronllerFromCustomControllerActivatorWithUnity() { //arrange IUnityContainer container = InitialUnityContainer(); IDependencyResolver dependencyResolver = new UnityDependencyResolver(container); //act var controllerActivator = dependencyResolver.GetService <IControllerActivator>(); var requestContext = new RequestContext(); var controller = controllerActivator.Create(requestContext, typeof(TestingController)); //assert Assert.NotNull(controllerActivator); Assert.NotNull(controller); Assert.IsType <TestingController>(controller); }
public void When_disposing_scope_then_does_not_dispose_container() { using (var container = new UnityContainer()) { container.RegisterInstance <IFoo>(new Foo { TestProperty = "value" }); using (var resolver = new UnityDependencyResolver(container)) { resolver.BeginScope().Dispose(); var actual = (IFoo)resolver.GetService(typeof(IFoo)); Assert.AreEqual("value", actual.TestProperty); } } }
// Handler writes image bytes public void ProcessRequest(HttpContext context) { var routeValues = _requestContext.RouteData.Values; if (routeValues.ContainsKey("id")) { var resolver = new UnityDependencyResolver("MyDefaultConnection"); var photoService = (IPhotoService)resolver.GetService(typeof(IPhotoService)); Photo img = photoService.Get(Convert.ToInt32(routeValues["id"])); string filename = _requestContext.HttpContext.Server.MapPath(img.ImagePath); System.IO.FileInfo fileInfo = new System.IO.FileInfo(filename); if (fileInfo.Exists) { _requestContext.HttpContext.Response.Clear(); _requestContext.HttpContext.Response.AddHeader("Content-Disposition", "inline;attachment; filename=\"" + fileInfo.Name + "\""); _requestContext.HttpContext.Response.AddHeader("Content-Length", fileInfo.Length.ToString()); _requestContext.HttpContext.Response.ContentType = img.MimeType; //"application/octet-stream"; _requestContext.HttpContext.Response.TransmitFile(fileInfo.FullName); _requestContext.HttpContext.Response.Flush(); // I use in to check !=null equation //Int32 id; // Check and get id value from url //if (_requestContext.HttpContext.Request.QueryString["id"] != null) //{ // id = Convert.ToInt32(context.Request.QueryString["id"]); //} //else //{ // throw new ArgumentException("Not specified parameter"); //} // Get image by id from database //Photo img = _photoService.Get(id); // Getting image type(png, jpg, jpeg, bmp, gif) //_requestContext.HttpContext.Response.ContentType = img.MimeType; //_requestContext.HttpContext.Response.WriteFile(img.ThumbPath); //// Write image bytes to current stream ////context.Response.OutputStream.Write(data, 0, data.Length); //_requestContext.HttpContext.Response.Flush(); } } }
/// <summary> /// Resolves the requested service instance. /// </summary> /// <param name="serviceType">Type of instance requested.</param> /// <param name="key">Name of registered service you want. May be null.</param> /// <returns>The requested service instance.</returns> protected override object DoGetInstance(Type serviceType, string key) { return(_unityDependencyResolver.GetService(serviceType)); }
public object GetService(Type serviceType) { return(_resolver.GetService(serviceType)); }