Showing posts with label Sandbox solution. Show all posts
Showing posts with label Sandbox solution. Show all posts

Wednesday, 11 December 2013

SharePoint 2010 Sandbox solution 2 – Architecture and Execution Model



In my previous blog, I took up overview of Sand boxed solutions. Now the time is to take a lift and peek into the architecture and execution model
Architecture

Sandbox solution is often called as user solution. The primary API for sandbox solution is Microsoft.SharePoint.UsserCode and the service that takes care of it is SharePoint 2010 User Code Host.

Now the first question arises is where the assemblies are deployed – The assembly is packaged in wsp file and the wsp is uploaded in the solution gallery. When, for the first time, the assembly is required – e.g. a page is accessed which contains a webpart of the sandboxed solution, then the assembly is extracted from the gallery and copied to the file system (C:\ProgramData\Microsoft\SharePoint\UCCache) of the server that is handling the request (this may not be web front end server as it is handled by User Code Host Service).

But who copies the assembly to the file system as SPUCWorkerProcess.exe can’t write to file system. The answer is SharePoint 2010 User Code Host service

Once the user session, which accessed the assembly, ends, the assembly stays in the cache for a short time. If the same assembly is required by some other session it can be accessed from the cache, if present. Otherwise again it needs to be extracted from the solution. Following is the detailed execution process


This chronicles the process of using a custom  Web Part and solution in a particular site.

  1. The SPSite admin uploads a new solution package (*.wsp) into the Solution Gallery of the SPSite.
  2. The SPSite admin "activates" the solution.  This activates the features within the solution.  Web Part files are copied into the Web Part gallery.

•       As part of the activation, solution is validated using the validation framework. Custom validator can be added for example to check that only solutions signed with certain key can be activated. Customers and partners can develop their own validators based on their needs.

  1. Sometime later, a user decides to add a Web Part to their home page.  They go into Web Part edit mode, and click "Add a Web Part".  They notice the additional Web Part options, and click Add.
  2. SharePoint now checks to see if the custom webpart.dll file, which backs this Web Part, is installed into the assembly cache.  It is not.
  3. The assembly is faulted into the assembly cache; it is extracted and copied from the solution file to temporary folder in disk and loaded to memory (disk is cleaned immediately). Now the Web Part is about to be used. 
  4. It is loaded into Sandbox Code service host.
  5. Processes deliver the Web Part to be executed to the service.

 

Execution Model


We’ll see the same execution from the perspective of execution model

 


  1. An incoming request comes in for a page with a Web Part from a partial trust assembly.   This is delegated to a Web Part proxy.  The Web Part proxy then in turn calls the worker process manager, and tells it to execute the Web Part
  2. The worker process manager queries the configuration database to figure out which machine and process it should send the request to
  3. The worker process then sends the request to the user code manager on that machine
  4. The user code manager needs to ensure that the assembly backing the Web Part is locally deployed.  To do this, it reaches back into the Web Part solution package, extracts the assembly, and places it into the assembly cache
  5. Now, the SPUserCodeManager (SPUserCodeHostService.exe) delegates the request to execute the code to SPUserCodeWorkerProcess.exe. The full trust Web Part wrapper works with the instantiated process to simulate the Web Part lifecycle
  6. The Web Part itself calls into the SharePoint OM to retrieve some set of data
  7. The resulting HTML and viewstate changes are bubbled back to the hosting process, which has been synchronously waiting for this infrastructure to complete
  8. The resulting page is sent back to the user
  9. Rendered results sent back to the requester

 

So, that’s it. Hope this blog served the purpose. I’ll be back with the next part of this series soon J

 

Tuesday, 10 December 2013

SharePoint 2010 Sandbox solution 1 – Overview of Sandboxed solution



SharePoint aims at reducing the involvement of IT team in solution lifecycle. Deployment and monitoring of custom solution is one area where Farm administrators (IT team) are still putting lots of their efforts. This effort can be reduced (I would use the word REDUCED effort instead of no effort because of the concept of Solution Validation) if the business needs can be fulfilled by Sandboxed solution.

In this series of blogs, I’ll be covering following topics in each block

1.       Overview of Sandboxed solution

2.       Architecture and Execution Model of Sandboxed solution

3.       Sandbox Proxy

4.       Solution Validation

Overview of Sandboxed solution


If I try to define sandboxed solution technically then the easiest way is — It runs under context of SPUCWorkerProcess (instead of w3wp) which has limited access to the resources

Before you decide on Sandboxed solution for your application, you need to be aware of the following

1.       It has access to only a subset of SharePoint object model. So limited operations can be performed

2.       It has no access to file system. So you cannot create Visual Web Parts or read/ write web.config file or log exceptions into log files

3.       No support to external web services

4.       No Email support using SPUtility.SendMail

5.       Impersonation is not allowed. So you cannot use SPSecurity.RunWithElevatedPrivileges

6.       Scope of the solution is limited. You can access only site collection level resources in which it is deployed

7.       Performance is another concern. If sand box worker process runs for more than 30 seconds then the UserCodeHosting service terminates the process

Oh my god! There are so many restrictions.

Don’t worry there are ways to perform full trust operation even after these restriction. I’ll show it in further blogs (3. Sandbox Proxy)

Now we are aware of, on high level, what we can’t do. Let’s see what we can do. We can create

1.       Web Parts (normal, not visual web part)

2.       Lists

3.       List Templates

4.       Custom Actions

5.       Event Receivers

6.       Content Types

7.       Site Columns

8.       Workflows

9.       Event Receivers

And many more…

 

Now we’ll look at the lifecycle of a Sandboxed solution at a very high level (as I’ll be diving deep in subsequent episodes)





 

1.       Deployment – The Site Collection Administrator (SCA) gets a solution as wsp file and uploads it into Solution Gallery. Once it is uploaded she needs to activate it. On activation the solution is validated against validation logic (either OOB or custom). IIS reset/ Web App pool recycling required. Great huh?

2.       Activation – If the validation is passed then the solution is activated which activates all the features contained in the solution. The SCA doesn’t need to activate the features separately(But cross checking is always good :P). If we assume that a feature contains a web part then on activation the web part would be populated into web part gallery

3.       Deactivation – On deactivating the solution, the web part no longer executes but still be present in the web part gallery

4.       Deletion – The solution is deleted. The web part, obviously, doesn’t executes but still it remains in the web part gallery

By now you would be aware of WHAT IS SANDBOXED SOLUTION, WHAT WE CAN AND CAN’T and LIFE CYCLE OF SANNDBOXED SOLUTION. Here I am concluding this episode of Sandboxed solutions and will take up Architecture and Execution model next