Использование MySqlDatabase (MySql.Data.MySqlClient) в Функциях Azure v2 - "Невозможно разрешить службу"

Я создал проект приложения Azure Functions ~v2, в котором используется MySql.Data.MySqlClient зависимость.

Проект настроен также на использование SwashBuckle библиотека для создания вызываемого API.

Когда я выполняю свой проект из своих локальных настроек, он работает нормально. Тем не менее, когда я публикую приложение Functions на нашем сервере Azure и пытаюсь протестировать функцию там, я получаю следующую ошибку:

System.InvalidOperationException: невозможно разрешить службу для типа "MyFunctionApp.MySqlDatabase" при попытке активировать "MyFunctionApp.PostsService".

в Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp,Type type,Type requiredBy,Boolean isDefaultParameterRequired)

...

Мой StartUp.cs:

using System;
using System.Reflection;
using AzureFunctions.Extensions.Swashbuckle;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Hosting;
using Microsoft.Extensions.DependencyInjection;
using MyFunctionApp;

[assembly: WebJobsStartup(typeof(SwashBuckleStartup))]
namespace MyFunctionApp
{
    internal class SwashBuckleStartup : IWebJobsStartup
    {
        public void Configure(IWebJobsBuilder builder)
        {
            ConfigureServices(builder.Services);
            builder.AddSwashBuckle(Assembly.GetExecutingAssembly());
        }

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddScoped<MySqlDatabase>(_ => new MySqlDatabase(Environment.GetEnvironmentVariable("MyFunctionApp-DbConn")));
        }
    }
}

Мой MySqlDatabase.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;

namespace MyFunctionApp
{
    public class MySqlDatabase : IDisposable
    {
        public MySqlConnection Connection;

        public MySqlDatabase(string connectionString)
        {
            Connection = new MySqlConnection(connectionString);
            this.Connection.Open();
        }

        public void Dispose()
        {
            Connection.Close();
        }
    }
}

Вот служба, которую я вызываю, которая выдает упомянутую выше ошибку (PostsService.cs):

using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using System.Net;
using MySql.Data.MySqlClient;

namespace MyFunctionApp
{
    public class PostsService
    {
        private readonly MySqlDatabase _MySqlDatabase;

        public PostsService(MySqlDatabase mySqlDatabase)
        {
            _MySqlDatabase = mySqlDatabase;
        }

        [FunctionName("InsertIntoPost")]
        public async Task<IActionResult> InsertIntoPost(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] PostClassObject request,
            ILogger log)
        {
            var cmd = _MySqlDatabase.Connection.CreateCommand() as MySqlCommand;
            cmd.CommandText = @"INSERT INTO PostsTable(ID) VALUES (12345)";

            int rowCount = await cmd.ExecuteNonQueryAsync();
            Console.WriteLine(String.Format("Number of rows inserted={0}", rowCount));

            return (ActionResult)new OkObjectResult(1);
        }
    }
}

1 ответ

Решение

Я закончил тем, что разделил StartUp.csв два файла. Теперь все работает!

WebJobsStartup.cs:

using AzureFunctions.Extensions.Swashbuckle;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Hosting;
using System.Reflection;
using MyFunctionApp;

[assembly: WebJobsStartup(typeof(WebJobsStartup))]
namespace MyFunctionApp
{
    public class WebJobsStartup : IWebJobsStartup
    {
        public void Configure(IWebJobsBuilder builder)
        {
            builder.AddSwashBuckle(Assembly.GetExecutingAssembly());
        }
    }

}

MyFunctionAppStartUp.cs:

using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using System;

[assembly: FunctionsStartup(typeof(MyFunctionApp.MyFunctionAppStartup))]
namespace MyFunctionApp
{
    public class MyFunctionApp : FunctionsStartup
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {
            builder.Services.AddTransient<MySqlDatabase>((s) =>
            {
                return new MySqlDatabase(Environment.GetEnvironmentVariable("MyFunctionApp-DbConn"));
            });
            builder.Services.AddSingleton<ServiceQueries>();
        }
    }
}
Другие вопросы по тегам