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

Null reference when injecting page properties on Page.Title? #799

Closed
ffMathy opened this issue Sep 22, 2016 · 6 comments
Closed

Null reference when injecting page properties on Page.Title? #799

ffMathy opened this issue Sep 22, 2016 · 6 comments
Labels

Comments

@ffMathy
Copy link

ffMathy commented Sep 22, 2016

Please see: http://stackoverflow.com/questions/25534755/why-cant-i-get-the-page-title-within-an-httpmodule

When Autofac tries to inject properties into a given page, I get the following error because of it:

[NullReferenceException: Object reference not set to an instance of an object.]
System.Web.UI.Page.get_Title() +68

[TargetInvocationException: Exception has been thrown by the target of an invocation.]
System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) +0
System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments) +260
System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) +146
System.Reflection.RuntimePropertyInfo.GetValue(Object obj, Object[] index) +37
Autofac.Core.DefaultPropertySelector.InjectProperty(PropertyInfo propertyInfo, Object instance) +133
Autofac.Core.Activators.Reflection.AutowiringPropertyInjector.InjectProperties(IComponentContext context, Object instance, IPropertySelector propertySelector, IEnumerable1 parameters) +699 Autofac.ResolutionExtensions.InjectUnsetProperties(IComponentContext context, TService instance) +69 Autofac.Integration.Web.Forms.PageInjectionBehavior.DoInjection(Func2 injector, Object target) +85
Autofac.Integration.Web.Forms.DependencyInjectionModule.OnPreRequestHandlerExecute(Object sender, EventArgs e) +150
System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +142
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +92

It seems to be related to Autofac attempting to read Page.Title to see if it is truly "unset" before injecting it. However, that throws an exception.

Not sure when this happened. Might have been when we switched to .NET 4.6.1, but could also be when we updated Autofac to version 4.

ASP .NET and Autofac packages in packages.config:

  <package id="Microsoft.AspNet.Mvc" version="5.2.3" targetFramework="net461" />
  <package id="Microsoft.AspNet.Razor" version="3.2.3" targetFramework="net461" />
  <package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebPages" version="3.2.3" targetFramework="net461" />
  <package id="Autofac" version="4.1.0" targetFramework="net461" />
  <package id="Autofac.Mvc5" version="4.0.0" targetFramework="net461" />
  <package id="Autofac.Web" version="4.0.0" targetFramework="net45" />
@ffMathy ffMathy changed the title Null reference when injecting page properties on Page.Title? Null reference when injecting page properties on Page.Title? Sep 22, 2016
@ffMathy
Copy link
Author

ffMathy commented Sep 22, 2016

Should this be in the Autofac.Web project instead?

@tillig
Copy link
Member

tillig commented Sep 22, 2016

I think this repo is fine; I'm guessing it has something to do with the updated property injection behavior we added in 4.0.

I was able to reproduce the issue. My current hypothesis is that the previous property injection handling caught exceptions during "get" operations and handled it. Going to have to research it a bit.

@tillig tillig added the bug label Sep 22, 2016
@tillig
Copy link
Member

tillig commented Sep 22, 2016

Verified that the code in the Page.Title property is the same between version 2.0.0.0 and 4.0.0.0 of System.Web (at least in my machine's GAC). So... nothing's changed there.

@tillig
Copy link
Member

tillig commented Sep 22, 2016

This is a defect in our new property injection mechanism.

In the old version, we checked to see if we could supply the value before we looked at the property to see if we needed to inject it. Since the page title is a string and most folks don't have string registered we never ran into any issues. Of course, in the very rare case where someone had string registered, they'd have hit this.

In the new version, we check to see if the property is something we should inject before looking to see if the object type can be resolved or if there's a parameter for the property. Which means, in the InjectUnsetProperties case, we're doing the "get" on the title property before looking at the parameters/context to see if it can be resolved - we filter the property list first.

So, basically, we just sort of got lucky in the previous version that getting a property that needed to be checked during InjectUnsetProperties didn't throw an exception. Or at least it wasn't reported.

I think the solution to this is to wrap the "get" operation in a try/catch and ignoring the property if it throws. If we can't check if it's unset, we assume it's set and move on.

@tillig tillig closed this as completed in c754726 Sep 22, 2016
@tillig
Copy link
Member

tillig commented Sep 22, 2016

Fix included in v4.1.1, pushed to NuGet.

@ffMathy
Copy link
Author

ffMathy commented Sep 22, 2016

Wow, that was fast! Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants