主頁 > .NET開發 > 使用 .NET Core 3.0 的 AssemblyLoadContext 實作插件熱加載

使用 .NET Core 3.0 的 AssemblyLoadContext 實作插件熱加載

2020-09-23 03:09:12 .NET開發

一般情況下,一個 .NET 程式集加載到程式中以后,它的型別資訊以及原生代碼等資料會一直保留在記憶體中,.NET 運行時無法回收它們,如果我們要實作插件熱加載 (例如 Razor 或 Aspx 模版的熱更新) 則會造成記憶體泄漏,在以往,我們可以使用 .NET Framework 的 AppDomain 機制,或者使用解釋器 (有一定的性能損失),或者在編譯一定次數以后重啟程式 (Asp.NET 的 numRecompilesBeforeAppRestart) 來避免記憶體泄漏,

因為 .NET Core 不像 .NET Framework 一樣支持動態創建與卸載 AppDomain,所以一直都沒有好的方法實作插件熱加載,好訊息是,.NET Core 從 3.0 開始支持了可回收程式集 (Collectible Assembly),我們可以創建一個可回收的 AssemblyLoadContext,用它來加載與卸載程式集,關于 AssemblyLoadContext 的介紹與實作原理可以參考 yoyofx 的文章 與 我的文章,

本文會通過一個 180 行左右的示例程式,介紹如何使用 .NET Core 3.0 的 AssemblyLoadContext 實作插件熱加載,程式同時使用了 Roslyn 實作動態編譯,最終效果是改動插件代碼后可以自動更新到正在運行的程式當中,并且不會造成記憶體泄漏,

完整源代碼與檔案夾結構

首先我們來看看完整源代碼與檔案夾結構,源代碼分為兩部分,一部分是宿主,負責編譯與加載插件,另一部分則是插件,后面會對源代碼的各個部分作出詳細講解,

檔案夾結構:

  • pluginexample (頂級檔案夾)
    • host (宿主的專案)
      • Program.cs (宿主的代碼)
      • host.csproj (宿主的專案檔案)
    • guest (插件的代碼檔案夾)
      • Plugin.cs (插件的代碼)
      • bin (保存插件編譯結果的檔案夾)
        • MyPlugin.dll (插件編譯后的 DLL 檔案)

Program.cs 的內容:

using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Loader;
using System.Threading;

namespace Common
{
    public interface IPlugin : IDisposable
    {
        string GetMessage();
    }
}

namespace Host
{
    using Common;

    internal class PluginController : IPlugin
    {
        private List<Assembly> _defaultAssemblies;
        private AssemblyLoadContext _context;
        private string _pluginName;
        private string _pluginDirectory;
        private volatile IPlugin _instance;
        private volatile bool _changed;
        private object _reloadLock;
        private FileSystemWatcher _watcher;

        public PluginController(string pluginName, string pluginDirectory)
        {
            _defaultAssemblies = AssemblyLoadContext.Default.Assemblies
                .Where(assembly => !assembly.IsDynamic)
                .ToList();
            _pluginName = pluginName;
            _pluginDirectory = pluginDirectory;
            _reloadLock = new object();
            ListenFileChanges();
        }

        private void ListenFileChanges()
        {
            Action<string> onFileChanged = path =>
            {
                if (Path.GetExtension(path).ToLower() == ".cs")
                    _changed = true;
            };
            _watcher = new FileSystemWatcher();
            _watcher.Path = _pluginDirectory;
            _watcher.IncludeSubdirectories = true;
            _watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName;
            _watcher.Changed += (sender, e) => onFileChanged(e.FullPath);
            _watcher.Created += (sender, e) => onFileChanged(e.FullPath);
            _watcher.Deleted += (sender, e) => onFileChanged(e.FullPath);
            _watcher.Renamed += (sender, e) => { onFileChanged(e.FullPath); onFileChanged(e.OldFullPath); };
            _watcher.EnableRaisingEvents = true;
        }

