Wednesday 5 July 2017

Assemblies 04 Probing

Assemblies are just dll's or exe's that can reference other dll's from each other. if a dll is a private assembly not signed then they can be located in the application folder, that is side by side with the assembly that's calling them.

When an Assembly is strongly named, that is signed using a public-private key; then it can reside in the GAC and be referenced by multiple Assemblies.

Configuration files can be leveraged to help assemblies find their references. There are three options for this:
  • Application configuration files 
  • Publisher policy files 
  • machine configuration files
Let's investigate application configuration files, let's open up our app.config file

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup>
        <supportedRuntime version="v4.0sku=".NETFramework,Version=v4.5" />
    </startup>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <probing privatePath ="myAssemblies" />
    </assemblyBinding>
  </runtime>
</configuration>

all the above tells the application is to not only to search the GAC and local directory, but to check the sub directory myAssemblies for any referenced assemblies. Now this app is made up of a main that passes a string to a person and has the person display the string.

Now we can specify in the app config that we want to use a newer version of our class library

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="myAssembly"
                          publicKeyToken="31ab3ca45f0a73f1" />
        <bindingRedirect oldVersion="1.0.0.0" newVersion="2.0.0.0"/>
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>