Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Adds DaprClient in Dapr.AspNetCore from AddSingleton to TryAddSingleton #867

Merged
merged 3 commits into from
May 17, 2022

Conversation

zhenlei520
Copy link
Contributor

…gleton

Signed-off-by: zhenlei520 wangzhenlei520@gmail.com

Description

I hope that AddDaprClient can be changed to TryAddSingleton. Although the AddDaprClient method only adds DaprClient to the current class library once, if other class libraries also add DaprClient and then use Dapr.AspNetCore, DaprClient will be added again.

…gleton

Signed-off-by: zhenlei520 <wangzhenlei520@gmail.com>
@zhenlei520 zhenlei520 requested review from a team as code owners April 20, 2022 15:46
@halspang
Copy link
Contributor

@zhenlei520 - Thanks for opening this! Though I think this is already solved in the code: https://github.com/dapr/dotnet-sdk/pull/867/files#diff-6b7b1ed59d457676840fddee293b56b4f8354d2b5bbad4df9c8d8fb11103ff15R41

@zhenlei520
Copy link
Contributor Author

@zhenlei520 - Thanks for opening this! Though I think this is already solved in the code: https://github.com/dapr/dotnet-sdk/pull/867/files#diff-6b7b1ed59d457676840fddee293b56b4f8354d2b5bbad4df9c8d8fb11103ff15R41

I am very happy to receive your reply, but I think there is a problem here. If you directly use the methods in the Dapr.AspNetCore package, both AddDaprClient and AddDapr support multiple additions, and DaprClient will not be added multiple times. but DaprClient is the core capability of Dapr.Client. If I only need to use service calls, then I can do Add DaprClient myself, but if I do this, once I use AddDapr again later, it will cause DaprClient to be called add multiple times

@zhenlei520
Copy link
Contributor Author

zhenlei520 commented Apr 21, 2022

@halspang

services.AddSingleton(_ =>
{
var builder = new DaprClientBuilder();
if (configure != null)
{
configure.Invoke(builder);
}
return builder.Build();
});

I think this should be replaced by TryAddSingleton, or the AddDaprClient method should be provided by Dapr.Client instead of Dapr.AspNetCore

@halspang
Copy link
Contributor

@zhenlei520 - Is the use case you're talking about adding DaprClient to the services manually, without calling one of the helper methods?

Also, I don't think Dapr.Client should own this code path. The services model that we're going off of here is an AspNet behavior and thus the code belongs in the Dapr.AspNetCore package.

@rynowak
Copy link
Contributor

rynowak commented Apr 22, 2022

Also, I don't think Dapr.Client should own this code path. The services model that we're going off of here is an AspNet behavior and thus the code belongs in the Dapr.AspNetCore package.

We left this out of Dapr.Client to avoid extra dependencies. It doesn't feel worthwhile to add a dependency so that we can add a method that includes one line of code.

@zhenlei520
Copy link
Contributor Author

zhenlei520 commented Apr 24, 2022

Also, I don't think Dapr.Client should own this code path. The services model that we're going off of here is an AspNet behavior and thus the code belongs in the Dapr.AspNetCore package.

We left this out of Dapr.Client to avoid extra dependencies. It doesn't feel worthwhile to add a dependency so that we can add a method that includes one line of code.

Right now I have a package just for sending messages. In fact, I only need to handle the injection of DaprClient by myself, and then pass it through the Pub of DaprClient. I don't need to rely on Dapr.AspNetCore, but if I did it would cause the DaprClientto register multiple times if the user calls the AddDapr method after using my package

@zhenlei520
Copy link
Contributor Author

Any news on this?

@codecov
Copy link

codecov bot commented May 17, 2022

Codecov Report

Merging #867 (1f2fd4d) into master (ce1315f) will not change coverage.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##           master     #867   +/-   ##
=======================================
  Coverage   69.13%   69.13%           
=======================================
  Files         142      142           
  Lines        4649     4649           
  Branches      517      517           
=======================================
  Hits         3214     3214           
  Misses       1318     1318           
  Partials      117      117           
Flag Coverage Δ
net5 69.09% <100.00%> (ø)
net6 69.09% <100.00%> (ø)
netcoreapp3.1 69.09% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
...Dapr.AspNetCore/DaprServiceCollectionExtensions.cs 87.50% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update ce1315f...1f2fd4d. Read the comment docs.

@halspang halspang merged commit 2651832 into dapr:master May 17, 2022
@halspang halspang added this to the v1.8 milestone Jun 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants