iT邦幫忙

2018 iT 邦幫忙鐵人賽
DAY 16
0
Software Development

.Net微服務輕旅行30天系列 第 16

Day 16 Steeltoe OSS提供的Config Server(3)

還沒支援.Net Core 2.0

自行練習的code,run時候一直出現下面的錯誤
System.MissingMethodException occurred
HResult=0x80131513
Message=Method not found: 'System.Collections.Generic.IEnumerable `1<Microsoft.Extensions.Configuration.IConfigurationSource> Microsoft.Extensions.Configuration.IConfigurationBuilder.get_Sources()'.

https://github.com/SteeltoeOSS/Configuration/issues/18
找到上面的issue才知道,尚未支援.NET Core 2.0的寫法,只支援讓1.1的code跑在2.0上(sample code就是),
只好拿開發中的版本來run

https://myget.org/feed/steeltoedev/package/nuget/Steeltoe.Extensions.Configuration.ConfigServer

由於放在myget.org上,首先需要在Visual Studio 2017的Toos > NuGet Package Manager > Package Manager Setting加入來源
https://ithelp.ithome.com.tw/upload/images/20180104/20107867eSzCF3K8tO.png

在Package Manager Console裡面打上
Install-Package Steeltoe.Extensions.Configuration.ConfigServer -Version 2.0.0-dev-00198 -Source https://www.myget.org/F/steeltoedev/api/v3/index.json
這樣就可以安裝開發中版本的套件了

小練習改成2.0可用

利用.Net Core2.0來實作,Startup.cs跟Model跟官方的samples一樣,
但在Controller裡使用的時候需要小更改,原本Sample(1.1)裡用的是IConfigurationRoot,
在Controller裡面使用的時候要改成IConfiguration,
(若是用.Net Core 2.0新增專案,Startup.cs也會使用IConfiguration)

IConfiguration 應該是.Net Core本身的提供的物件,但在這個練習裡面沒有用到,主要用途是reload設定的時候才會用到,有機會再研究摟
是從遠端Github收到的檔案內容,map到ConfigServerData
則是config server的設定,下面是他提供的一些property
https://ithelp.ithome.com.tw/upload/images/20180104/20107867lUHugXygAi.png

APIGateway\Controllers\ConfigController.cs

using System.Collections.Generic;
using APIGateway.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;
using Steeltoe.Extensions.Configuration.ConfigServer;

namespace APIGateway.Controllers
{
  [Produces("application/json")]
  [Route("api/Config")]
  public class ConfigController : Controller
  {
    private readonly IConfiguration configuration;
    private readonly IOptionsSnapshot<ConfigServerData> _configOptionsSnapshot;
    private readonly IOptions<ConfigServerClientSettings> _configServerClientSettings;

    public ConfigController(IConfiguration configuration, IOptionsSnapshot<ConfigServerData> configOptionsSnapshot, IOptions<ConfigServerClientSettings> configServerClientSettings)
    {
      if (configOptionsSnapshot != null)
        this.configuration = configuration;

      if (configServerClientSettings != null)
        this._configServerClientSettings = configServerClientSettings;
        
      this._configOptionsSnapshot = configOptionsSnapshot;
    }

    // GET: api/Config
    [HttpGet]
    public IEnumerable<string> Get()
    {
      var optionsSnapshot = _configOptionsSnapshot.Value;
      var configServerClientSettings = _configServerClientSettings.Value;
      return new string[] { $"Foo:{optionsSnapshot.Foo}",
        $"Bar:{optionsSnapshot.Bar}",
        $"Env:{configServerClientSettings.Environment}",
        $"Name:{configServerClientSettings.Name}",
        $"Uri:{configServerClientSettings.Uri}",
        $"RawUri:{configServerClientSettings.RawUri}"

      };
    }    
  }
}

意料之外,沒想到還沒支援2.0,希望後面的其他sample可以相容.Net core 2.0,


上一篇
Day 15 Steeltoe OSS提供的Config Server(2)
下一篇
Day 17 Steeltoe OSS提供的Service Registry and Discovery (1)
系列文
.Net微服務輕旅行30天30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言