.net核心控制台应用程序强类型配置

时间:2020-09-16 15:55:04

标签: c# .net-core-3.1

有一个关于如何在.net核心控制台应用程序上创建强类型配置的stackoverflow答案here,但我的问题是如何将这种强类型配置传递给其他服务;

类似的东西

<template>
  <div>
    <input type="text" />
    <kendo-grid :data-source="localDataSource" v-model="selectedItem" @rowClick="onRowClick">
      <kendo-grid-column @select="onRowClick" :selectable="true" :width="60">mne</kendo-grid-column>
      <kendo-grid-column  :field="'ProductName'" :width="180"></kendo-grid-column>
      <kendo-grid-column :field="'UnitPrice'" :title="'Unit Price'" :width="120" :format="'{0:c}'"></kendo-grid-column>
      <kendo-grid-column :field="'Category'" :title="'Category'" :width="180"></kendo-grid-column>
      <kendo-grid-column :field="'Discontinued'" :width="120"></kendo-grid-column>
    </kendo-grid>
    {{selectedItem}}
  </div>
</template>


<script>
import Vue from "vue";
import { GridInstaller } from "@progress/kendo-grid-vue-wrapper";

Vue.use(GridInstaller);

export default {
  methods: {
        onRowClick (event) {
            console.log('$> ', event);
        },
  },
  data() {
    return {
      selectedItem: [],
      localDataSource: {
        data: [
          {
            ProductID: 1,
            ProductName: "Chai",
            UnitPrice: 18,
            UnitsInStock: 39,
            Discontinued: false,
            Category: "Beverages",
          },
          {
            ProductID: 4,
            ProductName: "Chef Anton",
            UnitPrice: 22,
            UnitsInStock: 53,
            Discontinued: false,
            Category: "Beverages",
          },
          {
            ProductID: 5,
            ProductName: "Chef Gumbo Mix",
            UnitPrice: 21.35,
            UnitsInStock: 0,
            Discontinued: true,
            Category: "Dairy Products",
          },
          {
            ProductID: 6,
            ProductName: "Boysenberry Spread",
            UnitPrice: 25,
            UnitsInStock: 120,
            Discontinued: false,
            Category: "Condiments",
          },
        ],
      },
    };
  },
};
</script>

<style>
</style>

1 个答案:

答案 0 :(得分:0)

这是MS文档中的一个示例 https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/options?view=aspnetcore-3.1#accessing-options-during-startup

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

    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddOptions<MyConfigOptions>()
            .Bind(Configuration.GetSection(MyConfigOptions.MyConfig))
            .ValidateDataAnnotations();

        services.AddControllersWithViews();
    }

和消费者:

public HomeController(IOptions<MyConfigOptions> config,
                          ILogger<HomeController> logger)