0

I have the below script that uses CSharpCodeProvider and adds necessary system libraries. I've come into a situation where I need to include the Newtonsoft.Json.dll for the compiled program. Unfortunately, even though the dll is in the bin folder, I get

Error (CS0006) Newtonsoft.Json.dll could not be found

Any ideas would be helpful for me and others who may have the same problem.

string code = Encoding.UTF8.GetString(Convert.FromBase64String(Code.code));
CSharpCodeProvider provider = new CSharpCodeProvider();
CompilerParameters parameters = new CompilerParameters();
parameters.GenerateInMemory = true;
parameters.GenerateExecutable = false;
parameters.ReferencedAssemblies.Add("System.Net.Http.dll");
parameters.ReferencedAssemblies.Add("Newtonsoft.Json.dll");
CompilerResults results = provider.CompileAssemblyFromSource(parameters, code);
if (results.Errors.HasErrors)
{
    StringBuilder sb = new StringBuilder();
    foreach (CompilerError error in results.Errors)
    {
        sb.AppendLine(String.Format("Error ({0}): {1}", error.ErrorNumber, error.ErrorText));
    }
    string badResult = sb.ToString();
    return badResult;
}
Kyle
  • 2,339
  • 10
  • 33
  • 67

1 Answers1

0

I think Newtonsoft is not registered in the GAC.

The compiler is saying it can't find the assembly, so have you tried giving it the full path to the Newtonsoft DLL?

parameters.ReferencedAssemblies.Add(@"c:\somepath\Newtonsoft.Json.dll");
JuanR
  • 7,405
  • 1
  • 19
  • 30
  • Thanks for the answer, this sounds like it could be successfully. Although, I'm unable to test and verify this as this is deployed into Azure as a Web API. – Kyle May 05 '17 at 19:07
  • How about passing a relative path instead then? – JuanR May 05 '17 at 19:09