1
votes

Looking for some insight into this issue. To me, it looks like all the configuration aligns with what is expected, but whenever i try to run dotnet publish TestAPI.dll and attempt to hit an endpoint, I see the following:

ArgumentNullException: Value cannot be null. Parameter name: connectionString Microsoft.EntityFrameworkCore.Utilities.Check.NotEmpty(string value, string parameterName) Microsoft.EntityFrameworkCore.Infrastructure.RelationalOptionsExtension.WithConnectionString(string connectionString) Microsoft.EntityFrameworkCore.MySQLDbContextOptionsExtensions.UseMySQL(DbContextOptionsBuilder optionsBuilder, string connectionString, Action MySQLOptionsAction) TestAPI.Startup.b__4_0(DbContextOptionsBuilder options) in Startup.cs + options.UseMySQL(Configuration.GetConnectionString("DefaultConnection"))); Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions+<>c__DisplayClass0_0.b__0(IServiceProvider p, DbContextOptionsBuilder b) Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.DbContextOptionsFactory(IServiceProvider applicationServiceProvider, Action optionsAction) Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions+<>c__DisplayClass5_0.b__0(IServiceProvider p) Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactory(FactoryCallSite factoryCallSite, ServiceProvider provider) Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor.VisitCallSite(IServiceCallSite callSite, TArgument argument)

I can confirm that it's working as expected when I run the application from the IDE (Visual Studio for Mac). Here's my relevant config:

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();
    }
}

even tried editing the appsettings.Development.json and appsettings.json within the corresponding publish folder (e.g. bin/Release/netcoreapp2.0/publish)

Any help is greatly appreciated! Thanks all

1
Are you adding the JSON files to IConfiguration from the program.cs file? - Jason H
@JasonH No. It's working when I run the application from VS, though. I'll edit to share my Program.cs code - Steve Boniface
I have never tried to run an ASP.NET Core Project without configuring the appsettings.json load process...maybe VS is doing some 'magic' under the sheets that I never knew it did. Let me get you the code I think you need to add and give it a go... - Jason H
It's unclear how do you publish your app. Is this dotnet publish and then dotnet TestAPI.dll ? - xneg
@xneg Yes. dotnet publish --configuration Debug or dotnet publish --configuration Release (tried both). Then, after having navigated to the output directory from the CLI, I execute dotnet TestAPI.dll. This launches the app on localhost on the port I configured. When trying to hit an endpoint, I see the error I've described above. - Steve Boniface

1 Answers

0
votes

Try this and if does not work let me know.

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; }

        .
        .
        .
        .
        .

    }
}