dotnetcore/AspectCore-Framework

ArgumentException: Type 'Service.IAgentUserService' should be class.

Closed this issue · 1 comments

AspectCore.Core:1.2.0
Microsoft.AspNetCore.Mvc:2.2.0

VS:2017
image

参考:https://www.cnblogs.com/liuhaoyang/p/aspectcore-getting-started.html

先上代码:
`
SampleInterceptor :
public class SampleInterceptor : AbstractInterceptorAttribute
{
public override Task Invoke(AspectContext context, AspectDelegate next)
{
Console.WriteLine("call interceptor");
return context.Invoke(next);
}
}

BaseService:
public class BaseService : IBaseService
{
}

IAgentUserService :
[SampleInterceptor]
public interface IAgentUserService : IBaseService
{
///


/// 用户登录
///

///
///
Result Login(AgentUserLoginRequest request);
}

AgentUserService :
public class AgentUserService : BaseService, IAgentUserService
{

    #region 用户登录
    /// <summary>
    /// 用户登录
    /// </summary>
    /// <param name="request"></param>
    public Result Login(AgentUserLoginRequest request)
    {
             return new Result();
    }

}

#region 创建代理用户服务类
///


/// 创建代理用户服务类
///

///
public static IAgentUserService CreateAgentUserService()
{
ProxyGeneratorBuilder proxyGeneratorBuilder = new ProxyGeneratorBuilder();
IProxyGenerator proxyGenerator = proxyGeneratorBuilder.Build();

        return proxyGenerator.CreateClassProxy<IAgentUserService, AgentUserService>();

}
#endregion

在实际调用时:
public async Task ValidateAsync(ResourceOwnerPasswordValidationContext context)
{
var result = MyAgentUserService.Login(new AgentUserLoginRequest()
{
UserName = context.UserName,
UserPwd = context.Password
});
}

VS没有提示报错,但是在实际运行调用接口时会报错误:

An unhandled exception occurred while processing the request.

ArgumentException: Type 'Service.IAgentUserService' should be class.
Parameter name: serviceType

AspectCore.DynamicProxy.ProxyTypeGenerator.CreateClassProxyType(Type serviceType, Type implementationType)

TypeInitializationException: The type initializer for 'Api.Identity.ResourceOwnerPasswordValidator' threw an exception.

Api.Identity.ResourceOwnerPasswordValidator+<ValidateAsync>d__2.MoveNext() in ResourceOwnerPasswordValidator.cs, line 23

可以看到:Type 'Service.IAgentUserService' should be class. 但这里是和参考文档中一样的interface和SampleInterceptor,并且VS也没提示

`

创建接口代理请使用 CreateInterfaceProxy