SharePoint underground (Unmanaged Code and SharePoint Internals)

30
Karla Ponce Neudesic

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.

Transcript of SharePoint underground (Unmanaged Code and SharePoint Internals)

Page 1: SharePoint underground (Unmanaged Code and SharePoint Internals)

Karla Ponce

Neudesic

Page 2: SharePoint underground (Unmanaged Code and SharePoint Internals)

Microsoft MVP

More than 10 years of experience

Active speaker

I’m not any of that…

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

Page 3: SharePoint underground (Unmanaged Code and SharePoint Internals)

Unmanaged code vs. Managed code

Why should I care?

SharePoint object model Overview

SharePoint Internals: SPRequest

Common Issues

Best Practices

Owssvr.dll

Page 4: SharePoint underground (Unmanaged Code and SharePoint Internals)

Tambien se habla español.

Page 5: SharePoint underground (Unmanaged Code and SharePoint Internals)

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

Page 6: SharePoint underground (Unmanaged Code and SharePoint Internals)
Page 7: SharePoint underground (Unmanaged Code and SharePoint Internals)

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.

Page 8: SharePoint underground (Unmanaged Code and SharePoint Internals)

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.

Page 9: SharePoint underground (Unmanaged Code and SharePoint Internals)
Page 10: SharePoint underground (Unmanaged Code and SharePoint Internals)
Page 11: SharePoint underground (Unmanaged Code and SharePoint Internals)
Page 12: SharePoint underground (Unmanaged Code and SharePoint Internals)

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.

Page 13: SharePoint underground (Unmanaged Code and SharePoint Internals)

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.

Page 14: SharePoint underground (Unmanaged Code and SharePoint Internals)

Disposing objects

Using objects efficiently

Performance concerns – Memory allocation

Write scalable code

Page 15: SharePoint underground (Unmanaged Code and SharePoint Internals)

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.

Page 16: SharePoint underground (Unmanaged Code and SharePoint Internals)

Bad Coding Practice:

Page 17: SharePoint underground (Unmanaged Code and SharePoint Internals)

Applying a lock:

Page 18: SharePoint underground (Unmanaged Code and SharePoint Internals)

Good code practice:

Page 19: SharePoint underground (Unmanaged Code and SharePoint Internals)

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.

Page 20: SharePoint underground (Unmanaged Code and SharePoint Internals)

Bad coding practice

Page 21: SharePoint underground (Unmanaged Code and SharePoint Internals)

Good coding practice

Page 22: SharePoint underground (Unmanaged Code and SharePoint Internals)

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

{

SPWeb web1= mySite.OpenWeb();

SPWeb web2= mySite.RootWeb();

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

}

Page 23: SharePoint underground (Unmanaged Code and SharePoint Internals)

How many SPRequest objects are open in parallel?

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

Page 24: SharePoint underground (Unmanaged Code and SharePoint Internals)
Page 25: SharePoint underground (Unmanaged Code and SharePoint Internals)

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

Page 26: SharePoint underground (Unmanaged Code and SharePoint Internals)
Page 27: SharePoint underground (Unmanaged Code and SharePoint Internals)
Page 28: SharePoint underground (Unmanaged Code and SharePoint Internals)

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.

Page 29: SharePoint underground (Unmanaged Code and SharePoint Internals)