Could not load file or assembly 'Microsoft.WindowsAzure.ServiceRuntime, Version=1.7.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified

Could not load file or assembly exception for an Web/Worker Role hosted on Windows Azure PaaS deployment is very common and this kind of error messages are noticed from the inception of Windows Azure PaaS Roles. The common reason for this this exception is due to missing binaries from the Guest VM, where the application is deployed, and to resolve this issue, you can make sure copy local property for the particular assembly to true. There is n number of blogs were written about this behavior. So I know, you must be thinking, why again a new blog with the same issue, which was published in numerous blog and msdn article ?

One of the main reasons for this blog is, If you noticed the name of the assembly, ‘'Microsoft.WindowsAzure.ServiceRuntime’ , which is a part of Windows Azure runtime, and by default this assembly should be present in any Windows Azure VM, and developer should not need to include this assembly in the package. So I just thought of provide you full investigation details and fix on this particular blog, and also this blog will give you an information on how to leverage the .Net Framework Fusion log effectively, while troubleshooting the issues from Windows Azure VM

INVESTIGATION DETAILS

 

1. We got below mentioned error message, while browsing the web site

 

Could not load file or assembly 'Microsoft.WindowsAzure.ServiceRuntime, Version=1.7.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.WindowsAzure.ServiceRuntime, Version=1.7.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.

2.  I have rdped into the Virtual Machine, were application was deployed.

3.  Enabled the Microsoft .Net Framework Fusion log by following the below steps

                a.  Open the Registry [ Start –> Run –> Regedit.ext ]

                b.  Navigate to the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Fusion

                c. Created a DWORD value ‘EnableLog’ with the value ‘1’

4. After enabling the Fusion log, I start getting the proper error message as given below :

=== Pre-bind state information ===
LOG: User = NT AUTHORITY\NETWORK SERVICE
LOG: DisplayName = Microsoft.WindowsAzure.ServiceRuntime, Version=1.7.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
  (Fully-specified)
LOG: Appbase = file:///E:/sitesroot/0/
LOG: Initial PrivatePath = E:\sitesroot\0\bin
Calling assembly : Microsoft.WindowsAzure.Diagnostics, Version=1.7.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: E:\sitesroot\0\web.config
LOG: Using host configuration file: D:\Windows\Microsoft.NET\Framework64\v4.0.30319\aspnet.config
LOG: Using machine configuration file from D:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.config.
LOG: Redirect found in application configuration file: 1.7.0.0 redirected to 1.8.0.0.
LOG: Post-policy reference: Microsoft.WindowsAzure.ServiceRuntime, Version=1.8.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
LOG: The same bind was seen before, and was failed with hr = 0x80070002.

5. The highlighted area  in the above log indicates that there is an assembly re-direct attribute in the configuration file [Web.config/ machine.config]

6. I have reviewed the web.config file and found below entry in the web.config

<snip> 
< assemblyBinding>
    <snip>
    < dependentAssembly>
        < assemblyIdentity name="Microsoft.WindowsAzure.ServiceRuntime" publicKeyToken="31bf3856ad364e35" culture="neutral" />
         <bindingRedirect oldVersion="0.0.0.0-1.8.0.0" newVersion="1.8.0.0" />
      </dependentAssembly>
< /assemblyBinding>
< snip>

7.  I have comment out binding redirect entry from the web.config file, and redeploy the application, which eventually  resolved the issue.

 

SUMMARY

It is not a recommended or good practice to modify any of the Windows Azure runtime or infrastructure details manually.