简体   繁体   English

如何解决Autofac中的依赖关系?

[英]How to Resolve dependency in Autofac?

I have a Serializar helper class that will deserialize some xml for me. 我有一个Serializar帮助器类,它将为我反序列化一些xml。 I also have an Interface called IStorageService that has two implementation. 我还有一个名为IStorageService的接口,该接口有两个实现。

here is my IStorageService interface : 这是我的IStorageService接口:

    public interface IStorageService
    {
        string GetFullImageUri(string fileName);
    }

Here is the two implementation : 这是两个实现:

1- 1-

    public class AzureBlobStorageService : IStorageService
    {
        private readonly string _rootPath;
        public string GetFullImageUri(string fileName)
        {
            return _rootPath + fileName;
        }
    }

2- 2-

    public class FileSystemStorageService : IStorageService
    {    
        public string GetFullImageUri(string fileName)
        {
            return _applicationPath + "/Data/Images/"+ fileName;
        }
    }

Here is my Serializar class 这是我的Serializar课程

    public class Serializar
    {
        private readonly IStorageService _storageService;

        public Serializar(IStorageService storageService)
        {
            _storageService = storageService;
        }
        public static List<ProductType> DeserializeXmlAsProductTypes(object xml)
        {
            // do stuff here.

           // this method require using  _storageService.GetFullImageUri("");
        }
    }

I am getting this compiling error : 我收到此编译错误:

Error 32 An object reference is required for the non-static field, method, or property 'Serializar._storageService 错误32非静态字段,方法或属性'Serializar._storageService需要对象引用

How to resolve this in the IocConfig.cs using Autofac ? 如何使用Autofac在IocConfig.cs中解决此问题?

You can't solve this with Autofac. 您无法使用Autofac解决此问题。 The problem is in your code and the C# compiler tells you what's wrong. 问题出在您的代码中,C#编译器告诉您出了什么问题。

The problem is that your DeserializeXmlAsProductTypes method is static, but you try to access instance field. 问题是您的DeserializeXmlAsProductTypes方法是静态的,但是您尝试访问实例字段。 This is not possible in .NET and the C# compiler therefore presents you with an error. 在.NET中这是不可能的,因此C#编译器会向您显示错误。

The solution is to make the DeserializeXmlAsProductTypes an instance method, simply by removing the static keyword from the method definition. 解决方案是使DeserializeXmlAsProductTypes成为实例方法,只需从方法定义中删除static关键字即可。

This might however cause other code in your application to fail, because there might be some code that depends on this static method. 但是,这可能会导致应用程序中的其他代码失败,因为可能有一些代码依赖于此静态方法。 If this is the case, the solution here is to inject the Serializar into the constructor of such class, so that the failing code can make use of the Serializar instance and call the new DeserializeXmlAsProductTypes instance method. 如果是这种情况,这里的解决方案是将Serializar注入到此类的构造函数中,以便失败的代码可以使用Serializar实例并调用新的DeserializeXmlAsProductTypes实例方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Autofac解决CQRS CommandDispatcher中的依赖关系 - Autofac resolve dependency in CQRS CommandDispatcher 如何使用Autofac解决基于字符串类型的依赖关系 - How to resolve dependency based on type as string using Autofac 如何从解析依赖项将参数传递给 Autofac 模块 - How to pass parameter to an Autofac Module from a resolve dependency 如何解决 Autofac InstancePerHttpRequest - How to resolve Autofac InstancePerHttpRequest Autofac无法解决对TeamCity的依赖 - Autofac couldn't resolve dependency on TeamCity 无法使用autofac解析通用存储库的依赖性 - Unable to resolve dependency for generic repository using autofac 如何在 Autofac 依赖注入框架中注册多个类并将其解析为一个接口 - How to register and resolve many class to one interface in Autofac Dependency injection Framework 如何从 .NET Core 3.1 中的 Autofac 容器解析已注册的依赖项 - How can I resolve a registered Dependency from an Autofac Container in .NET Core 3.1 Autofac使用InjectProperties自动注入依赖项或使用Resolve &lt;&gt;手动解析 - Autofac to inject dependency automatically using InjectProperties or manually resolve using Resolve<> 如何解决Autofac循环依赖? - How to solve Autofac circular dependency?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM