Skip to content

Commit

Permalink
Fix the null reference exception which occurs when DependencyResolver…
Browse files Browse the repository at this point in the history
….Current.GetService<IConfigurationSource>() returns null.

And just in case, also eliminate the possibility of null refs caused by something setting DependencyResolver.Current to null.
  • Loading branch information
Tim Lovell-Smith committed Mar 2, 2013
1 parent 85619e6 commit 8a22ef9
Showing 1 changed file with 28 additions and 8 deletions.
36 changes: 28 additions & 8 deletions PoliteCaptcha/ReCaptchaGenerator.cs
Expand Up @@ -12,7 +12,7 @@ namespace PoliteCaptcha
/// </summary>
public class ReCaptchaGenerator : ICaptchaGenerator
{
readonly IConfigurationSource configSource;
readonly IConfigurationSource _configSource;

public ReCaptchaGenerator()
: this(new DefaultConfigurationSource())
Expand All @@ -21,7 +21,7 @@ public ReCaptchaGenerator()

public ReCaptchaGenerator(IConfigurationSource configSource)
{
this.configSource = configSource;
_configSource = configSource;
}

/// <summary>
Expand All @@ -31,15 +31,16 @@ public ReCaptchaGenerator(IConfigurationSource configSource)
/// <param name="fallbackMessage">An optional message to display above the CAPTCHA when it is displayed as a fallback.</param>
/// <returns>The reCAPTCHA HTML.</returns>
public IHtmlString Generate(
HtmlHelper htmlHelper,
HtmlHelper htmlHelper,
string fallbackMessage = null)
{
if (htmlHelper == null)
{
throw new ArgumentNullException("htmlHelper");
}

var configurationSource = DependencyResolver.Current.GetService<IConfigurationSource>();

var publicApiKey = configSource.GetConfigurationValue(Const.ReCaptchaPublicKeyAppSettingKey);
IConfigurationSource configurationSource = GetConfigurationSource();
var publicApiKey = configurationSource.GetConfigurationValue(Const.ReCaptchaPublicKeyAppSettingKey);
if (publicApiKey == null)
{
if (!htmlHelper.ViewContext.HttpContext.Request.IsLocal)
Expand Down Expand Up @@ -68,9 +69,28 @@ public ReCaptchaGenerator(IConfigurationSource configSource)
recaptchaControl.RenderControl(htmlWriter);

var captchaHtml = htmlWriter.InnerWriter.ToString();
var template = @"<div class=""PoliteCaptcha editor-field""><span class=""field-validation-error"" data-valmsg-for=""PoliteCaptcha""><span htmlfor=""PoliteCaptcha"">{0}</span></span>{1}</div>";


const string template = @"<div class=""PoliteCaptcha editor-field""><span class=""field-validation-error"" data-valmsg-for=""PoliteCaptcha""><span htmlfor=""PoliteCaptcha"">{0}</span></span>{1}</div>";
return new MvcHtmlString(string.Format(template, fallbackMessage ?? Const.DefaulFallbackMessage, captchaHtml));
}

private IConfigurationSource GetConfigurationSource()
{
if (_configSource != null)
{
return _configSource;
}

if (DependencyResolver.Current != null)
{
var resolvedConfigSource = DependencyResolver.Current.GetService<IConfigurationSource>();
if (resolvedConfigSource != null)
{
return resolvedConfigSource;
}
}

return new DefaultConfigurationSource();
}
}
}

0 comments on commit 8a22ef9

Please sign in to comment.