tnblog
首页
视频
资源
登录

Dapr .NetCore 订阅与发布(下)

5671人阅读 2021/10/25 13:23 总访问:3662626 评论:0 收藏:0 手机
分类: .net后台框架

Dapr .NetCore 订阅与发布(下)

解决关于运行是报错问题


我们将为InvokeMethodServerAPI项目做一些修改。
首先添加一些相关依赖

  1. <ItemGroup>
  2. <PackageReference Include="CloudNative.CloudEvents" Version="1.3.80" />
  3. <PackageReference Include="CloudNative.CloudEvents.AspNetCore" Version="1.3.80" />
  4. <PackageReference Include="Dapr.AspNetCore" Version="1.4.0" />
  5. <PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
  6. </ItemGroup>


创建RequestController控制器,设置/dapr/subscribe订阅与发布的配置,这里我们添加一个NewSong方法并且将路径设置为/playlist/hello.

  1. [ApiController]
  2. [Route("[controller]")]
  3. public class RequestController : ControllerBase
  4. {
  5. private readonly ILogger<RequestController> _logger;
  6. public RequestController(ILogger<RequestController> logger)
  7. {
  8. _logger = logger;
  9. }
  10. [HttpGet("/dapr/subscribe")]
  11. public ActionResult<IEnumerable<string>> Get()
  12. {
  13. var topics = new [] {new { pubsubname="pubsub",topic = "songs", route = "playlist/hello"}};
  14. return new OkObjectResult(topics);
  15. }
  16. [HttpPost("/playlist/hello")]
  17. public async Task<IActionResult> NewSong(CloudEvent cloudEvent)
  18. {
  19. var song = JsonConvert.DeserializeObject<Song>(cloudEvent.Data.ToString());
  20. _logger.LogInformation($"New song request: {song.Artist} – {song.Name}");
  21. return new OkResult();
  22. }
  23. }
  24. public class Song
  25. {
  26. [JsonProperty("id")]
  27. public int Id { get; set; }
  28. [JsonProperty("artist")]
  29. public string Artist { get; set; }
  30. [JsonProperty("name")]
  31. public string Name { get; set; }
  32. }


在 Startup 类中,为了帮助进行模型绑定,让我们为 CloudEvents添加一个输入格式化程序

  1. public void ConfigureServices(IServiceCollection services)
  2. {
  3. services.AddControllers().AddDapr();
  4. services.AddMvc(opts =>
  5. {
  6. opts.InputFormatters.Insert(0, new CloudNative.CloudEvents.CloudEventJsonInputFormatter());
  7. });
  8. }
  9. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  10. public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  11. {
  12. if (env.IsDevelopment())
  13. {
  14. app.UseDeveloperExceptionPage();
  15. }
  16. app.UseRouting();
  17. app.UseAuthorization();
  18. app.UseEndpoints(endpoints =>
  19. {
  20. endpoints.MapControllers();
  21. });
  22. }


运行测试

  1. dapr run --app-id myserer --app-port 5002 --dapr-http-port 3500 -- dotnet run


dapr cri调用

  1. dapr publish --publish-app-id myserer --pubsub pubsub --topic songs --data '{"id":17,"name":"dapr_cli_mysongs","artist":"hhh"}'


bash调用

  1. curl -X POST http://localhost:3500/v1.0/publish/pubsub/songs -H "Content-Type: application/json" -d '{"id":17,"name":"bash_mysongs","artist":"hhh"}'


从其他客户端dapr请求过来的也能响应到。


欢迎加群讨论技术,1群:677373950(满了,可以加,但通过不了),2群:656732739

评价

net core 使用 EF Code First

下面这些内容很老了看这篇:https://www.tnblog.net/aojiancc2/article/details/5365 项目使用多层,把数据库访问...

.net mvc分部页,.net core分部页

.net分部页的三种方式第一种:@Html.Partial(&quot;_分部页&quot;)第二种:@{ Html.RenderPartial(&quot;分部页&quot;);}...

StackExchange.Redis操作redis(net core支持)

官方git开源地址https://github.com/StackExchange/StackExchange.Redis官方文档在docs里边都是官方的文档通过nuget命令下...

.net core 使用session

tip:net core 2.2后可以直接启用session了,不用在自己添加一次session依赖,本身就添加了使用nuget添加引用Microsoft.AspN...

通俗易懂,什么是.net?什么是.net Framework?什么是.net core?

朋友圈@蓝羽 看到一篇文章写的太详细太通俗了,搬过来细细看完,保证你对.NET有个新的认识理解原文地址:https://www.cnblo...

asp.net core2.0 依赖注入 AddTransient与AddScoped的区别

asp.net core主要提供了三种依赖注入的方式其中AddTransient与AddSingleton比较好区别AddTransient瞬时模式:每次都获取一...

.net core 使用 Kestrel

Kestrel介绍 Kestrel是一个基于libuv的跨平台web服务器 在.net core项目中就可以不一定要发布在iis下面了Kestrel体验可以使...

net core中使用cookie

net core中可以使用传统的cookie也可以使用加密的cookieNET CORE中使用传统cookie设置:HttpContext.Response.Cookies.Appe...

net core项目结构简单分析

一:wwwrootwwwroot用于存放网站的静态资源,例如css,js,图片与相关的前端插件等lib主要是第三方的插件,例如微软默认引用...

net core使用EF之DB First

一.新建一个.net core的MVC项目新建好项目后,不能像以前一样直接在新建项中添加ef了,需要用命令在添加ef的依赖二.使用Nug...

.net core使用requestresponse下载文件下载excel等

使用request获取内容net core中request没有直接的索引方法,需要点里边的Query,或者formstringbase64=Request.Form[&quot;f...

iframe自适应高度与配合net core使用

去掉iframe边框frameborder=&quot;0&quot;去掉滚动条scrolling=&quot;no&quot;iframe 自适应高度如果内容是固定的,那么就...

net core启动报错Unable to configure HTTPS endpoint. No server certificate was specified

这是因为net core2.1默认使用的https,如果使用Kestrel web服务器的话没有安装证书就会报这个错其实仔细看他的错误提示,其...

net core中使用url编码与解码操作

net core中暂时还没有以前asp.net与mvc中的server对象。获取url的编码与解码操作不能使用以前的server对象来获取。使用的是...

下载net core

官方下载地址:https://dotnet.microsoft.com/download 进来之后就可以看到最新的下载版本可以直接点击下载,也可以下载其...

net core使用依赖注入来装载EF的上下文对象

妹子情人节快乐~.net core中用了不少的依赖注入,官方文档中也推荐使用。这样使用依赖注入来管理ef对象,还是比较科学,比如...
这一世以无限游戏为使命!
排名
41
文章
14
粉丝
3
评论
3
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2025TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:50010702506256
欢迎加群交流技术