[英]How to inject Common Service Dependency in Azure TableController
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".我正在尝试在 TableController 中注入 CommonService 依赖项,如下所示,如果我调用诸如“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)但是,当我使用“http://localhost:61558/tables/TodoItem”调用同一个端点时会引发错误(移动应用 SDK 调用此 URL 以同步数据的正确方法)
Exception: "ExceptionType": "System.InvalidOperationException"异常: “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.", ExceptionMessage : "尝试创建类型为 'TodoItemController' 的控制器时出错。确保控制器具有无参数的公共构造函数。",
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:我已经正确配置了 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;
}
}
Your table controller is not complete.您的表控制器不完整。 You need to pass the HttpControllerContext into the constructor and then to the base.您需要将 HttpControllerContext 传递给构造函数,然后传递给基类。 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.不调用Initialize()
,您的表控制器永远不会注册。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.