首页
视频
资源
登录
原
Dapr .NetCore 订阅与发布(下)
4253
人阅读
2021/10/25 13:23
总访问:
2537818
评论:
0
收藏:
0
手机
分类:
.net后台框架
![](https://img.tnblog.net/arcimg/hb/896fd38e95b346f9a0d98c54b135bb94.jpg) >#Dapr .NetCore 订阅与发布(下) [TOC] 解决关于运行是报错问题 ------------ tn2>我们将为`InvokeMethodServer`API项目做一些修改。 首先添加一些相关依赖 ```xml <ItemGroup> <PackageReference Include="CloudNative.CloudEvents" Version="1.3.80" /> <PackageReference Include="CloudNative.CloudEvents.AspNetCore" Version="1.3.80" /> <PackageReference Include="Dapr.AspNetCore" Version="1.4.0" /> <PackageReference Include="Newtonsoft.Json" Version="13.0.1" /> </ItemGroup> ``` tn2>创建`RequestController`控制器,设置`/dapr/subscribe`订阅与发布的配置,这里我们添加一个NewSong方法并且将路径设置为`/playlist/hello`. ```csharp [ApiController] [Route("[controller]")] public class RequestController : ControllerBase { private readonly ILogger<RequestController> _logger; public RequestController(ILogger<RequestController> logger) { _logger = logger; } [HttpGet("/dapr/subscribe")] public ActionResult<IEnumerable<string>> Get() { var topics = new [] {new { pubsubname="pubsub",topic = "songs", route = "playlist/hello"}}; return new OkObjectResult(topics); } [HttpPost("/playlist/hello")] public async Task<IActionResult> NewSong(CloudEvent cloudEvent) { var song = JsonConvert.DeserializeObject<Song>(cloudEvent.Data.ToString()); _logger.LogInformation($"New song request: {song.Artist} – {song.Name}"); return new OkResult(); } } public class Song { [JsonProperty("id")] public int Id { get; set; } [JsonProperty("artist")] public string Artist { get; set; } [JsonProperty("name")] public string Name { get; set; } } ``` tn2>在 Startup 类中,为了帮助进行模型绑定,让我们为 CloudEvents添加一个输入格式化程序 ```csharp public void ConfigureServices(IServiceCollection services) { services.AddControllers().AddDapr(); services.AddMvc(opts => { opts.InputFormatters.Insert(0, new CloudNative.CloudEvents.CloudEventJsonInputFormatter()); }); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); } ``` tn2>运行测试 ```bash dapr run --app-id myserer --app-port 5002 --dapr-http-port 3500 -- dotnet run ``` ![](https://img.tnblog.net/arcimg/hb/0717cb5c98b34ac7af855ee1a7dac4b4.png) tn2>dapr cri调用 ```bash dapr publish --publish-app-id myserer --pubsub pubsub --topic songs --data '{"id":17,"name":"dapr_cli_mysongs","artist":"hhh"}' ``` tn2>bash调用 ```bash curl -X POST http://localhost:3500/v1.0/publish/pubsub/songs -H "Content-Type: application/json" -d '{"id":17,"name":"bash_mysongs","artist":"hhh"}' ``` ![](https://img.tnblog.net/arcimg/hb/af8b7212ea734c21b64bbaadfbe626d2.png) ![](https://img.tnblog.net/arcimg/hb/c0fe5244531f498883643e01a23ba269.png) tn2>从其他客户端dapr请求过来的也能响应到。
欢迎加群讨论技术,1群:677373950(满了,可以加,但通过不了),2群:656732739
👈{{preArticle.title}}
👉{{nextArticle.title}}
评价
{{titleitem}}
{{titleitem}}
{{item.content}}
{{titleitem}}
{{titleitem}}
{{item.content}}
尘叶心繁
这一世以无限游戏为使命!
博主信息
排名
6
文章
6
粉丝
16
评论
8
文章类别
.net后台框架
166篇
linux
17篇
linux中cve
1篇
windows中cve
0篇
资源分享
10篇
Win32
3篇
前端
28篇
传说中的c
4篇
Xamarin
9篇
docker
15篇
容器编排
101篇
grpc
4篇
Go
15篇
yaml模板
1篇
理论
2篇
更多
Sqlserver
4篇
云产品
39篇
git
3篇
Unity
1篇
考证
2篇
RabbitMq
23篇
Harbor
1篇
Ansible
8篇
Jenkins
17篇
Vue
1篇
Ids4
18篇
istio
1篇
架构
2篇
网络
7篇
windbg
4篇
AI
18篇
threejs
2篇
人物
1篇
嵌入式
2篇
python
13篇
HuggingFace
8篇
pytorch
9篇
opencv
6篇
最新文章
最新评价
{{item.articleTitle}}
{{item.blogName}}
:
{{item.content}}
关于我们
ICP备案 :
渝ICP备18016597号-1
网站信息:
2018-2024
TNBLOG.NET
技术交流:
群号656732739
联系我们:
contact@tnblog.net
欢迎加群
欢迎加群交流技术