DoconutHost

จุดเริ่มต้นของการประกอบสำหรับ System.Web

DoconutHost (namespace Doconut) เป็นคลาสสแตติกที่เป็นเจ้าของบริการของ Doconut สำหรับกระบวนการทำงานทั้งหมด แอปพลิเคชัน ASP.NET Core จะสร้างบริการเหล่านั้นเข้าไปในคอนเทนเนอร์ของเฟรมเวิร์กเอง; ส่วน ASP.NET แบบคลาสสิกไม่มีคอนเทนเนอร์ ดังนั้น DoconutHost จะสร้างคอนเทนเนอร์ขึ้นมาและเก็บไว้ ตัวจัดการ DocImage.axd และโมดูลทรัพยากรจะดึงทุกอย่างที่ต้องการจากคอนเทนเนอร์นี้ รวมถึงโค้ดของคุณด้วย

สมาชิกวัตถุประสงค์
static void Initialize(Action<DoconutOptions>? configure = null, Action<IServiceCollection>? configureServices = null)สร้างบริการ — ครั้งเดียว
static IServiceProvider Services { get; }คอนเทนเนอร์ที่สร้างขึ้น
static bool IsInitialized { get; }true หลังจากที่ Initialize ทำงานแล้ว
static void Shutdown()หยุดงานเบื้องหลังและทำลายคอนเทนเนอร์

เริ่มต้น

เรียกจาก Application_Start ใน Global.asax.cs:

csharp
// Global.asax.cs, Application_Start - once per worker process. A second call is a no-op:
// the first call's options win.
Doconut.DoconutHost.Initialize(
    options =>
    {
        options.LicensePath = HostingEnvironment.MapPath("~/wwwroot/Doconut.Viewer.lic");
    },
    services =>
    {
        // Optional. Runs after Doconut's own registrations and before the container is built,
        // so it can add services or replace one Doconut registered.
        services.AddMemoryCache();
    });
  • configure รับค่า DoconutOptions: ใบอนุญาต, สวิตช์ความปลอดภัย, ปลั๊กอิน, วิดเจ็ต
  • configureServices เป็นตัวเลือก มันทำงานหลังจากที่ Doconut ลงทะเบียนบริการของตนเองและก่อนที่คอนเทนเนอร์จะถูกสร้าง, ดังนั้นจึงสามารถเพิ่มบริการ — AddDoconutDistributed สำหรับเว็บฟาร์มเป็นตัวเลือกทั่วไป — หรือแทนที่การลงทะเบียนที่ Doconut ทำไว้

การเรียกครั้งแรกชนะ Initialize มีลักษณะเป็นอิดิโมพอเทนท์: การเรียกครั้งที่สองจะคืนค่าทันที, และ ไม่มี delegate ใดทำงาน สิ่งนี้ทำให้ Application_Start ที่ซ้ำซ้อนไม่เป็นอันตราย, แต่ก็หมายความว่าการกำหนดค่าไม่สามารถเปลี่ยนแปลงได้โดยการเรียก Initialize อีกครั้ง — ให้รีไซเคิลแอปพลิเคชันพูลแทน

ข้อยกเว้นที่ถูกโยนที่นี่ — ปลั๊กอินที่ไม่มีใบอนุญาต, ตัวเลือกไม่ถูกต้อง, การกำหนดค่าเว็บฟาร์มที่ไม่สมบูรณ์ — เกิดขึ้นภายใน Application_Start. จากนั้น ASP.NET จะตอบทุกคำขอด้วย 500 ว่างเปล่า, และข้อยกเว้นจะถูกบันทึกไว้เฉพาะใน Windows event log (Application log, source ASP.NET).

บริการและ IsInitialized

System.Web ไม่มีการฉีดคอนสตรัคเตอร์สำหรับคอนโทรลเลอร์, หน้า, หรือแฮนด์เลอร์, ดังนั้นให้ดึงบริการของ Doconut ที่คุณใช้:

csharp
// System.Web has no constructor injection: resolve Doconut services where you use them.
var viewer = Doconut.DoconutHost.Services.GetRequiredService<Viewer>();

// Registered only when the Converter plugin is (see the plugin page).
var converter = Doconut.DoconutHost.Services.GetRequiredService<DocumentConverter>();

