Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Version 5.4 causes an exception trying to load Microsoft.Azure.Services.AppAuthentication #283

Closed
arex388 opened this issue May 9, 2020 · 22 comments
Labels
bug net-framework This issue is related to the legacy .NET Framework. up-for-grabs This issue waits for a contributor to fix it.

Comments

@arex388
Copy link

arex388 commented May 9, 2020

Please clearly describe what the SQL Sink is doing incorrectly:

As the title says, the latest version (5.4) causes an exception because its trying to load Microsoft.Azure.Services.AppAuthentication which doesn't exist. I don't use any Azure services and have never referenced any in my solution. Weirdly, it works fine when debugging locally, but as soon as its published to my server (IIS 10 on Windows Server 2019 on AWS EC2) the exception is thrown and I'm greeted with an ANCM 500.30 error. I've had to revert to 5.3 to avoid the exception on the server.

Here's the exception trace for details:

Unhandled exception. System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Azure.Services.AppAuthentication, Version=1.4.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified.

File name: 'Microsoft.Azure.Services.AppAuthentication, Version=1.4.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'

   at Serilog.Sinks.MSSqlServer.Sinks.MSSqlServer.Platform.AzureManagedServiceAuthenticator..ctor(Boolean useAzureManagedIdentity, String azureServiceTokenProviderResource)

   at Serilog.Sinks.MSSqlServer.Sinks.MSSqlServer.Dependencies.SinkDependenciesFactory.Create(String connectionString, SinkOptions sinkOptions, IFormatProvider formatProvider, ColumnOptions columnOptions, ITextFormatter logEventFormatter)

   at Serilog.Sinks.MSSqlServer.MSSqlServerSink..ctor(String connectionString, SinkOptions sinkOptions, IFormatProvider formatProvider, ColumnOptions columnOptions, ITextFormatter logEventFormatter)

   at Serilog.Sinks.MSSqlServer.Configuration.Factories.MSSqlServerSinkFactory.Create(String connectionString, SinkOptions sinkOptions, IFormatProvider formatProvider, ColumnOptions columnOptions, ITextFormatter logEventFormatter)

   at Serilog.LoggerConfigurationMSSqlServerExtensions.MSSqlServerInternal(LoggerSinkConfiguration loggerConfiguration, String connectionString, SinkOptions sinkOptions, IConfigurationSection sinkOptionsSection, IConfiguration appConfiguration, LogEventLevel restrictedToMinimumLevel, IFormatProvider formatProvider, ColumnOptions columnOptions, IConfigurationSection columnOptionsSection, ITextFormatter logEventFormatter, IApplySystemConfiguration applySystemConfiguration, IApplyMicrosoftExtensionsConfiguration applyMicrosoftExtensionsConfiguration, IMSSqlServerSinkFactory sinkFactory)

   at Serilog.LoggerConfigurationMSSqlServerExtensions.MSSqlServer(LoggerSinkConfiguration loggerConfiguration, String connectionString, SinkOptions sinkOptions, IConfigurationSection sinkOptionsSection, IConfiguration appConfiguration, LogEventLevel restrictedToMinimumLevel, IFormatProvider formatProvider, ColumnOptions columnOptions, IConfigurationSection columnOptionsSection, ITextFormatter logEventFormatter)

   at Serilog.LoggerConfigurationMSSqlServerExtensions.MSSqlServer(LoggerSinkConfiguration loggerConfiguration, String connectionString, String tableName, IConfiguration appConfiguration, LogEventLevel restrictedToMinimumLevel, Int32 batchPostingLimit, Nullable`1 period, IFormatProvider formatProvider, Boolean autoCreateSqlTable, ColumnOptions columnOptions, IConfigurationSection columnOptionsSection, String schemaName, ITextFormatter logEventFormatter)

   at Serilog.SerilogExtensions.ApplicationSqlServer(LoggerSinkConfiguration configuration, String connectionString) in REDACTED\Extensions\SerilogExtensions.cs:line 52

   at Serilog.LoggerConfigurationExtensions.Update(LoggerConfiguration logger, HostBuilderContext builder) in REDACTED\Extensions\LoggerConfigurationExtensions.cs:line 24

   at REDACTED.Program.<>c.<Main>b__0_1(HostBuilderContext builder, LoggerConfiguration logger) in REDACTED\Program.cs:line 16

   at Serilog.SerilogHostBuilderExtensions.<>c__DisplayClass1_0.<UseSerilog>b__0(HostBuilderContext context, IServiceCollection collection)

   at Microsoft.Extensions.Hosting.HostBuilder.CreateServiceProvider()

   at Microsoft.Extensions.Hosting.HostBuilder.Build()

   at REDACTED.Program.Main(String[] args) in REDACTED\Program.cs:line 10

   at REDACTED.Program.<Main>(String[] args)

Please clearly describe the expected behavior:

Not throwing an exception for Microsoft.Azure.Services.AppAuthentication. It seems to be looking for regardless if its used or not.

List the names and versions of all Serilog packages used in the project:

  • Serilog: 2.9.0
  • Serilog.AspNetCore: 3.2.0
  • Serilog.Sinks.MSSqlServer: 5.3.0

Target framework and operating system:

  • .NET Core 3.1
  • Windows Server 2019

Provide a simple reproduction of your Serilog configuration code:

N/A

Provide a simple reproduction of your Serilog configuration file, if any:

N/A

Provide a simple reproduction of your application code:

N/A

@darrenschwarz
Copy link

Please clearly describe what the SQL Sink is doing incorrectly:

As the title says, the latest version (5.4) causes an exception because its trying to load Microsoft.Azure.Services.AppAuthentication which doesn't exist. I don't use any Azure services and have never referenced any in my solution. Weirdly, it works fine when debugging locally, but as soon as its published to my server (IIS 10 on Windows Server 2019 on AWS EC2) the exception is thrown and I'm greeted with an ANCM 500.30 error. I've had to revert to 5.3 to avoid the exception on the server.

Here's the exception trace for details:

Unhandled exception. System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Azure.Services.AppAuthentication, Version=1.4.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified.

File name: 'Microsoft.Azure.Services.AppAuthentication, Version=1.4.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'

   at Serilog.Sinks.MSSqlServer.Sinks.MSSqlServer.Platform.AzureManagedServiceAuthenticator..ctor(Boolean useAzureManagedIdentity, String azureServiceTokenProviderResource)

   at Serilog.Sinks.MSSqlServer.Sinks.MSSqlServer.Dependencies.SinkDependenciesFactory.Create(String connectionString, SinkOptions sinkOptions, IFormatProvider formatProvider, ColumnOptions columnOptions, ITextFormatter logEventFormatter)

   at Serilog.Sinks.MSSqlServer.MSSqlServerSink..ctor(String connectionString, SinkOptions sinkOptions, IFormatProvider formatProvider, ColumnOptions columnOptions, ITextFormatter logEventFormatter)

   at Serilog.Sinks.MSSqlServer.Configuration.Factories.MSSqlServerSinkFactory.Create(String connectionString, SinkOptions sinkOptions, IFormatProvider formatProvider, ColumnOptions columnOptions, ITextFormatter logEventFormatter)

   at Serilog.LoggerConfigurationMSSqlServerExtensions.MSSqlServerInternal(LoggerSinkConfiguration loggerConfiguration, String connectionString, SinkOptions sinkOptions, IConfigurationSection sinkOptionsSection, IConfiguration appConfiguration, LogEventLevel restrictedToMinimumLevel, IFormatProvider formatProvider, ColumnOptions columnOptions, IConfigurationSection columnOptionsSection, ITextFormatter logEventFormatter, IApplySystemConfiguration applySystemConfiguration, IApplyMicrosoftExtensionsConfiguration applyMicrosoftExtensionsConfiguration, IMSSqlServerSinkFactory sinkFactory)

   at Serilog.LoggerConfigurationMSSqlServerExtensions.MSSqlServer(LoggerSinkConfiguration loggerConfiguration, String connectionString, SinkOptions sinkOptions, IConfigurationSection sinkOptionsSection, IConfiguration appConfiguration, LogEventLevel restrictedToMinimumLevel, IFormatProvider formatProvider, ColumnOptions columnOptions, IConfigurationSection columnOptionsSection, ITextFormatter logEventFormatter)

   at Serilog.LoggerConfigurationMSSqlServerExtensions.MSSqlServer(LoggerSinkConfiguration loggerConfiguration, String connectionString, String tableName, IConfiguration appConfiguration, LogEventLevel restrictedToMinimumLevel, Int32 batchPostingLimit, Nullable`1 period, IFormatProvider formatProvider, Boolean autoCreateSqlTable, ColumnOptions columnOptions, IConfigurationSection columnOptionsSection, String schemaName, ITextFormatter logEventFormatter)

   at Serilog.SerilogExtensions.ApplicationSqlServer(LoggerSinkConfiguration configuration, String connectionString) in REDACTED\Extensions\SerilogExtensions.cs:line 52

   at Serilog.LoggerConfigurationExtensions.Update(LoggerConfiguration logger, HostBuilderContext builder) in REDACTED\Extensions\LoggerConfigurationExtensions.cs:line 24

   at REDACTED.Program.<>c.<Main>b__0_1(HostBuilderContext builder, LoggerConfiguration logger) in REDACTED\Program.cs:line 16

   at Serilog.SerilogHostBuilderExtensions.<>c__DisplayClass1_0.<UseSerilog>b__0(HostBuilderContext context, IServiceCollection collection)

   at Microsoft.Extensions.Hosting.HostBuilder.CreateServiceProvider()

   at Microsoft.Extensions.Hosting.HostBuilder.Build()

   at REDACTED.Program.Main(String[] args) in REDACTED\Program.cs:line 10

   at REDACTED.Program.<Main>(String[] args)

