带有MySQL DB的.NET Core 2.0 WebAPI - dotnet publish - 运行.dll

时间:2018-06-16 19:03:05

标签: c# mysql asp.net-core-2.0 asp.net-core-webapi

寻找对此问题的一些见解。对我来说,看起来所有配置都符合预期,但每当我尝试运行dotnet publish TestAPI.dll并尝试命中端点时,我会看到以下内容:

  

ArgumentNullException:值不能为null。参数名称:   的connectionString   Microsoft.EntityFrameworkCore.Utilities.Check.NotEmpty(字符串值,   string parameterName)   Microsoft.EntityFrameworkCore.Infrastructure.RelationalOptionsExtension.WithConnectionString(串   的connectionString)   Microsoft.EntityFrameworkCore.MySQLDbContextOptionsExtensions.UseMySQL(DbContextOptionsBuilder   optionsBuilder,string connectionString,   动作MySQLOptionsAction)   TestAPI.Startup.b__4_0(DbContextOptionsBuilder   在Startup.cs中的options)   +                                                       options.UseMySQL(Configuration.GetConnectionString(" DefaultConnection&#34)));   Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions + LT;> c__DisplayClass0_0.b__0(的IServiceProvider   p,DbContextOptionsBuilder b)   Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.DbContextOptionsFactory(的IServiceProvider   applicationServiceProvider,Action optionsAction)   Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions + LT;> c__DisplayClass5_0.b__0(的IServiceProvider   p)   Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactory(FactoryCallSite   factoryCallSite,ServiceProvider提供商)   Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor.VisitCallSite(IServiceCallSite   callSite,TArgument argument)

当我从IDE(Visual Studio for Mac)运行应用程序时,我可以确认它是按预期工作的。这是我的相关配置:

appsettings.json

{
  "ConnectionStrings": {
    "DefaultConnection": "server=localhost;userid=root;pwd=root;port=3306;database=Expenses;sslmode=none;"
  },
  "Logging": {
    "IncludeScopes": false,
    "Debug": {
      "LogLevel": {
        "Default": "Warning"
      }
    },
    //"Console": {
    //  "LogLevel": {
    //    "Default": "Warning"
    //  }
    //}
  }
}

appsettings.Development.json

{
  "ConnectionStrings": {
    "DefaultConnection": "server=localhost;userid=root;pwd=root;port=3306;database=Expenses;sslmode=none;"
  },
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Debug",
      "System": "Information",
      "Microsoft": "Information"
    }
  }
}

Startup.cs

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using TestAPI.Data;
using TestAPI.Data.Models;
using System;
using Microsoft.Extensions.Logging;

namespace TestAPI
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddCors();

            services.AddMvc();

            services.AddDbContext<ExpensesDbContext>(options =>
                                                    options.UseMySQL(Configuration.GetConnectionString("DefaultConnection")));
            services.AddTransient<IBaseDa<Accounts>, AccountsDataAccess>();
            services.AddTransient<IExpensesDa, ExpensesDa>();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            env.EnvironmentName = "Development";

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseCors(builder => builder
                .AllowAnyOrigin()
                .AllowAnyMethod()
                .AllowAnyHeader()
                .AllowCredentials());

            app.UseMvc();
        }
    }
}

Program.cs的

using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;

namespace TestAPI
{
    public class Program
    {
        public static void Main(string[] args)
        {
            //var builder = new ContainerBuilder();

            //// register types here for DI
            //builder.RegisterType<AccountsDataAccess>().As<IBaseDa<Accounts>>();

            //_container = builder.Build();

            BuildWebHost(args).Run();
        }

        public static IWebHost BuildWebHost(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
                .Build();
    }
}

甚至尝试在相应的发布文件夹中编辑appsettings.Development.json和appsettings.json(例如bin / Release / netcoreapp2.0 / publish)

非常感谢任何帮助!谢谢大家

1 个答案:

答案 0 :(得分:0)

尝试这个,如果不起作用,请告诉我。

<强> startup.cs

namespace TestAPI
{
    public class Startup
    {
        public Startup(IHostingEnvironment env)
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(env.ContentRootPath)
                .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
                .AddEnvironmentVariables();
                Configuration = builder.Build();
        }

        public IConfiguration Configuration { get; }

        .
        .
        .
        .
        .

    }
}