49

I have two assemblies that unfortunately define the same type in the same namespace. I'm trying to use a an extern alias to work around the problem. In the Visual Studio IDE I have set the "Aliases" property of the reference to my alias. This is supposed to change the C# compiler command line to be something like this:

/reference:MyAlias=MyAssembly.dll

But it doesn't actually do that. The Visual Studio IDE seems to just ignore the Aliases property setting on the reference. So when I go and add the line extern alias MyAlias; at the top of my C# code file I get the error that the alias was not specified in a /reference option to the compiler. I can't figure out what I am doing wrong. Any ideas?

Brian Ensink
  • 11,092
  • 3
  • 50
  • 63
  • 1
    Please post the build output (especially the line that runs Csc.exe). – Allon Guralnek Apr 26 '10 at 07:11
  • 1
    Its VS2008 actually but yes that is my conclusion as well. – Brian Ensink May 03 '10 at 14:40
  • The build output can be seen in another question here http://stackoverflow.com/questions/2503232/visual-studio-compiles-wpf-application-twice-during-build. I now believe these issues are related. The build fails because the first compiler line does not include the alias. – Brian Ensink May 03 '10 at 14:42

16 Answers16

25

I have the same problem and I was able to reproduce the issue.

It turns out reference aliases are ignored on projects containing xaml files which has an xmlns definition to the output assembly like xmlns:local='clr-namespace:TestProject'.

If you think this is your case as well, please vote up my bug report at Microsoft Connect.

EDIT: There is a suggested workaround in the link above which requires editing the project file manually. In order for this to work, I had to give full path of the assembly. Add the following instructions to the end of your project file:

<Target Name="solveAliasProblem" >
<ItemGroup>
 <ReferencePath Remove="FullPath.dll"/>
 <ReferencePath Include="FullPath.dll">
    <Aliases>ourAlias</Aliases>
 </ReferencePath>
</ItemGroup>
</Target>
<PropertyGroup>
    <CoreCompileDependsOn>solveAliasProblem;$(PrepareResourcesDependsOn)</CoreCompileDependsOn>
</PropertyGroup>
orcun
  • 14,969
  • 1
  • 22
  • 25
  • I tried the above code and ended with this new error - Could not load file or assembly '[AliasName]' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. – Nithin Paul Aug 01 '14 at 10:57
  • The problem I ran into was that I typed in an alias and clicked away and I didn't realize that it didn't save. I had to click in another field in the properties window to commit it. – The Muffin Man Mar 14 '15 at 00:05
12

Brian, I had the same problem as you and I figured out how to fix it.

I would do the same thing as you:

  1. Use the properties window to change the alias for the assembly from 'global' to 'MyAlias'
  2. At the top of the file where the aliased assembly is used, put extern alias MyAlias. This must be before any using statements.
  3. Use the alias prefix to use the namespace you want, for example using MyAlias::MyNamespace.

And I would still get the error. After screwing around a bit, I figured out that the way to fix it is to set the referenced dll to a dll that is outside of the solution you are working in. Once I did that, the error message we were both seeing went away, and I was able to continue working on my project.

I hope that helps, happy coding!

sooprise
  • 22,657
  • 67
  • 188
  • 276
10

An update: It is a bug. It will be fixed in .Net 4.5.

As seen on the Microsoft Connect bug report.

Frederic Gaudet
  • 161
  • 1
  • 5
5

Working for me in VS2008 using these steps:

  1. Use the properties window to change the alias for the assembly from 'global' to 'MyAlias'
  2. At the top of the file where the aliased assembly is used, put extern alias MyAlias. This must be before any using statements.
  3. Use the alias prefix to use the namespace you want, for example using MyAlias::MyNamespace.
andypaxo
  • 6,171
  • 3
  • 38
  • 53
3

If you install .Net Framework 4.5 it will update your MSBuild to Microsoft (R) Build Engine version 4.0.30319.17929 which fixed this issue for my team.

Download .Net Framework 4.5:
http://www.microsoft.com/en-us/download/details.aspx?id=30653

Charlie
  • 846
  • 7
  • 21
2

I had exact similar issue, looks like when you clean your solution and build the project, first time MSBuild ignores references and gives error (may be it does not have some assemblies built yet with the references specified), however on second try of the build, it works.