Please clearly describe the expected behavior:

Not throwing an exception for Microsoft.Azure.Services.AppAuthentication. It seems to be looking for regardless if its used or not.

List the names and versions of all Serilog packages used in the project:

  • Serilog: 2.9.0
  • Serilog.AspNetCore: 3.2.0
  • Serilog.Sinks.MSSqlServer: 5.3.0

Target framework and operating system:

  • .NET Core 3.1
  • Windows Server 2019

Provide a simple reproduction of your Serilog configuration code:

N/A

Provide a simple reproduction of your Serilog configuration file, if any:

N/A

Provide a simple reproduction of your application code:

N/A

Hi @arex388, Would you be able to provide a working (or not as the case may be) sample application which reliably reproduces the problem in AWS, I'd then be more than happy to take a look and try to resolve for you. No Charge! :)

@ckadluba
Copy link
Member

Hi @arex388! Thank you for informing us about the issue.

Can you please give us the list of files in the output folder after you called dotnet publish on your app? Can you please also check if all those files were deployed to the target machine without errors?

@ronaldbarendse
Copy link

Looks like the dependency on Microsoft.Azure.Services.AppAuthentication is only added for the netcoreapp2.2 and net472 targets:

https://github.com/serilog/serilog-sinks-mssqlserver/blob/5dabed522d02860466fa726c19038b6a17c70856/src/Serilog.Sinks.MSSqlServer/Serilog.Sinks.MSSqlServer.csproj#L77-L87

