Replace CodeDom with Roslyn but “bin oslyncsc.exe not found!”

Replace CodeDom with Roslyn but “bin
oslyncsc.

exe not found!”Ali BahraminezhadBlockedUnblockFollowFollowingMay 29Most of the .

NET developers especially who tried to use C#6 or higher in ASPNET MVC might have faced this error and already know how to fix it, but don’t be so surprised if I tell you this article isn’t about ASPNET MVC it’s about WPF, WinForms or Console Applications!CodeDom is about dynamic compilation at runtime.

I have used it on several projects.

For faster compilation and support version 6 or higher of C# we decided to replace CodeDom with Rosylin but due to backward compatibility and try not to make lots of changes on the code we used Microsoft.

CodeDom.

Providers.

DotNetCompilerPlatform NuGet package.

Description from NuGet package:Replacement CodeDOM providers that use the new .

NET Compiler Platform (“Roslyn”) compiler as a service APIs.

This provides support for new language features in systems using CodeDOM (e.

g.

ASP.

NET runtime compilation) as well as improving the compilation performance of these systems.

So I only replaced System.

CSharp.

CSharpCodeProvider with DotNetCompilerPlatform.

CSharpCodeProvider!var compiler = new DotnetCompilerPlatform.

CSharpCodeProvider(); // new CSharpCodeProviderBuilt the solution and everything went fine but in the runtime when the app supposed to compile a dynamic code it throws a FileNotFound exception.

output_of_my_winforms_appinRoslyncsc.

exe not foundAs we all .

NET developers know a Console, WPF, WinForms or class libraries don’t have bin folder like ASP.

NET does.

So why it’s looking for a bin folder?.Well, it’s very clear, Microsoft released this package for ASPNET to support the higher versions of C# inside cshtmland codes, and that’s why it looks for a bin folder.

Microsoft in the home repo page of the RoslynCodeDomProvider described several ways to define the absolute path of Roslyn compilers (e.

g csc.

exe or vbc.

exe) such as defining an environment variable, aspnet: key in app.

config or as a msbuild.

exe command argument!But hold on a minute!.We are going to compile a dynamic code at runtime.

We don’t know the exact absolute path of csc.

exe so using env variables, app.

config key or msbuild.

exe is out of the options.

Let’s look inside the CSharpCodeProvider source code on GitHub.

One of the constructors of this class has compilerSettings parameter.

All we need to do is create a class which implements ICompilerSettings interface.

It has only 2 property to get implemented:1.

CompilerFullPath compiler2.

CompilerTTLSo, I only needed to pass the settings object to the code provider:var compiler = new DotNetCompilePlatform.

CSharpCodeProvider(new CompilerSettings());Now things work well.

Done.

P.

S:Specify the TTL of Roslyn compiler server — Microsoft.

CodeDom.

Providers.

DotNetCompilerPlatform leverages Roslyn compiler server(VBCSCompiler.

exe) to compile the generated code.

In order to save system resources, VBCSCompiler.

exe will be shutdown after idling 10 seconds in the server environment.

However, in the development environment(running your web application from visual studio) the idle time is set to 15 mininutes.

The reason behind this is to improve the startup performance of your web application when you run/debug the application in Visual Studio, since VBCSCompiler.

exe takes several seconds to start if relevant Roslyn assemblies are not NGen’d.

With this setting, you can control the idle time of VBCSCompiler.

exe.

.

. More details

Leave a Reply