BerkGarip
BerkGarip

Reputation: 544

Aggregate Exception Asp.Net Core 5.0

I have an Mvc project which is based on Asp.Net Core 5.0 . I have my own Core Layer and i have my own Photo,Video uploader method which is based my FileRepo class. Here is my FileModel class:

    public class FileModel
    {
        public int FileID { get; set; }
        public string FileName { get; set; }
        public string FileType { get; set; }
        public string FileExtension { get; set; }
        public string FileSlug { get; set; }
        public string FilePath { get; set; }
        public byte[] Data { get; set; }
    }

Here is my File Uploader method :

public interface IFileUploader
    {
        Task<FileModel> FileUploadToDatabase(List<IFormFile> files);
        Task<FileModel> FileUploadToPath(List<IFormFile> files);
        Task<bool> FileDeleteFromPath(int id);
    }
 public class FileUploader : IFileUploader
    {
        private FileModel _fileModel;

        public FileUploader(FileModel fileModel)
        {
            _fileModel = fileModel;
        }
        public async Task<FileModel> FileUploadToDatabase(List<IFormFile> files)
        {
            foreach (var file in files)
            {
                var fileName = Path.GetFileNameWithoutExtension(file.FileName);
                var extension = Path.GetExtension(file.FileName);
                _fileModel = new FileModel
                {
                    FileName = fileName,
                    FileType = file.ContentType
                };
                using (var dataStream = new MemoryStream())
                {
                    await file.CopyToAsync(dataStream);
                    _fileModel.Data = dataStream.ToArray();
                }
            }
            return _fileModel;
        }

        public async Task<FileModel> FileUploadToPath(List<IFormFile> files)
        {
            foreach (var file in files)
            {
                var basePath = Path.Combine(Directory.GetCurrentDirectory() + "\\Files\\");
                bool basePathExists = Directory.Exists(basePath);
                if (!basePathExists) Directory.CreateDirectory(basePath);
                var fileName = Path.GetFileNameWithoutExtension(file.FileName);
                var filePath = Path.Combine(basePath, file.FileName);
                var extension = Path.GetExtension(file.FileName);
                if (!File.Exists(filePath))
                {
                    using (var stream = new FileStream(filePath, FileMode.Create))
                    {
                        await file.CopyToAsync(stream);
                    }
                    _fileModel = new FileModel
                    {
                        FileName = fileName,
                        FileType = file.ContentType,
                        FilePath = filePath
                    };
                }
            }
            return _fileModel;
        }
    }

As u guys can see,its a different layer and there is nothing related with my Mvc project. Im getting error when i add those extension to my Mvc project. The error says me that :

'Some services are not able to be constructed (Error while validating the service descriptor 
'ServiceType: CORE_HBKSOFTWARE.Interfaces.IFileUploader Lifetime: Singleton ImplementationType: 
CORE_HBKSOFTWARE.Classes.FileUploader': Unable to resolve service for type 
'CORE_HBKSOFTWARE.Models.FileModel' while attempting to activate 
'CORE_HBKSOFTWARE.Classes.FileUploader'.)'

I dont know why im getting that error. That FileModel class is seems to okey to me and i still dont know. I allready add my services.AddSingleton<IFileUploader, FileUploader>(); to my Startup.cs .

Thanks for any suggestion !

Upvotes: 0

Views: 133

Answers (1)

David Eggenberger
David Eggenberger

Reputation: 542

If you want to use constructor injection to create an instance of FileModel in your FileUploader class you need to register it to the IoC Container. You do that by calling following method in the ConfigureServices method:

services.AddSingleton<FileModel>();

You can also choose a different lifetime by calling AddScoped or AddTransient.

Upvotes: 1

Related Questions