IMO Serilog.Sinks.MSSqlServer shouldn't have an Azure specific dependency, but only provide a way to set this up (using interfaces, like PR #265 seems to achive). So if you'd require Managed Service Identities (PR #222), you'd need to install this dependency yourself or this should be bundled in a new package (e.g. Serilog.Sinks.MSSqlServer.AzureMsi).

@ckadluba
Copy link
Member

Looks like the dependency on Microsoft.Azure.Services.AppAuthentication is only added for the netcoreapp2.2 and net472 targets:

These conditions do specify minimum versions. The Azure Manages Identities features also works when using the sink in a program targeting .NET Core 3.1. I tested this and documented the result in PR 222:

#222 (comment)

The conditional dependency is necessary because the referenced assembly is not supported on all frameworks the sink targets. On the frameworks where it is not supported, the dependency is not used and the implementation is swapped with a stub that does nothing except throwing an exception when enabling the feature by configuration.

@ckadluba
Copy link
Member

ckadluba commented May 12, 2020

But probably we can change the code to call the ctor of AzureManagedServiceAuthenticator only if Azure Managed Identities is enabled.

I will take a look into it.

@ckadluba ckadluba self-assigned this May 12, 2020
@darrenschwarz
Copy link

But probably we can change the code to call the ctor of AzureManagedServiceAuthenticator only if Azure Managed Identities is enabled.

I will take a look into it.

Hi @ckadluba could we have a catch up?Can provide the temp email again if needed.

@ckadluba
Copy link
Member

Hi @ckadluba could we have a catch up?Can provide the temp email again if needed.

I sent you my contact now. :)

