squadwuschel
squadwuschel

Reputation: 3428

C# T4 using assemblybinding / bindingRedirect from web.config

I've created my own T4 Tempalte in my web project which is loading the used assemblies from my current Solution and creates some files.

That works fine so far. But now I've updated one of my NuGet components to a new version and I've added a assemblyBinding section so that a older version and the newer version of my component work. That works also fine so far for my website itself.

But my T4 template now brings a

The File or Assembly "\xxx...\...." or a dependency could not be found

It seems that my T4 Template is not reading my assemblyBinding section in the web.config is that possible and is there a workaround for this withoug updating my assembly reference in every dependent dll.

I am using T4 Template with a compiled Dll for code Implementation and in my DLL I'am loading my Assemblies, what works fine.

  var assemblies = Factory.CreateAssemblyManager().LoadAssemblies(proxySettings.WebProjectName, proxySettings.FullPathToTheWebProject);

And then I'm searching inside every assembly for my custom attribute and here I get the error above, that the file or assembly could not be loaded.

  foreach (Assembly assembly in assemblies)
  {
      var types = assembly.GetTypes().Where(type  => type.GetMethods().Any(p => p.GetCustomAttributes(typeof (CreateProxyBaseAttribute), true).Any())).ToList();
   }

German Error:

{"Die Datei oder Assembly \"Internal.Xyz.DomainModel, Version=2.1.0.0, Culture=neutral, PublicKeyToken=d72343e09836ed19\" oder eine Abhängigkeit davon wurde nicht gefunden. Das System kann die angegebene Datei nicht finden.":"Internal.Xyz.DomainModel, Version=2.1.0.0, Culture=neutral, PublicKeyToken=d72343e09836ed19"}

Upvotes: 2

Views: 959

Answers (1)

squadwuschel
squadwuschel

Reputation: 3428

The problem is, that the T4 Template is not using the web.config inside the project, the T4 template is running in the context from visual studio and uses the config from visual Studio.

to solve the problem is editing the config from visual studio and adding the dependentAssembly bindings there

for VS 2015 use the following path

c:\users\[user]\AppData\Local\Microsoft\VisualStudio\14.0\devenv.exe.config

in VS 2017 you need to use this path

C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE

you NEED to RESTART visual studio

Upvotes: 2

Related Questions