0

I'm working with microsoft visual studio 2015 and my solution has 3 projects:

  • a net core mvc for restfull service.
  • a net core library for entities.
  • a net core library for data access.

An example of data access class and members is:

namespace creaworlds.Framework.WebApi.DataAccess
public static partial class Customer {
    public static bool Exists(string id) {
        var exists = false;
        if(id == null) {
            throw new Exception("The id can't be null");
        } else {
            /****************************************
            *   now is time to work with database,  *
            *   loading the connection string from  *
            *   appsettings.json                *
            *****************************************/
        }
        return exists;
    }
}

My Startup.cs in the mvc project is:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace creaworlds.Framework.WebApi.WebService
{
    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 IConfigurationRoot Configuration { get; }

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

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            loggerFactory.AddConsole(Configuration.GetSection("Logging"));
            loggerFactory.AddDebug();

            app.UseMvc();
        }
    }
}

The question is: Exists some way to inject the IConfigurationRoot specified at Startup.cs of MVC project in Data Access project?

shopi
  • 1
  • 1
    Possible duplicate of [Access from class library to appsetting.json in Asp.net-core](http://stackoverflow.com/questions/38398022/access-from-class-library-to-appsetting-json-in-asp-net-core) – Sanket Feb 12 '17 at 09:01
  • check this one. http://stackoverflow.com/questions/41695889/access-appsettings-json-file-setting-from-the-class-library-project/41698042 – Ahmar Feb 13 '17 at 05:12

0 Answers0