AspNetDependencyInjection
AspNetDependencyInjection copied to clipboard
Exception thrown: 'System.InvalidOperationException' in AspNetDependencyInjection.dll
I am running .Net 4.8 Web Forms application and getting InvalidOperationException. Any help would be appreciated.
internal static void PreStart()
{
System.Diagnostics.Debug.WriteLine(nameof(RegisterServices) + "." + nameof(PreStart) + "() called.");
// If you are using ASP.NET Web Forms without any ASP.NET MVC functionality, remove the call to `.AddMvcDependencyResolver()`.
// If you are using ASP.NET MVC, regardless of whether you're using ASP.NET Web Forms, use `.AddMvcDependencyResolver()`:
_di = new ApplicationDependencyInjectionBuilder()
.ConfigureServices(ConfigureServices)
//.AddMvcDependencyResolver() // Uncomment this out if you're using ASP.NET MVC.
//.AddWebApiDependencyResolver() // Uncomment this out if you're using ASP.NET Web API.
//.AddScopedSignalRDependencyResolver() // Uncomment this out if you're using ASP.NET SignalR (and want to use services scoped to request or operation lifetime). NOTE: You cannot have both `AddScopedSignalRDependencyResolver` and `AddUnscopedSignalRDependencyResolver` at the same time. You must also configure SignalR below.
//.AddUnscopedSignalRDependencyResolver() // Uncomment this out if you're using ASP.NET SignalR (and only need Singleton or Transient lifetime services).
.Build();
}
private static void ConfigureServices(IServiceCollection services)
{
// TODO: Add any dependencies needed here
//services
// // Useful services built-in to AspNetDependencyInjection:
// .AddDefaultHttpContextAccessor() // Adds `IHttpContextAccessor`
// .AddWebConfiguration(); // Adds `IWebConfiguration`
// Example services:
//.AddScoped<Service1>()
//.AddTransient<Service2>()
//.AddScoped<IExampleRequestLifelongService, ExampleRequestLifelongService>()
//.AddScoped<Service4>()
//.AddSingleton<SingletonService>();
}
/// <summary>Invoked at the end of ASP.NET application start-up, after Global's Application_Start method runs. Dependency-injection re-configuration may be called here if you have services that depend on Global being initialized.</summary>
internal static void PostStart()
{
System.Diagnostics.Debug.WriteLine(nameof(RegisterServices) + "." + nameof(PostStart) + "() called.");
}
This error is happening during program startup and prevent the application from starting.

