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)


I am thinking we just freeze once resolution starts at all. So if you call
one of those methods after resolve fired, InvalidOperationException.

On Thursday, July 24, 2014, Adam Ralph <notifications@github.com> wrote:

> In src/ScriptCs.Core/AppDomainAssemblyResolver.cs:
>
> > +
> > +        public void AddAssemblyPaths(IEnumerable<string> assemblyPaths)
> > +        {
> > +            foreach (var assemblyPath in assemblyPaths)
> > +            {
> > +                var info = new AssemblyInfo {Path = assemblyPath};
> > +                var name = AssemblyName.GetAssemblyName(assemblyPath);
> > +                info.Version = name.Version;
> > +
> > +                AssemblyInfo foundInfo = null;
> > +                var found = _assemblyInfoMap.TryGetValue(name.Name, out foundInfo );
> > +
> > +                if (!found || foundInfo.Version.CompareTo(info.Version) < 0)
> > +                {
> > +                    _logger.DebugFormat("Mapping Assembly {0} to version:{1}", name.Name, name.Version);
> > +                    _assemblyInfoMap[name.Name] = info;
>
> OK, so throw an InvalidOperationException if someone calls it after
> resolve? That would work.
>
> Mind you, the custom resolver would only be invoked and therefore locked
> when the first assembly fails to resolve. This means a host may execute a
> script, add some assemblies, execute another script, try to add some more
> assemblies and only find it's locked then because an assembly failed to be
> resolved during the execution of the second script only.
>
> As usual, I'm thinking in edge cases ;-)
>
> Alternatively, we could throw an exception when a consumer tries to add an
> assembly which has already been custom-resolved.
>
> —
> Reply to this email directly or view it on GitHub
> <https://github.com/scriptcs/scriptcs/pull/798/files#r15341259>.
>