You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.3 KiB

1 year ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Http.Dependencies;
  6. using Unity;
  7. using Unity.Exceptions;
  8. namespace JsonRx.Resolver
  9. {
  10. public class UnityResolver : IDependencyResolver
  11. {
  12. protected IUnityContainer container;
  13. public UnityResolver(IUnityContainer container)
  14. {
  15. if (container == null)
  16. {
  17. throw new ArgumentNullException("container");
  18. }
  19. this.container = container;
  20. }
  21. public IDependencyScope BeginScope()
  22. {
  23. var child = container.CreateChildContainer();
  24. return new UnityResolver(child);
  25. }
  26. public object GetService(Type serviceType)
  27. {
  28. try
  29. {
  30. return container.Resolve(serviceType);
  31. }
  32. catch (ResolutionFailedException)
  33. {
  34. return null;
  35. }
  36. }
  37. public IEnumerable<object> GetServices(Type serviceType)
  38. {
  39. try
  40. {
  41. return container.ResolveAll(serviceType);
  42. }
  43. catch (ResolutionFailedException)
  44. {
  45. return new List<object>();
  46. }
  47. }
  48. public void Dispose()
  49. {
  50. container.Dispose();
  51. }
  52. }
  53. }