i'm working on a webapplication written in C# using the ASP.NET MVC framework.
I want to allow my clients to write their own plugins for the web application. Each client get it's own database.
I got some ideas how to do this:
I will provide an interface which allows the user to upload his assembly dll. This will store the dll in a specific plugins-folder for the client.
I will provide some C# Interfaces and Attributes for Class and Method annotations.
The business logic of the server will check each plugin and search for classes and methods with these attributes or interface implementations to override or alter it's default behavior on certain points.
For performance, I'll implement some form of caching that gets invalidated every time a new plugin is uploaded.
Now my question:
How can i allow the user to write a plugin? I mean how to provide some kind of SDK for this? The user primary needs the C# interfaces and Annotations but the developer also want's to test the plugin before uploading it to the production server. Can i pack my webapplication in some kind of DLL which could be loaded by the developer for testing purposes but not read the source code? Or is there any other way of doing this?
Thank you for any info on this!
You should take a look at MEF (Managed Extensibility Framework) this is a framework Microsoft created to enable applications for plugins.
In short: You create a library with contracts (interfaces). You give that library to your customers and tell them to write plugins based on those interfaces.
Then you tell your application that when it needs IPlugin it needs to search the plugin folder for an implementation of IPlugin and use that.
Some research urls:
https://msdn.microsoft.com/en-us/library/dd460648(v=vs.110).aspx
http://mikehadlow.blogspot.nl/2010/10/experimental-aspnet-mvc-add-ins.html
https://blogs.msdn.microsoft.com/brada/2008/09/29/simple-introduction-to-extensible-applications-with-the-managed-extensions-framework/
http://blog.maartenballiauw.be/post/2009/04/21/ASPNET-MVC-and-the-Managed-Extensibility-Framework-%28MEF%29.aspx
Related
I have a Web API project (4.5) that has all the DTO objects stored in a separate project for simplicity. I was wondering if there is anyway to link from the Web API help page to these class libraries so that my end users can not only see what they need to send but so that they can see the XML comments on those objects specifying what each property of the object does/means.
I suppose you are talking on "WCF Web API Test Client",
AFAIK it uses Microsoft.AspNet.WebApi.WebHost.dll provided by Microsoft. The sources are not available but if it is not obfuscated you can change it after ILSPY.
Beside this you can't decorate it, normally you just redirect the page from global.asax.
Another option : Maybe you should write your own. But you will have much work with attribute reading and reflection :) Maybe you should start a project in CodePlex & git.
I’m writing an ASP.NET MVC 2 application where developers could extend it with plugins. I’ve started to consider how create a plugin system. (e.g. like Joomla’s)
I have some doubts about how to do this. I have searched over the net and I found that there are two potential solutions.
using interfaces or abstract classes to implement methods and a plugin-structure, load the assembly, cast the object to the interface and call the methods that the plugins should have when i need to hook the system.
using C# events to load the assembly and raise the events where I need to hook the system. The plugin in the constructor will register the function with the event provided by my application.
What is the best solution? What performance considerations are there? What other approaches could there be?
Rather than writing your own plugin system, you should probably consider the Managed Extensibility Framework (MEF) which is Microsoft's own plug-in framework.
Have a look at the Orchard project on Codeplex. The Microsoft developers have build a plugin mechanism similar to what you are describing. Its pretty complex stuff...
Does anyone know how to do this? I built a backend c# class in asp.net but want to access these same classes without recreating them in silverlight. Is this a possibility?
You can reuse the cs files by adding them to your project AS LINK. Right click in your project and select Add Existing...Browse to your file and in the Open Button, use the pulldown arrow on the right to select Add As Link. You will see the file added to your project with an icon that with the little Windows Shortcut icon overlayed on it.
Just remember - the ASP.Net runs on the .Net runtime. Silverlight runs on the CoreCLR (Silverlight runtime.) Not everything that compiles in oone will compile in the other...
To separate things a little bit, #if directives can help, you can also use Partial Classes and partial methods (to add content that only runs on the server or on the client.)
RIA Services is definitely the way to go for sharing code between ASP.Net and Silverlight.
As well as the previously mentioned generation of domain service models, it also lets you share individual files between the web-app and Silverlight by simply inserting "shared" in to the filenames. e.g. "MyClass.shared.cs".
RIA services does not take long to get to terms with (and there are good tutorials about). Try this one.
Well, ASP.NET itself isn't going to work (ditto many of the full libraries), but I'm assuming you just mean you local domain model etc.
IIRC you can try to simply reference it, but it may well generate a warning message. Of course you need to be exceptionally careful not to use anything that the other platform doesn't support...
IMO, the better option here is to create a second csproj that includes the same .cs files (or cheat with a wildcard/deep include). And build both. Same C#, different dll/platform.
Is isn't uncommon to find that you need a very small usage of #if directives, too.
WCF RIA Services may help you solve your problem. Silverlight does not use the same runtime as ASP.Net does and you cannot directly share assemblies containing model classes on the client and the server side. To solve that WCF RIA Services will transparently generate classes on the client side based on model classes on the server side. Obviously WCF RIA Services will also allow you to create, read, update and delete objects of these classes using a web service.
MSDN has more specific information about WCF RIA Services Client Code Generation.
How can I develop applications in C# that can be further extended using DLL add-ins?
Like .NET Reflector can browse and load DLLs at runtime.
I searched for tutorials but I couldn't find anything. Maybe I wasn't searching using the right keywords.
Have a look at the Managed Extensibility Framework: http://mef.codeplex.com/
Basically in very brief and high-level terms, what you need to do is define one or more interfaces that your application will use to interact with the extension. Then individuals that want to write an extension for your application will implement your predefined interfaces and then register the implementation with your application. Registration could be as simple as dropping the extension DLL in a directory and your application will enumerate the directory load the extension dlls and start interacting with the classes that implement you expectd interfaces.
Take a look at for following methods for a starting point on dynamically loading assemblies and creating instances of classes at runtime
System.Activator.CreateInstance
System.Reflection.Assembly.LoadFrom
Of course there is much more to it, but this should give you a starting point.
Many popular applications such as Wordpress, WHMCS and the majority of PHP forums allow plugins to hook into core application events (such as registration, logging in, create post etc.) by simply specifying a function with a particular name.
I understand that these applications are not pre compiled, but is it possible to do something of the sort with C#? I've looked into event handlers, but it seems that you can only accomplish this if the plugin has the ability to instantiate the class that we want to hook into (or at least thats what searching has lead me to believe)
Ideally, these hooks would be into business layer class events/methods and can be hooked into by multiple objects, so it would function in either WinForms or ASP.NET MVC.
Given Alex's answer, this should be useful:
System.AddIn Tools and Samples
http://clraddins.codeplex.com/
If you design your application for extensibility, this is easy. The Managed Extensibility Framework is designed for exactly this sort of scenario, and makes it very easy.
It will be included as part of the core framework in .NET 4, but is downloadable now for use in 3.5.
There is an AddIn framework shipping with .NET 3.5. The framework provides very powerful mechanisms to expose interfaces by a host application and to manage, dynamically load-unload addins etc.
Why the core concept that comes up to my mind first, is Dependency Injection? I haven't ever played with a plug-in system in any app of mine. Does DI could help anyhow with that??
Specifics on our senerio would be helpful, but generally, You may want to explore the provider psttern.
There are 3 components:
- An abstraction of a piece of functionality, (Interface/baseClass)
- A Factory method that looks to config to determine what type of Class to create
- [your] Custom Class which extends/implements the abstraction. for example, a Membership provider class that hits a custom dasta source for user info.
This is very useful when switching out logic. If you want to create an app with swappble UI components, it is another story.
There is support for this in ASP.NET, starting with 2.0.
More info on the provider:
http://msdn.microsoft.com/en-us/library/ms972319.aspx