Search code examples
c#asp.net-coredependency-injectionattributes

asp.net core error CS0428 Cannot conver method Group GetService to non delegate type


I am doing and ASP.Net Core MVC Application.

I have a Error Attribute as a header of the Methods in the controller.

Here is my Attribute

 public class ClientWebErrorAttribute : ExceptionFilterAttribute
    {
        private readonly bool isPartial ;
        public ClientWebErrorAttribute(bool _isPartial)
        {
            isPartial = _isPartial;
        }

     public override void OnException(ExceptionContext context)
            {
                IActivityLogging _activityLogging=context.HttpContext.RequestServices.GetService<IActivityLogging>)();
                 var viewData = new ViewDataDictionary(new EmptyModelMetadataProvider(), context.ModelState);
            .......            
                context.ExceptionHandled = true;
            }
 }

I need to get access to an Interface and I am using this line

IActivityLogging _activityLogging=context.HttpContext.RequestServices.GetService<IActivityLogging>)();

But I got this error

 CS0428 Cannot convert method Group GetService to non delegate type IActivityLogging

I have

using Microsoft.Extensions.DependencyInjection;

but it is not recognized, is in black...

this is my interface IActivityLogging

namespace ClientWeb.UI.Interfaces
{
    public interface IActivityLogging
    {
        Task<List<ActivityLoggingResponseModel>?>  LogError(HttpRequest httpRequest, Exception ex, string typeId);
        Task<HttpResponseMessage?> LogNonError(string message, HttpRequest httpRequest, string typeId);
    }
}

In Program.cs I have this

builder.Services.AddScoped<IActivityLogging, ActivityLogging>();

What I am missing?


Solution

  • I tried with your codes and reproduced your error: enter image description here

    It seems the error was just caused by the redundent ) remove it and there're no complie errors

    enter image description here