Cassette.Nancy icon indicating copy to clipboard operation
Cassette.Nancy copied to clipboard

Error when using async routes

Open michael-wolfenden opened this issue 10 years ago • 2 comments

I am getting an error when using an async route for my homepage:

Get["/", true] = async (_, __) => await RenderHomepage();
System.NullReferenceException was unhandled by user code
  HResult=-2147467261
  Message=Object reference not set to an instance of an object.
  Source=Cassette.Nancy
  StackTrace:
       at Cassette.Nancy.NancyContextLifetimeProvider.GetObject() in c:\development\Cassette.Nancy\src\Cassette.Nancy\NancyContextLifetimeProvider.cs:line 20
       at Cassette.TinyIoC.TinyIoCContainer.CustomObjectLifetimeFactory.GetObject(Type requestedType, TinyIoCContainer container, NamedParameterOverloads parameters, ResolveOptions options)
       at Cassette.TinyIoC.TinyIoCContainer.ResolveInternal(TypeRegistration registration, NamedParameterOverloads parameters, ResolveOptions options)
  InnerException: 

I believe the issue is due to the NancyContext being stored as a ThreadLocal. The response is being returned on a different thread due to async and so the call to getContext() is returning null as we set the context on a different thread.

michael-wolfenden avatar Aug 06 '14 02:08 michael-wolfenden

I'm also running into this issue.

@michael-wolfenden Did you find a workaround?

mattyway avatar Apr 30 '15 00:04 mattyway

@mattyway.. Nope, ended up using SquishIt which is a shame as this is a much simpler / nicer library

michael-wolfenden avatar Apr 30 '15 00:04 michael-wolfenden