TimothyMeadows / reCAPTCHA.AspNetCore

Google reCAPTCHA v2/v3 for .NET Core 3.x
MIT License
144 stars 51 forks source link

Error in Example #64

Closed utech626 closed 4 years ago

utech626 commented 4 years ago

When running the sample web application an exception is thrown

An unhandled exception occurred while processing the request. InvalidOperationException: No service for type 'reCAPTCHA.AspNetCore.RecaptchaSettings' has been registered. Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)

InvalidOperationException: No service for type 'reCAPTCHA.AspNetCore.RecaptchaSettings' has been registered.

utech626 commented 4 years ago

error occurs in @inject RecaptchaSettings RecaptchaSettings

TimothyMeadows commented 4 years ago

Can't currently reproduce this, example works just fine for me.

mo-esmp commented 4 years ago

Perhaps @utech626 forgot to pull the latest version.

utech626 commented 4 years ago

The example fails on 2 different development machine, plus if I try in production it fails: .NET Core SDK (reflecting any global.json): Version: 3.1.301 Commit: 7feb845744

Runtime Environment: OS Name: Windows OS Version: 10.0.19041 OS Platform: Windows RID: win10-x64 Base Path: C:\Program Files\dotnet\sdk\3.1.301\

Host (useful for support): Version: 3.1.5 Commit: 65cd789777

.NET Core SDKs installed: 3.0.100 [C:\Program Files\dotnet\sdk] 3.1.100 [C:\Program Files\dotnet\sdk] 3.1.101 [C:\Program Files\dotnet\sdk] 3.1.301 [C:\Program Files\dotnet\sdk]

.NET Core runtimes installed: Microsoft.AspNetCore.All 2.1.19 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.All 2.2.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.App 2.1.19 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 2.2.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 3.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 3.0.3 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 3.1.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 3.1.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 3.1.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.NETCore.App 2.1.19 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 2.2.8 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 3.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 3.0.3 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 3.1.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 3.1.1 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 3.1.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.WindowsDesktop.App 3.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 3.0.3 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 3.1.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 3.1.1 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 3.1.5 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

To install additional .NET Core runtimes or SDKs: https://aka.ms/dotnet-download

utech626 commented 4 years ago

Add to Startup.cs foreach (var _item in services) { if (_item.ServiceType.FullName.Contains("reCAPTCHA", StringComparison.OrdinalIgnoreCase)) { Console.WriteLine($"Service: {_item.ServiceType.FullName}"); Console.WriteLine($"Lifetime: {_item.Lifetime}\n Instance:"); Console.WriteLine($"{_item.ImplementationType?.FullName}"); } }

output shows: Service: Microsoft.Extensions.Options.IConfigureOptions`1[[reCAPTCHA.AspNetCore.RecaptchaSettings, reCAPTCHA.AspNetCore, Version=3.0.5.0, Culture=neutral, PublicKeyToken=null]] Lifetime: Singleton Instance:

Service: reCAPTCHA.AspNetCore.IRecaptchaService Lifetime: Transient Instance:

registered instance is a IConfigureOptions

Add to HomeController.cs constructor public HomeController(IRecaptchaService recaptcha, IConfigureOptions settings) { recaptchaSettings = new RecaptchaSettings(); settings.Configure(recaptchaSettings); return; }

and now you can get an instance of RecaptchaSettings, this would need to be set in model

TimothyMeadows commented 4 years ago

I just pulled a fresh copy of the repo and was able to reproduce this. It looks like my local copy had a stashed version of the Contact view which had changes for IOptions.

RecaptchaSettings does indeed no longer exist it's IOptions.

image