Note that there are some explanatory texts on larger screens.

plurals
  1. POLifetimeScoping error in Web API self hosted app and Simple Injector
    primarykey
    data
    text
    <p>I read these (<a href="https://simpleinjector.codeplex.com/wikipage?title=Web%20API%20Integration&amp;referringTitle=Integration%20Guide" rel="nofollow noreferrer">+</a> , <a href="https://simpleinjector.codeplex.com/wikipage?title=Self%20Hosted%20Web%20API%20Integration&amp;referringTitle=Web%20API%20Integration" rel="nofollow noreferrer">+</a> , <a href="https://simpleinjector.codeplex.com/discussions/430603" rel="nofollow noreferrer">+</a> and <a href="https://stackoverflow.com/questions/10955077/simple-injector-inject-same-unitofwork-instance-across-services-of-the-same-gra">+</a>) pages, but I cannot figure out what should I do.</p> <p>I have this simple interface and concrete type:</p> <pre class="lang-cs prettyprint-override"><code>public interface IIdentifierGenerator { long Generate(Type type); long Generate&lt;TType&gt;(TType type); } public HiloIdentifierGenerator : IIdentifierGenerator { /* implementation... */ } </code></pre> <p>I create this <code>DependencyResolver</code>:</p> <pre class="lang-cs prettyprint-override"><code>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&lt;object&gt; GetServices(Type serviceType) { return _container.GetAllInstances(serviceType); } public void Dispose() { if (_lifetimeScope != null) _lifetimeScope.Dispose(); } } </code></pre> <p>and I configured my server like this:</p> <pre class="lang-cs prettyprint-override"><code>_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&lt;Container&gt;()); _server = new HttpSelfHostServer(_config); /* etc. */ </code></pre> <p>And this is my controller:</p> <pre class="lang-cs prettyprint-override"><code>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); } } </code></pre> <p>Now, when I call the action method, I get this error:</p> <blockquote> <p>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.</p> </blockquote> <p>Where am I doing wrong? Can you help please?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload