I am trying to create Type Provider which uses Newtonsoft.Json. There is Provided Constructor accepting JObject
:
let constr = ProvidedConstructor([ProvidedParameter("json",typeof<JObject>)])
constr.InvokeCode <- fun args ->
<@@
let jObject = %%args.[0]:JObject
jObject
@@>
Client code :
type ProvidedType = MyProvider<"source.file">
let json = JObject.Parse(str)
let objct = ProvidedType("""{ "name" = "foo bar" }""")
It fails in design time with the following error:
Type mismatch when splicing expression into quotation literal. The type of the
expression tree being inserted doesn't match the type expected by the splicing
operation.
Expected 'Newtonsoft.Json.Linq.JObject', but received type
'Newtonsoft.Json.Linq.JObject'.
Consider type-annotating with the expected expression type, e.g.,
(%% x : string) or (%x : string).
When debugging the type provider, I can see that there are two versions of Newtonsoft.Json.dll: version 6.0.3.17227 from nuget download location in my project and version 4.5.11.15520 from C:\Program Files\Common Files\Microsoft Shared\Visual Studio\12.0\Newtonsoft.Json.dll.
The later, apparently, is loaded by Visual Studio 2013 Update 2 for Json editor.
Both client and type provider assembly specify version redirect:
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.3.0" />
</dependentAssembly>
How do I solve this?
EDIT:
Turns out that just getting rid of VS version of Json.NET is not a good solution. VS throws exception on start which it hides until you try to login with different account. I don't even want to try and guess the logic behind it, the short answer is - don't mess with VS.
For the Type Provider, the workaround is to replace JObject with a string in all TP-provided methods and parse it into JObject inside the implementation.
Let's hope VS team will do something about it.
Check if the NewtonSoft.Json in the Visual Studio directory should actually be there. I had amazing pain trying to develop a type provider at one point, until I realised that for several days 3 months earlier NuGet had been unpacking all of the dependencies of anything I installed to C:\Program Files\Common Files\Microsoft Shared\Visual Studio\12.0\ - and those got loaded before the type provider could load anything. As a (very small) consolation prize, if you have the same issue as me the TP will run in FSI/compiled code - just not in Visual Studio.