        private void UnloadPlugin()
        {
            _instance?.Dispose();
            _instance = null;
            _context?.Unload();
            _context = null;
        }

        private Assembly CompilePlugin()
        {
            var binDirectory = Path.Combine(_pluginDirectory, "bin");
            var dllPath = Path.Combine(binDirectory, $"{_pluginName}.dll");
            if (!Directory.Exists(binDirectory))
                Directory.CreateDirectory(binDirectory);
            if (File.Exists(dllPath))
            {
                File.Delete($"{dllPath}.old");
                File.Move(dllPath, $"{dllPath}.old");
            }

            var sourceFiles = Directory.EnumerateFiles(
                _pluginDirectory, "*.cs", SearchOption.AllDirectories);
            var compilationOptions = new CSharpCompilationOptions(
                OutputKind.DynamicallyLinkedLibrary,
                optimizationLevel: OptimizationLevel.Debug);
            var references = _defaultAssemblies
                .Select(assembly => assembly.Location)
                .Where(path => !string.IsNullOrEmpty(path) && File.Exists(path))
                .Select(path => MetadataReference.CreateFromFile(path))
                .ToList();
            var syntaxTrees = sourceFiles
                .Select(p => CSharpSyntaxTree.ParseText(File.ReadAllText(p)))
                .ToList();
            var compilation = CSharpCompilation.Create(_pluginName)
                .WithOptions(compilationOptions)
                .AddReferences(references)
                .AddSyntaxTrees(syntaxTrees);

            var emitResult = compilation.Emit(dllPath);
            if (!emitResult.Success)
            {
                throw new InvalidOperationException(string.Join("\r\n",
                    emitResult.Diagnostics.Where(d => d.WarningLevel == 0)));
            }
            //return _context.LoadFromAssemblyPath(Path.GetFullPath(dllPath));
            using (var stream = File.OpenRead(dllPath))
            {
                var assembly = _context.LoadFromStream(stream);
                return assembly;
            }
        }

        private IPlugin GetInstance()
        {
            var instance = _instance;
            if (instance != null && !_changed)
                return instance;

            lock (_reloadLock)
            {
                instance = _instance;
                if (instance != null && !_changed)
                    return instance;

                UnloadPlugin();
                _context = new AssemblyLoadContext(
                    name: $"Plugin-{_pluginName}", isCollectible: true);

                var assembly = CompilePlugin();
                var pluginType = assembly.GetTypes()
                    .First(t => typeof(IPlugin).IsAssignableFrom(t));
                instance = (IPlugin)Activator.CreateInstance(pluginType);

                _instance = instance;
                _changed = false;
            }

            return instance;
        }

        public string GetMessage()
        {
            return GetInstance().GetMessage();
        }

        public void Dispose()
        {
            UnloadPlugin();
            _watcher?.Dispose();
            _watcher = null;
        }
    }

    internal class Program
    {
        static void Main(string[] args)
        {
            using (var controller = new PluginController("MyPlugin", "../guest"))
            {
                bool keepRunning = true;
                Console.CancelKeyPress += (sender, e) => {
                    e.Cancel = true;
                    keepRunning = false;
                };
                while (keepRunning)
                {
                    try
                    {
                        Console.WriteLine(controller.GetMessage());
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine($"{ex.GetType()}: {ex.Message}");
                    }
                    Thread.Sleep(1000);
                }
            }
        }
    }
}

host.csproj 的內容:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.3.1" />
  </ItemGroup>

</Project>

Plugin.cs 的內容:

using System;
using Common;

namespace Guest
{
    public class MyPlugin : IPlugin
    {
        public MyPlugin()
        {
            Console.WriteLine("MyPlugin loaded");
        }

        public string GetMessage()
        {
            return "Hello 1";
        }

        public void Dispose()
        {
            Console.WriteLine("MyPlugin unloaded");
        }
    }
}

運行示例程式

進入 pluginexample/host 下運行 dotnet run 即可啟動宿主程式,這時宿主程式會自動編譯與加載插件,檢測插件檔案的變化并在變化時重新編譯加載,你可以在運行后修改 pluginexample/guest/Plugin.cs 中的 Hello 1Hello 2,之后可以看到類似以下的輸出:

