ServiceStack IContainerAdapter适应Autofac 5.2.0版本

问题描述 投票:1回答:1

我正在尝试将最新的Autofac程序包升级到5.2.0,但由于界面更改而没有真正成功,

来自(Autofac 4.9.4

public static class ResolutionExtensions
{
    public static bool TryResolve<T>(this IComponentContext context, out T instance);
}

至(Autofac 5.2.0

public static class ResolutionExtensions
{
    public static bool TryResolve<T>(this IComponentContext context, out T instance) 
        where T : class;
}    

ServiceStack程序包具有IContainerAdapter接口(ServiceStack.Interfaces 5.8.0

public interface IResolver
{
    T TryResolve<T>();
}  

public interface IContainerAdapter : IResolver
{
    T Resolve<T>();
}

我的AutofacIocAdapter实现了此IContainerAdapter

public class AutofacIocAdapter : IContainerAdapter
{
    public T TryResolve<T>()
    {
        if (m_Container.TryResolve<Autofac.ILifetimeScope>(out var scope) &&
            scope.TryResolve<T>(out var scopeComponent))
        {
            return scopeComponent;
        }

        if (m_Container.TryResolve<T>(out var component))
        {
            return component;
        }

        return default(T);
    }
}

但是升级Autofac后出现编译错误”>

Error   CS0452  The type 'T' must be a reference type in order to use it as parameter 'T' in the generic type or method 'ResolutionExtensions.TryResolve<T>(IComponentContext, out T?)'

是否有任何解决建议?

我正在尝试将最新的Autofac程序包升级到5.2.0,但由于接口更改而未能真正成功,来自(Autofac 4.9.4)public static class ResolutionExtensions {public static ...

c# servicestack autofac
1个回答
0
投票
如果没有C#的约束,您将无法从方法中调用具有类约束的类,但可以使用反射来调用它。
© www.soinside.com 2019 - 2024. All rights reserved.