How to Specify AssemblyKeyFile Attribute in .NET Assembly and Issues

Posted on Microsoft .NET Support Team See other posts from Microsoft .NET Support Team
Published on Mon, 28 Dec 2009 10:43:00 +0000 Indexed on 2010/03/16 15:31 UTC
Read the original article Hit count: 836

How to specify strong key file in assembly?

Answer: You can specify snk file information using following line
[assembly: AssemblyKeyFile(@"c:\Key2.snk")]

Where to specify an strong key file (snk file)?

Answer: You have two options to specify the AssemblyKeyFile infromation.
1. In class
2. In AssemblyInfo.cs

[assembly: AssemblyKeyFile(@"c:\Key2.snk")]

1. In Class you must specify above line before defining namespace of the class and after all the imports or usings

Example: See Line 7 in bellow sample class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

[assembly: AssemblyKeyFile(@"c:\Key1.snk")]
namespace Csharp3Part1
{
class Person
{
public string GetName()
{
return "Smith";
}
}
}

2. In AssemblyInfo.cs

You can aslo specify assembly information in AssemblyInfo.cs

Example: See Line 16 in bellow sample AssemblyInfo.cs

using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Csharp3Part1")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Deloitte")]
[assembly: AssemblyProduct("Csharp3Part1")]
[assembly: AssemblyCopyright("Copyright © Deloitte 2009")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: AssemblyKeyFile(@"c:\Key1.snk")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("4350396f-1a5c-4598-a79f-2e1f219654f3")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
Issues:

You should not sepcify this in following ways.

1. In multiple classes.
2. In both class and AssemblyInfo.cs
If you did wrong in either one of the above ways, Visual Studio or C#/VB.NET compilers shows following Error Duplicate 'AssemblyKeyFile' attribute and warning

Use command line option '/keyfile' or appropriate project settings instead of 'AssemblyKeyFile'

To avoid this, Please specity your keyfile information only one time either only in one class or in AssemblyInfo.cs file. It is suggested to specify this at AssemblyInfo.cs file

You might also encounter the errors like Error: type or namespace name 'AssemblyKeyFileAttribute' and 'AssemblyKeyFile' could not be found. Solution. Please find here

© Microsoft .NET Support Team or respective owner

Related posts about .NET Assembly

Related posts about .NET Tips and Tricks