GithubHelp home page GithubHelp logo

medicalmask's Introduction

MedicalMask


以EF Core整合SQLite儲存口罩剩餘數量資訊

  • 建立API串接第三方平台

    1. 製作 Model
    2. 撰寫 Service
    3. 注冊 Service
    4. 呼叫 Service
  • 以EF Core整合SQLite儲存口罩剩餘數量資訊

    • 安裝 SQLITE
      # PowerShell
      Install-Package Microsoft.EntityFrameworkCore.Sqlite -Version 5.0.1
      # .NET Core
      dotnet add package Microsoft.EntityFrameworkCore.Sqlite --version 5.0.1
    • 進行 MIGRATION
      如果未安裝EF Core 命令列工具 (dotnet ef),須額外安裝
      # PowerShell
      Install-Package Microsoft.EntityFrameworkCore.Tools -Version 5.0.1
      Install-Package Microsoft.EntityFrameworkCore.Design -Version 5.0.1
      Add-Migration Initial
      Update-Database
      # .NET Core
      dotnet tool install --global dotnet-ef
      dotnet add package Microsoft.EntityFrameworkCore.Design
      dotnet ef migrations add Initial
      dotnet ef database update
      • 驗證安裝
        # PowerShell
        Get-Help about_EntityFrameworkCore
        # .NET Core
        dotnet ef 
        輸出後會看到獨角獸!
                     _/\__  
               ---==/    \\  
         ___  ___   |.    \|\  
        | __|| __|  |  )   \\\  
        | _| | _|   \_/ |  //|\\  
        |___||_|       /   \\\/\\  

Hangfire設定

  • 安裝 HANGFIRE + SQLITE 套件
    # PowerShell
    Install-Package HangFire.Core -Version 1.7.18
    Install-Package Hangfire.AspNetCore -Version 1.7.18
    Install-Package Hangfire.Storage.SQLite -Version 0.2.4
    Install-Package sqlite-net-pcl -Version 1.7.335
  • 加入 HANGFIRE 組態
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddHangfire(configuration => configuration
            .UseSimpleAssemblyNameTypeSerializer()
            .UseRecommendedSerializerSettings()
            .UseSQLiteStorage());
        services.AddHangfireServer();
    }
    先測試HangFire是否正常執行。
    另外,recurringJob 支援Cron 設定。
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        app.UseHangfireDashboard();
        BackgroundJob.Enqueue(() => Console.WriteLine("Hello HangFire."));
        RecurringJob.AddOrUpdate("Hello", () => Console.WriteLine("Hello, recurringJob."), Cron.Minutely());
    }
    因為 Hangfire 走的是 MVC 路由,而我們開的是 Web API 專案,預設情況下吃不到 MVC 路由。
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{id?}");
        });
    }

整合 Hangfire 來排程更新口罩剩餘數量資料

  • MaskService 有使用到 HttpClient ,改為使用 IHttpClientFactory 注入
        private HttpClient _client;
    
        public MaskService(IHttpClientFactory client)
        {
            _client = client.CreateClient();
            _client.BaseAddress = new Uri("https://www..../");
        }
  • 將原本 MaskController 裡面下載/更新資料庫的行為改由 MaskSchedule 處理,再由 HangFire 執行
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            BackgroundJob.Enqueue<MaskSchedule>(x => x.InitialDb());
            RecurringJob.AddOrUpdate<MaskSchedule>("MaskDataUpdate", x => x.MaskDataUpdate(), Cron.Minutely());
        }

參考資料:

medicalmask's People

Contributors

robbinhsu avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.