NancyFx / Nancy.Serialization.JsonNet

NewtonSoft.Json serializer for Nancy
MIT License
40 stars 32 forks source link

Failing to automatically register dependencies #27

Closed oscarsan closed 8 years ago

oscarsan commented 8 years ago

I'm trying to use the JsonNet package but it is not working with self hosted Nancy. I think I'm doing everything as needed, installing the package from Visual Studio will result in versions

<packages>
  <package id="Nancy" version="1.4.3" targetFramework="net45" />
  <package id="Nancy.Hosting.Self" version="1.4.1" targetFramework="net45" />
  <package id="Nancy.Serialization.JsonNet" version="1.4.1" targetFramework="net45" />
  <package id="Newtonsoft.Json" version="8.0.2" targetFramework="net45" />
</packages>

Code is very basic

    static void Main(string[] args)
    {
        using (var host = new NancyHost(new Uri("http://localhost:1234"), new NancyBootstrapper()))
        {
            host.Start();
            Console.ReadLine();
            host.Stop();
        }

    }

For running nancy and small bootstrapper and custom serializer

public class NancyBootstrapper : DefaultNancyBootstrapper
{

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

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

This is the exception message

An unhandled exception of type 'System.InvalidOperationException' occurred in Nancy.dll

Additional information: 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.

And further Innerexception

{"Unable to resolve type: Nancy.NancyEngine"} {"Unable to resolve type: Nancy.Routing.DefaultRequestDispatcher"} {"Unable to resolve type: Nancy.Routing.DefaultRouteResolver"} {"Unable to resolve type: Nancy.Routing.DefaultNancyModuleBuilder"} {"Unable to resolve type: Nancy.DefaultResponseFormatterFactory"} {"Could not load file or assembly 'Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)":"Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed"}

oscarsan commented 8 years ago

I actually discover the problem at the same time that was writing the issue, when installing the package it will actually install latest Newton.Json version which is 8.0.x, this will not work. You need to install first the Newton.Json version 6.0.1 (Haven't test anything newer) and then install this package, any idea what has change in the newer Newton.Json that generates this problem?

khellang commented 8 years ago

Yeah. This is a problem with Newtonsoft.Json's strategy to strong-name the assembly and occasionally bump the major version.

First of all, we should probably ship a newer version of this package soon, it'll probably be in the 2.0 timeframe. That's right around the corner.

Before that, I think you should be able to solve this by using some assembly binding redirects:

<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <dependentAssembly>
            <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral"/>
            <bindingRedirect oldVersion="0.0.0.0-8.0.0.0" newVersion="8.0.0.0"/>
        </dependentAssembly>
    </assemblyBinding>
</runtime>

Try adding the xml above to your app.config :smile:

oscarsan commented 8 years ago

Ok, when i write that in the app.config I can use latest version of Json.net.

Thanks

kingofnull commented 6 years ago

I have same problem on windows service project and Newtonsoft.Json 11 . I test the cli and run without problem but I got dependency exactly like you when I move code to windows service wrapper. I try your xml with json.net version 8.1 and 7.1 but both of them made same problem.