找不到配置文件“ config.json”,它不是可选的

时间:2019-11-12 22:51:34

标签: azure configuration azure-functions azure-function-app

我使用Azure Functions v2 我尝试创建启动文件:

<div class="hidden-xs" style="position:fixed;z-index:2456;top:1em;right:0em;background-color:#fff;">
                <table class="table cart table-hover table_price" id="current_price">
                    <thead>
                        <tr>
                            <th colspan="2">Bid</th>
                            <th colspan="2">Ask</th>
                        </tr>
                        <tr>
                            <th><div style="width:35px;">Size</div></th>
                            <th>Price</th>
                            <th>Price</th>
                            <th><div style="width:35px;">Size</div></th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr><td class="price_bid">334</td><td class="price_bid">8,858.00</td><td class="price_ask">8,870.00</td><td class="price_ask">210</td></tr>
                        <tr><td class="price_bid">286</td><td class="price_bid">8,858.50</td><td class="price_ask">8,869.50</td><td class="price_ask">8,832</td></tr>
                        <tr><td class="price_bid">287</td><td class="price_bid">8,859.00</td><td class="price_ask">8,868.50</td><td class="price_ask">490</td></tr>
                        <tr><td class="price_bid">315</td><td class="price_bid">8,860.00</td><td class="price_ask">8,868.00</td><td class="price_ask">367</td></tr>
                        <tr><td class="price_bid">1</td><td class="price_bid">8,860.50</td><td class="price_ask">8,867.50</td><td class="price_ask">7,044</td></tr>
                        <tr><td class="price_bid">5</td><td class="price_bid">8,862.00</td><td class="price_ask">8,867.00</td><td class="price_ask">400</td></tr>
                        <tr><td class="price_bid">10</td><td class="price_bid">8,862.50</td><td class="price_ask">8,866.50</td><td class="price_ask">3,253</td></tr>
                        <tr><td class="price_bid">286</td><td class="price_bid">8,863.00</td><td class="price_ask">8,866.00</td><td class="price_ask">56</td></tr>
                        <tr><td class="price_bid">286</td><td class="price_bid">8,863.50</td><td class="price_ask">8,865.50</td><td class="price_ask">400</td></tr>
                        <tr><td class="price_bid">634</td><td class="price_bid">8,864.00</td><td class="price_ask">8,864.50</td><td class="price_ask">921</td></tr>
                    </tbody>
                </table>
            </div>

文件“ config.json”存在,并且具有“始终要复制”选项:

[assembly: FunctionsStartup(typeof(AzureAppDomainRegistration.Startup))]
namespace AzureAppDomainRegistration
{
    public class Startup : FunctionsStartup
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {
            var config = new ConfigurationBuilder()
                .AddJsonFile("config.json")
                .Build();

            var connString = config["ConnectionStrings:DataContext"];
            builder.Services.AddDbContext<DataContext>(options => options
                .UseLazyLoadingProxies()
                .UseSqlServer(connString));

            builder.Services.AddTransient<IActionsRegistrationInfo, EfActionsRegistrationInfo>();
        }
    }
}

但是当我启动Azure Function(在本地或发布到Azure)时,会出现错误:

  

Microsoft.Extensions.Configuration.FileExtensions:配置   找不到文件“ config.json”,它不是可选的。

怎么了?

2 个答案:

答案 0 :(得分:1)

.AddJsonFile()方法之前,添加以下代码行:.SetBasePath(Environment.CurrentDirectory)。然后在您的项目中,右键单击“ config.json”->选择属性->然后将“复制到输出目录”设置为“始终复制”。

请注意,我使用的是IWebJobsStartup而不是FunctionsStartup

这是适合我的代码:

using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using System;
using System.IO;

//using WebJobsStartup instead of FunctionsStartup
[assembly: WebJobsStartup(typeof(FunctionApp20.Startup))]
namespace FunctionApp20
{
    public static class Function1
    {
        [FunctionName("Function1")]
        public static void Run([BlobTrigger("samples-workitems/{name}", Connection = "AzureWebJobsStorage")]Stream myBlob, string name, ILogger log)
        {
            log.LogInformation($"C# Blob trigger function Processed blob\n Name:{name} \n Size: {myBlob.Length} Bytes");
        }
    }

    //using IWebJobsStartup instead of FunctionsStartup
    public class Startup : IWebJobsStartup
    {
        public void Configure(IWebJobsBuilder builder)
        {            
            var config = new ConfigurationBuilder()   
                .SetBasePath(Environment.CurrentDirectory)
                .AddJsonFile("config.json",true,true)
                .Build();            
        }
    }

}

答案 1 :(得分:0)

在更高的.net核心版本中,将使用* .csproj文件而不是project.json文件。 您可以通过添加以下内容来修改文件以获得所需的结果:

    @Override
    public void onClick(View v) {

        listener.itemClick();
        // i need to do .notifyDataSetChanged() here but im not sure how
    }