@ckadluba
Copy link
Member

ckadluba commented May 17, 2020

But probably we can change the code to call the ctor of AzureManagedServiceAuthenticator only if Azure Managed Identities is enabled.

I will take a look into it.

I thought about this. I think Microsoft.Azure.Services.AppAuthentication is a dependency like any other and should not require any special treatment in the sink. The assembly should be deployed with the consumer app along with all other nuget packages.

@arex388 have you had yet an opportunity to check the publish artifacts and the deployment of your app? Is the assembly missing there?

@arex388
Copy link
Author

arex388 commented May 18, 2020

@ckadluba I haven’t had a chance to yet. I’ve been stuck doing post-release bug fixes on the project where I discovered the issue. Once things begin to settle down with it I’ll take a look, but it most likely will be at least a week or two before that happens. Hopefully...

@FletcherJohnsonZ
Copy link

FletcherJohnsonZ commented Jun 18, 2020

Hi folks - I have this same problem. What I have found:
I remove the MSQL Sink and then the Azure library (I don't use Azure either). I comment out the references. Everything works. And there are NO Azure libraries.

I add the MSQL Sink back to my application (which adds the 1.4 version of the Azure library) via NuGet. Rebuild the solution. Then I reenable the commented out code that builds the Sink. When I run, I get an exception. It appears that there is an accidental reference to both the 1.5 and 1.4 version of the Azure library.

Why would the Sink install the 1.4 version of the Azure library when it needs the 1.5? I still get errors if I install the 1.5 version of the Azure library.

Here is the exception (note the 1.5 reference):
Could not load file or assembly 'Microsoft.Azure.Services.AppAuthentication, Version=1.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

And here is the Inner Exception (note the 1.4 reference):
InnerException {"Could not load file or assembly 'Microsoft.Azure.Services.AppAuthentication, Version=1.4.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)":"Microsoft.Azure.Services.AppAuthentication, Version=1.4.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"} System.Exception {System.IO.FileLoadException}

If I try to install the 1.5 version of the Azure library via NuGet, I get the following error:TF400024: The change on xxx\packages\Microsoft.Azure.Services.AppAuthentication.1.5.0\Microsoft.Azure.Services.AppAuthentication.1.5.0.nupkg cannot be undone because a file already exists at xxx\packages\Microsoft.Azure.Services.AppAuthentication.1.5.0\Microsoft.Azure.Services.AppAuthentication.1.5.0.nupkg. The file must be deleted from disk for the undo to succeed.

I have tried everything. The only thing that seems to work is going with an older version of the MSQ sink that doesn't want the Azure library.

@FletcherJohnsonZ
Copy link

One more thing. It appears to be related to the addition of SinkOptions in 5.4. If I go back to v. 5.3 I am able to run.

@ckadluba
Copy link
Member

@FletcherJohnsonZ thank you for the investigation and for sharing your findings. Do you have a sample project to reproduce the problem that you could provide me?

Some points about your observations:

  • The .NET Framework 4.7+ and .NET Core 2.2+ builds of Serilog.Sinks.MSSqlServer.dll reference Microsoft.Azure.Services.AppAuthentication 1.4.0. There should be no references to version 1.5.0 of that assembly. Here is the relevant snippet from the csproj file.
<ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp2.2' Or '$(TargetFramework)' == 'net472' ">
<PackageReference Include="System.Configuration.ConfigurationManager" Version="4.5.0" />
<PackageReference Include="Microsoft.Data.SqlClient" Version="1.1.3" />
<PackageReference Include="Microsoft.Azure.Services.AppAuthentication" Version="1.4.0" />
  • In version 5.4.0 of the sink we added support for Azure manages identities and that was also the PR that introduced the nuget reference to Microsoft.Azure.Services.AppAuthentication 1.4.0. The problem has nothing to do with the introduction of the SinkOptions class (which was also added in 5.4.0).

@FletcherJohnsonZ
Copy link

FletcherJohnsonZ commented Jun 21, 2020 via email

@ckadluba
Copy link
Member

@FletcherJohnsonZ thanks for the sample. I'll try to reproduce the problem but I'm afk for the next two weeks, so I can probably do it only after the 7th of july.

What target framework does your project use?

@FletcherJohnsonZ
Copy link

FletcherJohnsonZ commented Jun 22, 2020 via email

@ckadluba
Copy link
Member

ckadluba commented Jul 25, 2020

I tried to reproduce and fix the problem you noticed but it seems to be more complicated than I expected. When I use a sample targeting .NET Framework (like samples\AppConfigDemo), I get not quite the error you have but a different exception on application start:

System.TypeInitializationException: 'The type initializer for 'Microsoft.Data.SqlClient.SNINativeMethodWrapper' threw an exception.'

Same thing when I use the sample you provided.

To me it is still unclear why that happens although there seemed to be an open issue in Microsoft.Data.SqlClient:: dotnet/SqlClient#361

Anyway, I had no success with adding a reference to Microsoft.Data.SqlClient.SNI v1.1.1 as suggested in their issue.

I will continue investigating this but I cannot promise if or when I will be able to fix this. Therefore I will also mark this issue as "up for grabs" so hopefully someone will have a good idea how to solve it.

@ckadluba ckadluba removed their assignment Jul 25, 2020
@ckadluba ckadluba added up-for-grabs This issue waits for a contributor to fix it. bug net-framework This issue is related to the legacy .NET Framework. labels Jul 25, 2020
@ckadluba
Copy link
Member

ckadluba commented Jul 27, 2020

I could fix the SNI problem. To summarize it again, since the migration from System.Data.SqlClient to Microsoft.Data.SqlClient in MSSqlServer sink 5.5.0, when the sink is used from a .NET Framework project, an exception about a missing sni.dll might occurr. I could reproduce this by just running samples\AppConfigDemo.

image

Like stated by the Microsoft.Data.SqlClient team in their issue 361 (linked above in my last comment), this can be fixed by adding a reference to Microsoft.Data.SqlClient in the consuming application. After I added such a reference to the AppConfigDemo project, the problem disappeared. I committed this change to the sink repo.

So, if you use the sink from a .NET Framework (>= 4.6.1) project, you have to add a reference to Microsoft.Data.SqlClient to your project to avoid this exception.

With AppConfigDemo the sink just works now, so I still could not reproduce the problem described by @FletcherJohnsonZ and others regarding a missing Microsoft.Azure.Services.AppAuthentication, Version=1.4.0.0. I'll keep this under investigation.

@FletcherJohnsonZ
Copy link

FletcherJohnsonZ commented Jul 27, 2020 via email

@ie-zero
Copy link
Contributor

ie-zero commented Sep 26, 2020

Hi,

I encounter the System.TypeInitializationException: 'The type initializer for 'Microsoft.Data.SqlClient.SNINativeMethodWrapper' threw an exception.' access on the samples\AppConfigDemo connecting to a local SQL Server Express database.

The application is executed on Framework 4.6.1 and in my case the Microsoft.Data.SqlClient.SNI v1.1.1 does resolve the issue.

As I can see Microsoft.Data.SqlClient.SNI does not have any dependencies on v1.1.0 and v1.1.1 (for Framework) so it might worth upgrading the samples\AppConfigDemo anyway.

I am using the code on dev branch

@arex388
Copy link
Author

arex388 commented Sep 27, 2020

@ckadluba Sorry for the long delay in responding. Been busy with the project... Anyway, today I decided to give updating a try and I updated:

  • Serilog from 2.9.0 to 2.10.0.
  • Serilog.AspNetCore from 3.2.0 to 3.4.0.
  • Serilog.Sinks.MSSqlServer from 5.3.0 to 5.5.1.

After publishing to the server the Microsoft.Azure.Services.AppAuthentication.dll file is published and running the applications seems to no longer trigger the exception. So, as of right now it seems to be working.

@ie-zero
Copy link
Contributor

ie-zero commented Sep 28, 2020

Hi @ckadluba,

It is really great that this issue has been fixed but the same exception is thrown by the samples\AppConfigDemo. Should I raise a separate issue and resolve this one?

@ckadluba
Copy link
Member

@arex388 that is good news. Thank you very much for taking the time and trying this out with the new versions and coming back with feedback. We appreciate this a lot. I'm closing this issue now.

@ie-zero thanks for reporting the problem in samples\AppConfigDemo. Please report this in a new issue as you suggested. Thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug net-framework This issue is related to the legacy .NET Framework. up-for-grabs This issue waits for a contributor to fix it.
Projects
None yet
Development

No branches or pull requests

6 participants