8

Azure त्रुटि है:विन्यास फाइल 'appsettings.json' नहीं मिला था और वैकल्पिक नहीं है

.Net Core: Application startup exception: System.IO.FileNotFoundException: The configuration file 'appsettings.json' was not found and is not optional.

तो यह थोड़ा अस्पष्ट है। मैं इसे कम करने के लिए प्रतीत नहीं कर सकता। मैं Azure करने के लिए एक नेट कोर वेब एपीआई परियोजना को तैनात करने की कोशिश कर रहा हूँ, और मैं इस त्रुटि हो रही है:

:( Oops. 500 Internal Server Error An error occurred while starting the application.

मैं सादे पुराने नेट वेबएपीआई की और वे काम किया है नियोजित किए गए। मैंने ऑनलाइन ट्यूटोरियल्स का पालन किया है और उन्होंने काम किया है। लेकिन किसी भी तरह मेरी परियोजना तोड़ दी गई है।

2016-08-26T02:55:12 Welcome, you are now connected to log-streaming service. 
Application startup exception: System.IO.FileNotFoundException: The configuration file 'appsettings.json' was not found and is not optional. 
    at Microsoft.Extensions.Configuration.FileConfigurationProvider.Load(Boolean reload) 
    at Microsoft.Extensions.Configuration.FileConfigurationProvider.Load() 
    at Microsoft.Extensions.Configuration.ConfigurationRoot..ctor(IList`1 providers) 
    at Microsoft.Extensions.Configuration.ConfigurationBuilder.Build() 
    at Quanta.API.Startup..ctor(IHostingEnvironment env) in D:\Source\Workspaces\Quanta\src\Quanta.API\Startup.cs:line 50 
--- End of stack trace from previous location where exception was thrown --- 
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 
    at Microsoft.Extensions.Internal.ActivatorUtilities.ConstructorMatcher.CreateInstance(IServiceProvider provider) 
    at Microsoft.Extensions.Internal.ActivatorUtilities.CreateInstance(IServiceProvider provider, Type instanceType, Object[] parameters) 
    at Microsoft.Extensions.Internal.ActivatorUtilities.GetServiceOrCreateInstance(IServiceProvider provider, Type type) 
    at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetServiceOrCreateInstance(IServiceProvider provider, Type type) 
    at Microsoft.AspNetCore.Hosting.Internal.StartupLoader.LoadMethods(IServiceProvider services, Type startupType, String environmentName) 
    at Microsoft.AspNetCore.Hosting.WebHostBuilderExtensions.<>c__DisplayClass1_0.<UseStartup>b__1(IServiceProvider sp) 
    at Microsoft.Extensions.DependencyInjection.ServiceLookup.FactoryService.Invoke(ServiceProvider provider) 
    at Microsoft.Extensions.DependencyInjection.ServiceProvider.ScopedCallSite.Invoke(ServiceProvider provider) 
    at Microsoft.Extensions.DependencyInjection.ServiceProvider.SingletonCallSite.Invoke(ServiceProvider provider) 
    at Microsoft.Extensions.DependencyInjection.ServiceProvider.<>c__DisplayClass12_0.<RealizeService>b__0(ServiceProvider provider) 
    at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType) 
    at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType) 
    at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider) 
    at Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureStartup() 
    at Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureApplicationServices() 
    at Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication() 
Hosting environment: Production 
Content root path: D:\home\site\wwwroot 
Now listening on: http://localhost:30261 
Application started. Press Ctrl+C to shut down. 

ठीक है, इतना आसान लगता है: Web.config पर stdoutLogEnabled को सक्षम करने और Azure स्ट्रीमिंग लॉग्स को देखकर मुझे इस देता है। यह appsettings.json नहीं मिल रहा है। मेरी कॉन्फ़िगरेशन (startup.cs) को देखकर यह बहुत अच्छी तरह से परिभाषित लगता है। मेरा स्टार्टअप इस तरह दिखता है:

public class Startup 
{ 
    private static string _applicationPath = string.Empty; 
    private static string _contentRootPath = string.Empty; 
    public IConfigurationRoot Configuration { get; set; } 
    public Startup(IHostingEnvironment env) 
    { 
     _applicationPath = env.WebRootPath; 
     _contentRootPath = env.ContentRootPath; 
     // Setup configuration sources. 

     var builder = new ConfigurationBuilder() 
      .SetBasePath(_contentRootPath) 
      .AddJsonFile("appsettings.json") 
      .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true); 

     if (env.IsDevelopment()) 
     { 
      // This reads the configuration keys from the secret store. 
      // For more details on using the user secret store see http://go.microsoft.com/fwlink/?LinkID=532709 
      builder.AddUserSecrets(); 
     } 

     builder.AddEnvironmentVariables(); 
     Configuration = builder.Build(); 
    } 
    private string GetXmlCommentsPath() 
    { 
     var app = PlatformServices.Default.Application; 
     return System.IO.Path.Combine(app.ApplicationBasePath, "Quanta.API.xml"); 
    } 

    // This method gets called by the runtime. Use this method to add services to the container. 
    // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940 
    public void ConfigureServices(IServiceCollection services) 
    { 
     var pathToDoc = GetXmlCommentsPath(); 


     services.AddDbContext<QuantaContext>(options => 
      options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"], 
      b => b.MigrationsAssembly("Quanta.API"))); 

     //Swagger 
     services.AddSwaggerGen(); 
     services.ConfigureSwaggerGen(options => 
     { 
      options.SingleApiVersion(new Info 
      { 
       Version = "v1", 
       Title = "Project Quanta API", 
       Description = "Quant.API", 
       TermsOfService = "None" 
      }); 
      options.IncludeXmlComments(pathToDoc); 
      options.DescribeAllEnumsAsStrings(); 
     }); 

     // Repositories 
     services.AddScoped<ICheckListRepository, CheckListRepository>(); 
     services.AddScoped<ICheckListItemRepository, CheckListItemRepository>(); 
     services.AddScoped<IClientRepository, ClientRepository>(); 
     services.AddScoped<IDocumentRepository, DocumentRepository>(); 
     services.AddScoped<IDocumentTypeRepository, DocumentTypeRepository>(); 
     services.AddScoped<IProjectRepository, ProjectRepository>(); 
     services.AddScoped<IProtocolRepository, ProtocolRepository>(); 
     services.AddScoped<IReviewRecordRepository, ReviewRecordRepository>(); 
     services.AddScoped<IReviewSetRepository, ReviewSetRepository>(); 
     services.AddScoped<ISiteRepository, SiteRepository>(); 

     // Automapper Configuration 
     AutoMapperConfiguration.Configure(); 

     // Enable Cors 
     services.AddCors(); 

     // Add MVC services to the services container. 
     services.AddMvc() 
      .AddJsonOptions(opts => 
      { 
       // Force Camel Case to JSON 
       opts.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); 
      }); 
    } 

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. 
    public void Configure(IApplicationBuilder app) 
    { 
     app.UseStaticFiles(); 
     // Add MVC to the request pipeline. 
     app.UseCors(builder => 
      builder.AllowAnyOrigin() 
      .AllowAnyHeader() 
      .AllowAnyMethod()); 

     app.UseExceptionHandler(
      builder => 
      { 
       builder.Run(
       async context => 
       { 
        context.Response.StatusCode = (int)HttpStatusCode.InternalServerError; 
        context.Response.Headers.Add("Access-Control-Allow-Origin", "*"); 

        var error = context.Features.Get<IExceptionHandlerFeature>(); 
        if (error != null) 
        { 
         context.Response.AddApplicationError(error.Error.Message); 
         await context.Response.WriteAsync(error.Error.Message).ConfigureAwait(false); 
        } 
       }); 
      }); 

     app.UseMvc(routes => 
     { 
      routes.MapRoute(
       name: "default", 
       template: "{controller=Home}/{action=Index}/{id?}"); 

      // Uncomment the following line to add a route for porting Web API 2 controllers. 
      //routes.MapWebApiRoute("DefaultApi", "api/{controller}/{id?}"); 
     }); 


     //Ensure DB is created, and latest migration applied. Then seed. 
     using (var serviceScope = app.ApplicationServices 
      .GetRequiredService<IServiceScopeFactory>() 
      .CreateScope()) 
     { 
      QuantaContext dbContext = serviceScope.ServiceProvider.GetService<QuantaContext>(); 
      dbContext.Database.Migrate(); 
      QuantaDbInitializer.Initialize(dbContext); 
     } 


     app.UseSwagger(); 
     app.UseSwaggerUi(); 


    } 
} 

यह स्थानीय रूप से ठीक काम करता है। लेकिन एक बार जब हम Azure को प्रकाशित करते हैं, तो यह विफल हो जाता है। मुझे हानि हो रही है। मैंने नया .Net कोर प्रोजेक्ट बनाया है जो Azure पर तैनात है बस ढूंढें। लेकिन यह एक परियोजना है, जिसे मैंने अपना पूरा समय दिया है, असफल लगता है। मैं इस परियोजना से बाहर कोड कॉपी और पेस्ट करने के लिए तैयार हूं जो एक नई परियोजना में भागने में विफल रहता है, लेकिन मैं इसे तोड़ने पर वास्तव में उत्सुक हूं।

कोई विचार?

संपादित करें: तो मेरी Program.cs था:

using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
using System.Threading.Tasks; 
using Microsoft.AspNetCore.Hosting; 

namespace Quanta.API 
{ 
    public class Program 
    { 
     public static void Main(string[] args) 
     { 
      var host = new WebHostBuilder() 
       .UseKestrel() 
       .UseContentRoot(Directory.GetCurrentDirectory()) 
       .UseIISIntegration() 
       .UseStartup<Startup>() 
       .Build(); 

      host.Run(); 
     } 
    } 
} 

EDIT2: प्रति Frans, मैं publishOptions जाँच की। यह था:

"publishOptions": { 
"include": [ 
    "wwwroot", 
    "web.config" 
] 

},

मैं एक काम परियोजना से एक publishOptions लिया और :: को

"publishOptions": { 
    "include": [ 
    "wwwroot", 
    "Views", 
    "Areas/**/Views", 
    "appsettings.json", 
    "web.config" 
    ] 
    }, 

यह अभी भी एक 500 त्रुटि दे दी है यह बदल गया है, लेकिन यह नहीं दिया एक स्टैक ट्रेस कह रहा है कि यह appsettings.json लोड कर सकता है। अब यह एसक्यूएल के कनेक्शन के बारे में शिकायत कर रहा था। मैंने देखा कि मेरे एसक्यूएल कनेक्शन स्ट्रिंग कोड का उल्लेख आरसी 1 ब्लॉग पोस्ट में किया गया है। .NET कोर के आरसी 2 ने इसे बदल दिया। तो मैं यह करने के लिए अद्यतन:

"Data": { 
    "ConnectionStrings": { 
     "DefaultConnection": "Server=(localdb)\\MSSQLLocalDB;Database=QuantaDb;Trusted_Connection=True;MultipleActiveResultSets=true" 
    } 
    }, 

और करने के लिए अपने स्टार्टअप बदल दिया है:

services.AddDbContext<QuantaContext>(options => 
     options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"), 
     b => b.MigrationsAssembly("Quanta.API"))); 

अंत में, यह काम किया।

मुझे पुराने आरसी 1 उदाहरण का पालन करना होगा और इसे महसूस नहीं किया होगा।

+0

त्रुटि संदेश में 'सामग्री रूट पथ: डी: \ home \ site \ wwwroot' है। क्या इसकी उम्मीद है? फ़ोल्डर में 'appsettings.json' है? –

उत्तर

7

प्रोजेक्ट.जेसन में प्रकाशन विकल्प देखें और सुनिश्चित करें कि "शामिल" अनुभाग में "appsettings.json" है। उन्होंने आरटीएम में प्रकाशित मॉडल को बदल दिया है ताकि आप संकलन निर्देशिका से प्रतिलिपि बनाई गई सभी चीज़ों को वेब फ़ोल्डर में निर्दिष्ट कर सकें।

+0

मुझे लगता है कि आप appsettings.json का मतलब है, और यह प्रकाशन ऑप्शन का हिस्सा नहीं था। मैंने एक प्रोजेक्ट प्रोजेक्ट से एक प्रकाशन ऑप्शन की प्रतिलिपि बनाई: – Frank

+0

ओह, हाँ :) जब मैं अपने फोन पर जवाब देता हूं तो यही होता है :) – Frans

22

अपने project.json

सुनिश्चित करें कि आप एक copyToOutput

"buildOptions": { 
    "emitEntryPoint": true, 
    "preserveCompilationContext": true, 
    "copyToOutput": { 
    "include": [ "appsettings.json" ] 
    } 
}, 
+0

आपकी टिप्पणी के लिए धन्यवाद। मेरी समस्या हल –

20

बाद में .net कोर संस्करणों में के रूप में appsettings.json inlcuded है में एक * .csproj फ़ाइल project.json फ़ाइल के बजाय प्रयोग किया जाता है ।

आप जोड़कर वांछित परिणाम प्राप्त करने के लिए फ़ाइल को संशोधित कर सकते हैं:

<ItemGroup> 
     <Content Update="appsettings.json"> 
     <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> 
     </Content> 
    </ItemGroup> 
3

आप विकल्पों प्रकाशित करने के लिए अपने .json फ़ाइल जोड़ने की जरूरत नहीं है। यह सिर्फ इतना है कि यह गलत पथ पर फ़ाइल की तलाश में है।

आधार पथ सेट करें और फिर जेसन फ़ाइल जोड़ें और यह काम करेगा।

public Startup(IHostingEnvironment environment) 
    { 
     var builder = new ConfigurationBuilder() 
      .SetBasePath(environment.ContentRootPath) 
      .AddJsonFile("TestJson.json"); 

     Configuration = builder.Build(); 
    } 

यहां, स्टार्टअप कन्स्ट्रक्टर होस्टिंगएनिविर्नमेंट के साथ बनाया गया है और बेस पथ वर्तमान रूट पथ पर सेट है। और यह काम करेगा!

संबंधित मुद्दे