1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- using System;
- using System.Collections.Generic;
- using System.Configuration;
- using System.Linq;
- using System.Text.Json;
- using System.Threading.Tasks;
- using Grpc.Extension.AspNetCore;
- using Microsoft.AspNetCore.Builder;
- using Microsoft.AspNetCore.Hosting;
- using Microsoft.AspNetCore.Http;
- using Microsoft.Extensions.Configuration;
- using Microsoft.Extensions.DependencyInjection;
- using Microsoft.Extensions.Hosting;
- using TEAMModelOS.GRPC.Services.Syllabus;
- using TEAMModelOS.SDK.Context.Configuration;
- using TEAMModelOS.SDK.Module.AzureBlob.Configuration;
- using TEAMModelOS.SDK.Module.AzureCosmosDB.Configuration;
- using TEAMModelOS.SDK.Module.AzureCosmosDBV3;
- using TEAMModelOS.SDK.Module.AzureTable.Implements;
- using TEAMModelOS.SDK.Module.AzureTable.Interfaces;
- namespace TEAMModelOS.Grpc
- {
- public class Startup
- {
- private IConfiguration _conf;
- public Startup(IConfiguration conf, IWebHostEnvironment env)
- {
- _conf = conf;
- BaseConfigModel.SetBaseConfig(_conf, env.ContentRootPath, env.WebRootPath);
- }
- // This method gets called by the runtime. Use this method to add services to the container.
- // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
- public void ConfigureServices(IServiceCollection services)
- {
- services.AddGrpc();
- //添加Grpc扩展
- services.AddGrpcExtensions(_conf);
- // Table配置
- services.AddScoped<IAzureTableDBRepository, AzureTableDBRepository>();
- //使用Blob配置
- services.AddAzureBlobStorage().AddConnection(_conf.GetSection("Azure:Blob").Get<AzureBlobOptions>());
- //使用CosmosDB
- services.AddAzureCosmosDBV3().AddCosmosDBV3Connection(_conf.GetSection("Azure:CosmosDB").Get<AzureCosmosDBOptions>())
- .AddCosmosSerializer(new SystemTextJsonCosmosSerializer(new JsonSerializerOptions() { IgnoreNullValues = true }));
-
- //注入CSRedis
- var csredis = new CSRedis.CSRedisClient(_conf.GetSection("Azure:Redis:ConnectionString").Get<string>());
- RedisHelper.Initialization(csredis);
- }
- // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
- public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IAzureCosmosDBV3Repository cosmosDBV3Repository)
- {
- if (env.IsDevelopment())
- {
- app.UseDeveloperExceptionPage();
- }
- cosmosDBV3Repository.InitializeDatabase();
- app.UseRouting();
- app.UseEndpoints(endpoints =>
- {
- endpoints.MapGrpcService<GreeterService>();
- endpoints.MapGet("/", async context =>
- {
- 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");
- });
- });
- //CodeFirst的Grpc(会自动扫描TStartup所在程序集下的IGrpcSerivce)
- app.UseGrpcExtensions<KnowledgeService>(options =>
- {
- //CodeFirst配制
- options.GlobalPackage = "math";
- options.ProtoNameSpace = "MathGrpc";
- })
- //CodeFirst生成proto
- .UseProtoGenerate("protos", false);
- }
- }
- }
|