翻譯|使用教程|編輯:龔雪|2025-04-23 14:04:41.890|閱讀 115 次
概述:本文將為大家介紹DevExpress XAF如何將.NET Aspire集成到Blazor項(xiàng)目中,歡迎下載最新版組件體驗(yàn)!
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
DevExpress XAF是一款強(qiáng)大的現(xiàn)代應(yīng)用程序框架,允許同時(shí)開(kāi)發(fā)ASP.NET和WinForms。DevExpress XAF采用模塊化設(shè)計(jì),開(kāi)發(fā)人員可以選擇內(nèi)建模塊,也可以自行創(chuàng)建,從而以更快的速度和比開(kāi)發(fā)人員當(dāng)前更強(qiáng)有力的方式創(chuàng)建應(yīng)用程序。
.NET Aspire是一組工具、模板和包,用于構(gòu)建可觀察的、可生產(chǎn)的應(yīng)用程序。DevExpress XAF團(tuán)隊(duì)花費(fèi)了一些時(shí)間考慮Aspire的功能,試圖找到最好的集成點(diǎn),讓XAF開(kāi)發(fā)人員能夠利用Aspire開(kāi)箱即用的業(yè)務(wù)流程特性。該團(tuán)隊(duì)認(rèn)為Aspire是一個(gè)潛在的“一鍵解決方案”,是XAF解決方案向?qū)е幸恍┠0宓母F(xiàn)代的替代品,它可以實(shí)現(xiàn)以下一些目標(biāo):
團(tuán)隊(duì)希望能夠?yàn)榭蛻艄?jié)省大量的時(shí)間,無(wú)論他們是否使用XAF!目前,由于Microsoft仍在快速開(kāi)發(fā)Aspire,這在很大程度上是一個(gè)移動(dòng)的目標(biāo)。與此同時(shí),一些功能已經(jīng)被廣泛使用,可以認(rèn)為是穩(wěn)定的,所以現(xiàn)在我們決定發(fā)布一些細(xì)節(jié),來(lái)幫助您在自己的XAF項(xiàng)目中使用Aspire工具。
在上文中(),我們主要為大家介紹了如何將將Aspire添加到XAF Blazor項(xiàng)目,本文將繼續(xù)介紹第三方軟件包的更新等。
DevExpress技術(shù)交流群11:749942875 歡迎一起進(jìn)群討論
正如之前內(nèi)容中所解釋的,由于Aspire是尖端技術(shù),堅(jiān)持使用最新版本是有意義的。為了保持一致性,官方建議使用所有內(nèi)容的最新版本,建議在進(jìn)行其他更改之前先這樣做。
您可以為每個(gè)項(xiàng)目打開(kāi)NuGet包管理器并以這種方式安裝更新,但官方更建議使用Package Manager Console窗口(您可以從Visual Studio的工具菜單中打開(kāi)它),Get-Package -updates命令顯示所有可用的更新:
PM> Get-Package -updates Id Versions Description ProjectName -- -------- ----------- ----------- Microsoft.EntityFrameworkCore.Sq... {9.0.3} Microsoft SQL Server database provider for... XafAspireDemo.Module Microsoft.Data.SqlClient {6.0.1} The current data provider for SQL Server a... XafAspireDemo.Module System.IdentityModel.Tokens.Jwt {8.6.1} Includes types that provide support for cr... XafAspireDemo.Module Microsoft.EntityFrameworkCore.In... {9.0.3} In-memory database provider for Entity Fra... XafAspireDemo.Module Microsoft.EntityFrameworkCore.Pr... {9.0.3} Lazy loading proxies for Entity Framework ... XafAspireDemo.Module No package updates are available from the current package source for project 'XafAspireDemo.Blazor.Server'. Aspire.Hosting.AppHost {9.1.0} Core library and MSBuild logic for .NET As... XafAspireDemo.AppHost OpenTelemetry.Instrumentation.As... {1.11.1} ASP.NET Core instrumentation for OpenTelem... XafAspireDemo.ServiceDefaults OpenTelemetry.Exporter.OpenTelem... {1.11.2} OpenTelemetry protocol exporter for OpenTe... XafAspireDemo.ServiceDefaults OpenTelemetry.Instrumentation.Ru... {1.11.1} .NET runtime instrumentation for OpenTelem... XafAspireDemo.ServiceDefaults Microsoft.Extensions.Http.Resili... {9.3.0} Resilience mechanisms for HttpClient. XafAspireDemo.ServiceDefaults Microsoft.Extensions.ServiceDisc... {9.1.0} Provides extensions to HttpClient that ena... XafAspireDemo.ServiceDefaults OpenTelemetry.Extensions.Hosting {1.11.2} Contains extensions to start OpenTelemetry... XafAspireDemo.ServiceDefaults OpenTelemetry.Instrumentation.Http {1.11.1} Http instrumentation for OpenTelemetry .NET. XafAspireDemo.ServiceDefaults
現(xiàn)在運(yùn)行Update-Package命令,等待所有的包都更新到最新版本。
在此之后還有一個(gè)小問(wèn)題:AppHost項(xiàng)目文件中的Sdk引用仍然指向9.0.0版本:
<Sdk Name="Aspire.AppHost.Sdk" Version="9.0.0" />
將此手動(dòng)修復(fù)到9.1.0版本,因?yàn)檫@是最新的可用版本。
<Sdk Name="Aspire.AppHost.Sdk" Version="9.1.0" />
注意:您可以使用新的.NET Upgrade Assistant Visual Studio Extension來(lái)自動(dòng)更新依賴項(xiàng),包括SDK版本。
首先,可以簡(jiǎn)單地從Module項(xiàng)目中刪除額外的Extensions.cs副本,這是不必要的。
在ServiceDefaults項(xiàng)目中的Extensions.cs中實(shí)現(xiàn)的類Extensions可以用下面的代碼完全替換,注意,這個(gè)實(shí)現(xiàn)包含與原始文件使用的完全相同的默認(rèn)代碼,不同之處在于如何應(yīng)用代碼。標(biāo)準(zhǔn)文件在大多數(shù)情況下使用IHostApplicationBuilder接口,而替換文件直接使用IServiceCollection,這使得與Blazor應(yīng)用程序的啟動(dòng)代碼交互變得容易得多。
public static class Extensions { public static void AddAspireServiceDefaults(this IServiceCollection services) { services.AddMetrics(); services.AddOpenTelemetry(); services.AddHealthChecks().AddCheck("self", () => HealthCheckResult.Healthy(), ["live"]); services.AddServiceDiscovery(); services.ConfigureHttpClientDefaults(http => { http.AddStandardResilienceHandler(); http.AddServiceDiscovery(); }); } public static IServiceCollection ConfigureOpenTelemetry( this IServiceCollection services, IConfiguration configuration, IWebHostEnvironment environment ) { services .AddLogging(logging => { logging.AddOpenTelemetry(options => { options.IncludeFormattedMessage = true; options.IncludeScopes = true; }); }) .AddOpenTelemetry() .WithMetrics(metrics => { metrics .AddAspNetCoreInstrumentation() .AddHttpClientInstrumentation() .AddRuntimeInstrumentation(); }) .WithTracing(tracing => { tracing .AddSource(environment.ApplicationName) .AddSource("Microsoft.AspNetCore.SignalR.Server") .AddAspNetCoreInstrumentation() // Uncomment the following line to enable gRPC instrumentation //.AddGrpcClientInstrumentation() .AddHttpClientInstrumentation(); }); AddOpenTelemetryExporters(services, configuration); return services; } private static void AddOpenTelemetryExporters( IServiceCollection services, IConfiguration configuration ) { var useOtlpExporter = !string.IsNullOrWhiteSpace( configuration["OTEL_EXPORTER_OTLP_ENDPOINT"] ); if (useOtlpExporter) { services.AddOpenTelemetry().UseOtlpExporter(); } } public static void MapDefaultAspireDevEndpoints(this IEndpointRouteBuilder endpoints) { endpoints.MapHealthChecks("/health"); endpoints.MapHealthChecks( "/alive", new HealthCheckOptions { Predicate = r => r.Tags.Contains("live") } ); } }
與向?qū)?chuàng)建的原始文件一樣,您應(yīng)該將此視為一個(gè)起點(diǎn),并根據(jù)自己的應(yīng)用程序系統(tǒng)需要調(diào)整擴(kuò)展方法的代碼。與標(biāo)準(zhǔn)代碼相比,所應(yīng)用的一個(gè)變化是增加了額外的跟蹤源Microsoft.AspNetCore.SignalR.Server,這使得來(lái)自SignalR的遙測(cè)信息在Aspire儀表板中可用,這是需要根據(jù)您自己的需求進(jìn)行更改的示例。
所有這些更改都應(yīng)用于Blazor.Server項(xiàng)目中的Startup.cs文件,我們首先擴(kuò)展構(gòu)造函數(shù),除了配置之外,還包括對(duì)環(huán)境對(duì)象的引用:
public Startup(IConfiguration configuration, IWebHostEnvironment webHostEnvironment) { Configuration = configuration; WebHostEnvironment = webHostEnvironment; } public IConfiguration Configuration { get; } public IWebHostEnvironment WebHostEnvironment { get; } 就在ConfigureServices方法的開(kāi)頭,添加了對(duì)新擴(kuò)展方法的調(diào)用,這兩個(gè)新行在這里有一個(gè)-->前綴: public void ConfigureServices(IServiceCollection services) { --> services.AddAspireServiceDefaults(); --> services.ConfigureOpenTelemetry(Configuration, WebHostEnvironment); services.AddSingleton( typeof(Microsoft.AspNetCore.SignalR.HubConnectionHandler<>), typeof(ProxyHubConnectionHandler<>)); ...
在這一點(diǎn)上,我們還應(yīng)用了一個(gè)可選的擴(kuò)展,在項(xiàng)目中添加了NuGet包OpenTelemetry.Instrumentation.EntityFrameworkCore(這目前是一個(gè)預(yù)發(fā)布版本?。?,然后用這些額外的代碼行啟用EF Core的instrumentation:
public void ConfigureServices(IServiceCollection services) { services.AddAspireServiceDefaults(); services.ConfigureOpenTelemetry(Configuration, WebHostEnvironment); --> services.ConfigureOpenTelemetryTracerProvider(builder => --> { --> builder.AddEntityFrameworkCoreInstrumentation(); --> }); services.AddSingleton( typeof(Microsoft.AspNetCore.SignalR.HubConnectionHandler<>), typeof(ProxyHubConnectionHandler<>)); ...
最后,對(duì)Configure方法做了一個(gè)更改。在它的最后添加了一個(gè)塊,它使用Aspire向?qū)?chuàng)建的標(biāo)準(zhǔn)擴(kuò)展方法有條件地添加端點(diǎn)映射:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { ... app.UseEndpoints(endpoints => { endpoints.MapXafEndpoints(); endpoints.MapBlazorHub(); endpoints.MapFallbackToPage("/_Host"); endpoints.MapControllers(); }); --> if (env.IsDevelopment()) --> { --> app.UseEndpoints(endpoints => --> { --> endpoints.MapDefaultAspireDevEndpoints(); --> }); --> } }
此時(shí),可以運(yùn)行應(yīng)用程序了,Aspire Dashboard在瀏覽器中正確地出現(xiàn)了。然而Blazor項(xiàng)目的資源沒(méi)有顯示任何端點(diǎn),這使得它實(shí)際上無(wú)法訪問(wèn)。
AppHost項(xiàng)目中的Program.cs中的代碼現(xiàn)在是應(yīng)用程序系統(tǒng)的入口點(diǎn),這是Aspire配置其編排的地方,即使現(xiàn)在只有一個(gè)項(xiàng)目需要配置。根據(jù)自己的需求修改這段代碼并不罕見(jiàn),在這種情況下,發(fā)現(xiàn)需要一個(gè)顯式調(diào)用來(lái)幫助Aspire理解Blazor應(yīng)用程序有一個(gè)HTTPS端點(diǎn):
var builder = DistributedApplication.CreateBuilder(args); builder .AddProject<Projects.XafAspireDemo_Blazor_Server>("xafaspiredemo-blazor-server") --> .WithHttpsEndpoint(); builder.Build().Run();
現(xiàn)在,演示應(yīng)用程序有了一個(gè)端點(diǎn)條目,通過(guò)它可以像以前一樣訪問(wèn)應(yīng)用程序。
在Blazor前端運(yùn)行一些操作后,Aspire Dashboard也開(kāi)始在Traces和Metrics面板中顯示條目,您可以在這里深入研究并詳細(xì)分析應(yīng)用程序操作。
更多產(chǎn)品資訊及授權(quán),歡迎來(lái)電咨詢:023-68661681
慧都是?家?業(yè)數(shù)字化解決?案公司,專注于軟件、?油與?業(yè)領(lǐng)域,以深?的業(yè)務(wù)理解和?業(yè)經(jīng)驗(yàn),幫助企業(yè)實(shí)現(xiàn)智能化轉(zhuǎn)型與持續(xù)競(jìng)爭(zhēng)優(yōu)勢(shì)。
慧都科技是DevExpress的中國(guó)區(qū)的合作伙伴,DevExpress作為用戶界面領(lǐng)域的優(yōu)秀產(chǎn)品,幫助企業(yè)高效構(gòu)建權(quán)限管理、數(shù)據(jù)可視化(如網(wǎng)格/圖表/儀表盤)、跨平臺(tái)系統(tǒng)(WinForms/ASP.NET/.NET MAUI)及行業(yè)定制解決方案,加速開(kāi)發(fā)并強(qiáng)化交互體驗(yàn)。
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:慧都網(wǎng)