How to find out the Public Key Token for an assembly (DLL) file?

In your ASP.NET project, you may need to use public key token of an assembly file while adding it as a reference. Here is an example reference declaration from project.csproj file:

<Reference Include="Oracle.DataAccess, Version=4.122.1.0, Culture=neutral, PublicKeyToken=89b483f429c47342, processorArchitecture=x86">
  <SpecificVersion>False</SpecificVersion> 
  <HintPath>\bin\Oracle.DataAccess.dll</HintPath>
</Reference>

As specified in Microsoft’s documentation, publicKeyToken field is optional. However, it is recommended to use it for a strong mapping of your assemblies.

Before going further; What is the public key token? A good description:

The public key token is a small number which is a convenient “token” representing a public key. Public keys are quite long; the purpose of the public key token is to let you refer to keys without saying the whole key.

The question comes up at this point: How to find out this value?

Find public key token value of an assembly file

The easiest way of finding out this value is that using Strong Name Tool. It is used for assembly signing, key management, signature generation and verification. Strong Name Tool is automatically installed with Visual Studio.

Follow the steps below to get values from your assembly file:

  1. Open Visual Studio Command Prompt (Start > Visual Studio > Visual Studio Tools > Developer Command Prompt)
  2. Run the command below. It will display both public key token and public key. Make sure to replace C:\Temp\Oracle.DataAccess.dll with the full path of your DLL file
sn -Tp C:\Temp\Oracle.DataAccess.dll

Assembly public key token

If you only want to see the public key token, you can only use -T parameter.

sn -T C:\Temp\Oracle.DataAccess.dll

Even if you configure everything correctly, you may run into “Could not load file or assembly” issue. Read about Assembly Binding Logging to troubleshoot these kind of issues.

Alternative Way (PowerShell)

If you don’t have Visual Studio installed, you can display the same information by using a PowerShell command. Follow the steps below to to view this information.

  1. Open PowerShell (Start > Search > PowerShell)
  2. Run the command below. Make sure to replace C:\Temp\Oracle.DataAccess.dll with the full path of your DLL file
([system.reflection.assembly]::loadfile("C:\Temp\Oracle.DataAccess.dll")).FullName

DLL public key token powershell

Reference:

What is the difference between Ref and Out keywords in C#?

We use ref and out keywords to pass parameters to a method. What exactly the different between these 2 keywords?

Here is an example:

// A simple example for usage of Ref keyword in C#

static void Main(string[] args)
{
    int myVariable = 5;

    string returnText = PrintNumber(ref myVariable);

    Console.WriteLine(returnText);
}

public static string PrintNumber(ref int myVariable)
{
    string returnText = "Result: " + myVariable.ToString();

    return returnText;
}

Output:

You may ask: “Why do we need to use ref or out? Can’t we just pass the parameter without using them?. The answer is “Yes”. However, if you don’t use ref or out, the parameters are sent to methods as values by default. It means, the parameter you pass becomes a new variable in the called method. If you change its value, the variable in the main method won’t change.

In contrast, if you use ref or out keywords,  parameter is passed to the method as reference. It means, it is still the same variable in the main method and called method. If you change the value of this variable, the value of the variable in the main method will change too.

Here is the extended version of our example above:

// Changing the value of the parameter passed by Ref keyword in C#
static void Main(string[] args)
{
    int myVariable = 5;

    Console.WriteLine("Initial value (in the main method): " + myVariable.ToString());

    string returnText = PrintNumber(ref myVariable);

    Console.WriteLine("New value (in the main method): " + returnText.ToString());
}

public static string PrintNumber(ref int myVariable)
{
    Console.WriteLine("Initial value (in the called method): " + myVariable.ToString());

    myVariable++;

    Console.WriteLine("New value (in the called method): " + myVariable.ToString());

    return myVariable.ToString();
}

Output:

Difference between Ref and Out keywords

The main difference is that you have to initialize (or assign a value to) variable in the main method before passing it by ref keyword. However, you don’t have to initialize it in the main method if you are passing it by out parameter.

// The example that shows the difference between ref and out keywords in C#

static void Main(string[] args)
{
    int myVariable = 5;

    string returnText1 = PrintNumber1(ref myVariable);
    string returnText2 = PrintNumber2(out myVariable);

    Console.WriteLine("Passed by ref: " + returnText1.ToString());
    Console.WriteLine("Passed by out: " + returnText2.ToString());

    Console.ReadLine();
}

public static string PrintNumber1(ref int myVariable)
{
    // No initialization here

    return myVariable.ToString();
}

public static string PrintNumber2(out int myVariable)
{
    myVariable = 6; // It must be initialized here

    return myVariable.ToString();
}

Output:

In the called method, if you don’t initialize a parameter passed by out keyword, you will receive the error messages below:

The out parameter ‘myVariable’ must be assigned to before control leaves the current
Use of unassigned out parameter ‘myVariable’

Comparison table

Ref Out
The parameter must be initialized before it is passed a method Parameter doesn’t have to be initialized in the main method
It is not required to initialize (or assign a value to) the parameter in the called method Parameter has to be initialized or a value should be assigned to it in the called method
Ref is useful when the called method needs to modify the passed parameter Out is useful when multiple values need to be returned from a function or method
Data can be passed bi-directionally. It work two-ways Data is passed only one-way (from the called method to the main method)

Notes:

  • Both ref and out keywords are treated the same at compile time but they are treated differently at run time
  • Properties cannot be passed by ref or out keywords

Bonus 1:

Starting from C# 7.0, you can declare variables on the fly while calling a method with out parameter:

int.TryParse("123", out int x);
Console.WriteLine(x);

The code lines above are equivalent to the following lines:

int x;
int.TryParse("123", out x);
Console.WriteLine(x);

Bonus 2:

You don’t have to pass variables/values for every out parameter defined by a method. You can ignore out parameters by simply passing an underscore (_). For example:

TestMethod(out _, out _, out int number, out _)

In the example above, we ignored all out parameters except the third one.

References

How to enable Assembly Binding Logging debugging .NET applications?

If you receive an “Assembly not found” error for your .NET application hosted in IIS, you may feel hopeless for not having much debugging details. IIS doesn’t show the binding trace as this slows down applications. However, there is a way to enable logging.

Example of an assembly/DLL binding issue:

Could not load file or assembly ‘Oracle.DataAccess’ or one of its dependencies. An attempt was made to load a program with an incorrect format.

Solution

Use this quick trick to enable Assembly Binding Logging so that you can see where the binding is failing.

  1. Go to “Start > Run”
  2. Enter “regedit
  3. Go to this key: “HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Fusion”
  4. Change the value of “EnableLog” to “1”. If this key doesn’t exist, create it with DWORD(32-bit) type

Note: Another way of viewing details for failed assembly bindings is to use Microsoft’s Fuslogvw tool. This tool is installed with Visual Studio. Refer to the link below for more details.

Reference: