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;
}
}
RegisterComponents
is executed – oleksanew MobileAppConfiguration().UseDefaultConfiguration().ApplyTo(config);
– Adrian Hall