Services จะโยน InvalidOperationException (DoconutHost.Initialize must be called in Application_Start (Global.asax) before any Doconut request is served.) หากถูกอ่านก่อน Initialize. โค้ดที่อาจทำงานก่อนหน้านั้น — โมดูล, คอนสตรัคเตอร์สแตติก — สามารถตรวจสอบ IsInitialized ก่อนได้

สิ่งที่โค้ดของคุณมักจะดึง:

บริการอายุการใช้งานบทบาท
Viewerชั่วคราวเปิด, ปิด, คำอธิบาย, ตัวช่วยทรัพยากร — ดู Viewer
DocumentConverterชั่วคราวการแปลง — ลงทะเบียนเฉพาะกับปลั๊กอิน Converter
IDoconutLicenseServiceเดี่ยวใบอนุญาตที่โหลดและความสามารถของมัน — ดู Licensing
DoconutOptionsเดี่ยวตัวเลือกที่ส่งให้กับ Initialize
HealthCheckServiceเดี่ยวทำการตรวจสอบสุขภาพ "doconut" (ด้านล่าง)

ดึงจาก DoconutHost.Services เท่านั้น, อย่าใช้คอนเทนเนอร์ของคุณเอง การลงทะเบียน Doconut ลงในคอนเทนเนอร์อื่นจะสร้างชุดบริการที่สองแยกจากกันซึ่งแฮนด์เลอร์และโมดูลจะไม่เห็น: เอกสารที่เปิดผ่านมันจะไม่เป็นที่รู้จักสำหรับ DocImage.axd.

ปิดระบบ

csharp
// Global.asax.cs, Application_End - stops background work and disposes the container.
// Safe to call twice, and safe when Initialize never ran.
Doconut.DoconutHost.Shutdown();

Shutdown หยุดงานเบื้องหลังของ Doconut (ใช้ในโหมดกระจาย) และทำลายคอนเทนเนอร์. สามารถเรียกได้สองครั้งโดยปลอดภัย, และปลอดภัยเมื่อ Initialize ไม่เคยทำงาน — เช่นเมื่อแอปพลิเคชันพูลรีไซเคิลก่อนคำขอแรก.

การตรวจสอบสุขภาพ

Doconut ลงทะเบียนการตรวจสอบสุขภาพชื่อ "doconut" ที่รายงานสถานะใบอนุญาต. ASP.NET แบบคลาสสิกไม่มี endpoint การตรวจสอบสุขภาพของตนเอง, ดังนั้นให้เปิดเผยจากคอนโทรลเลอร์:

csharp
// GET /Health - Doconut registers a "doconut" health check that reports the license state:
// Healthy (valid), Degraded (trial or no license file), Unhealthy (expired).
public async Task<ActionResult> Index()
{
    var health = Doconut.DoconutHost.Services.GetRequiredService<HealthCheckService>();
    HealthReport report = await health.CheckHealthAsync();

    Response.StatusCode = report.Status == HealthStatus.Unhealthy ? 503 : 200;
    return Content(report.Status.ToString(), "text/plain");
}
สถานะความหมาย
Healthyใบอนุญาตที่ถูกต้องถูกโหลด
Degradedทดลอง, หรือไม่พบไฟล์ใบอนุญาต
Unhealthyใบอนุญาตหมดอายุ

เมธอดส่วนขยายของ ASP.NET Core

แพคเกจนี้ยังมี UseDoconut() และ UseDoconutResources(), ซึ่งเป็นส่วนขยายของ pipeline ของ ASP.NET Core, เนื่องจากมันแชร์โค้ดกับแพคเกจ ASP.NET Core. ในแพคเกจนี้ทั้งสองถูกทำเครื่องหมายว่า [Obsolete] และไม่ได้ใช้: แอปพลิเคชัน System.Web ไม่มี pipeline ของคำขอที่จะเพิ่มเข้าไป. การแทนที่ของพวกมันคือการลงทะเบียนสองรายการใน Web.config จาก Installation:

ASP.NET Core.NET Framework 4.7.2
builder.Services.AddDoconut(...)DoconutHost.Initialize(...) in Application_Start
app.UseDoconut()Doconut.DocImageHandler on DocImage.axd in Web.config
app.UseDoconutResources()Doconut.DoconutResourceModule in Web.config
Constructor injectionDoconutHost.Services.GetRequiredService<T>()
Host shutdownDoconutHost.Shutdown() in Application_End

หน้านี้เป็นประโยชน์หรือไม่?