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

Exception thrown when using WebOptimizer in ASP.NET Core MVC #519

Closed
roncrush opened this issue Aug 18, 2022 · 7 comments
Closed

Exception thrown when using WebOptimizer in ASP.NET Core MVC #519

roncrush opened this issue Aug 18, 2022 · 7 comments
Assignees
Labels
bug Something isn't working
Milestone

Comments

@roncrush
Copy link

Thanks for the great library! I've hit an issue with using DryIoc in an ASP.NET Core MVC application that also happens to use the WebOptimizer library. When running the application sometimes the following exception gets thrown in the console logs or as a developer exception page. It looks like there's sometimes an issue resolving the type Microsoft.Extensions.Options.IOptionsSnapshot<WebOptimizer.WebOptimizerOptions> :

System.ArgumentException: Generic types are not valid. (Parameter 'methodInfo')
at System.Reflection.Emit.DynamicILGenerator.EmitCall(OpCode opcode, MethodInfo methodInfo, Type[] optionalParameterTypes)
at DryIoc.FastExpressionCompiler.LightExpression.ExpressionCompiler.EmittingVisitor.TryEmitMethodCall(Expression expr, IParameterProvider paramExprs, ILGenerator il, ClosureInfo& closure, CompilerFlags setup, ParentFlags parent) in //src/DryIoc/FastExpressionCompiler.cs:line 4154
at DryIoc.FastExpressionCompiler.LightExpression.ConvertViaCastClassIntrinsicExpression`1.TryEmit(CompilerFlags config, ClosureInfo& closure, IParameterProvider paramExprs, ILGenerator il, ParentFlags parent, Int32 byRefIndex) in /
/src/DryIoc/Expression.cs:line 2321
at DryIoc.FastExpressionCompiler.LightExpression.ExpressionCompiler.EmittingVisitor.EmitNewArrayInit(NewArrayExpression expr, IParameterProvider paramExprs, ILGenerator il, ClosureInfo& closure, CompilerFlags setup, ParentFlags parent) in //src/DryIoc/FastExpressionCompiler.cs:line 3459
at DryIoc.FastExpressionCompiler.LightExpression.ExpressionCompiler.EmittingVisitor.TryEmitNew(Expression expr, IParameterProvider paramExprs, ILGenerator il, ClosureInfo& closure, CompilerFlags setup, ParentFlags parent) in /
/src/DryIoc/FastExpressionCompiler.cs:line 2115
at DryIoc.FastExpressionCompiler.LightExpression.NoByRefOneArgumentNewExpression.TryEmit(CompilerFlags setup, ClosureInfo& closure, IParameterProvider paramExprs, ILGenerator il, ParentFlags parent, Int32 byRefIndex) in //src/DryIoc/Expression.cs:line 2873
at DryIoc.FastExpressionCompiler.LightExpression.ExpressionCompiler.TryCompileNestedLambda(NestedLambdaInfo nestedLambdaInfo, CompilerFlags setup) in /
/src/DryIoc/FastExpressionCompiler.cs:line 1613
at DryIoc.FastExpressionCompiler.LightExpression.ExpressionCompiler.TryCompileBoundToFirstClosureParam(Type delegateType, Expression bodyExpr, IParameterProvider paramExprs, Type[] closurePlusParamTypes, Type returnType, CompilerFlags flags) in //src/DryIoc/FastExpressionCompiler.cs:line 496
at DryIoc.FactoryDelegateCompiler.CompileToFactoryDelegate(Expression expression, Boolean preferInterpretation) in /
/src/DryIoc/Container.cs:line 3936
at DryIoc.Container.TryInterpretOrCompileCachedExpression(IResolverContext r, KeyedFactoryCacheEntry cacheEntry, Rules rules, Object& result) in //src/DryIoc/Container.cs:line 572
at DryIoc.Container.ResolveAndCacheKeyed(Int32 serviceTypeHash, Type serviceType, Object serviceKey, IfUnresolved ifUnresolved, Object scopeName, Type requiredServiceType, Request preResolveParent, Object[] args) in /
/src/DryIoc/Container.cs:line 491
at DryIoc.Container.ResolveAndCache(Int32 serviceTypeHash, Type serviceType, IfUnresolved ifUnresolved) in //src/DryIoc/Container.cs:line 404
at DryIoc.Container.System.IServiceProvider.GetService(Type serviceType) in /
/src/DryIoc/Container.cs:line 338
at Microsoft.AspNetCore.Builder.UseMiddlewareExtensions.GetService(IServiceProvider sp, Type type, Type middleware)
at lambda_method1(Closure , Object , HttpContext , IServiceProvider )
at Microsoft.AspNetCore.Builder.UseMiddlewareExtensions.<>c__DisplayClass5_1.b__2(HttpContext context)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

Reproduction Files

test/test.csproj

<Project Sdk="Microsoft.NET.Sdk.Web">

    <PropertyGroup>
        <TargetFramework>net6.0</TargetFramework>
        <Nullable>enable</Nullable>
        <ImplicitUsings>enable</ImplicitUsings>
    </PropertyGroup>

    <ItemGroup>
      <PackageReference Include="codeessentials.WebOptimizer.Dotless" Version="3.1.0" />
      <PackageReference Include="DryIoc.Microsoft.DependencyInjection" Version="6.0.2" />
    </ItemGroup>
    
</Project>

test/Program.cs

using DryIoc;
using DryIoc.Microsoft.DependencyInjection;

var builder = WebApplication.CreateBuilder(args);
var container = new Container(Rules.MicrosoftDependencyInjectionRules);
var dependencyInjectionFactory = new DryIocServiceProviderFactory(container);
builder.Host.UseServiceProviderFactory(dependencyInjectionFactory);
builder.Services.AddControllersWithViews();
builder.Services.AddWebOptimizer(options =>
{
    options.AddLessBundle("/css/test.css", "wwwroot/css/test.less").UseContentRoot();
});

var app = builder.Build();
app.UseDeveloperExceptionPage();
app.UseWebOptimizer();
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

app.Run();

test/wwwroot/css/test.less

body {
  background: lightcyan;
}

test/Controllers/HomeController.cs

using Microsoft.AspNetCore.Mvc;

namespace test.Controllers;

public class HomeController : Controller
{
    public IActionResult Index()
    {
        return View();
    }
}

test/Views/Home/Index.cshtml

@{
    ViewData["Title"] = "Home Page";
}

<link rel="stylesheet" href="/css/test.css"/>

<div class="text-center">
    <h1 class="display-4">Welcome</h1>
    <p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
</div>

test/Views/_ViewImports.cshtml

@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@dadhi
Copy link
Owner

dadhi commented Aug 19, 2022

@roncrush
Wow, thanks for detailed reproductiom info!
I will look.

@dadhi dadhi self-assigned this Aug 19, 2022
@dadhi dadhi added the bug Something isn't working label Aug 19, 2022
@dadhi dadhi added this to the v5.2.2 milestone Aug 19, 2022
@dadhi
Copy link
Owner

dadhi commented Aug 19, 2022

@roncrush Meanwhile if the exception is critical for you, you may want to switch to interpretation new Container(Rules.Default.WithUseInterpretation())

@roncrush
Copy link
Author

@dadhi Thanks!

@dadhi
Copy link
Owner

dadhi commented Aug 21, 2022

@roncrush Reproduced the exception locally.

@dadhi
Copy link
Owner

dadhi commented Aug 23, 2022

@roncrush fixed at least.
You have found a very sneaky beast - so thanks ;)

dadhi added a commit that referenced this issue Aug 23, 2022
@dadhi dadhi closed this as completed in 0473ea0 Aug 23, 2022
@dadhi
Copy link
Owner

dadhi commented Aug 23, 2022

DI v5.2.2 is out on NuGet

@roncrush
Copy link
Author

@dadhi Awesome, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants