Dev Team Assemble

Evil beware!
Add to Technorati Favorites

Archive

Category: ASP.NET

I have been working with xml serialization over the last week or so and ran into a strange issue today. It only came to light when I decided to implement IXmlSerializable and write my own methods for reading and writing the xml. I posted recently about using he XmlRootAttribute as a fix for the xmlns='' > was not expected error I was seeing. This all worked fine and dandy until I really started to utilize that method over and over again in my code. I found the performance of this to be horrible...so horrible in fact it was taking 2-3 minutes to deserialize a moderately complex 1MB xml file. After doing some digging (on "The Google") I discovered that microsoft decided to only implement caching on the Xml serializer when you use one of the two "more common" constructors (see documentation). Of course it would seem the one I need would not be one of them.

I was thinking of somehow adding the XmlSerializers for each type/root pair that I was using to the application level cache or something like that but thought that might bring with it its own set of challenges. I found a great article here on stack overflow that shows a possible workaround for the problem using a static class to act as a cache or you.

Please note this code is taken directly from the article above:


public static class XmlSerializerCache
{
private static readonly Dictionary cache =
new Dictionary();

public static XmlSerializer Create(Type type, XmlRootAttribute root)
{
var key = String.Format( CultureInfo.InvariantCulture, "{0}:{1}", type, root.ElementName);

if (!cache.ContainsKey(key))
{
cache.Add(key, new XmlSerializer(type, root));
}

return cache[key];
}
}

Here is how you use it to create an XmlSerializer object:


var xmlRootAttribute = new XmlRootAttribute("ExampleElement");
var serializer = XmlSerializerCache.Create(target.GetType(), xmlRootAttribute);

After implementing that I was still experiencing the 2-3 minute deserialization one the first call but all subsequent calls to that code with similar xml took only 2 seconds.

NOTE:
Seems microsoft is in the habit of only half implementing caching in their code. This problem is almost identical to the problems with the CrossListQueryCache object in SharePoint....where they again only implemented caching for two of the four available method signatures. Here is an interesting article I found in the nether-regions of cyberspace where microsoft state this half implementation is by design...

Technorati Tags: , , , , , , ,

I had what turns out to be a fairly common error the other day when trying to deserialize an xml file in one of my applications.  Here is the code I was running:


XmlSerializer ser = new XmlSerializer(typeof(MyObject));
XmlReader xRdr = XmlReader.Create(new StringReader(xmlData));
MyObject tvd = (MyObject)ser.Deserialize(xRdr);

As it turns out this was causing the error: xmlns=''> was not expected during deserialization. Alot of the resources online (including the msdn article here) were pointing me to the namespaces not being the same on the serializer and document. I am not sure why it wasn't working for me because I was playing around with the namespaces but couldn't seem to figure out how to get that working. Maybe because I was only serializing a fragment of a document I'm not sure. Anyway it turns out that specifying the XmlRootAttribute in the XmlSerializer constructor fixed the problem for me. This Stack Overflow article really helped Here is the snippet of code that I am using now:


XmlRootAttribute xRoot = new XmlRootAttribute();
xRoot.ElementName = elementName;
xRoot.IsNullable = true;

XmlSerializer ser = new XmlSerializer(typeof(MyObject), xRoot);
XmlReader xRdr = XmlReader.Create(new StringReader(xmlData));
MyObject tvd = (MyObject)ser.Deserialize(xRdr);

Hope this helps

Technorati Tags: , , , , ,

I came across a great post today that talks about ASP.NET caching (applies to SharePoint) and what to look for.  Thanks to Gunnar Peipman for the great insight.

http://weblogs.asp.net/gunnarpeipman/archive/2010/04/02/measuring-asp-net-and-sharepoint-output-cache.aspx

Basically you need to setup the following performance counters in windows:

  • Total number of objects added
  • Total object discards
  • Cache hit count
  • Cache hit ratio

Technorati Tags: , , ,

Its seems that SharePoint has a knack for surprising me with seriously strange errors sometimes. Usually at the core of the error there is a sound explanation as to why this is happening but nonetheless I am not pleased with the obscurity of them sometimes. Case in point, while working on a Console application that would load FBA (Forms Based Authentication) users into a sharepoint sie collection and then create a site for each one (granting permissions and so on) I came across a very strange error - SPException: SharePoint cannot find the user.

This kind of surprised me at first and I was wondering if somehow my CustomMembershipProvider setup stopped working on my portal site, but that was fine. So after hammering away at it for a while I came across a great article written by Waldek Mastykarz on this exact problem.  After reading through his investigation it makes sense why it doesn't work in a console application but it works under the context of a SharePoint website. Essentially the web application has access to a context object and this has access to the providers node in your web.config file,  BUT the console application does not have this information available.  So when the SharePoint assemblies attempt to access this information they cannot because it does not exist.

The way around this is to create an HttpContext in your application before attempting to run SPWeb.EnsureUser("blah") like so:

if (HttpContext.Current == null) { HttpRequest request = new HttpRequest("", web.Url, ""); HttpContext.Current = new HttpContext(request, new HttpResponse(new StringWriter())); HttpContext.Current.Items["HttpHandlerSPWeb"] = web; }

Then add the system.web/membership/providers node to your app.config file (which you may have to create in the project)..  Mine looks like so:

<system.web> <membership> <providers> <add name="CustomSqlProvider" applicationName="/Portal" connectionStringName="sqlData" type="CustomProviders.CustomSqlMembershipProvider, CustomProviders, Version=1.0.0.0, Culture=neutral, PublicKeyToken=91a4fcd60b73a0e8" /> </providers> </membership> </system.web> <connectionStrings> <add name="sqlData" connectionString="Data Source=sqlpd; Initial Catalog=PUsers; Integrated Security=True; MultipleActiveResultSets=True " providerName="System.Data.SqlClient" /> </connectionStrings>

After adding this information and running a few tests it started to work. In fact it worked really well on my dev box...the only problem was it was a little intermittent on my production machine. Which is funny because the provider on the site itself works just fine all the time but in order to get my console application working (the EnsureUser() portion) the site would require an IIS reset. This is the one MAJOR stumbling block that I have yet to overcome...the only saving grace I have is that I can import the users manually into the system first using the UI and then run my console application that will load the sites and grant permissions to sites based on an external configuration file.

Many thanks to Waldek Mastykarz for all his help on this one so far...I know I will be coming back to this one in the near future but my head hurts a little and I need a beer.

Canadian SharePint event anyone?

Technorati Tags: , , , ,

From November 2008:

Whew...its been a long week and its only Thursday. This years conference was much the same as last years with the exception of being held in various portions of the Mandalay Bay convention center as opposed to on one floor. This made for lots and lots and lots and lots of walking. But I'm still young (yes i am) so it makes for good excercise to walk off all the food they shovel in your mouth.

Most of the sessions I attended were really good with the exception of a couple. I wouldn't blame the speakers because these problems were mostly due to technical issues like the computers not working, the projectors broken or feedback in the presenters microphone sort of thing. All in all there were some great things presented this week.

Some of the sessions that really stuck out were:

Kimberly Tripp - Index Internals and Usage
This was a great session that talked about thins like SQL Server Statistics, Query Optimization, Types of indexes, diminishing returns on performance, etc. Of all the session I think this one stood out the most and because it is probably the most relevant for me right now being one of the "SQL Server DBA" for my company (Christie Digital). I say it that way because its a committee based DBA :)

Rick Strahl - Using WCF for JSON and REST Services with ASP.Net

Great session....last one of the conference for me so it really sticks out but essentially this covers the next gen replacement for ASMX or first gen webservices in the .NET Framework.

John Papa - Practical Strategies with the Entity Framework

For me this was an introduction to the Entity Framework that was released in VS 2008 SP1. John did a great job of quickly introducing the technology and getting on to the meat and potatoes of what we were there to discuss. There was a good discussion on how to use the IDE to build the data mappings and what happens after you make changes to the backend server, which coinceidently can be any data source...not just SQL as Linq to SQL supports.

Things I would change...(and only because its my blog and I can say whatever I want!!!)

...the time between sessions was ridiculous...one hour or in some cases and hour and a half is just way too long. They could have added an extra session or two to the day. I realize they want you in the expo hall but they could have extended the day to facilitate that or leave it open all day and then some people may skip a couple of sessions here and there to go and see what the vendors are offering. this was my gripe last year and its my gripe this year. Its probably going to be the thing that keeps me from going to this in the future.

...provide video or podcasts of the sessions so we can take in the whole conference...even the sessions we didn't get to go to. I see there point about this being intellectual property and the speakers are consultants and this is their livelyhood but I mean if they are willing to teach this at a conference...and we pay to see it...shouldn't we be entitled to review this information after the fact? Maybe they will supply it on their site (i heard they were recording the sessions this year).

...ease up on the food. Damn, there was too much...I ate too much...I felt stuffed the whole time...I guess this is really my fault! Okay dont change that :)

Technorati Tags: , , , , , ,

This was taken from the following url: http://forums.msdn.microsoft.com/en-US/sharepointcustomization/thread/8ddb9f2e-12ca-4285-9bd1-1a29e0d5efc4/

Using a SharePoint Data View Drop down list This is the following syntax used to connect to an SPDatasource

< runat="server" id="dvddl{$Pos}" fieldname="HRRecruitRequisition" datasourceid="Requisitions2" datatextfield="Title" datavaluefield="ID" bind="{ddwrt:DataBind('i',concat('dvddl', $Pos), 'SelectedValue', 'SelectedIndexChanged', 'ID', ddwrt:EscapeDelims(string(@ID)), '@HRRecruitRequisition')}" appenddatabounditems="True" width="250px">

The meat and potatoes of this is in the __designer:Bind and FieldName attiributes. Obviously the DataSourceID needs to match the ID of the SPDataSource you are looking o display in the dropdown.

Technorati Tags: , , , ,

Inline code is disabled in sharepoint 2007 (not sure about SPS 2003) with the exception of files in the _layouts directory and will throw an error.  I was researching this a while back and thought I would throw it up here so I don't forget...maybe you can get some use out this article:

http://msdn.microsoft.com/en-us/library/bb862025.aspx

Essentially you need to add a PageParserPath entry to the web.config file for the web application

Technorati Tags: , ,

Well I finally found a solution to this. I had been having this issue for a while. I felt shame...but alas no more!!!!!!

Check out this blog http://www.elumenotion.com/Blog/Lists/Posts/Post.aspx?ID=23

The fix to this is so easy its almost embarrassing...I mean its not even a fix. Essentially this is what you do:

  • In your Visual Studio IDE > Open Tools Options.
  • Check Show all settings if needed and locate Enable Just My Code (Managed only) .
  • Uncheck this box and click OK.
  • Attach to your process
  • Open the modules window to ensure that your module appears and that Symbols have been loaded for it.