MyPlugin loaded
Hello 1
Hello 1
Hello 1
MyPlugin unloaded
MyPlugin loaded
Hello 2
Hello 2

我們可以看到程式自動更新并執行修改以后的代碼,如果你有興趣還可以測驗插件代碼語法錯誤時會出現什么,

源代碼講解

接下來是對宿主的源代碼中各個部分的詳細講解:

IPlugin 介面

public interface IPlugin : IDisposable
{
    string GetMessage();
}

這是插件專案需要的實作介面,宿主專案在編譯插件后會尋找程式集中實作 IPlugin 的型別,創建這個型別的實體并且使用它,創建插件時會呼叫建構式,卸載插件時會呼叫 Dispose 方法,如果你用過 .NET Framework 的 AppDomain 機制可能會想是否需要 Marshalling 處理,答案是不需要,.NET Core 的可回收程式集會加載到當前的 AppDomain 中,回收時需要依賴 GC 清理,好處是使用簡單并且運行效率高,壞處是 GC 清理有延遲,只要有一個插件中型別的實體沒有被回收則插件程式集使用的資料會一直殘留,導致記憶體泄漏,

PluginController 型別

internal class PluginController : IPlugin
{
    private List<Assembly> _defaultAssemblies;
    private AssemblyLoadContext _context;
    private string _pluginName;
    private string _pluginDirectory;
    private volatile IPlugin _instance;
    private volatile bool _changed;
    private object _reloadLock;
    private FileSystemWatcher _watcher;

這是管理插件的代理類,在內部它負責編譯與加載插件,并且把對 IPlugin 介面的方法呼叫轉發到插件的實作中,類成員包括默認 AssemblyLoadContext 中的程式集串列 _defaultAssemblies,用于加載插件的自定義 AssemblyLoadContext _context,插件名稱與檔案夾,插件實作 _instance,標記插件檔案是否已改變的 _changed,防止多個執行緒同時編譯加載插件的 _reloadLock,與監測插件檔案變化的 _watcher

PluginController 的建構式

public PluginController(string pluginName, string pluginDirectory)
{
    _defaultAssemblies = AssemblyLoadContext.Default.Assemblies
        .Where(assembly => !assembly.IsDynamic)
        .ToList();
    _pluginName = pluginName;
    _pluginDirectory = pluginDirectory;
    _reloadLock = new object();
    ListenFileChanges();
}

建構式會從 AssemblyLoadContext.Default.Assemblies 中獲取默認 AssemblyLoadContext 中的程式集串列,包括宿主程式集、System.Runtime 等,這個串列會在 Roslyn 編譯插件時使用,表示插件編譯時需要參考哪些程式集,之后還會呼叫 ListenFileChanges 監聽插件檔案是否有改變,

PluginController.ListenFileChanges

private void ListenFileChanges()
{
    Action<string> onFileChanged = path =>
    {
        if (Path.GetExtension(path).ToLower() == ".cs")
            _changed = true;
    };
    _watcher = new FileSystemWatcher();
    _watcher.Path = _pluginDirectory;
    _watcher.IncludeSubdirectories = true;
    _watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName;
    _watcher.Changed += (sender, e) => onFileChanged(e.FullPath);
    _watcher.Created += (sender, e) => onFileChanged(e.FullPath);
    _watcher.Deleted += (sender, e) => onFileChanged(e.FullPath);
    _watcher.Renamed += (sender, e) => { onFileChanged(e.FullPath); onFileChanged(e.OldFullPath); };
    _watcher.EnableRaisingEvents = true;
}

這個方法創建了 FileSystemWatcher,監聽插件檔案夾下的檔案是否有改變,如果有改變并且改變的是 C# 源代碼 (.cs 擴展名) 則設定 _changed 成員為 true,這個成員標記插件檔案已改變,下次訪問插件實體的時候會觸發重新加載,

你可能會有疑問,為什么不在檔案改變后立刻觸發重新加載插件,一個原因是部分檔案編輯器的保存檔案實作可能會導致改變的事件連續觸發幾次,延遲觸發可以避免編譯多次,另一個原因是編譯程序中出現的例外可以傳遞到訪問插件實體的執行緒中,方便除錯與除錯 (盡管使用 ExceptionDispatchInfo 也可以做到),

PluginController.UnloadPlugin

private void UnloadPlugin()
{
    _instance?.Dispose();
    _instance = null;
    _context?.Unload();
    _context = null;
}

這個方法會卸載已加載的插件,首先呼叫 IPlugin.Dispose 通知插件正在卸載,如果插件創建了新的執行緒可以在 Dispose 方法中停止執行緒避免泄漏,然后呼叫 AssemblyLoadContext.Unload 允許 .NET Core 運行時卸載這個背景關系加載的程式集,程式集的資料會在 GC 檢測到所有型別的實體都被回收后回收 (參考文章開頭的鏈接),

PluginController.CompilePlugin

private Assembly CompilePlugin()
{
    var binDirectory = Path.Combine(_pluginDirectory, "bin");
    var dllPath = Path.Combine(binDirectory, $"{_pluginName}.dll");
    if (!Directory.Exists(binDirectory))
        Directory.CreateDirectory(binDirectory);
    if (File.Exists(dllPath))
    {
        File.Delete($"{dllPath}.old");
        File.Move(dllPath, $"{dllPath}.old");
    }

    var sourceFiles = Directory.EnumerateFiles(
        _pluginDirectory, "*.cs", SearchOption.AllDirectories);
    var compilationOptions = new CSharpCompilationOptions(
        OutputKind.DynamicallyLinkedLibrary,
        optimizationLevel: OptimizationLevel.Debug);
    var references = _defaultAssemblies
        .Select(assembly => assembly.Location)
        .Where(path => !string.IsNullOrEmpty(path) && File.Exists(path))
        .Select(path => MetadataReference.CreateFromFile(path))
        .ToList();
    var syntaxTrees = sourceFiles
        .Select(p => CSharpSyntaxTree.ParseText(File.ReadAllText(p)))
        .ToList();
    var compilation = CSharpCompilation.Create(_pluginName)
        .WithOptions(compilationOptions)
        .AddReferences(references)
        .AddSyntaxTrees(syntaxTrees);

    var emitResult = compilation.Emit(dllPath);
    if (!emitResult.Success)
    {
        throw new InvalidOperationException(string.Join("\r\n",
            emitResult.Diagnostics.Where(d => d.WarningLevel == 0)));
    }
    //return _context.LoadFromAssemblyPath(Path.GetFullPath(dllPath));
    using (var stream = File.OpenRead(dllPath))
    {
        var assembly = _context.LoadFromStream(stream);
        return assembly;
    }
}

這個方法會呼叫 Roslyn 編譯插件代碼到 DLL,并使用自定義的 AssemblyLoadContext 加載編譯后的 DLL,首先它需要洗掉原有的 DLL 檔案,因為卸載程式集有延遲,原有的 DLL 檔案在 Windows 系統上很可能會洗掉失敗并提示正在使用,所以需要先重命名并在下次洗掉,接下來它會查找插件檔案夾下的所有 C# 源代碼,用 CSharpSyntaxTree 決議它們,并用 CSharpCompilation 編譯,編譯時參考的程式集串列是建構式中取得的默認 AssemblyLoadContext 中的程式集串列 (包括宿主程式集,這樣插件代碼才可以使用 IPlugin 介面),編譯成功后會使用自定義的 AssemblyLoadContext 加載編譯后的 DLL 以支持卸載,

這段代碼中有兩個需要注意的部分,第一個部分是 Roslyn 編譯失敗時不會拋出例外,編譯后需要判斷 emitResult.Success 并從 emitResult.Diagnostics 找到錯誤資訊;第二個部分是加載插件程式集必須使用 AssemblyLoadContext.LoadFromStream 從記憶體資料加載,如果使用 AssemblyLoadContext.LoadFromAssemblyPath 那么下次從同一個路徑加載時仍然會回傳第一次加載的程式集,這可能是 .NET Core 3.0 的實作問題并且有可能在以后的版本修復,

PluginController.GetInstance

private IPlugin GetInstance()
{
    var instance = _instance;
    if (instance != null && !_changed)
        return instance;

    lock (_reloadLock)
    {
        instance = _instance;
        if (instance != null && !_changed)
            return instance;

        UnloadPlugin();
        _context = new AssemblyLoadContext(
            name: $"Plugin-{_pluginName}", isCollectible: true);

        var assembly = CompilePlugin();
        var pluginType = assembly.GetTypes()
            .First(t => typeof(IPlugin).IsAssignableFrom(t));
        instance = (IPlugin)Activator.CreateInstance(pluginType);

        _instance = instance;
        _changed = false;
    }

    return instance;
}?

這個方法是獲取最新插件實體的方法,如果插件實體已創建并且檔案沒有改變,則回傳已有的實體,否則卸載原有的插件、重新編譯插件、加載并生成實體,注意 AssemblyLoadContext 型別在 netstandard (包括 2.1) 中是 abstract 型別,不能直接創建,只有 netcoreapp3.0 才可以直接創建 (目前也只有 .NET Core 3.0 支持這項機制),如果需要支持可回收則創建時需要設定 isCollectible 引數為 true,因為支持可回識訓讓 GC 掃描物件時做一些額外的作業所以默認不啟用,

PluginController.GetMessage

public string GetMessage()
{
    return GetInstance().GetMessage();
}

這個方法是代理方法,會獲取最新的插件實體并轉發呼叫引數與結果,如果 IPlugin 有其他方法也可以像這個方法一樣寫,

PluginController.Dispose

public void Dispose()
{
    UnloadPlugin();
    _watcher?.Dispose();
    _watcher = null;
}

這個方法支持主動釋放 PluginController,會卸載已加載的插件并且停止監聽插件檔案,因為 PluginController 沒有直接管理非托管資源,并且 AssemblyLoadContext 的解構式 會觸發卸載,所以 PluginController 不需要提供解構式,

主函式代碼

static void Main(string[] args)
{
    using (var controller = new PluginController("MyPlugin", "../guest"))
    {
        bool keepRunning = true;
        Console.CancelKeyPress += (sender, e) => {
            e.Cancel = true;
            keepRunning = false;
        };
        while (keepRunning)
        {
            try
            {
                Console.WriteLine(controller.GetMessage());
            }
            catch (Exception ex)
            {
                Console.WriteLine($"{ex.GetType()}: {ex.Message}");
            }
            Thread.Sleep(1000);
        }
    }
}

主函式創建了 PluginController 實體并指定了上述的 guest 檔案夾為插件檔案夾,之后每隔 1 秒呼叫一次 GetMessage 方法,這樣插件代碼改變的時候我們可以從控制臺輸出中觀察的到,如果插件代碼包含語法錯誤則呼叫時會拋出例外,程式會繼續運行并在下一次呼叫時重新嘗試編譯與加載,

寫在最后

本文的介紹就到此為止了,在本文中我們看到了一個最簡單的 .NET Core 3.0 插件熱加載實作,這個實作仍然有很多需要改進的地方,例如如何管理多個插件、怎么在重啟宿主程式后避免重新編譯所有插件,編譯的插件代碼如何除錯等,如果你有興趣可以解決它們,做一個插件系統嵌入到你的專案中,或者寫一個新的框架,

關于 ZKWeb,3.0 會使用了本文介紹的機制實作插件熱加載,但因為我目前已經退出 IT 行業,所有開發都是業余空閑時間做的,所以基本上不會有很大的更新,ZKWeb 更多的會作為一個框架的實作參考,此外,我正在使用 C++ 撰寫 HTTP 框架 cpv-framework,主要著重性能 (吞吐量是 .NET Core 3.0 的兩倍以上,與 actix-web 持平),目前還沒有正式發布,

關于書籍,出版社約定 11 月但目前還沒有讓我看修改過的稿件 (盡管我問的時候會回答),所以很大可能會繼續延期,抱歉讓期待出版的同學們久等了,書籍目前還是基于 .NET Core 2.2 而不是 .NET Core 3.0,

轉載請註明出處,本文鏈接:https://www.uj5u.com/net/108771.html

標籤:.NET Core

上一篇:.NET開發者必須學習.NET Core

下一篇:.Net Core2.2 在IIS發布

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • WebAPI簡介

