SharePoint underground (Unmanaged Code and SharePoint Internals)

Preview:

DESCRIPTION

This session covers the unmanaged world of SharePoint. Internally, most of the things done in SharePoint using either the UI, the object model or web services, are done through unmanaged code. To be more specific: web Parts, web services and other objects in SharePoint are layers over the unmanaged code, and the object model is a wrapper that calls the unmanaged code. This session explains what is the SharePoint unmanaged code, what are the internals of the unmanaged code and how exactly it works with the object model. An example on the use of the owssvr.dll (principal dll of the unmanaged code) is also presented. Some unmanaged code internals are a must know for SharePoint developers. Understanding how our managed code works, will help us to write better code, troubleshoot, and design solutions in a better way.

Citation preview

Karla Ponce

Neudesic

Microsoft MVP

More than 10 years of experience

Active speaker

I’m not any of that…

• Twitter - @SPKarlaPonce• Blog – http://SharePointWithAttitudecom

Unmanaged code vs. Managed code

Why should I care?

SharePoint object model Overview

SharePoint Internals: SPRequest

Common Issues

Best Practices

Owssvr.dll

Tambien se habla español.

SharePoint uses unmanaged code to perform the majority of the work. The managed part of it is much smaller than the unmanaged part.

Managed Code:◦ It compiles to Intermediate Language (IL).

◦ It runs and is managed by the CLR.

◦ The runtime provides services such as security, memory management, threading, etc.

Unmanaged Code:◦ It compiles directly to machine code.

◦ It does not run and is not managed by the CLR.

◦ It doesn't get the services of the runtime.

For an understanding of how things work:◦ OM is a wrapper of the unmanaged code.◦ The managed part is small; the unmanaged

part of is much larger.

Understanding will help us to:◦ Troubleshoot issues, fix bugs and do

performance tuning.◦ Write scalable, quality and solid code.◦ Avoid memory issues and poor performance.

Potentially excessive number of SPRequestobjects (9) currently unreleased on thread 6. Ensure that this object or its parent (such as an SPWeb or SPSite) is being properly disposed. This object is holding on to a separate native heap.

An SPRequest object was reclaimed by the garbage collector instead of being explicitly freed. To avoid wasting system resources, dispose of this object or its parent (such as an SPSite or SPWeb) as soon as you are done using it. Allocation Id: {6E0B2F08-BEE0-43D4-BEDA-FDA2A43F40D8} To determine where this object was allocated, create a registry key at HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Shared Tools\Web Server Extensions\HeapSettings. Then create a new DWORD named SPRequestStackTrace with the value 1 under this key.

Disposing objects

Using objects efficiently

Performance concerns – Memory allocation

Write scalable code

Caching Objects that contain an embedded reference to SPWeb or SPSite objects.

SPWeb and SPSite contain reference to unmanaged code (not thread safety) and should not be cached.

Bad Coding Practice:

Applying a lock:

Good code practice:

Always ensure to correctly dispose all SPWeb and SPSite. Dispose all objects implementing the IDisposable interface .

Object disposal: Dispose method; using clause; try, catch, and finally blocks.

SPDisposeCheck, free tool that inspects your assemblies for coding practices that cause memory leaks because of improper handling and disposal of SharePoint objects.

Bad coding practice

Good coding practice

Using(SPSite mySite = new SPSite(“http://moss”)

{

SPWeb web1= mySite.OpenWeb();

SPWeb web2= mySite.RootWeb();

SPWeb web3 = SPContext.Current.Site.OpenWeb();

}

How many SPRequest objects are open in parallel?

Do SPRequest objects still exist at the end of the thread that created it?

The principal DLL of the unmanaged code is owssvr.dll

OWSSVR.DLL is an ISAPI extension registered in IIS and its methods can be called directly via an HTTP request to /_vti_bin/owssvr.dll.

Returning all data for a SharePoint list, including its XSD:http://WebApp/[site]/_vti_bin/owssvr.dll?Cmd=Display&List={ListGuid}&Query=*&XMLDATA=TRUE

SharePoint executes all the read/write operations to the DB through unmanaged code.

SPWeb and SPSite contain a reference to the unmanaged code.

Unmanaged objects are created in form of a SPRequest.

Because SPRequest does not run under the .NET Runtime, we have to dispose those objects to avoid memory issues.

Recommended