You haven't provided sufficient troubleshooting detail in your post.
Please post the full stack-trace, including that of any InnerException.
This is what I was able to pull from the EventViewer
Log Name: Application
Source: ASP.NET 4.0.30319.0
Date: 8/30/2021 7:49:08 AM
Event ID: 1310
Task Category: Web Event
Level: Warning
Keywords: Classic
User: N/A
Computer: 2005-61477.corp.dsg.local
Description:
Event code: 3006
Event message: A parser error has occurred.
Event time: 8/30/2021 7:49:08 AM
Event time (UTC): 8/30/2021 11:49:08 AM
Event ID: 74bd77aa348f4161967acbd1efb18875
Event sequence: 1
Event occurrence: 1
Event detail code: 0
Application information:
Application domain: /LM/W3SVC/2/ROOT-1-132747977163515329
Trust level: Full
Application Virtual Path: /
Application Path: C:\src\mp2\UI\PingByPhone.UI.Web\
Machine name: 2005-61477
Process information:
Process ID: 33452
Process name: iisexpress.exe
Account name: CORP\spowell
Exception information:
Exception type: HttpException
Exception message: HttpContext.Current is null.
at System.Web.Compilation.BuildManager.ReportTopLevelCompilationException()
at System.Web.Compilation.BuildManager.EnsureTopLevelFilesCompiled()
at System.Web.Compilation.BuildManager.CallAppInitializeMethod()
at System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, HostingEnvironmentParameters hostingParameters, PolicyLevel policyLevel, Exception appDomainCreationException)
HttpContext.Current is null.
at System.Web.UI.TemplateParser.ParseString(String text, VirtualPath virtualPath, Encoding fileEncoding)
at System.Web.UI.TemplateParser.ParseFile(String physicalPath, VirtualPath virtualPath)
at System.Web.UI.TemplateParser.ParseInternal()
at System.Web.UI.TemplateParser.Parse()
at System.Web.Compilation.BaseTemplateBuildProvider.get_CodeCompilerType()
at System.Web.Compilation.BuildProvider.GetCompilerTypeFromBuildProvider(BuildProvider buildProvider)
at System.Web.Compilation.BuildProvidersCompiler.ProcessBuildProviders()
at System.Web.Compilation.BuildProvidersCompiler.PerformBuild()
at System.Web.Compilation.ApplicationBuildProvider.GetGlobalAsaxBuildResult(Boolean isPrecompiledApp)
at System.Web.Compilation.BuildManager.CompileGlobalAsax()
at System.Web.Compilation.BuildManager.EnsureTopLevelFilesCompiled()
HttpContext.Current is null.
at AspNetDependencyInjection.Internal.DependencyInjectionWebObjectActivator.GetServiceProviderForCurrentHttpContext() in C:\git\me\Unity.WebForms\Unity.WebForms\AspNetDependencyInjection\AspNetDependencyInjection\DependencyInjectionWebObjectActivator.cs:line 32
at AspNetDependencyInjection.ObjectFactoryCache.GetRequiredService(Func`1 getServiceProvider, Type serviceType, Boolean useOverrides) in C:\git\me\Unity.WebForms\Unity.WebForms\AspNetDependencyInjection\AspNetDependencyInjection\ObjectFactoryCache.cs:line 52
at System.Web.Util.HttpEncoder.GetCustomEncoderFromConfig()
at System.Lazy`1.CreateValue()
at System.Lazy`1.LazyInitValue()
at System.Web.Util.HttpEncoder.get_Current()
at System.Web.UI.TemplateParser.ProcessAttributes(String text, Match match, ParsedAttributeCollection& attribs, Boolean fDirective, String& duplicateAttribute)
at System.Web.UI.TemplateParser.ParseStringInternal(String text, Encoding fileEncoding)
at System.Web.UI.TemplateParser.ParseString(String text, VirtualPath virtualPath, Encoding fileEncoding)
Request information:
Request URL: http://localhost:8000/
Request path: /
User host address: ::1
User:
Is authenticated: False
Authentication Type:
Thread account name: CORP\spowell
Thread information:
Thread ID: 9
Thread account name: CORP\spowell
Is impersonating: False
Stack trace: at System.Web.Compilation.BuildManager.ReportTopLevelCompilationException()
at System.Web.Compilation.BuildManager.EnsureTopLevelFilesCompiled()
at System.Web.Compilation.BuildManager.CallAppInitializeMethod()
at System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, HostingEnvironmentParameters hostingParameters, PolicyLevel policyLevel, Exception appDomainCreationException)
Custom event details:
Event Xml:
<Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event">
<System>
<Provider Name="ASP.NET 4.0.30319.0" />
<EventID Qualifiers="32768">1310</EventID>
<Version>0</Version>
<Level>3</Level>
<Task>3</Task>
<Opcode>0</Opcode>
<Keywords>0x80000000000000</Keywords>
<TimeCreated SystemTime="2021-08-30T11:49:08.6501869Z" />
<EventRecordID>2075922</EventRecordID>
<Correlation />
<Execution ProcessID="0" ThreadID="0" />
<Channel>Application</Channel>
<Computer>2005-61477.corp.dsg.local</Computer>
<Security />
</System>
<EventData>
<Data>3006</Data>
<Data>A parser error has occurred.</Data>
<Data>8/30/2021 7:49:08 AM</Data>
<Data>8/30/2021 11:49:08 AM</Data>
<Data>74bd77aa348f4161967acbd1efb18875</Data>
<Data>1</Data>
<Data>1</Data>
<Data>0</Data>
<Data>/LM/W3SVC/2/ROOT-1-132747977163515329</Data>
<Data>Full</Data>
<Data>/</Data>
<Data>C:\src\mp2\UI\PingByPhone.UI.Web\</Data>
<Data>2005-61477</Data>
<Data>
</Data>
<Data>33452</Data>
<Data>iisexpress.exe</Data>
<Data>CORP\spowell</Data>
<Data>HttpException</Data>
<Data>HttpContext.Current is null.
at System.Web.Compilation.BuildManager.ReportTopLevelCompilationException()
at System.Web.Compilation.BuildManager.EnsureTopLevelFilesCompiled()
at System.Web.Compilation.BuildManager.CallAppInitializeMethod()
at System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, HostingEnvironmentParameters hostingParameters, PolicyLevel policyLevel, Exception appDomainCreationException)
HttpContext.Current is null.
at System.Web.UI.TemplateParser.ParseString(String text, VirtualPath virtualPath, Encoding fileEncoding)
at System.Web.UI.TemplateParser.ParseFile(String physicalPath, VirtualPath virtualPath)
at System.Web.UI.TemplateParser.ParseInternal()
at System.Web.UI.TemplateParser.Parse()
at System.Web.Compilation.BaseTemplateBuildProvider.get_CodeCompilerType()
at System.Web.Compilation.BuildProvider.GetCompilerTypeFromBuildProvider(BuildProvider buildProvider)
at System.Web.Compilation.BuildProvidersCompiler.ProcessBuildProviders()
at System.Web.Compilation.BuildProvidersCompiler.PerformBuild()
at System.Web.Compilation.ApplicationBuildProvider.GetGlobalAsaxBuildResult(Boolean isPrecompiledApp)
at System.Web.Compilation.BuildManager.CompileGlobalAsax()
at System.Web.Compilation.BuildManager.EnsureTopLevelFilesCompiled()
HttpContext.Current is null.
at AspNetDependencyInjection.Internal.DependencyInjectionWebObjectActivator.GetServiceProviderForCurrentHttpContext() in C:\git\me\Unity.WebForms\Unity.WebForms\AspNetDependencyInjection\AspNetDependencyInjection\DependencyInjectionWebObjectActivator.cs:line 32
at AspNetDependencyInjection.ObjectFactoryCache.GetRequiredService(Func`1 getServiceProvider, Type serviceType, Boolean useOverrides) in C:\git\me\Unity.WebForms\Unity.WebForms\AspNetDependencyInjection\AspNetDependencyInjection\ObjectFactoryCache.cs:line 52
at System.Web.Util.HttpEncoder.GetCustomEncoderFromConfig()
at System.Lazy`1.CreateValue()
at System.Lazy`1.LazyInitValue()
at System.Web.Util.HttpEncoder.get_Current()
at System.Web.UI.TemplateParser.ProcessAttributes(String text, Match match, ParsedAttributeCollection& attribs, Boolean fDirective, String& duplicateAttribute)
at System.Web.UI.TemplateParser.ParseStringInternal(String text, Encoding fileEncoding)
at System.Web.UI.TemplateParser.ParseString(String text, VirtualPath virtualPath, Encoding fileEncoding)
</Data>
<Data>http://localhost:8000/</Data>
<Data>/</Data>
<Data>::1</Data>
<Data>
</Data>
<Data>False</Data>
<Data>
</Data>
<Data>CORP\spowell</Data>
<Data>9</Data>
<Data>CORP\spowell</Data>
<Data>False</Data>
<Data> at System.Web.Compilation.BuildManager.ReportTopLevelCompilationException()
at System.Web.Compilation.BuildManager.EnsureTopLevelFilesCompiled()
at System.Web.Compilation.BuildManager.CallAppInitializeMethod()
at System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, HostingEnvironmentParameters hostingParameters, PolicyLevel policyLevel, Exception appDomainCreationException)
</Data>
</EventData>
</Event>
And my register services:
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.DependencyInjection;
using AspNetDependencyInjection;
using MacroPoint.LITE.UI.Web.Helpers;
using WebActivatorEx;
[assembly: PreApplicationStartMethod(typeof(RegisterServices), methodName: nameof(RegisterServices.PreStart))]
[assembly: PostApplicationStartMethod(typeof(RegisterServices), methodName: nameof(RegisterServices.PostStart))] // Uncomment this if you have any Post-start logic you want to run.
namespace MacroPoint.LITE.UI.Web.Helpers
{
public static class RegisterServices
{
private static ApplicationDependencyInjection _di;
internal static void PreStart()
{
// If you are using ASP.NET Web Forms without any ASP.NET MVC functionality, remove the call to `.AddMvcDependencyResolver()`.
// If you are using ASP.NET MVC, regardless of whether you're using ASP.NET Web Forms, use `.AddMvcDependencyResolver()`:
try
{
Console.WriteLine(nameof(RegisterServices) + "." + nameof(PreStart) + "() called.");
_di = new ApplicationDependencyInjectionBuilder()
.ConfigureServices(ConfigureServices)
// .AddMvcDependencyResolver() // Uncomment this out if you're using ASP.NET MVC.
// .AddWebApiDependencyResolver() // Uncomment this out if you're using ASP.NET Web API.
//.AddScopedSignalRDependencyResolver() // Uncomment this out if you're using ASP.NET SignalR (and want to use services scoped to request or operation lifetime). NOTE: You cannot have both `AddScopedSignalRDependencyResolver` and `AddUnscopedSignalRDependencyResolver` at the same time. You must also configure SignalR below.
//.AddUnscopedSignalRDependencyResolver() // Uncomment this out if you're using ASP.NET SignalR (and only need Singleton or Transient lifetime services).
.Build();
}
catch (Exception ex)
{
var s = ex.ToString();
Console.WriteLine(s);
throw;
}
}
private static void ConfigureServices(IServiceCollection services)
{
// TODO: Add any dependencies needed here
services
// Useful services built-in to AspNetDependencyInjection:
.AddDefaultHttpContextAccessor() // Adds `IHttpContextAccessor`
.AddWebConfiguration(); // Adds `IWebConfiguration`
// Example services:
//.AddScoped<Service1>()
//.AddTransient<Service2>()
//.AddScoped<IExampleRequestLifelongService, ExampleRequestLifelongService>()
//.AddScoped<Service4>()
//.AddSingleton<SingletonService>();
}
/// <summary>Invoked at the end of ASP.NET application start-up, after Global's Application_Start method runs. Dependency-injection re-configuration may be called here if you have services that depend on Global being initialized.</summary>
internal static void PostStart()
{
Console.WriteLine(nameof(RegisterServices) + "." + nameof(PostStart) + "() called.");
}
}
}
Running the SampleWebApplication results in similar behavior:
Request is not available in this context
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.Web.HttpException: Request is not available in this context
Source Error:
Line 70:
Line 71: // These conditions should never happen, but just-in-case:
Line 72: if( httpContext.Request is null ) throw new ArgumentException( message: "httpContext.Request is null." , paramName: nameof( httpContext ) );
Line 73: if( httpContext.Request.RequestContext is null ) throw new ArgumentException( message: "httpContext.Request.RequestContext is null." , paramName: nameof( httpContext ) );
Line 74: if( httpContext.Request.RequestContext.HttpContext is null ) throw new ArgumentException( message: "httpContext.Request.RequestContext.HttpContext is null.", paramName: nameof( httpContext ) );
Source File: C:\src\AspNetDependencyInjection\AspNetDependencyInjection\AspNetDependencyInjection\Extensions.cs Line: 72
Stack Trace:
[HttpException (0x80004005): Request is not available in this context]
System.Web.HttpContext.get_Request() +9951194
AspNetDependencyInjection.AndiExtensions.GetHttpContextBase(HttpContext httpContext) in C:\src\AspNetDependencyInjection\AspNetDependencyInjection\AspNetDependencyInjection\Extensions.cs:72
AspNetDependencyInjection.ApplicationDependencyInjection.GetServiceProviderForHttpContext(HttpContext httpContext) in C:\src\AspNetDependencyInjection\AspNetDependencyInjection\AspNetDependencyInjection\ApplicationDependencyInjection.cs:180
AspNetDependencyInjection.Internal.DependencyInjectionWebObjectActivator.GetServiceProviderForCurrentHttpContext() in C:\src\AspNetDependencyInjection\AspNetDependencyInjection\AspNetDependencyInjection\DependencyInjectionWebObjectActivator.cs:34
AspNetDependencyInjection.ObjectFactoryCache.GetRequiredService(Func`1 getServiceProvider, Type serviceType, Boolean useOverrides) in C:\src\AspNetDependencyInjection\AspNetDependencyInjection\AspNetDependencyInjection\ObjectFactoryCache.cs:54
AspNetDependencyInjection.Internal.DependencyInjectionWebObjectActivator.GetService(Type serviceType) in C:\src\AspNetDependencyInjection\AspNetDependencyInjection\AspNetDependencyInjection\DependencyInjectionWebObjectActivator.cs:25
System.Web.HttpRuntime.CreateNonPublicInstanceByWebObjectActivator(Type type) +9949351
System.Web.HttpApplication.BuildIntegratedModuleCollection(List`1 moduleList) +174
System.Web.HttpApplication.InitInternal(HttpContext context, HttpApplicationState state, MethodInfo[] handlers) +824
System.Web.HttpApplicationFactory.GetNormalApplicationInstance(HttpContext context) +209
System.Web.HttpApplicationFactory.GetApplicationInstance(HttpContext context) +113
System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +342
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.8.4330.0