    Web體系結構: 有三個核心:資源(resource),URL(統一資源識別符號)和表示 他們的關系是這樣的:一個資源由一個URL進行標識,HTTP客戶端使用URL定位資源,表示是從資源回傳資料,媒體型別是資源回傳的資料格式。 接下來我們說下HTTP. HTTP協議的系統是一種無狀態的方式,使用請求/ ......

    uj5u.com 2020-09-09 22:07:47 more
  • asp.net core 3.1 入口:Program.cs中的Main函式

    本文分析Program.cs 中Main()函式中代碼的運行順序分析asp.net core程式的啟動,重點不是剖析原始碼,而是理清程式開始時執行的順序。到呼叫了哪些實體,哪些法方。asp.net core 3.1 的程式入口在專案Program.cs檔案里,如下。ususing System; us ......

    uj5u.com 2020-09-09 22:07:49 more
  • asp.net網站作為websocket服務端的應用該如何寫

    最近被websocket的一個問題困擾了很久,有一個需求是在web網站中搭建websocket服務。客戶端通過網頁與服務器建立連接,然后服務器根據ip給客戶端網頁發送資訊。 其實,這個需求并不難,只是剛開始對websocket的內容不太了解。上網搜索了一下,有通過asp.net core 實作的、有 ......

    uj5u.com 2020-09-09 22:08:02 more
  • ASP.NET 開源匯入匯出庫Magicodes.IE Docker中使用

