在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
Recently I built plug-ins support to my TemperatureStation IoT solution web site. The code for .NET Core is different from what we have seen on full .NET Framework (application domains etc) but there’s still nothing complex. This blog post describes how to build simple plug-ins support to ASP.NET Core web application.
After some searching in web and some small experimenting I came out with simple solution that covers the following:
The code shown here is also kind of experimental and it is taken from my open-source IoT solution called TemperatureStation. Calculator plug-insTemperatureStation has plug-ins called Calculators. Calculators are classes that can be bound to measurements and when readings are reported then Calculators are run on readings. They provide different calculations like finding freezing point of liquid, estimating the time it takes for liquid to get to freezing point etc. For calculators there is ICalculator interface shown below. public interface ICalculator Calculators use CalculatorAttribute to define some metadata. [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)] And here is the example of dummy calculator. public class DummyCalculator : ICalculator Finding plug-in typesTo detect plug-ins I wrote CalculatorsLoader class. This a static class that creates list of calculator types. Rule is simple: class must implement ICalculator interface and must have CalculatorAttribute. Consider this class as internal matter of application. public static class CalculatorsLoader Automatic registering of plug-in typesTo use ASP.NET Core dependency injection I wrote class that provides extension method for IServiceCollection. Dependency injection is needed because Calculators may use constructor injection to access services and database. public static class CalculatorExtensions This is how to use AddCalculators extension method in Startup class of web application. public void ConfigureServices(IServiceCollection services) When web application starts then ConfigureServices method is called and Calculators are automatically registered. Plug-in providerTo access calculators I wrote ICalculatorProvider interface and CalculatorProvider class. This class with CalculatorExtensions are the only classes that access CalculatorsLoader directly. All other classes in system use provider to query calcutor types. ICalculatorProvider is introduced to ASP.NET Core dependency injection in application Startup class. GetCalculators() method uses framework level dependency injection to create instances of ICalculator. public interface ICalculatorProvider Using plug-ins in codeHere is the example about how I use ICalculatorProvider in controller code to output the list of available calculators. public class DummyController : Controller The real usage in my solution is more complex but this example gives the point. Wrapping upOn .NET Core things work a little bit differently compared to full .NET Framework. Although this solution is not perfect it was still pretty easy to find information and make code work. It was also easy to automatically register plug-in types with ASP.NET Core framework-level dependency injection and come out with simple classes that architecturally fit in to web application. For my IoT system this solution today is good enough to go with.
原文:http://gunnarpeipman.com/2017/01/aspnet-core-plugins/
|
请发表评论