1

I’m trying to change the Assembly name of my dll during build. I’ve created another exe that changes the assembly name in the csproj file, which I execute during pre-build. But it seems like my changes to the csproj file only take effect after the build has finished, which is of course not what I want.

The assembly name has to be unique every time it gets built, so I create it by appending a GUID.

var xmlDoc = XDocument.Load(projectFile);
XNamespace ns = "http://schemas.microsoft.com/developer/msbuild/2003";
string newAssemblyName = originalAssemblyName + "_" + Guid.NewGuid().ToString("N");

xmlDoc.Element(ns + "Project").Element(ns + "PropertyGroup").Element(ns + "AssemblyName").Value = newAssemblyName;

xmlDoc.Save(projectFile);

I was wondering if there is maybe a way to ‘force’ reload the csproj during pre-build or if there is another way I could get a unique assembly name everytime I build my solution.

VincentC
  • 245
  • 4
  • 14
  • Copy after build could be cheap and dirty solution... Also what you doing feels strange and possibly there is better solution for your original problem you trying to work around (like unload plugin app-domain). – Alexei Levenkov May 06 '15 at 23:54
  • I'm not saying you're doing something wrong, but I'm also *extremely* curious about the reason you need a unique assembly name each time. – Ron Beyer May 07 '15 at 00:13
  • I am indeed loading the dll in another app. My original problem was that the other app locks the dll so I need to shut it down, build my dll, load the app again. So I tried to automate my manual workaround. I've tried renaming the old dll and then building a new dll, but I think somehow the app I load it in has it cached or something because the only way I could get it to load my new dll is by changing the Assembly name. But I'm open for better solutions of course too! – VincentC May 07 '15 at 00:16
  • Why don't the applications using the DLL copy it to a local temporary directory before they load it? This way the absolute path creates a unique dll name per application using it. – Steve May 07 '15 at 01:05

1 Answers1

1

I had some success doing this inside the csproj file in VS2013:

<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
[...]
  <PropertyGroup>
    <MyNewGuid>$([System.Guid]::NewGuid())</MyNewGuid>
  </PropertyGroup>
[...]
  <AssemblyName>$(MyNewGuid)</AssemblyName>

At least, every time I do a Rebuild it appears to generate an assembly with a different GUID for a name.

If you are using an especially old version of msbuild, you might want to look here for an alternative way to create GUIDs: http://phoebix.com/2013/08/08/who-got-the-func-part-3-generating-a-guid-in-msbuild/

ravuya
  • 8,586
  • 4
  • 30
  • 33