SharePoint underground (Unmanaged Code and SharePoint Internals)


Published on

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.

Published in: Technology
1 Comment
  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

SharePoint underground (Unmanaged Code and SharePoint Internals)

  1. 1. Unmanaged code and SharePoint Internals<br />Karla Ponce<br />Neudesic<br />
  2. 2. Who is Karla Ponce?<br />Microsoft MVP<br />More than 10 years of experience<br />Active speaker<br />I’m not any of that…<br /><ul><li>Twitter - @SPKarlaPonce
  3. 3. Blog – http://SharePointWithAttitudecom</li></li></ul><li>Unmanaged code vs. Managed code<br />Why should I care?<br />SharePoint object model Overview<br />SharePoint Internals: SPRequest<br />Common Issues<br />Best Practices<br />Owssvr.dll<br />Agenda<br />
  4. 4. Tambien se habla español.<br />
  5. 5. SharePoint uses unmanaged code to perform the majority of the work. The managed part of it is much smaller than the unmanaged part. <br />Premise<br />
  6. 6. Unmanaged code vs Managed code<br />
  7. 7. Managed Code:<br />It compiles to Intermediate Language (IL).<br />It runs and is managed by the CLR.<br />The runtime provides services such as security, memory management, threading, etc.<br />Unmanaged Code:<br />It compiles directly to machine code.<br />It does not run and is not managed by the CLR.<br />It doesn't get the services of the runtime.<br />Unmanaged code vs Managed code<br />
  8. 8. For an understanding of how things work:<br />OM is a wrapper of the unmanaged code.<br />The managed part is small; the unmanaged part of is much larger.<br />Understanding will help us to:<br />Troubleshoot issues, fix bugs and do performance tuning.<br />Write scalable, quality and solid code.<br />Avoid memory issues and poor performance.<br />Why should I care?<br />
  9. 9. SharePoint Dlls<br />
  10. 10. Site Architecture and Object Model<br />
  11. 11. SharePoint Internals<br />
  12. 12. Potentially excessive number of SPRequest objects (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. <br />Common Issues<br />
  13. 13. 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_MACHINESOFTWAREMicrosoftShared ToolsWeb Server ExtensionsHeapSettings. Then create a new DWORD named SPRequestStackTrace with the value 1 under this key. <br />Common Issues<br />
  14. 14. Disposing objects<br />Using objects efficiently<br />Performance concerns – Memory allocation<br />Write scalable code<br />Unmanaged Code: Best Practices<br />
  15. 15. Caching Objects that contain an embedded reference to SPWeb or SPSite objects.<br />SPWeb and SPSite contain reference to unmanaged code (not thread safety) and should not be cached.<br />Using objects efficiently<br />
  16. 16. Bad Coding Practice:<br />Caching objects and Thread Safety<br />
  17. 17. Applying a lock:<br />
  18. 18. Good code practice:<br />
  19. 19. Always ensure to correctly dispose all SPWeb and SPSite. Dispose all objects implementing the IDisposable interface .<br />Object disposal: Dispose method; using clause; try, catch, and finally blocks. <br />SPDisposeCheck, free tool that inspects your assemblies for coding practices that cause memory leaks because of improper handling and disposal of SharePoint objects.<br />Disposing SharePoint Objects<br />
  20. 20. Bad coding practice<br />
  21. 21. Good coding practice<br />
  22. 22. Using(SPSite mySite = new SPSite(“http://moss”)<br />{<br /> SPWeb web1= mySite.OpenWeb();<br /> SPWeb web2= mySite.RootWeb();<br /> SPWeb web3 = SPContext.Current.Site.OpenWeb();<br />}<br />
  23. 23. Performance concerns – Memory allocation<br />How many SPRequest objects are open in parallel?<br />Do SPRequest objects still exist at the end of the thread that created it?<br />
  24. 24. SharePoint 2010 Developer Dashboard<br />
  25. 25. The principal DLL of the unmanaged code is owssvr.dll<br />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. <br />Returning all data for a SharePoint list, including its XSD:http://WebApp/[site]/_vti_bin/owssvr.dll?Cmd=Display&List={ListGuid}&Query=*&XMLDATA=TRUE<br />Owssvr.dll<br />
  26. 26.
  27. 27. Demo<br />
  28. 28. SharePoint executes all the read/write operations to the DB through unmanaged code.<br />SPWeb and SPSite contain a reference to the unmanaged code.<br />Unmanaged objects are created in form of a SPRequest.<br />Because SPRequest does not run under the .NET Runtime, we have to dispose those objects to avoid memory issues.<br />Summary<br />
  29. 29. Q&A<br />
  30. 30. Object Model: <br /><br />Best Practices: Disposing SharePoint Objects<br /><br />URL Protocol:<br /><br />SharePoint Dispose Checker Tool<br /><br />Resources<br />