How to Resolve Error "CS0281: Friend access was granted to 'AssemblyName1', but the output assembly is named 'AssemblyName2'" in C#
The Compiler Error CS0281 is an identity mismatch error related to the [InternalsVisibleTo] attribute.
In C#, you can grant "Friend Access" to another assembly, allowing it to see your internal members (commonly used for Unit Testing). You do this by adding the [InternalsVisibleTo("Name")] attribute to your source project.
This error occurs when Project A grants access to a specific name (e.g., "MyTests"), but the project trying to use that access compiles into an assembly with a different name (e.g., "MyTests.Integration"). The compiler rejects the connection because the "Guest" does not match the name on the "Invitation."
Understanding InternalsVisibleTo
- Provider (Host): The project containing the internal code. It declares
[assembly: InternalsVisibleTo("ConsumerName")]. - Consumer (Guest): The project referencing the Provider. Its Assembly Name must match "ConsumerName" exactly.
If the Consumer's assembly name differs even slightly (case sensitivity, extra suffixes, or completely different names), CS0281 is triggered.
Scenario: The Name Mismatch
This often happens when you rename a project in the Solution Explorer but forget to update the [InternalsVisibleTo] string in the referenced code, or if you manually set a custom <AssemblyName> in the .csproj file.
The Setup
1. The Provider (CoreLogic.dll)
Defines an internal class and grants access to a specific friend named "UnitTestProject".
using System.Runtime.CompilerServices;
// Grant access strictly to "UnitTestProject"
[assembly: InternalsVisibleTo("UnitTestProject")]
public class Calculator
{
internal int Add(int a, int b) => a + b;
}
2. The Consumer (RenamedProject.dll)
You created a project and perhaps renamed it to UnitTestProject.V2, or the project file settings specify a different output name.
RenamedProject.csproj:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<!-- The actual name of the DLL produced is 'UnitTestProject.V2' -->
<AssemblyName>UnitTestProject.V2</AssemblyName>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>
</Project>
The Code causing the Error:
public class Tests
{
public void TestAdd()
{
var calc = new Calculator();
// ⛔️ Error CS0281: Friend access was granted to 'UnitTestProject',
// but the output assembly is named 'UnitTestProject.V2'.
// This assembly is not on the guest list.
int result = calc.Add(1, 2);
}
}
Solution 1: Update the Attribute (The "Invitation")
If the consumer's name is correct (e.g., you intentionally renamed your test project), you must update the source code in the Provider project to match the new name.
Update CoreLogic.cs:
using System.Runtime.CompilerServices;
// ✅ Correct: Updated to match the actual output name of the test project.
[assembly: InternalsVisibleTo("UnitTestProject.V2")]
public class Calculator
{
internal int Add(int a, int b) => a + b;
}
Solution 2: Rename the Output Assembly (The "Guest")
If the attribute in the Provider is correct (e.g., strictly "UnitTestProject"), but your Consumer project is outputting the wrong name, you need to change the Consumer's configuration.
Option A: Edit .csproj
Open the Consumer's project file and change the <AssemblyName>.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<!-- ✅ Correct: Renamed to match the InternalsVisibleTo string -->
<AssemblyName>UnitTestProject</AssemblyName>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>
</Project>
Option B: Visual Studio Properties
- Right-click the Consumer Project > Properties.
- Go to the Application (or General) tab.
- Find Assembly name.
- Change it to match the string in the
[InternalsVisibleTo]attribute.
Strong Naming:
If the Provider assembly is Strong-Named (Signed), the InternalsVisibleTo attribute must include the consumer's Public Key as well (e.g., [InternalsVisibleTo("Name, PublicKey=...")]). If the names match but the key is missing or wrong, you might get CS0281 or related errors indicating a signature mismatch.
Conclusion
CS0281 is the compiler checking IDs at the door.
- Check the Attribute: Look at the
[InternalsVisibleTo("X")]line in the Provider code. - Check the Output: Look at the Assembly Name property of the project generating the error.
- Match Them: Change either the attribute string or the project's assembly name so they are identical strings.