简体   繁体   中英

Sharepoint ClientContext usage in services

I use the SharePoint SDK in one of our WCF services.

To get a byte array (document) from SharePoint I use the ClientContext class of the SDK. The calls to SharePoint are all wrapped in a custom class called SharePointConnector(like a sort of repository pattern).

My goal is to inject the SharePointConnector class in my wcf service by using an interface ISharePointConnector.

My questions:

  1. Should I create the ClientContext in the constructor of SharePointConnector or should use it per call, wrapped in a using block ? Concerns that I have: is it an expensive call to create the ClientContext ? Thread safe when multiple calls are made on the same instance of clientContext member variable?
  2. At what point should I register the url so that it can be used in the constructor of ClientContext ?

Note that by implementing it by creating the ClientContext instance in the constructor, there is a difficulty to get the url that is required to create an instance of the ClientContext because the constructor injection by the DI container....

I would very much recommend you to use the ClientContext within the scope of the Methods (or whatever calls are in your case) and not instanciating it in the constructor, mainly for two reasons:

  1. Flexibility: You will be able to adjust the ClientContext to your needs in a given situation.
  2. Resources: As you already pointed out, the ClientContext implements the IDisposable interface and should therefore be disposed/released after using it, this will save you some resources. You know the drill:

    using (ClientContext clientCtx = new ClientContext(siteUrl)){ //do your stuff... }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM