Startup.cs 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Configuration;
  4. using System.Linq;
  5. using System.Text.Json;
  6. using System.Threading.Tasks;
  7. using Grpc.Extension.AspNetCore;
  8. using Microsoft.AspNetCore.Builder;
  9. using Microsoft.AspNetCore.Hosting;
  10. using Microsoft.AspNetCore.Http;
  11. using Microsoft.Extensions.Configuration;
  12. using Microsoft.Extensions.DependencyInjection;
  13. using Microsoft.Extensions.Hosting;
  14. using TEAMModelOS.GRPC.Services.Syllabus;
  15. using TEAMModelOS.SDK.Context.Configuration;
  16. using TEAMModelOS.SDK.Module.AzureBlob.Configuration;
  17. using TEAMModelOS.SDK.Module.AzureCosmosDB.Configuration;
  18. using TEAMModelOS.SDK.Module.AzureCosmosDBV3;
  19. using TEAMModelOS.SDK.Module.AzureTable.Implements;
  20. using TEAMModelOS.SDK.Module.AzureTable.Interfaces;
  21. namespace TEAMModelOS.Grpc
  22. {
  23. public class Startup
  24. {
  25. private IConfiguration _conf;
  26. public Startup(IConfiguration conf, IWebHostEnvironment env)
  27. {
  28. _conf = conf;
  29. BaseConfigModel.SetBaseConfig(_conf, env.ContentRootPath, env.WebRootPath);
  30. }
  31. // This method gets called by the runtime. Use this method to add services to the container.
  32. // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
  33. public void ConfigureServices(IServiceCollection services)
  34. {
  35. services.AddGrpc();
  36. //添加Grpc扩展
  37. services.AddGrpcExtensions(_conf);
  38. // Table配置
  39. services.AddScoped<IAzureTableDBRepository, AzureTableDBRepository>();
  40. //使用Blob配置
  41. services.AddAzureBlobStorage().AddConnection(_conf.GetSection("Azure:Blob").Get<AzureBlobOptions>());
  42. //使用CosmosDB
  43. services.AddAzureCosmosDBV3().AddCosmosDBV3Connection(_conf.GetSection("Azure:CosmosDB").Get<AzureCosmosDBOptions>())
  44. .AddCosmosSerializer(new SystemTextJsonCosmosSerializer(new JsonSerializerOptions() { IgnoreNullValues = true }));
  45. //注入CSRedis
  46. var csredis = new CSRedis.CSRedisClient(_conf.GetSection("Azure:Redis:ConnectionString").Get<string>());
  47. RedisHelper.Initialization(csredis);
  48. }
  49. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  50. public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IAzureCosmosDBV3Repository cosmosDBV3Repository)
  51. {
  52. if (env.IsDevelopment())
  53. {
  54. app.UseDeveloperExceptionPage();
  55. }
  56. cosmosDBV3Repository.InitializeDatabase();
  57. app.UseRouting();
  58. app.UseEndpoints(endpoints =>
  59. {
  60. endpoints.MapGrpcService<GreeterService>();
  61. endpoints.MapGet("/", async context =>
  62. {
  63. await context.Response.WriteAsync("Communication with gRPC endpoints must be made through a gRPC client. To learn how to create a client, visit: https://go.microsoft.com/fwlink/?linkid=2086909");
  64. });
  65. });
  66. //CodeFirst的Grpc(会自动扫描TStartup所在程序集下的IGrpcSerivce)
  67. app.UseGrpcExtensions<KnowledgeService>(options =>
  68. {
  69. //CodeFirst配制
  70. options.GlobalPackage = "math";
  71. options.ProtoNameSpace = "MathGrpc";
  72. })
  73. //CodeFirst生成proto
  74. .UseProtoGenerate("protos", false);
  75. }
  76. }
  77. }