Akash Kava
  • 39,066
  • 20
  • 121
  • 167
2

I had the same problem and it was resolved by doing the following.

Go to properties window of the assembly with the conflict and change the Aliases property from 'global' to 'X' (X - your alias name)

Hope this helps.

1

Set the project's target framework to .NET 4.5.1.

kat
  • 577
  • 4
  • 7
1

myprojectHad the same issue, but the above solutions didn't work my project file looked like

    <Reference Include="AntiXSSLibrary" Condition="'$(TargetFrameworkVersion)' == 'v4.0' or '$(TargetFrameworkVersion)' == 'v4.5'">
  <HintPath>..\Dependencies\Third-party\.net 4\AntiXSSLibrary.dll</HintPath>
  <Aliases>antixss</Aliases>
</Reference>
<Reference Include="AntiXssLibrary">
  <HintPath>..\..\lib\myproject\AntiXssLibrary.dll</HintPath>     
</Reference>

Adding the alias the actual project dll worked. So

    <Reference Include="AntiXSSLibrary" Condition="'$(TargetFrameworkVersion)' == 'v4.0' or '$(TargetFrameworkVersion)' == 'v4.5'">
  <HintPath>..\Dependencies\Third-party\.net 4\AntiXSSLibrary.dll</HintPath>
  <Aliases>antixss</Aliases>
</Reference>
<Reference Include="AntiXssLibrary">
  <HintPath>..\..\lib\myproject\AntiXssLibrary.dll</HintPath>
  <Aliases>antixss</Aliases>
</Reference>
Sean Fleming
  • 121
  • 7
1

I had the same problem and I finally found that the .csproj file has target version of 4.5 while the dll which I faced the problem with is targeted to 4.5.2.

I changed my .csproj file target version to 4.5.2 and got successful build.

Sudheer
  • 81
  • 7
1

I had the same problem, and I could solve it only when I changed the target version to 4.6.1. The versions 4, 4.5, 4.5.1, or 4.6 did not help.

JarmoP
  • 331
  • 3
  • 3
1

Slightly different solution from the one orcun's suggested:

Manually add this to the project csproj file:

  <Target Name="ChangeAliasesOfStrongNameAssemblies" BeforeTargets="FindReferenceAssembliesForReferences;ResolveReferences">
    <ItemGroup>
      <!-- Use the file name, no need to specify the full path. -->
      <ReferencePath Condition="'%(FileName)' == 'StackExchange.Redis.StrongName'">
        <!-- Give the desired alias to the file here -->
        <Aliases>myAlias</Aliases>
      </ReferencePath>
    </ItemGroup>
  </Target>

Than, you can use it by adding the alias on the top of your using list:

extern alias myAlias;
...
var test = new myAlias::Fully.Qualified.NameSpace();

BTW, this one works for both References and PackageReferences!

Solution was found here: https://github.com/NuGet/Home/issues/4989#issuecomment-311042085

itsho
  • 4,640
  • 3
  • 46
  • 68
1

Our Build agent had this issue for a .net core 3.1 app. Fixed it by installing .net 5.0 sdk on the build agent, which upgraded the build engine version and it worked.

0

Please check your Project Target framework Version. Must be same version.

I change the same version fixed the issue.

enter image description here

Jason Yang
  • 11
  • 1
0

Had the same issue after installing some NUget packages if the path to my project contains "=" sign. I had a directory like "D:\=Projects..." and renaming it solved the issue.

Vladimir
  • 429
  • 7
  • 21
0

Using core 3.1 and VS 16.10.4:

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <RuntimeIdentifier>win-x64</RuntimeIdentifier>
    <OutputType>Exe</OutputType>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Hl7.Fhir.STU3" Version="3.4.0">
      <Aliases>Stu3</Aliases>
    </PackageReference>
    <PackageReference Include="Hl7.Fhir.R4" Version="3.4.0">
      <Aliases>R4</Aliases>
    </PackageReference>
  </ItemGroup>

  <ItemGroup>
    <TransitivePackageReference Include="Hl7.Fhir.STU3.Core" Alias="STU3" />
    <TransitivePackageReference Include="Hl7.Fhir.R4.Core" Alias="R4" />
  </ItemGroup>

</Project>
Jon Ediger
  • 949
  • 1
  • 10
  • 27