LifetimeScoping error in WebApi self hosted app

May 19, 2013 at 12:36 PM
Hey. I read these (+ , + , + and + ) pages, but I connot figure out what should I do.
I have this simple interface and concrete type:
public interface IIdentifierGenerator {
    long Generate(Type type);
    long Generate<TType>(TType type);
}
public HiloIdentifierGenerator : IIdentifierGenerator { /* implementation... */ }
I create this DependencyResolver:
public class SelfHostedSimpleInjectorWebApiDependencyResolver : IDependencyResolver {

        private readonly Container _container;
        private readonly LifetimeScope _lifetimeScope;

        public SelfHostedSimpleInjectorWebApiDependencyResolver(
            Container container)
            : this(container, false) {
        }

        private SelfHostedSimpleInjectorWebApiDependencyResolver(
            Container container, bool createScope) {
            _container = container;

            if (createScope) {
                _lifetimeScope = container.BeginLifetimeScope();
            }
        }

        public IDependencyScope BeginScope() {
            return new SelfHostedSimpleInjectorWebApiDependencyResolver(
                _container, true);
        }

        public object GetService(Type serviceType) {
            return ((IServiceProvider)_container)
                .GetService(serviceType);
        }

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

        public void Dispose() {
            if (_lifetimeScope != null) {
                _lifetimeScope.Dispose();
            }
        }
    }
and I configured my server like this:
            _config = new HttpSelfHostConfiguration("http://192.168.1.100:20000");
            _config.Routes.MapHttpRoute(
                "API Default", "api/{controller}/{id}",
                new { id = RouteParameter.Optional });
            _config.DependencyResolver =
                new SelfHostedSimpleInjectorWebApiDependencyResolver(
                    IoC.Wrapper.GetService<Container>());
            _server = new HttpSelfHostServer(_config);
            /* etc. */
And this is my controller:
    public class IdentifierController : ApiController {

        private readonly IIdentifierGenerator _identifierGenerator;

        public IdentifierController(IIdentifierGenerator identifierGenerator) {
            _identifierGenerator = identifierGenerator;
        }

        public long Get(string id) {
            var type = Type.GetType(id, false, true);
            return type == null ? -1 : _identifierGenerator.GetIdentifier(type);
        }
    }
Now, when I call the action method, I get this error:
It is not safe to use a LifetimeScope instance across threads. Make sure the complete operation that the lifetime scope surrounds gets executed within the same thread and make sure that the LifetimeScope instance gets disposed on the same thread as it gets created. Dispose was called on thread with ManagedThreadId 28, but was created on thread with id 29.
Where am I doing wrong? Can you help please?
Coordinator
May 20, 2013 at 9:59 AM
Edited Feb 21, 2014 at 1:46 PM
Marked as answer by dot_NET_Junkie on 2/21/2014 at 6:45 AM