publicvoidConfigureServices(IServiceCollection services) {services.AddControllersWithViews();//注入数据库services.AddDbContext<PubDBContext>(options &#61;> options.UseSqlServer(Configuration["ConnectionStrings:AngelDBContext"]));//注册cap事件services.AddCap(x &#61;>{x.UseEntityFramework<PubDBContext>();x.UseRabbitMQ(rb &#61;>{rb.HostName &#61;"localhost";rb.UserName &#61;"admin";rb.Password &#61;"admin";rb.Port &#61;5672;rb.VirtualHost &#61;"Angel2022";rb.ExchangeName &#61;"AngelCapExchange";rb.QueueMessageExpires &#61;24*3600*10;});});}publicvoidConfigure(IApplicationBuilder app,IWebHostEnvironment env,PubDBContext efCoreApp) {if(env.IsDevelopment()){app.UseDeveloperExceptionPage();}else{app.UseExceptionHandler("/Home/Error");// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.app.UseHsts();}app.UseHttpsRedirection();app.UseStaticFiles();app.UseRouting();efCoreApp.Database.EnsureCreated();//数据库不存在自动创建app.UseAuthorization();app.UseEndpoints(endpoints &#61;>{endpoints.MapControllerRoute(name:"default",pattern:"{controller&#61;Home}/{action&#61;Index}/{id?}");}); }
创建发布接口
[Route("api/[controller]/[action]")] [ApiController] publicclassPubController:ControllerBase {privateICapPublisher _capBus;publicPubController(ICapPublisher capBus){this._capBus &#61; capBus;}[HttpGet]publicIActionResultSendMsg(){for(var i &#61;0; i <1000; i&#43;&#43;){DateTime date &#61; DateTime.Now;_capBus.Publish("angel",$"******发布者&#xff0c;发布的内容为&#xff1a; {date},i:{i}*****");}returnContent("发送成功");} }
publicvoidConfigureServices(IServiceCollection services) {services.AddControllersWithViews();//注入数据库services.AddDbContext<SubDBContext>(options &#61;> options.UseSqlServer(Configuration["ConnectionStrings:AngelDBContext"]));//注册cap事件services.AddCap(x &#61;>{x.UseEntityFramework<SubDBContext>();x.UseRabbitMQ(rb &#61;>{rb.HostName &#61;"localhost";rb.UserName &#61;"admin";rb.Password &#61;"admin";rb.Port &#61;5672;rb.VirtualHost &#61;"Angel2022";rb.ExchangeName &#61;"AngelCapExchange";rb.QueueMessageExpires &#61;24*3600*10;//队列中消息自动删除时间(默认10天)});});}publicvoidConfigure(IApplicationBuilder app,IWebHostEnvironment env,SubDBContext efCoreApp) {if(env.IsDevelopment()){app.UseDeveloperExceptionPage();}else{app.UseExceptionHandler("/Home/Error");// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.app.UseHsts();}app.UseHttpsRedirection();app.UseStaticFiles();app.UseRouting();efCoreApp.Database.EnsureCreated();//数据库不存在自动创建app.UseAuthorization();app.UseEndpoints(endpoints &#61;>{endpoints.MapControllerRoute(name:"default",pattern:"{controller&#61;Home}/{action&#61;Index}/{id?}");}); }