简体   繁体   English

如何在 Azure TableController 中注入公共服务依赖

[英]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.

相关问题 等待Azure移动服务TableController中的InsertAsync错误 - await InsertAsync error in Azure Mobile Service TableController Azure Mobile Service TableController不返回内部对象 - Azure Mobile Service TableController not returning inner objects 配置服务时如何通过依赖注入在 Azure Function V3 中注入或使用 IConfiguration - How to inject or use IConfiguration in Azure Function V3 with Dependency Injection when configuring a service 登录Microsoft Azure TableController - Logging in Microsoft Azure TableController UnitTest移动服务TableController - UnitTest Mobile Service TableController 如何使用Azure移动应用TableController获取相关实体 - How to get related Entities with azure mobile app Tablecontroller 如何在自主机环境中注入对用作 RESTFul 服务的 WCF 服务的依赖 - How to inject dependency on WCF service used as RESTFul service on selfhost enviroment 无法在服务中注入依赖 - Can't inject dependency in service 如何注射ILogger <T> 依赖于ConfigureServices中的服务 - .net core 2.0 - How to inject ILogger<T> dependency to a service in ConfigureServices - .net core 2.0 如何将服务依赖注入 IHostedService(计划作业)? - How can you dependency inject an service into an IHostedService (scheduled job)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM