This is how the interface looks like
Issues with Task and Custom Classes in C# Interfaces
In a C# project with an Infrastructure.DataAccess.Shared
assembly containing interfaces for various data providers, only primitive types can be utilized in these interfaces. Attempts to use types like Task
for asynchronous methods or custom classes result in the following compilation error:
CS0246: The type or namespace name 'Task' could not be found (are you missing a using directive or an assembly reference?)
Additionally, the Task
type appears in white in the code editor, indicating it may not be recognized properly, despite having the using System.Threading.Tasks;
directive included. Could this be related to assembly references, or is there a limitation on the types that can be used in this specific project? It seems to be a Visual Studio problem. I already tried restarting VS and reinstalling VS, but the problem still occurs. Colleagues do not have this problem and can build the solution successfully. Any insights or solutions would be appreciated. Thank you!
Developer technologies ASP.NET ASP.NET Core
Developer technologies C#
2 answers
Sort by: Most helpful
-
Nikola Hadzhiev 0 Reputation points
2024-10-29T12:56:07.0566667+00:00 -
Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
2024-10-29T15:21:08.09+00:00 What .net framework version is the project the interface is defined in?
with .netfiddle and roslyn 4.8 or .net 8 the following code compiles and runs:
using System; using System.Threading.Tasks; using System.Collections.Generic; namespace ContractsModuleServer.Infrastructure.DataAccess.Shared.DataProviders; public class Program { public static void Main() { Console.WriteLine("Hello World"); } } public interface RecalculationDataProvider { Task AddMonthChunks(List<RentItemContractMonthChunk> monthChunksToAdd); Task DeleteMonthChunks(Guid rentItemToContractPeriodId); } public class RentItemContractMonthChunk {}