[Question] Best practices ServiceClient & Dependency Injection #312
-
Do you have any guidance / best practices when using the ServiceClient in web applications or function apps? These will use dependency injection. Kind of similar to what was done for the HttpClient. So basically: What is the correct way of using the ServiceClient with Dependency Injection in web applications? |
Beta Was this translation helpful? Give feedback.
Replies: 7 comments 14 replies
-
@mVermaat I think that is not coevered in the Dataverse docs (yet) but here's a nice example by Temmy @temmyraharjo: https://temmyraharjo.wordpress.com/2022/07/09/how-to-make-azure-functions-dataverse-service-client/ |
Beta Was this translation helpful? Give feedback.
-
Yep. this is on the "To-Do" list for samples for the client, |
Beta Was this translation helpful? Give feedback.
-
Good to know, thanks for the information. With the current constraints, what is the best approach? I know I can make it 'work' by implementing something like in the link. However I'm not sure what the consequences are for using in example Scoped or Singleton. |
Beta Was this translation helpful? Give feedback.
-
Scoped or Singleton uses cases are situational, IE, what are you trying to accomplish in the function or asp.net core app. Mainly what we are testing is memory and isolation management options for us in API services (function and the like), when running at scale over a long period of time. Once we settle on a specific approach, we will build it into the client as an extension. In the meantime, we may provide a sample or 2 on the PowerApps Samples site based on current work. |
Beta Was this translation helpful? Give feedback.
-
@MattB-msft any updates I am having a hard time with the dependency injection taking about a second to create an instance of service client any suggestions? this is what I am doing builder.Services.AddScoped(x => |
Beta Was this translation helpful? Give feedback.
-
It works in both Singleton and Scoped setups, depending on your usecase it can be used differently but my recommendation would be to always use it as a singleton. Scoped: Singleton: If your running on consumptionbased azure functions you have a limit of 600 outbound connections, everytime you scope a new instances of the serviceclient it reserves one or more connections and they have to be unused for 5 minutes to be removed again. - thus making a singleton the only way to go in any sort of high load situation. Personally I always use it as a singelton in my own custom build framework that ensures a connection is recreated if needed, handles the API limits and share the load between different connections if running in a high performance scenario. (We have tested it to 1400 calls/s sustained for an hour without running into API limits or azure connection limits). |
Beta Was this translation helpful? Give feedback.
-
@FarmerenHE thanks for your valuable insights! are you also tweaking Max Concurrent Calls in the Azure Functions with Service Bus? |
Beta Was this translation helpful? Give feedback.
Scoped or Singleton uses cases are situational, IE, what are you trying to accomplish in the function or asp.net core app.
We do not have a strong opinion on it and are using both processes in different services.
Mainly what we are testing is memory and isolation management options for us in API services (function and the like), when running at scale over a long period of time. Once we settle on a specific approach, we will build it into the client as an extension. In the meantime, we may provide a sample or 2 on the PowerApps Samples site based on current work.