Saturday, May 12, 2007

Fixing up the System.Web.Profile

I am certainly not a wise person. Instead of learning from a Dan Hounshell's struggle, I wasted tons of time, trying to make Microsoft profile work for Web Application Project. Dan gave a perfect description of how I felt after fruitless fighting with MS Profile Provider until smart people (who read more than me) pointed me in the right direction.

To make long story short here is the recipe:

  • Decide to utilize Microsoft (widely advertised) Profile feature.
  • Use Web Application instead of the native Web Site.
  • Fail miserably and admit that you are stupid doing something wrong.

When Microsoft patched a Visual Studio to give an alternative to the hated Web Site application, their developers broke a support for a Profile mechanism. The schema will refuse to function unless you download ASP.NET Profile Generator - it will replace a Web Site's automatic CommonProfile generation. (hurry up - Microsoft is abandoning GotDotNet by the summer 2007).

Even after you successfully generated the profile class, there are still some limitations. You are pretty much safe if the profile properties come from the web.config file:

<profile>
<properties>
<add name="FirstName" />
... etc ...
</properties>
</profile>
It is a little bit more difficult if you want your profile class to be more intelligent. The natural choice is to create your own class and inherit profile:
<profile inherits="MyProject.MyCustomProfile"/>
In this case I would recommend to create the partial class - one part have your custom properties in the following form:
public virtual string FirstName
{
get
{
return ((string)(this.GetPropertyValue("FirstName")));
}
set
{
this.SetPropertyValue("FirstName", value);
}
}
and another part will contain the required plumbing provided by the generator.

And the last tip: the Runtime will try to cast your profile class to the ProfileCommon type and fail if you didn't inherit your class from the System.Web.Profile.ProfileBase. Better do it in the generated partial class. The full example of the Web Application Profile class you can download here CustomProfile.cs.

Again - it is a good idea to keep your custom part separated from the generated code so nobody would accidentally regenerate the profile. To make these sad accidents even less likely - do not install the ASP.NET Profile Generator and simply use the example code.

10 comments:

Samson said...

I can't seem to download your CustomProfile.cs

Michael Goldobin said...

Hmm... Seems fine to me. There should be a link "Click here to start download..."
I apolofize if it won't work. In this case leave me your email and I will send it.
I guess I should look for another file hosting.

Joe said...

For whatever reason, my Context.Profile still acts like its ProfileCommon and I just can't get past this. Any ideas on what I may have missed.

private WebProfile Profile
{
get { return new WebProfile(Context.Profile); }
}

Thanks,
Joe

MG said...

I assume that you're running Web Application. As soon as the WebProfile is actually your custom profile class it should be OK. Relative to the code from the example the accessor should look like that:
private CustomProfile Profile
{
get { return new CustomProfile (Context.Profile); }
}
The weird thing here is that CustomProfile is not actually inheriting the Microsoft profile but rather wrapping it. I should check how they did it in Orcas....

Anonymous said...

Hi, I need some help because the example code in the readme that comes with the Web Profile Generator does not appear to be correct and not many sites talk about using this tool. Anyways, the readme states the following:

// Using GetProfile and BaseProfile in Visual Basic
Dim webProfile As WebProfile = WebProfile.GetProfile("username")
webProfile.MyProperty = "value"
PassToFunctionThatWantsBaseProfile webProfile.BaseProfile

This VB Code doesn't work because 1) VB is case insensitive
2) If I change the statement above to read as Dim myWebProfile As WebProfile = WebProfile.GetUser("username")
Then the ide complains that I am using a null reference object.(Reference to a non shared(static) members requires an object reference.
I don't even see how the c# code example would work. Please help you can give me C# code example if you want it doesn't matter to me.

Thank You

Michael Goldobin said...

Tty to download the attached example code - it works out of the box...

mathieu_cupryk@hotmail.com said...

I get the following error when compiling in asp.net 3.5 WAP
Error 105 The type or namespace name 'CustomType' could not be found (are you missing a using directive or an assembly reference?) C:\inetpub\wwwroot\OmegaLove\OmegaLove.Web\Components\OmegaLoveCustomProfile.cs 49 24 OmegaLove.Web

Michael Goldobin said...

I was going to investigate the Profile behavior for the 3.5 but didn't find time to do this. Is it still broken? It is quite possible that you don't really need this workaround.

r4 revolution said...

What do you mean by profile of a site??????Please provide more information over it. Provide links to related topics if possible.keep posting. Will be visiting back soon.

Michael Goldobin said...

Hmm... That's Profile Provider feature, what else to say? You can start here ASP.NET Profile Providers


© 2008-2013 Michael Goldobin. All rights reserved