[英]ServiceProvider.GetService<Type> throw exception for every usage
I'm new to the whole C#.Net environment and I'm trying to deploy a simple Console.NetCore 3 application to windows server 2012 R2 from MacOS 10.14 the problem is the application does work on macOS but after deployment on windows it doesn't work on the server here's the code I'm new to the whole C#.Net environment and I'm trying to deploy a simple Console.NetCore 3 application to windows server 2012 R2 from MacOS 10.14 the problem is the application does work on macOS but after deployment on windows it doesn'无法在服务器上工作这是代码
IServiceCollection services = new ServiceCollection();
services.AddLogging();
var serviceProvider = services.BuildServiceProvider();
var loggerFactory = serviceProvider.GetService<ILoggerFactory>();
Console.ReadKey();
I've debugged the code multiple time and exception only get thrown when GetService method is used like here我已经多次调试了代码,只有在使用 GetService 方法时才会抛出异常,就像这里一样
var loggerFactory = serviceProvider.GetService<ILoggerFactory>();
The exception message is异常消息是
The type initializer for Microsoft.Extentions.DependecnyInjection.DependencyInjectionEventSource threw an exception
Microsoft.Extentions.DependecnyInjection.DependencyInjectionEventSource 的类型初始化程序引发异常
I've searched for solutions but didn't find any not even a close one.我已经搜索了解决方案,但没有找到任何甚至没有接近的解决方案。
edit 1:编辑1:
here's more details about the error, hope it can helps这是有关错误的更多详细信息,希望对您有所帮助
Exception Message:异常消息:
The type initializer for 'Microsoft.Extensions.DependencyInjection.DependencyInjectionEventSource' threw an exception.
'Microsoft.Extensions.DependencyInjection.DependencyInjectionEventSource' 的类型初始化程序引发了异常。
at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.CreateServiceAccessor(Type serviceTyp e) at System.Collections.Concurrent.ConcurrentDictionary
2.GetOrAdd(TKey key, Func
2 valueFactory) at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.GetService(Type serviceType, ServiceP roviderEngineScope serviceProviderEngineScope)在 Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.CreateServiceAccessor(Type serviceTyp e) 在 System.Collections.Concurrent.ConcurrentDictionary
2.GetOrAdd(TKey key, Func
2 valueFactory) 在 Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.GetService(Type serviceType) at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType) at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetService[T](IServiceProvider provider)在 Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.GetService(Type serviceType) 在 Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType) 在 Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetService[T](IServiceProvider provider)
at Inj.Program.Main(String[] args) in /Users/programmer/VSProjects/Inj/Inj/Program.cs:line 19
在/Users/programmer/VSProjects/Inj/Inj/Program.cs:line 19 中的 Inj.Program.Main(String[] args)
System.ArgumentException: External component has thrown an exception.
System.ArgumentException:外部组件已引发异常。 at System.Globalization.CompareInfo.CompareString(ReadOnlySpan
1 string1, ReadOnlySpan
1 string2, CompareOptions opti ons) at System.Globalization.CompareInfo.Compare(String string1, String string2, CompareOptions options) at System.Globalization.TextInfo.PopulateIsAsciiCasingSameAsInvariant()在 System.Globalization.CompareInfo.CompareString(ReadOnlySpan
1 string1, ReadOnlySpan
1 string2, CompareOptions 选项) 在 System.Globalization.CompareInfo.Compare(String string1, String string2, CompareOptions options) 在 System.Globalization.TextInfo.PopulateIsAsciiCasingSameAsInvariant()
at System.Globalization.TextInfo.ChangeCaseCommon[TConversion](String source) at System.Globalization.TextInfo.ToUpper(String str) at System.String.ToUpperInvariant() at System.Diagnostics.Tracing.EventSource.GetGuid(Type eventSourceType)在 System.Globalization.TextInfo.ChangeCaseCommon[TConversion](String source) 在 System.Globalization.TextInfo.ToUpper(String str) 在 System.String.ToUpperInvariant() 在 System.Diagnostics.Tracing.EventSource.GetGuid(Type eventSourceType)
at System.Diagnostics.Tracing.EventSource..ctor(EventSourceSettings settings, String[] traits) at System.Diagnostics.Tracing.EventSource..ctor(EventSourceSettings settings) at Microsoft.Extensions.DependencyInjection.DependencyInjectionEventSource..ctor() at Microsoft.Extensions.DependencyInjection.DependencyInjectionEventSource..cctor()在 System.Diagnostics.Tracing.EventSource..ctor(EventSourceSettings settings, String[] traits) 在 System.Diagnostics.Tracing.EventSource..ctor(EventSourceSettings settings) 在 Microsoft.Extensions.DependencyInjection.DependencyInjectionEventSource..ctor() 在 Microsoft .Extensions.DependencyInjection.DependencyInjectionEventSource..cctor()
It seems the problem is related to running the app as dll what need to do is to publish the app as self contained executable for the targeted platform so in my case I've just published on Windows server 2012 R2 from MacOs 10.14 using the following commands (this commands will not publish to the server directly you need to move the resulted folder to the shared dir on your server)似乎问题与以 dll 身份运行应用程序有关,需要做的是将应用程序发布为目标平台的自包含可执行文件,因此在我的情况下,我刚刚使用以下命令从 MacOs 10.14 在 Windows 服务器 2012 R2 上发布(此命令不会直接发布到服务器,您需要将结果文件夹移动到服务器上的共享目录)
dotnet publish -c release -r win10-x64
dotnet 发布 -c 发布 -r win10-x64
yet better更好
dotnet publish -r win-x64 -c Release /p:PublishSingleFile=true
dotnet publish -r win-x64 -c Release /p:PublishSingleFile=true
after moving the folders I've run the.exe windows executable and now it works like charm移动文件夹后,我运行了.exe windows 可执行文件,现在它就像魅力一样
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.