Nirman
Nirman

Reputation: 6793

ResolutionException - Getting "Required dependency of type *********** could not be resolved"

Following is the exact scenario in my application.

I have used ServiceStack 3.9.48 and AutoFac 4.6.0 to develop a REST service.

Following is the code of AppHost which is inherited from AppHostBase

    public AppHost()
        :base("My Service Host", typeof(NotificationService).Assembly)
    {
        var builder = new ContainerBuilder();
        builder.RegisterType<ConfigurationProvider>().As<IConfigurationProvider>();
        builder.RegisterType<Logging>().As<ILogging>();

        IoCContainer = builder.Build();            
    }

    public override void Configure(Container container)
    {
        using (var scope = IoCContainer.BeginLifetimeScope())
        {
            var _logging = scope.Resolve<ILogging>();

            JsConfig.EmitCamelCaseNames = true;

            base.RequestFilters.Add(delegate (IHttpRequest req, IHttpResponse res, object dto)
            {
                HandleUncaughtExceptionDelegate uncaughtExceptionDelegate = null;
                if (DateTime.Now.Year <= 2019)
                {
                    if (uncaughtExceptionDelegate == null)
                    {
                        uncaughtExceptionDelegate = delegate (IHttpRequest request, IHttpResponse response, string operationName, Exception ex)
                        {
                            res.StatusCode = 0x191;
                            res.Write("Error: This service is unavailable till 2019: " + operationName);
                        };
                    }
                    base.ExceptionHandler = uncaughtExceptionDelegate;
                    HttpResponse originalResponse = res.OriginalResponse as HttpResponse;
                    originalResponse.SuppressFormsAuthenticationRedirect = false;
                    res.End();
                }
            });

            base.ServiceExceptionHandler = delegate (object request, Exception exception)
            {
                _logging.Log(exception);
                return DtoUtils.HandleException(this, request, exception);
            };
        }
    }

I can see this code working fine, and logging the exception if the condition is not satisfied.

However, there is an issue when I try to make a call to the API endpoint which invokes following:

    public class NotificationService: Service
{
    private IConfigurationProvider _configurationProvider;
    public NotificationService(IConfigurationProvider _configurationProvider)
    {
        _configurationProvider = configurationProvider;
    }

    public object Post(SendEventNotification request)
    {
        return new SendEventNotificationResponse { SentStatus = SendNotification(_configurationProvider.GetValue("EncId")) };
    }
}

It gives me an error saying -

Required dependency of type IConfigurationProvider could not be resolved.

Can anyone please suggest what could be the reason here? I believe the instances which were initialized during AppHost have not been persisted.

I am sure, something is missing, but unable to figure it out.

Any help on this will be much appreciated.

Thanks and Regards,

Nirman

Upvotes: 1

Views: 1124

Answers (1)

Nirman
Nirman

Reputation: 6793

I figured it out an issue of ServiceStack only. There was no need to use Autofac as Servicestack itself provides DI resolution. Also, I had to use "RegisterAutoWiredAs" method of ServiceStack's Container object.

Upvotes: 4

Related Questions