    Magicodes.IE在Docker中使用 更新歷史 2019.02.13 【Nuget】版本更新到2.0.2 【匯入】修復單列匯入的Bug,單元測驗“OneColumnImporter_Test”。問題見(https://github.com/dotnetcore/Magicodes.IE/is ......

    uj5u.com 2020-09-09 22:08:05 more
  • 在webform中使用ajax

    如果你用過Asp.net webform, 說明你也算是.NET 開發的老兵了。WEBform應該是2011 2013左右,當時還用visual studio 2005、 visual studio 2008。后來基本都用的是MVC。 如果是新開發的專案,估計沒人會用webform技術。但是有些舊版 ......

    uj5u.com 2020-09-09 22:08:50 more
  • iis添加asp.net網站,訪問提示:由于擴展配置問題而無法提供您請求的

    今天在iis服務器配置asp.net網站,遇到一個問題,記錄一下: 問題:由于擴展配置問題而無法提供您請求的頁面。如果該頁面是腳本,請添加處理程式。如果應下載檔案,請添加 MIME 映射。 WindowServer2012服務器,添加角色安裝完.netframework和iis之后,運行aspx頁面 ......

    uj5u.com 2020-09-09 22:10:00 more
  • WebAPI-處理架構

    帶著問題去思考,大家好! 問題1:HTTP請求和回傳相應的HTTP回應資訊之間發生了什么? 1:首先是最底層,托管層,位于WebAPI和底層HTTP堆疊之間 2:其次是 訊息處理程式管道層,這里比如日志和快取。OWIN的參考是將訊息處理程式管道的一些功能下移到堆疊下端的OWIN中間件了。 3:控制器處理 ......

    uj5u.com 2020-09-09 22:11:13 more
  • 微信門戶開發框架-使用指導說明書

    微信門戶應用管理系統,采用基于 MVC + Bootstrap + Ajax + Enterprise Library的技術路線,界面層采用Boostrap + Metronic組合的前端框架,資料訪問層支持Oracle、SQLServer、MySQL、PostgreSQL等資料庫。框架以MVC5,... ......

    uj5u.com 2020-09-09 22:15:18 more
  • WebAPI-HTTP編程模型

    帶著問題去思考,大家好!它是什么?它包含什么?它能干什么? 訊息 HTTP編程模型的核心就是訊息抽象,表示為:HttPRequestMessage,HttpResponseMessage.用于客戶端和服務端之間交換請求和回應訊息。 HttpMethod類包含了一組靜態屬性: private stat ......

    uj5u.com 2020-09-09 22:15:23 more
  • 部署WebApi隨筆

    一、跨域 NuGet參考Microsoft.AspNet.WebApi.Cors WebApiConfig.cs中配置: // Web API 配置和服務 config.EnableCors(new EnableCorsAttribute("*", "*", "*")); 二、清除默認回傳XML格式 ......

    uj5u.com 2020-09-09 22:15:48 more
最新发布
  • C#多執行緒學習(二) 如何操縱一個執行緒

    <a href="https://www.cnblogs.com/x-zhi/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/2943582/20220801082530.png" alt="" /></...

    uj5u.com 2023-04-19 09:17:20 more
  • C#多執行緒學習(二) 如何操縱一個執行緒

    C#多執行緒學習(二) 如何操縱一個執行緒 執行緒學習第一篇:C#多執行緒學習(一) 多執行緒的相關概念 下面我們就動手來創建一個執行緒,使用Thread類創建執行緒時,只需提供執行緒入口即可。(執行緒入口使程式知道該讓這個執行緒干什么事) 在C#中,執行緒入口是通過ThreadStart代理(delegate)來提供的 ......

    uj5u.com 2023-04-19 09:16:49 more
  • 記一次 .NET某醫療器械清洗系統 卡死分析

    <a href="https://www.cnblogs.com/huangxincheng/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/214741/20200614104537.png" alt="" /&g...

    uj5u.com 2023-04-18 08:39:04 more
  • 記一次 .NET某醫療器械清洗系統 卡死分析

    一:背景 1. 講故事 前段時間協助訓練營里的一位朋友分析了一個程式卡死的問題,回過頭來看這個案例比較經典,這篇稍微整理一下供后來者少踩坑吧。 二:WinDbg 分析 1. 為什么會卡死 因為是表單程式,理所當然就是看主執行緒此時正在做什么? 可以用 ~0s ; k 看一下便知。 0:000> k # ......

    uj5u.com 2023-04-18 08:33:10 more
  • SignalR, No Connection with that ID,IIS

    <a href="https://www.cnblogs.com/smartstar/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/u36196.jpg" alt="" /></a>...

    uj5u.com 2023-03-30 17:21:52 more
  • 一次對pool的誤用導致的.net頻繁gc的診斷分析

    <a href="https://www.cnblogs.com/dotnet-diagnostic/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/3115652/20230225090434.png" alt=""...

    uj5u.com 2023-03-28 10:15:33 more
  • 一次對pool的誤用導致的.net頻繁gc的診斷分析

    <a href="https://www.cnblogs.com/dotnet-diagnostic/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/3115652/20230225090434.png" alt=""...

    uj5u.com 2023-03-28 10:13:31 more
  • C#遍歷指定檔案夾中所有檔案的3種方法

    <a href="https://www.cnblogs.com/xbhp/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/957602/20230310105611.png" alt="" /></a&...

    uj5u.com 2023-03-27 14:46:55 more
  • C#/VB.NET:如何將PDF轉為PDF/A

    <a href="https://www.cnblogs.com/Carina-baby/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/2859233/20220427162558.png" alt="" />...

    uj5u.com 2023-03-27 14:46:35 more
  • 武裝你的WEBAPI-OData聚合查詢

    <a href="https://www.cnblogs.com/podolski/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/616093/20140323000327.png" alt="" /><...

    uj5u.com 2023-03-27 14:46:16 more