簡體   English   中英

Azure Function V3 配置與 DI

[英]Azure Function V3 configuration with DI

我有一個帶有 2 個觸發器的 Azure Function: 在此處輸入圖像描述

我正在我的 Startup 中注冊 IService ,如下所示: 在此處輸入圖像描述

我需要在服務 class 中進行不同的配置,具體取決於調用 DoWork() 的觸發器? 如何使用 DI 實現這一目標?

public class Service : IService
{
    public Service(/*Configuration to be injected depends on calling trigger */)
    { }
 
    public void DoWork()
    { }
}

配置摘錄: 在此處輸入圖像描述

在此處輸入圖像描述

謝謝用戶1672994 發布您的建議作為答案,以便對面臨類似問題的其他社區成員有所幫助。

下面是實現 todo 工作項的示例代碼,這將有助於解決您的問題。

using AZV3CleanArchitecture.Models;
using AZV3CleanArchitecture.Options;
using AZV3CleanArchitecture.Providers;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;

namespace AZV3CleanArchitecture.Services
{
    public class ToDoItemsService : IToDoItemsService
    {
        private readonly HttpClient httpClient;
        private readonly ToDoItemsServiceOptions toDoItemsServiceOptions;
        private readonly ILogger<ToDoItemsService> logger;

        public ToDoItemsService(HttpClient httpClient, IOptions<ToDoItemsServiceOptions> toDoItemsServiceOptions, ILogger<ToDoItemsService> logger)
        {
            this.httpClient = httpClient;
            this.toDoItemsServiceOptions = toDoItemsServiceOptions.Value;
            this.logger = logger;
        }

        public async Task<ToDoItem> GetToDoItem(int id)
        {
            logger.LogInformation($"Retrieving item: {{{Constants.TodoItemId}}}", id);

            var getUrl = $"{this.toDoItemsServiceOptions.BaseUrl.TrimEnd('/')}/todos/{id}";
            using (var requestMessage = new HttpRequestMessage(HttpMethod.Get, getUrl))
            {
                using (var response = await this.httpClient.SendAsync(requestMessage))
                {
                    string responseString = await response.Content.ReadAsStringAsync();
                    logger.LogWarning($"Retrieved item: {{{Constants.TodoItemId}}}. Logged as warning for demo.", id);
                    return JsonConvert.DeserializeObject<ToDoItem>(responseString);
                }
            }
        }

        public async Task<IEnumerable<ToDoItem>> GetAllToDoItems(int id)
        {
            logger.LogInformation($"Retrieving all todo items");
            var getUrl = $"{this.toDoItemsServiceOptions.BaseUrl.TrimEnd('/')}/todos";
            using (var requestMessage = new HttpRequestMessage(HttpMethod.Get, getUrl))
            {
                using (var response = await this.httpClient.SendAsync(requestMessage))
                {
                    string responseString = await response.Content.ReadAsStringAsync();
                    return JsonConvert.DeserializeObject<IEnumerable<ToDoItem>>(responseString);
                }
            }
        }

        public async Task<ToDoItem> CreateToDoItem(ToDoItem toDoItem)
        {
            // call service and return the output

            return await Task.FromResult(new ToDoItem() { Id = 1, UserId = 1, Title = "Some Dummy Title", Completed = true });
        }

        public Task<ToDoItem> UpdateToDoItem(ToDoItem toDoItem)
        {
            throw new System.NotImplementedException();
        }
    }
}

有關更多信息,請查看ToDoItemServices鏈接。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM