BT

ASP.NET vNext: Custom Project Loaders and Language Support

by Roopesh Shenoy on Sep 01, 2014 |

The ASP.NET vNext runtime uses the Roslyn compiler to compile and load the C# sources before running them. David Fowler shows how you can leverage the DI-by-design approach of KRuntime to inject support for your own language.

The KRuntime contains all the bits needed to buld and run an ASP.NET vNext application. As a matter of principle, the runtime does not recognise a "design-time". Compilation and loading happen simultaneously. For this, KRuntime has different loaders. These walk through the dependencies listed in the project.json file and build them, along with the sources themselves.

The project reference loader is interesting. By default, it uses the RoslynProjectReferenceProvider which in-turn returns the RoslynProjectReference. As the name suggests, this uses the Roslyn C# compiler to compile the C# sources and load the assemblies.

However, this behavior can be overridden, and David Fowler shows how.

F# Support For ASP.NET vNext

Refer David's vNextLanguageSupport github project. He defines an implementation of IProjectReferenceProvider (FSharpProjectReferenceProvider). This in-turn returns a custom implementation of the IMetadataProjectReference (FSharpProjectReference).

The FSharpProjectReference does the bulk of the work. It's Emit method -

  • goes through the project references
  • copies them to a temp-path
  • compiles the source files along with the references using the fsc compiler, and then
  • deletes the temp files.

Both these classes are just housed in the FSharpSupport project, so that they can be compiled and then referenced as an assembly. Note that even though the implementations are for providing F# support, they are written in C# themselves.

He then uses this in a sample F# project. For this he defines the projectReferenceProviderType in his project.json file -

(snippet)

    "language": {
        "name": "F#",
        "assembly": "FSharpSupport",
        "projectReferenceProviderType": "FSharpSupport.FSharpProjectReferenceProvider"
    },

And just like that, F# is now supported for your ASP.NET vNext app. The startup class is now an F# type instead of a C# class.

This example uses F#, but you can use the same technique to build support for any CLR language. All you have to do is provide your implementations of IMetadataProjectReference and IProjectReferenceProvider.

Aleksander Heintz has a few detailed articles explaining David's sample further -

The example is not trivial. But it demonstrates the power of the inbuilt-DI approach taken by KRuntime, which allows a lot of flexibility for adding new features. As explained in the KRuntime structure, every layer of the runtime, including the Native Process, Host, Managed Entry Point and the Application Host, is resolved via Dependency Injection. (for e.g. KRuntime works on Linux and OSX using Mono).

ASP.NET vNext represents a fundamental change to how Microsoft constructs and deploys web frameworks.

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

This is great by wakin imgen

ASP.NET MVC is already very solid web framework. But with ASP.NET vNext, it will be the best, second to none.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

1 Discuss

Educational Content

General Feedback
Bugs
Advertising
Editorial
InfoQ.com and all content copyright © 2006-2014 C4Media Inc. InfoQ.com hosted at Contegix, the best ISP we've ever worked with.
Privacy policy
BT