0
votes

I am trying to inject CommonService dependency in TableController as like below and it works fine if I call table controller endpoint like "http://localhost:61558/api/TodoItem".

but, It throws an error when I call the same endpoint using "http://localhost:61558/tables/TodoItem"(Correct way as Mobile App SDK call this URL to sync data)

Exception: "ExceptionType": "System.InvalidOperationException"

ExceptionMessage: "An error occurred when trying to create a controller of type 'TodoItemController'. Make sure that the controller has a parameterless public constructor.",

Startup.cs

public partial class Startup
    {
        public void ConfigureAuth(IAppBuilder app)
        {
            HttpConfiguration config = new HttpConfiguration();

            new MobileAppConfiguration()
                .AddMobileAppHomeController()
                .MapApiControllers()
                .AddTables(new MobileAppTableConfiguration()
                   .MapTableControllers()
                   .AddEntityFramework()
                )
                .ApplyTo(config);

            config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;

            app.UseWebApi(config);

        }
    } 

I have properly configured DIUnityConfig:

 public static void RegisterComponents()
        {
            var container = new UnityContainer();
            container.RegisterType(typeof(ICommonService), typeof(CommonService));
            Current = container;
        }

Here is the table controller code:

 [Authorize]
    public class TodoItemController : TableController<TodoItem>
    {
        private readonly ICommonService _ICommonService;
        public TodoItemController(ICommonService commonService)
        {
            _ICommonService = commonService;
        }
        protected override void Initialize(HttpControllerContext controllerContext)
        {
            base.Initialize(controllerContext);
            MobileServiceContext context = new MobileServiceContext();
            DomainManager = new EntityDomainManager<TodoItem>(context, Request, enableSoftDelete: true);
        }

        // PATCH tables/TodoItem/48D68C86-6EA6-4C25-AA33-223FC9A27959
        public async Task<TodoItem> PatchTodoItem(string id, Delta<TodoItem> patch)
        {
            var item = await UpdateAsync(id, patch);
            await PushToSyncAsync("todoitem", item.Id);
            
            _ICommonService.SendMail();// Want to send mail on business logic.

            return item;
        }
    }
1
your controller code is fine. And seems that container configuration code is fine. Please check that you have Unity injected into controller creation pipeline. And the RegisterComponents is executedoleksa
@oleksa Thanks. I am calling UnityConfig.RegisterComponents() in Application_Start method in Global.asax. it should be injected all services.H_H
it looks like you had forget to set DependencyResolver.SetResolveroleksa
What does your MobileAppConfiguration look like in your App_Start? That is critical to wiring up the table controllers properly. You need something like new MobileAppConfiguration().UseDefaultConfiguration().ApplyTo(config);Adrian Hall
@AdrianHall I have App_Start with below lines of code: new MobileAppConfiguration() .AddMobileAppHomeController() .MapApiControllers() .AddTables(new MobileAppTableConfiguration() .MapTableControllers() .AddEntityFramework() ) .ApplyTo(config);H_H

1 Answers

0
votes

Your table controller is not complete. You need to pass the HttpControllerContext into the constructor and then to the base. Something like this is normal:

public class DatesController : TableController<Dates>
{
    protected override void Initialize(HttpControllerContext controllerContext)
    {
        base.Initialize(controllerContext);
        var context = new AppDbContext();
        DomainManager = new EntityDomainManager<Dates>(context, Request);
    }
    // Rest of your controller here
}

Without calling Initialize(), your table controller is never registered.