经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » 编程经验 » 查看文章
微服务新体验之Aspire初体验
来源:cnblogs  作者:shiningrise  时间:2024/5/31 9:30:05  对本文有异议

安装aspire

查看vs版本

我这的版本是17.9.7,不支持aspire,所以需要升级

更新VS

点击 帮助->检查更新

点击更新

静等安装升级

创建aspire项目

项目创建成功,如下图

运行Aspire项目

在AspireApp1.AppHost的launchSettings.json文件中加 "ASPIRE_ALLOW_UNSECURED_TRANSPORT": "true"

  1. {
  2. "$schema": "https://json.schemastore.org/launchsettings.json",
  3. "profiles": {
  4. "http": {
  5. "commandName": "Project",
  6. "dotnetRunMessages": true,
  7. "launchBrowser": true,
  8. "applicationUrl": "http://localhost:15177",
  9. "environmentVariables": {
  10. "ASPNETCORE_ENVIRONMENT": "Development",
  11. "DOTNET_ENVIRONMENT": "Development",
  12. "DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "http://localhost:19239",
  13. "DOTNET_RESOURCE_SERVICE_ENDPOINT_URL": "http://localhost:20241",
  14. "ASPIRE_ALLOW_UNSECURED_TRANSPORT": "true"
  15. }
  16. }
  17. }
  18. }

运行AspireApp1.AppHost项目

访问AspireApp1.Web项目

这里只要运行AspireApp1.AppHost项目就可以了

分析

看AspireApp1.AppHost项目的Program.cs文件,这里运行了两个项目

  1. var builder = DistributedApplication.CreateBuilder(args);
  2. var cache = builder.AddRedis("cache");
  3. var apiService = builder.AddProject<Projects.AspireApp1_ApiService>("apiservice"); //这里是后端项目
  4. builder.AddProject<Projects.AspireApp1_Web>("webfrontend")//这里是前端项目
  5. .WithExternalHttpEndpoints()
  6. .WithReference(cache)
  7. .WithReference(apiService);
  8. builder.Build().Run();

AspireApp1.Web这里注入了对apiservice的访问

  1. using AspireApp1.Web;
  2. using AspireApp1.Web.Components;
  3. var builder = WebApplication.CreateBuilder(args);
  4. // Add service defaults & Aspire components.
  5. builder.AddServiceDefaults();
  6. builder.AddRedisOutputCache("cache");
  7. // Add services to the container.
  8. builder.Services.AddRazorComponents()
  9. .AddInteractiveServerComponents();
  10. builder.Services.AddHttpClient<WeatherApiClient>(client => client.BaseAddress = new("http://apiservice")); //这里注入后端项目的API服务
  11. var app = builder.Build();
  12. if (!app.Environment.IsDevelopment())
  13. {
  14. app.UseExceptionHandler("/Error", createScopeForErrors: true);
  15. }
  16. app.UseStaticFiles();
  17. app.UseAntiforgery();
  18. app.UseOutputCache();
  19. app.MapRazorComponents<App>()
  20. .AddInteractiveServerRenderMode();
  21. app.MapDefaultEndpoints();
  22. app.Run();

看WeatherApiClient.cs文件

  1. namespace AspireApp1.Web;
  2. public class WeatherApiClient(HttpClient httpClient)
  3. {
  4. public async Task<WeatherForecast[]> GetWeatherAsync(int maxItems = 10, CancellationToken cancellationToken = default)
  5. {
  6. List<WeatherForecast>? forecasts = null;
  7. await foreach (var forecast in httpClient.GetFromJsonAsAsyncEnumerable<WeatherForecast>("/weatherforecast", cancellationToken))//调用后端API获取天气预报数据
  8. {
  9. if (forecasts?.Count >= maxItems)
  10. {
  11. break;
  12. }
  13. if (forecast is not null)
  14. {
  15. forecasts ??= [];
  16. forecasts.Add(forecast);
  17. }
  18. }
  19. return forecasts?.ToArray() ?? [];
  20. }
  21. }
  22. public record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary)
  23. {
  24. public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
  25. }

作者

吴晓阳 微信号:shiningrise

原文链接:https://www.cnblogs.com/shiningrise/p/18222147

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号