webadvanced / Structuremap.WebAPI2

Apache License 2.0
12 stars 1 forks source link

OWIN Self Host WebAPI 2 #3

Open myles-mcdonnell opened 9 years ago

myles-mcdonnell commented 9 years ago

I am trying to bootstrap StructureMap.WebAPI2 from an integration test suite project that self hosts a WebAPI project

http://stackoverflow.com/questions/29879799/webapi-app-and-integration-tests-in-same-solution

http://www.asp.net/web-api/overview/hosting-aspnet-web-api/use-owin-to-self-host-web-api

Is this known to work or not work? I have a suspicion it doesn't due to the MVC5 cant self host restriction?

Thanks Myles

rickj33 commented 9 years ago

Not sure if you ever found a solution to your issue or not, but I had the same problem and solved it by changing the implementation of the StructureMap.WebApi 2 sources installed by the StructureMap.WebApi2 V 3.0.4.125 nuget package. After I made the changes, I was able to use the Microsoft.Owin.Testing.TestServer in my unit tests to test my controllers.

File ControllerConvention.cs

if (type.CanBeCastTo<**Controller**>() && !type.IsAbstract) To if (type.CanBeCastTo<**ApiController**>() && !type.IsAbstract)

File StructureMapWebApiDependencyResolver.cs

File StructureMapWebApiDependencyScope.cs

StructureMapWebApiDependencyResolver.cs Code

public class StructureMapWebApiDependencyResolver :  IDependencyResolver
    {
        #region Constructors and Destructors
        private IContainer _container;
        /// <summary>
        /// Initializes a new instance of the <see cref="StructureMapWebApiDependencyResolver"/> class.
        /// </summary>
        /// <param name="container">
        /// The container.
        /// </param>
        public StructureMapWebApiDependencyResolver(IContainer container)
        {
            _container = container;
        }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The begin scope.
        /// </summary>
        /// <returns>
        /// The System.Web.Http.Dependencies.IDependencyScope.
        /// </returns>
        public IDependencyScope BeginScope()
        {
            IContainer child = this._container.GetNestedContainer();
            return new StructureMapWebApiDependencyScope(child);
        }

        #endregion

        public object GetService(System.Type serviceType)
        {
            if (serviceType == null)
            {
                return null;
            }
            try
            {
                if (serviceType.IsAbstract || serviceType.IsInterface)
                    return _container.TryGetInstance(serviceType);

                return _container.GetInstance(serviceType);
            }
            catch
            {
                return null;
            }
        }

        public System.Collections.Generic.IEnumerable<object> GetServices(System.Type serviceType)
        {
            return _container.GetAllInstances<object>().Where(x => x.GetType() == serviceType);

        }

        public void Dispose()
        {
            _container.Dispose();
            _container = null;
        }
    }

StructureMapWebApiDependencyScope.cs Code

  private IContainer _container;
        public StructureMapWebApiDependencyScope(IContainer container )
        {
            _container = container;
        }

        public  object GetService(Type serviceType)
        {
            if (serviceType == null)
            {
                return null;
            }
            try
            {
                if (serviceType.IsAbstract || serviceType.IsInterface)
                    return _container.TryGetInstance(serviceType);

                return _container.GetInstance(serviceType);
            }
            catch
            {
                return null;
            }
        }

        public IEnumerable<object> GetServices(Type serviceType)
        {
            return _container.GetAllInstances<object>().Where(x => x.GetType() == serviceType);
        }

        public void Dispose()
        {
            _container.Dispose();
            _container = null;
        }
    }