1

I'm trying to use nancy with JSON.net, follow the 2 ways that i found to register the dependencies but all way get me to an InvalidOperationException with a message "Something went wrong when trying to satisfy one of the dependencies during composition, make sure that you've registered all new dependencies in the container and inspect the innerexception for more details." with an inner exection of {"Unable to resolve type: Nancy.NancyEngine"}.

I'm using self hosting to run nancy and jeep everything really simple to been able just to test.

   public static void Main(string[] args)
    {
        try
        {
            var host = new NancyHost(new Uri("http://localhost:8888/"));
            host.Start(); // start hosting
            Console.ReadKey();
            host.Stop();  // stop hosting
        }
        catch
        {
            throw;
        }
    }

First I create a customSerializer

public class CustomJsonSerializer : JsonSerializer
{
    public CustomJsonSerializer()
    {
        ContractResolver = new CamelCasePropertyNamesContractResolver();
        Formatting = Formatting.Indented;
    }
}

and then i tried 2 ways of registering Using IRegistrations:

public class JsonRegistration : IRegistrations
{
    public IEnumerable<TypeRegistration> TypeRegistrations
    {
        get
        {
            yield return new TypeRegistration(typeof(JsonSerializer), typeof(CustomJsonSerializer));
        }
    }

    public IEnumerable<CollectionTypeRegistration> CollectionTypeRegistrations { get; protected set; }
    public IEnumerable<InstanceRegistration> InstanceRegistrations { get; protected set; }
}

And also using Bootstrapper

public class NancyBootstrapper : DefaultNancyBootstrapper
{

    protected override void ConfigureApplicationContainer(TinyIoCContainer container)
    {
        base.ConfigureApplicationContainer(container);
        container.Register<JsonSerializer, CustomJsonSerializer>();
    }

}

Which means that when self hosting I add the custom bootstrapper

var host = new NancyHost(new Uri("http://localhost:8888/"), new NancyBootstrapper());

Both way return the same error.

OscarSanhueza
  • 317
  • 2
  • 13

2 Answers2

0

Problem is actually the versions, the nancy json.net package is using Newton.Json 6.0.0.0, BUT when installing the package it will install automatically newer version that will create this problem. Not sure what has change in the Newton.JSON that will actually create this.

https://github.com/NancyFx/Nancy.Serialization.JsonNet/issues/27

OscarSanhueza
  • 317
  • 2
  • 13
0

Just to add my hard won knowledge in this area, after a greatly frustrating few hours using Nancy 1.4.1.

If you use a custom bootstrapper, make sure you make the call to base.ConfigureApplicationContainer(container); before you start your custom registrations.

So, not:

public class MyCustomBootstrapper : DefaultNancyBootstrapper
{
    protected override void ConfigureApplicationContainer(TinyIoCContainer container)
    {
        // MY BITS HERE...

        base.ConfigureApplicationContainer(container);
    }
}

but,

public class MyCustomBootstrapper : DefaultNancyBootstrapper
{
    protected override void ConfigureApplicationContainer(TinyIoCContainer container)
    {
        base.ConfigureApplicationContainer(container); // Must go first!!

        // MY BITS HERE...
    }
}

If you don't do this you will get the following error:

Something went wrong when trying to satisfy one of the dependencies during composition, make sure that you've registered all new dependencies in the container and inspect the innerexception for more details.

with a helpful inner exception of:

Unable to resolve type: Nancy.NancyEngine

The solution of changing the order of these C# statements was actually alluded to in @StevenRobbins' excellent answer here (which I could have saved myself several hours of pain if I'd only read properly the first time).

Says Steven:

By calling "base" after you've made a manual registration you are effectively copying over your original registration by autoregister. Either don't call base, or call it before you do your manual registrations.

tom redfern
  • 30,562
  • 14
  • 91
  • 126