John
John

Reputation: 1296

ServiceStack - Error trying to resolve Service {X} or one of its autowired dependencies

I am using servicestack and having problems with auto wiring.

Error trying to resolve Service '{Service}' or one of its autowired dependencies (see inner exception for details)

I don't need help figuring how exactly what the problem is. What I actually want is a way to see the inner exception. The inner exception should tell me the except problem without me having to figure it out but it not displayed in either the exception returned, or in the logs.

Setting DebugMode doesn't help either, it just includes the stack track of the topmost exception.

So basically, how do I stop servicestack from hiding the inner exception details?

Upvotes: 1

Views: 7303

Answers (4)

Merrin
Merrin

Reputation: 524

I have exactly the same exception.

In my case, it happens once migrated to ServiceStack v4. With v3, all works perfectly.

IoC configuration

public class AppHost : AppHostBase
{
    public AppHost() : base("Northwind web services", typeof(CustomersService).Assembly)
    { }        


    public override void Configure( Container container )
    {
        SetConfig(new HostConfig
        {
            DebugMode = true,
            ReturnsInnerException = true,
        });         

        var dbFactory = new OrmLiteConnectionFactory("~/Northwind.sqlite".MapHostAbsolutePath(), SqliteDialect.Provider);

        container.Register(dbFactory);

        // Dependencies
        container.RegisterAs<CustomerEntityRepository, ICustomerEntityRepository>();

        container.RegisterAutoWired<CustomersService>();

    }
}

Base class

public abstract class Repository<TEntity> : IRepository<TEntity> where TEntity : IEntity, new()
{        

    protected IDbConnectionFactory dbFactory { get; set; }

    public Repository( IDbConnectionFactory factory )
    {
        dbFactory = factory;
    }
}

Inherited class

public class CustomerEntityRepository : Repository<CustomerEntity>, ICustomerEntityRepository
    {
        public CustomerEntityRepository( IDbConnectionFactory dbFactory )
            : base(dbFactory)
        {
        }
    }
}

Only solution I've found is:

container.RegisterAs<ICustomerEntityRepository>(c => new CustomerEntityRepository(dbFactury));

Here's full exception message returned http://pastebin.com/jJntNN5p

Upvotes: 0

Alexis Reyes
Alexis Reyes

Reputation: 199

I ran into this same issue and it ended up being that there was an exception being thrown inside of the constructor that I had created for the particular endpoint class. Example...

public class PartnerService : Service
{

    private PartnerManagementService _partnerManagementService;

    public PartnerService()
    {
        var configuration = new Configuration();
        _partnerManagementService = new PartnerManagementService(configuration);
    }

    public object Get(PartnerGet partner)
    {

        try
        {
            var partners = _partnerManagementService.getPartners();

            if (!partners.Any())
            {
                return new HttpError(HttpStatusCode.NotFound, "Partners Could not be found");
            }

            return partners;
        }
        catch (Exception e)
        {

            return new HttpError(HttpStatusCode.InternalServerError, e);
        }
    }

If it so happens that an exception is thrown inside of the constructor, ServiceStack will not be able to resolve the service or one of its dependencies, in this case that dependency being the constructor for the class.

If you put a try/catch in the constructor for the class you could get an exception that actually makes sense.

Upvotes: 4

mythz
mythz

Reputation: 143339

ServiceStack should already return the inner Exception, i.e. here's the source of the error:

private Exception CreateResolveException<TService>(Exception ex)
{
    var errMsg = "Error trying to resolve Service '{0}' or one of its autowired dependencies (see inner exception for details).".Fmt(typeof(TService).FullName);
    return new Exception(errMsg, ex);
}

Basically there was a problem with your IOC configuration and that one of the dependencies caused an error.

You can change ServiceStack to serialize the Inner Exception with:

SetConfig(new EndpointHostConfig {
    ReturnsInnerException = true,
});

But this already defaults to true.

So the exception should already contain the Inner Exception, are you referring to what Exception gets serialized or the exception thrown in code?

Upvotes: 2

JMK
JMK

Reputation: 28059

One option could be to grab the actual source code from Github and add it as a project to your solution, as opposed to using a compiled DLL, then you could step through the actual code and see exactly where the exception is raised and why.

Upvotes: 0

Related Questions