Adding dynamic redirects. Fix for #793

Fix for #793 

*Do not merge this yet!!!!*

This change removes all static binding redirects in app.config, and introduces dynamic redirects in core. The way this is done is by listening to the AssemblyResolve event on the AppDomain, and loading / returning the assembly to redirect to. It turns out using this technique you can swap on the fly and give a different assembly than what was requested.

At startup, it builds a map in map in memory based on the assemblies that scriptcs ships with ensuring that any requests for the same names will be redirected to it's version. It also ensures that any packages in the scriptcs packages folder take precedence over what the script pack / packages assemblies were actually compiled against.

Assembly black magic at its finest....

It appears to work so far :-)

This does not fix the Service Stack script pack issue, as there appears to be no magical fix for that other than updating the script pack. However, I was able to get the old Web Api script pack to work with the latest Web Api (see comments)


good catch. Come to think of it I am not sure why it is even optional.
Anyway, I will figure it out. I am planning to get this done with unit
tests now. Stay tuned.


On Sun, Jul 27, 2014 at 3:56 AM, Adam Ralph <notifications@github.com>
wrote:

> In src/ScriptCs.Core/AppDomainAssemblyResolver.cs:
>
> > +using ScriptCs.Contracts;
> > +using System.IO;
> > +
> > +namespace ScriptCs
> > +{
> > +    public class AppDomainAssemblyResolver : IAppDomainAssemblyResolver
> > +    {
> > +        private readonly ILog _logger;
> > +        private readonly IFileSystem _fileSystem;
> > +        private readonly IAssemblyResolver _resolver;
> > +        private readonly IAssemblyUtility _assemblyUtility;
> > +        private IDictionary<string, AssemblyInfo> _assemblyInfoMap;
> > +
> > +        public AppDomainAssemblyResolver(ILog logger, IFileSystem fileSystem, IAssemblyResolver resolver, IDictionary<string, AssemblyInfo> assemblyInfoMap = null, IAssemblyUtility assemblyUtility = null )
> > +        {
> > +            if (_assemblyUtility == null)
>
> OK, fair point about the null guards, but the current check is still
> wrong. I realise it's an optional parameter but it's not being used at all.
>
> After a second look, I guess you are just missing a line of code:
>
> _assemblyUtility = assemblyUtility; // this line is missingif (_assemblyUtility == null){
>     _assemblyUtility = new AssemblyUtility();}
>
> Alternatively, you could replace these four lines with one line:
>
> _assemblyUtility = assemblyUtility ?? new AssemblyUtility();
>
> Similarly for _assemblyInfoMap.
>
> —
> Reply to this email directly or view it on GitHub
> <https://github.com/scriptcs/scriptcs/pull/798/files#r15438600>.
>