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

兄弟,文档不更新都是坑啊 #262

Open
LightCHY opened this issue May 19, 2021 · 7 comments
Open

兄弟,文档不更新都是坑啊 #262

LightCHY opened this issue May 19, 2021 · 7 comments

Comments

@LightCHY
Copy link

新版本的方法,使用都改了,文档没跟,使用者踩着坑,浪费时间啊!
var container = services.ToServiceContainer();
return container.Build();
看例子的这些都没法用........

@LGinC
Copy link

LGinC commented Jun 16, 2021

.net 5 项目表示都不知道怎么替换默认依赖注入了

@LGinC
Copy link

LGinC commented Jun 16, 2021

找到了,在IHostBuilder调用UseServiceContext扩展方法(在AspectCore.Extensions.Hosting 包中)
image
在Controller里注入还需要在AddControllers时调用AddControllersAsServices
image

经测试属性注入正常

@liuhaoyang
Copy link
Member

例子都还是.net core 2的,我更新到.net 5

@bnn00023
Copy link

我發現屬性或是方法都沒有寫註解.... 有人知道是為甚麼嗎?

@pengqian089
Copy link

确实,看文档看了一小时,下载Nuget包准备用的时候,满满的挫败感

@liuhaoyang
Copy link
Member

确实,看文档看了一小时,下载Nuget包准备用的时候,满满的挫败感

你的具体问题是啥呢

@pengqian089
Copy link

确实,看文档看了一小时,下载Nuget包准备用的时候,满满的挫败感

你的具体问题是啥呢

不好意思,今天才回复。在asp.net中确实尝试成功了

Program.cs

using AspectCore.Extensions.DependencyInjection;
using AspectDemo.WebApplication1.Services;
using Microsoft.AspNetCore.Mvc;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddTransient<IDemoService, DemoService>();
builder.Services.ConfigureDynamicProxy();

builder.Host.UseServiceProviderFactory(new DynamicProxyServiceProviderFactory());
var app = builder.Build();

app.UseRouting();

app.MapGet("/", () => "hi");

app.MapGet("/id", ([FromServices]IDemoService service) => service.GeneratorId());

app.Run();

IDemoService.cs

public interface IDemoService
{
    [Log]
    string GeneratorId();
}

public class LogAttribute : AbstractInterceptorAttribute
{
    public override async Task Invoke(AspectContext context, AspectDelegate next)
    {
        var watch = Stopwatch.StartNew();
        await next(context);
        watch.Stop();
        Console.WriteLine($"{context.ProxyMethod.Name} invoke time-consuming:{watch.Elapsed}ms");
    }
}

但是在控制台程序或WPF、WinForm上,AOP相关代码一直不执行,示例太少,也不知道哪里没配置好

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

No branches or pull requests

5 participants