.Net Null Reference Exception in DocumentXPathNavigator - asp.net

I've run into an interesting exception. On one of my production servers, occasionally I'll get the following exception.
System.NullReferenceException: Object reference not set to an instance of an object.
at System.Xml.DocumentXPathNavigator.get_NameTable()
at System.Xml.Xsl.Runtime.XmlQueryContext.get_DefaultNameTable()
at System.Xml.Xsl.Runtime.XmlQueryRuntime..ctor(XmlQueryStaticData data, Object defaultDataSource, XmlResolver dataSources, XsltArgumentList argList, XmlSequenceWriter seqWrt)
at System.Xml.Xsl.XmlILCommand.Execute(Object defaultDocument, XmlResolver dataSources, XsltArgumentList argumentList, XmlSequenceWriter results)
at System.Xml.Xsl.XmlILCommand.Execute(Object defaultDocument, XmlResolver dataSources, XsltArgumentList argumentList, XmlWriter writer, Boolean closeWriter)
at System.Xml.Xsl.XmlILCommand.Execute(IXPathNavigable contextDocument, XmlResolver dataSources, XsltArgumentList argumentList, XmlWriter results)
at System.Xml.Xsl.XslCompiledTransform.Transform(IXPathNavigable input, XsltArgumentList arguments, TextWriter results)
at sftControls.SearchListLandingPage.getHtml(IXPathNavigable document, String Xsl, String headerLinks)
Opening up the IL and looking through each method suggests that the DocumentXPathNavigator => this.document is null. What's strange is that I can in no way duplicate this issue locally or on our test servers.
A little background, and this might be the key...
This is a web application(running .Net 3.5) that caches the XmlDocument that gets passed into XslCompiledTransform.Transform(). Our caching is serviced through a distributed cache server. The XmlDocument is serialized and cached so a reference to the specific object should not continue to exist. That leads me to believe that either something is explicitly preventing this.document from ever being set when the implicit XmlDocument -> DocumentXPathNavigator happens or some reference somewhere is removed while the Transform() is running. Either way I am perplexed.
So, I guess my questions are:
Has anyone else seen this before?
Does anyone know what could be causing it or how to deal with it?
Thanks
EDIT:
After further thinking about this, the only thing that would make sense is that it's a race condition. The caching appliance is somehow expiring the object while the application is using the object.
EDIT/EDIT:
.Net 3.5, the assembilies being referenced above are 2.0

In case anyone has this same exception.
We found that this exception occurs when xmldocument.HasChildNodes is false or DocumentElement is null. Verifying that the document has elements, BEFORE querying on it, resolves this.

I experienced a very similar exception inside an XslCompiledTransform:
System.Web.HttpUnhandledException (0x80004005): Exception of type 'System.Web.HttpUnhandledException' was thrown. ---> System.NullReferenceException: Object reference not set to an instance of an object.
at MS.Internal.Xml.Cache.XPathDocumentNavigator.get_NodeType()
at System.Xml.XPath.XPathNavigator.MoveToNonDescendant()
at System.Xml.Xsl.Runtime.DescendantMergeIterator.MoveNext(XPathNavigator input)
at <xsl:template name="SystemPartIncluded">(XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime, IList`1 PartNumber, XPathNavigator Description, IList`1 DescriptionOverride, IList`1 Quantity, IList`1 ExtendedPrice, String LabelWhiteSpace)
at <xsl:template name="compiler:generated"> (30)(XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime, XPathNavigator {urn:schemas-microsoft-com:xslt-debug}current, XPathNavigator isBundledWithCanvas, XPathNavigator SoftwareExtendedPrice, XPathNavigator SoftwareUnitPrice, IList`1 PixelNet, IList`1 Revision_Date, IList`1 Catalyst)
at <xsl:template name="compiler:generated"> (45)(XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime, XPathNavigator {urn:schemas-microsoft-com:xslt-debug}current, XPathNavigator SoftwareUnitPrice, XPathNavigator HardwareExtendedPrice, XPathNavigator HardwareUnitPrice, XPathNavigator PixelNetUnitPrice, XPathNavigator FusionUnitPrice)
at <xsl:template match="Quote">(XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime, XPathNavigator {urn:schemas-microsoft-com:xslt-debug}current, Double {urn:schemas-microsoft-com:xslt-debug}position, Double {urn:schemas-microsoft-com:xslt-debug}last)
at <xsl:template match="/">(XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime, XPathNavigator {urn:schemas-microsoft-com:xslt-debug}current)
at Root(XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime)
at System.Xml.Xsl.XmlILCommand.Execute(Object defaultDocument, XmlResolver dataSources, XsltArgumentList argumentList, XmlWriter writer)
at MyApp.clsXML.XslTransformationToHtml(IQEntities DC, Int32 loggedOnUserID, IXPathNavigable input, String xslStylesheetFilePath, XsltArgumentList xslArgs, String XmlRepositoryVersion) in MyApp\App_Data\Static Classes\xml_routines.cs:line 510
at MyApp.clsQuote.RenderOneQuote(IQEntities DC, Int32 loggedOnUserID, Int32 quoteID, Boolean booPrintFormat, Boolean booDiscount_Prices, Boolean booIs_Super_Administrator, Boolean isHostRep, String strOrganization_Table_Caption, Boolean showBottomLineOnly) in MyApp\App_Data\Static Classes\quote_routines.cs:line 476
at MyApp.Pages.AJAX.BrowseQuotes.RenderSelectedQuote(Int32 quoteID) in MyApp\Pages\AJAX\BrowseQuotes.aspx.cs:line 66
at MyApp.Pages.AJAX.BrowseQuotes.ucQuotePicker1_OnQuoteSelectedEvent(Object sender, Int32 quoteID) in b:\DotNet\IQ\WinMetrics.IQ\Pages\AJAX\BrowseQuotes.aspx.cs:line 56
at System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
at System.Web.UI.Page.HandleError(Exception e)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
at System.Web.UI.Page.ProcessRequest()
at System.Web.UI.Page.ProcessRequest(HttpContext context)
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
Like you, we were passing an XPathDocument to the Xsl transform and the XPathDocument was being retrieved from distributed cache. Unfortunately, an XPathDocument is not serializable and therefore unusable after it is retrieved from the distributed cache. We fixed the problem by using an XmlDocument instead of an XPathDocument and using SerializedXmlDocument to serialize and deserialize it as follows:
public static XmlDocument CachedXmlDoc
{
get
{
lock (_locker)
{
// maintain the Part XmlDocument in the distributed cache so that it can be reset from any production instance
SerializedXmlDocument serializedXmlDoc = Reliable.Application[Global.CACHED_XML_DOC] as SerializedXmlDocument;
XmlDocument cachedDoc = (serializedXmlDoc == null) ? null : serializedXmlDoc.XmlDocument;
if (cachedDoc == null)
{
cachedDoc = myController.CreateXmlDoc();
Reliable.Application[Global.CACHED_XML_DOC] = new SerializedXmlDocument(cachedDoc);
}
return cachedDoc;
}
}
set
{
lock (_locker)
{
if (value == null)
{
Reliable.Application[Global.CACHED_XML_DOC] = null;
}
else
{
throw new Exception("Tried to set the cached XmlDocument to something other than null.");
}
}
}
}

Related

Unable to serialize the session state 'System.Web.SessionState.HttpSessionState'

Getting "Unable to serialize the session state" error, where I'm trying to store HttpSessionState object in Session. Below is my code which is causing issue. Since HttpSessionState is not serializable class and trying to put into session.
Public Shared Property CurrentSession As HttpSessionState
Get
Return TryCast(HttpContext.Current.Session("CurrentSessionObject"), HttpSessionState)
End Get
Set(value As HttpSessionState)
HttpContext.Current.Session("CurrentSessionObject") = value
End Set
End Property
Server Error in '/' Application.
Unable to serialize the session state. In 'StateServer' and 'SQLServer' mode, ASP.NET will serialize the session state objects, and as a result non-serializable objects or MarshalByRef objects are not permitted. The same restriction applies if similar serialization is done by the custom session state store in 'Custom' mode.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Web.HttpException: Unable to serialize the session state. In 'StateServer' and 'SQLServer' mode, ASP.NET will serialize the session state objects, and as a result non-serializable objects or MarshalByRef objects are not permitted. The same restriction applies if similar serialization is done by the custom session state store in 'Custom' mode.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[SerializationException: Type 'System.Web.SessionState.HttpSessionState' in Assembly 'System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' is not marked as serializable.]
System.Runtime.Serialization.FormatterServices.InternalGetSerializableMembers(RuntimeType type) +12672675
System.Runtime.Serialization.<>c__DisplayClass9_0.b__0(MemberHolder _) +42
System.Collections.Concurrent.ConcurrentDictionary2.GetOrAdd(TKey key, Func2 valueFactory) +73
System.Runtime.Serialization.FormatterServices.GetSerializableMembers(Type type, StreamingContext context) +186
System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitMemberInfo() +166
System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter, SerializationBinder binder) +187
System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.Serialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter, SerializationBinder binder) +53
System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(Object graph, Header[] inHeaders, __BinaryWriter serWriter, Boolean fCheck) +571
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph, Header[] headers, Boolean fCheck) +137
System.Web.Util.AltSerialization.WriteValueToStream(Object value, BinaryWriter writer) +1644
[HttpException (0x80004005): Unable to serialize the session state. In 'StateServer' and 'SQLServer' mode, ASP.NET will serialize the session state objects, and as a result non-serializable objects or MarshalByRef objects are not permitted. The same restriction applies if similar serialization is done by the custom session state store in 'Custom' mode.]
System.Web.Util.AltSerialization.WriteValueToStream(Object value, BinaryWriter writer) +1733
System.Web.SessionState.SessionStateItemCollection.WriteValueToStreamWithAssert(Object value, BinaryWriter writer) +36
System.Web.SessionState.SessionStateItemCollection.Serialize(BinaryWriter writer) +652
System.Web.SessionState.SessionStateUtility.Serialize(SessionStateStoreData item, Stream stream) +246
System.Web.SessionState.SessionStateUtility.SerializeStoreData(SessionStateStoreData item, Int32 initialStreamSize, Byte[]& buf, Int32& length, Boolean compressionEnabled) +65
System.Web.SessionState.SqlSessionStateStore.SetAndReleaseItemExclusive(HttpContext context, String id, SessionStateStoreData item, Object lockId, Boolean newItem) +138
System.Web.SessionState.SessionStateModule.OnReleaseState(Object source, EventArgs eventArgs) +589
System.Web.SessionState.SessionStateModule.OnEndRequest(Object source, EventArgs eventArgs) +9947060
System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +144
System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step) +50
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +73
You need to mark the type/s your pushing in to SessionState as serializable, with the [Serializable] attribute.

ASP.NET MVC 4, The "WebSecurity.InitializeDatabaseConnection" method can be called only once

I am developing a code first web app in Visual Studio 2012 Express.
I use this connection string in the web.config:
<add name="myContext" connectionString="Data Source=.;Integrated Security=True;Initial Catalog=cityKingMVC4" providerName="System.Data.SqlClient" />
I am using SimpleMembership.
I am trying to seed 1 administrator from Filters/InitializeSimpleMembershipAttribute.cs:
...
WebSecurity.InitializeDatabaseConnection("myContext", "Users", "UserId", "Email", autoCreateTables: true);
// A: Create Admin user
if (!WebSecurity.ConfirmAccount("admin#mydom.com"))
{
WebSecurity.CreateUserAndAccount("admin#mydom.com", "password");
}
// B: Create admin role if not exist
if (!Roles.RoleExists("Administrator"))
{
Roles.CreateRole("Administrator");
Roles.AddUserToRole("admin#mydom.com", "Administrator");
}
If I comment A & B it doesn't crash. If I don't I get this:
The "WebSecurity.InitializeDatabaseConnection" method can be called only once.
If I debug and put a breakpoint on 'WebSecurity.InitializeDatabaseConnection' - it only calls it once and there is no other code calling WebSecurity.InitializeDatabaseConnection anywhere.
If I debug - it crashes on a different line higher up in the file (standard SimpleAuthentication file):
LazyInitializer.EnsureInitialized(ref _initializer, ref _isInitialized, ref _initializerLock);
with this error:
Exception has been thrown by the target of an invocation.
Stack Trace:
[InvalidOperationException: The "WebSecurity.InitializeDatabaseConnection" method can be called only once.]
WebMatrix.WebData.WebSecurity.InitializeMembershipProvider(SimpleMembershipProvider simpleMembership, DatabaseConnectionInfo connect, String userTableName, String userIdColumn, String userNameColumn, Boolean createTables) +87978
WebMatrix.WebData.WebSecurity.InitializeProviders(DatabaseConnectionInfo connect, String userTableName, String userIdColumn, String userNameColumn, Boolean autoCreateTables) +86
myapPMVC4.Filters.SimpleMembershipInitializer..ctor() in c:\Users\name\Documents\Visual Studio 2012\Projects\myapPMVC4\myapPMVC4\Filters\InitializeSimpleMembershipAttribute.cs:43
[InvalidOperationException: The ASP.NET Simple Membership database could not be initialized. For more information, please see http://go.microsoft.com/fwlink/?LinkId=256588]
myapPMVC4.Filters.SimpleMembershipInitializer..ctor() in c:\Users\name\Documents\Visual Studio 2012\Projects\myapPMVC4\myapPMVC4\Filters\InitializeSimpleMembershipAttribute.cs:88
[TargetInvocationException: Exception has been thrown by the target of an invocation.]
System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck) +0
System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) +159
System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) +256
System.Activator.CreateInstance(Type type, Boolean nonPublic) +127
System.Activator.CreateInstance(Type type) +11
System.Threading.LazyHelpers`1.ActivatorFactorySelector() +72
System.Threading.LazyInitializer.EnsureInitializedCore(T& target, Boolean& initialized, Object& syncLock, Func`1 valueFactory) +241
System.Threading.LazyInitializer.EnsureInitialized(T& target, Boolean& initialized, Object& syncLock) +139
myapPMVC4.Filters.InitializeSimpleMembershipAttribute.OnActionExecuting(ActionExecutingContext filterContext) in c:\Users\name\Documents\Visual Studio 2012\Projects\myapPMVC4\myapPMVC4\Filters\InitializeSimpleMembershipAttribute.cs:22
System.Web.Mvc.Async.AsyncControllerActionInvoker.InvokeActionMethodFilterAsynchronously(IActionFilter filter, ActionExecutingContext preContext, Func`1 nextInChain) +145
System.Web.Mvc.Async.AsyncControllerActionInvoker.InvokeActionMethodFilterAsynchronously(IActionFilter filter, ActionExecutingContext preContext, Func`1 nextInChain) +840201
System.Web.Mvc.Async.<>c__DisplayClass37.<BeginInvokeActionMethodWithFilters>b__31(AsyncCallback asyncCallback, Object asyncState) +266
System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +146
System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate`1 endDelegate, Object tag, Int32 timeout) +202
System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate`1 endDelegate, Object tag) +112
System.Web.Mvc.Async.<>c__DisplayClass25.<BeginInvokeAction>b__1e(AsyncCallback asyncCallback, Object asyncState) +839055
System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +146
System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate`1 endDelegate, Object tag, Int32 timeout) +166
System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate`1 endDelegate, Object tag) +27
System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__17(AsyncCallback asyncCallback, Object asyncState) +50
System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +146
System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate`1 endDelegate, Object tag, Int32 timeout) +166
System.Web.Mvc.Controller.BeginExecuteCore(AsyncCallback callback, Object state) +826145
System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +146
System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate`1 endDelegate, Object tag, Int32 timeout) +166
System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate endDelegate, Object tag) +27
System.Web.Mvc.Controller.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) +401
System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__2(AsyncCallback asyncCallback, Object asyncState) +786250
System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +146
System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate`1 endDelegate, Object tag, Int32 timeout) +166
System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate endDelegate, Object tag) +27
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +343
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +12550291
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +288
What's going on?
Thx
using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Threading;
using System.Web.Mvc;
using WebMatrix.WebData;
using System.Web.Security;
using myapPMVC4.Models;
namespace myapPMVC4.Filters
{
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public sealed class InitializeSimpleMembershipAttribute : ActionFilterAttribute
{
private static SimpleMembershipInitializer _initializer;
private static object _initializerLock = new object();
private static bool _isInitialized;
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
LazyInitializer.EnsureInitialized(ref _initializer, ref _isInitialized, ref _initializerLock);
}
private class SimpleMembershipInitializer
{
public SimpleMembershipInitializer()
{
Database.SetInitializer<UsersContext>(null);
try
{
using (var context = new UsersContext())
{
if (!context.Database.Exists())
{
// Create the SimpleMembership database without Entity Framework migration schema
((IObjectContextAdapter)context).ObjectContext.CreateDatabase();
}
}
//WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);
if (!WebSecurity.Initialized)
{
WebSecurity.InitializeDatabaseConnection("myapPMVC4DBContext", "Users", "UserId", "Email", autoCreateTables: true);
}
// Create Admin user
if (!WebSecurity.ConfirmAccount("admin#myapp.com"))
{
//WebSecurity.CreateUserAndAccount("admin", "pass", new { email = "a#b.com" });
WebSecurity.CreateUserAndAccount("admin#myapp.com", "pass");
}
// Create admin role if not exist
if (!Roles.RoleExists("Administrator"))
{
Roles.CreateRole("Administrator");
Roles.AddUserToRole("admin#myapp.com", "Administrator");
}
}
catch (Exception ex)
{
throw new InvalidOperationException("The ASP.NET Simple Membership database could not be initialized. For more information, please see http://go.microsoft.com/fwlink/?LinkId=256588", ex);
}
}
}
}
}
The problem ...
is that InitializeDatabaseConnection calls WebSecurity.InitializeProviders internally, and this method is not thread-safe, then combine this with the fact that WebSecurity typically needs initializing from various places. This has implications as web applications are inherently multi-threaded environments ... and WebSecurity.Initialized and WebSecurity.InitializeDatabaseConnection are not thread-safe when used together - they create a typical race condition.
Seeding (for migrations) means that your WebSecurity can be initialized more than once as you may also need to initialise it in Global.asax.cs for deployments with seeding turned off, and your InitializeSimpleMembershipAttribute can potentially be called multiple times simultaneusly by http requests in live deployments etc.
Putting the initialisation code in multiple places also breaks your DRYness
The solution ...
Make sure your init calls are thread-safe, and only occur once per instance of an AppDomain. Use a thread-safe singleton class to do this; and reduce duplication of code.
Call the singleton's EnsureInitialize method from any/all of the following, as appropriate for your application:
Global.asax.cs (Application_Start method before anything else)
Migrations\Configuration.cs Seed method (before creating the users)
Filters\InitializeSimpleMembershipAttribute.cs (in the SimpleMembershipInitializer constructor after the context is initialised)
Here is a simple example singleton:
// Call this with WebSecurityInitializer.Instance.EnsureInitialize()
public class WebSecurityInitializer {
private WebSecurityInitializer() { }
public static readonly WebSecurityInitializer Instance = new WebSecurityInitializer();
private bool isNotInit = true;
private readonly object SyncRoot = new object();
public void EnsureInitialize() {
if (isNotInit) {
lock (this.SyncRoot) {
if (isNotInit) {
isNotInit = false;
WebSecurity.InitializeDatabaseConnection("MyContextName",
userTableName: "UserProfile", userIdColumn: "UserId", userNameColumn: "UserName",
autoCreateTables: true);
}
}
}
}
}
Once I had done this, my case of the error you mention disappeared, not to be seen again.
Footnotes
The singleton class also keeps your code DRY, which is especially useful during early stage app development if you need to later change the configuration of your WebSecurity.InitializeDatabaseConnection as it will only be in one place (end edit)
I also keep the SimpleMembershipInitializer clean, and instead seed my users along with the common seeding in Migrations\Configuration.cs Seed method. This helps with testability of seeding through my migrations by keeping everything in one place. I use unit testing to make sure we can always go up and down the migrations tree, so this makes it easier to do that.
However the location of your seeding code won't matter, it is more just making sure that, globally, you have initialised WebSecurity only once within your AppDomain, and that any call to InitializeDatabaseConnection is thread-safe.
Add this code to Global.asax.cs. This will makes sure that your database is always Initialized before any other executions. Also make sure its the first registration in Application_Start()
if (!WebSecurity.Initialized)
WebSecurity.InitializeDatabaseConnection("DefaultConnection",
"UserProfile", "UserId", "UserName", autoCreateTables: true);
Get rid of Filters/InitializeSimpleMembershipAttribute.cs or just comment the code inside, in case you would like to go back to it.
Remove [InitializeSimpleMembership] at the top of AccountController.cs
Also if you haven't already enabled migrations, i would encourage you do so. That way, you can do your seeds in Configuration.cs created inside the Migration folder when you run Enable-Migrations
If it is already initialized then make sure your first call:
if (!WebSecurity.Initialized)
{
// Do the initialization first.
}
// The rest of the code
This way you'll be sure that you don't repeat the initialization process.
Do you also have the InitializeSimpleMembership attribute on your AccountController class? (this is the default). If so, you are initializing twice.
To make sure that the WebSecurity.InitializeDatabaseConnection is not called twice just use the WebSecurity.Initialized to check if it was already called. This blog post provides detailed instructions on seeding and customizing SimpleMembership. There is a series in this blog on using SimpleMembership and I would also recommend looking at decoupling SimpleMembership from your ASP.NET MVC Application. You can get the complete source code for these examples here.

ArgumentException: Unterminated string passed in

I recently encountered this error in one of the pages in my site.
[ArgumentException: Unterminated string passed in. (26): {"selectedIndexes":["0"],"]
at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeString()
at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeMemberName()
at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeDictionary(Int32\ depth)
at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32\ depth)
at System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String\ input, Int32 depthLimit, JavaScriptSerializer serializer)
at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer\ serializer, String input, Type type, Int32 depthLimit)
at System.Web.Script.Serialization.JavaScriptSerializer.DeserializeObject(String\ input)
at Telerik.Web.UI.RadCompositeDataBoundControl.LoadPostData(String postDataKey,\ NameValueCollection postCollection)
at Telerik.Web.UI.RadCompositeDataBoundControl.System.Web.UI.IPostBackDataHandler.LoadPostData(String\ postDataKey, NameValueCollection postCollection)
at System.Web.UI.Page.ProcessPostData(NameValueCollection postData, Boolean\ fBeforeLoad)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint,\ Boolean includeStagesAfterAsyncPoint)
[HttpUnhandledException: Exception of type 'System.Web.HttpUnhandledException' was thrown.]
at System.Web.UI.Page.HandleError(Exception e)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint,\ Boolean includeStagesAfterAsyncPoint)
at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint,\ Boolean includeStagesAfterAsyncPoint)
at System.Web.UI.Page.ProcessRequest()
at System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context)
at System.Web.UI.Page.ProcessRequest(HttpContext context)
at ASP.pages_appeal_appealaction_aspx.ProcessRequest(HttpContext context)
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
What could be the reason for this exception ? Any ideas how to solve it ??
Yep. This error happens when the ASP.NET system receives a request, routes it to your handler and ModelBinder takes over to map values passed in to parameters in your handler. When one of those parms is a string and that string is started but not terminated, it throws the System.ArgumentException from the System.String.DeserializeString() method.
You have two options.
Fix the string data on the browser in JavaScript before you send it. (duh).
Create a custom Model Binder to handle the exception gracefully and send back a 500-series HTTP error or better yet, fix the data in a predictable way. That fixing is not always possible like in a sound file, image, movie or other base64-encoded blob.
Here's how to do #2 - Creating a custom model binder.

Checkbox Helper: Odd string to bool conversion error

I've a web using asp.net MVC 3 with razor.
In one view I'm having an odd error with the checkbox helper.
Here is the razor code:
#Html.CheckBox("rememberPassword", Model.RememberPassword, new { tabindex = "4", style = "width:15px" })
The property in the model (which I set to true in the Model constructor):
public bool RememberPassword { get; set; }
And the logged error:
2012-04-13 01:20:33.334 [13 ] Error - Reference: 0413-012033-334 - Global site error, page: /es/login
System.InvalidOperationException: The parameter conversion from type 'System.String' to type 'System.Boolean' failed. See the inner exception for more information. ---> System.FormatException: -1' is not a valid value for Boolean. ---> System.FormatException: String was not recognized as a valid Boolean.
at System.Boolean.Parse(String value)
at System.ComponentModel.BooleanConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
--- End of inner exception stack trace ---
at System.ComponentModel.BooleanConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
at System.Web.Mvc.ValueProviderResult.ConvertSimpleType(CultureInfo culture, Object value, Type destinationType)
--- End of inner exception stack trace ---
at System.Web.Mvc.ValueProviderResult.ConvertSimpleType(CultureInfo culture, Object value, Type destinationType)
at System.Web.Mvc.ValueProviderResult.UnwrapPossibleArrayType(CultureInfo culture, Object value, Type destinationType)
at System.Web.Mvc.ValueProviderResult.ConvertTo(Type type, CultureInfo culture)
at System.Web.Mvc.HtmlHelper.GetModelStateValue(String key, Type destinationType)
at System.Web.Mvc.Html.InputExtensions.InputHelper(HtmlHelper htmlHelper, InputType inputType, ModelMetadata metadata, String name, Object value, Boolean useViewData, Boolean isChecked, Boolean setId, Boolean isExplicitValue, IDictionary`2 htmlAttributes)
at System.Web.Mvc.Html.InputExtensions.CheckBoxHelper(HtmlHelper htmlHelper, ModelMetadata metadata, String name, Nullable`1 isChecked, IDictionary`2 htmlAttributes)
at System.Web.Mvc.Html.InputExtensions.CheckBox(HtmlHelper htmlHelper, String name, Boolean isChecked, Object htmlAttributes)
at ASP._Page_Views_Login_Index_cshtml.Execute() in d:\[...]\Views\Login\Index.cshtml:line 49
at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
at System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
at System.Web.WebPages.StartPage.RunPage()
at System.Web.WebPages.StartPage.ExecutePageHierarchy()
at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
at System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance)
at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer)
at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult)
at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19()
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)
at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass1c.<>c__DisplayClass1e.<InvokeActionResultWithFilters>b__1b()
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult)
at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)
Why is this happening?
Note:
It keeps getting odder and odder. As magically as the error started appearing (in a productive site some day without any updates or releases) not it has stopped. It's been three days without the error. However, I'd still like to know why was it.
It had nothing to do with the way of creating the checkbox. In some very odd scenarios (which I had forgotten at all), this forms may be submitted (from other sites) without this parameter. In this cases the bool field was trying to obtained from the string "-1".
One way of resolving this issue (that I have discovered to be a good practice) is to avoid "not-nullable" fields in the Model.
Just try this instead
Html.CheckBoxFor(model => model.RememberPassword , chkHtmlAttributes)
And define the chkHtmlAttributes to these
tabindex = "4", style = "width:15px"

ASP.NET MVC 3 HtmlHelper Exception does not recognize ModelMetadata on inherited interface

After upgrading to MVC 3 RTM I get an exception where it previously worked.
Here is the scenario. I have several objects that use the same underlying interfaces IActivity and IOwned.
IActivity implements IOwned (another interface)
public interface IActivity:IOwned {...}
public interface IOwned
{
int? AuthorId {get;set;}
}
I have a partial view that uses IActivity for reuse from other concrete partials.
Here is the definition of the Activity Partial.
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IActivity>" %>
<%: Html.HiddenFor(item => item.AuthorId) %>
However, it throws an exception. It can not find AuthorId in the ModelMetadata.
I am guessing that in the previous version it looked at the interfaces IActivity implemented.
Any ideas, suggestions, short of duplicating similar interfaces everywhere?
Copied the stack trace below.
[ArgumentException: The property IActivity.AuthorId could not be found.]
System.Web.Mvc.AssociatedMetadataProvider.GetMetadataForProperty(Func`1 modelAccessor, Type containerType, String propertyName) +498313
System.Web.Mvc.ModelMetadata.GetMetadataFromProvider(Func`1 modelAccessor, Type modelType, String propertyName, Type containerType) +101
System.Web.Mvc.ModelMetadata.FromLambdaExpression(Expression`1 expression, ViewDataDictionary`1 viewData) +393
System.Web.Mvc.Html.InputExtensions.HiddenFor(HtmlHelper`1 htmlHelper, Expression`1 expression, IDictionary`2 htmlAttributes) +57
System.Web.Mvc.Html.InputExtensions.HiddenFor(HtmlHelper`1 htmlHelper, Expression`1 expression) +51
ASP.views_shared_activity_ascx.__Render__control1(HtmlTextWriter __w, Control parameterContainer) in c:\Users\...\Documents\Visual Studio 2010\Projects\ngen\trunk\...\Views\Shared\Activity.ascx:3
System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +109
System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +8
System.Web.UI.Control.Render(HtmlTextWriter writer) +10
System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +100
System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +208
System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +8
System.Web.UI.Page.Render(HtmlTextWriter writer) +29
System.Web.Mvc.ViewPage.Render(HtmlTextWriter writer) +43
System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +100
System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3060
From the MVC team:
Unfortunately, the code was actually
exploiting a bug that was fixed, where
the container of an expression for
ModelMetadata purposes was
inadvertently set to the declaring
type instead of the containing type.
This bug had to be fixed because of
the needs of virtual properties and
validation/model metadata.
Having interface-based models is not
something we encourage (nor, given the
limitations imposed by the bug fix,
can realistically support). Switching
to abstract base classes would fix the
issue.
There has been a breaking change/bug in ASP.NET MVC 3 in the System.Web.Mvc.ModelMetadata. FromLambdaExpression method which explains the exception you are getting:
ASP.NET MVC 2.0:
...
case ExpressionType.MemberAccess:
{
MemberExpression body = (MemberExpression) expression.Body;
propertyName = (body.Member is PropertyInfo) ? body.Member.Name : null;
containerType = body.Member.DeclaringType;
flag = true;
break;
}
...
ASP.NET MVC 3.0
...
case ExpressionType.MemberAccess:
{
MemberExpression body = (MemberExpression) expression.Body;
propertyName = (body.Member is PropertyInfo) ? body.Member.Name : null;
containerType = body.Expression.Type;
flag = true;
break;
}
...
Notice how the containerType variable is assigned a different value. So in your case in ASP.NET MVC 2.0 it was assigned the value of IOwned which is the correct declaring type of the AuthorId property whereas in ASP.NET MVC 3.0 it is assigned to IActivity and later when the framework tries to find the property it crashes.
That's the cause. As far as the resolution is concerned I would wait for some official statement from Microsoft. I can't find any relevant information about this in the Release Notes document. Is it a bug or some feature which needs to be workarounded here?
For now you could either use the non-strongly typed Html.Hidden("AuthorId") helper or specify IOwned as type for your control (I know both suck).
With thanks to Burcephal, whos answer pointed me in the right direction
You can create a MetaDataProvider which will get around this issue, the code here adds to the code in the base class checking for the property on implemented interfaces of a model which is itself an interface.
public class MyMetadataProvider
: EmptyModelMetadataProvider {
public override ModelMetadata GetMetadataForProperty(
Func<object> modelAccessor, Type containerType, string propertyName) {
if (containerType == null) {
throw new ArgumentNullException("containerType");
}
if (String.IsNullOrEmpty(propertyName)) {
throw new ArgumentException(
"The property &apos;{0}&apos; cannot be null or empty", "propertyName");
}
var property = GetTypeDescriptor(containerType)
.GetProperties().Find(propertyName, true);
if (property == null
&& containerType.IsInterface) {
property = (from t in containerType.GetInterfaces()
let p = GetTypeDescriptor(t).GetProperties()
.Find(propertyName, true)
where p != null
select p
).FirstOrDefault();
}
if (property == null) {
throw new ArgumentException(
String.Format(
CultureInfo.CurrentCulture,
"The property {0}.{1} could not be found",
containerType.FullName, propertyName));
}
return GetMetadataForProperty(modelAccessor, containerType, property);
}
}
and as above, set your provider the global.asax Application_Start
ModelMetadataProviders.Current = new MyMetaDataProvider();
f you are interrested, I have implemented a little work around for it in my app.
As I was going through the MVC source code, I found that the FromLambdaExpression method named below will be calling the MetaDataProvider which is an overridable singleton. So we could just implement that class that will actually try the inhereted intefaces if the first one does not work.
It will also go up the tree of intefaces.
public class MyMetaDataProvider : EmptyModelMetadataProvider
{
public override ModelMetadata GetMetadataForProperty(Func<object> modelAccessor, Type containerType, string propertyName)
{
try
{
return base.GetMetadataForProperty(modelAccessor, containerType, propertyName);
}
catch(Exception ex)
{
//Try to go up to type tree
var types = containerType.GetInterfaces();
foreach (var container in types)
{
if (container.GetProperty(propertyName) != null)
{
try
{
return GetMetadataForProperty(modelAccessor, container, propertyName);
}
catch
{
//This interface did not work
}
}
}
//If nothing works, then throw the exception
throw ex;
}
}
}
and then, just cange the implementation of the MetaDataProvider in the global.asax Application_Start()
ModelMetadataProviders.Current = new MyMetaDataProvider();
It is not the best code ever, but it does the job.
Try using typecast. It works for my project although resharper highlights it as redundant.
For your code the solution would be
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IActivity>" %>
<%: Html.HiddenFor(item => ((IOwned)item).AuthorId) %>
Furthering Anthony Johnston's answer, you might find that you get exceptions when using DataAnnotations, as the AssociatedValidatorProvider.GetValidatorsForProperty() method will try to use the inheriting interface as the container type rather than the base one and therefore fail to find the property again.
This is the reflected code from the GetValidatorsForProperty method (it is the second line which causes the propertyDescriptor variable to be null and thus the exception to be thrown) :
private IEnumerable<ModelValidator> GetValidatorsForProperty(ModelMetadata metadata, ControllerContext context)
{
ICustomTypeDescriptor typeDescriptor = this.GetTypeDescriptor(metadata.ContainerType);
PropertyDescriptor propertyDescriptor = typeDescriptor.GetProperties().Find(metadata.PropertyName, true);
if (propertyDescriptor != null)
{
return this.GetValidators(metadata, context, propertyDescriptor.Attributes.OfType<Attribute>());
}
else
{
object[] fullName = new object[] { metadata.ContainerType.FullName, metadata.PropertyName };
throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, MvcResources.Common_PropertyNotFound, fullName), "metadata");
}
}
If so, I believe that the following code may help, as it makes sure that the ContainerType is set to the type which the property is on, not the type of the view model.
Disclaimer: It seems to work fine, but I have not fully tested it yet so it might have undesired effects! I also understand it is not written perfectly, but I was trying to keep the format similar to the previous answer for ease of comparison. :)
public class MyMetadataProvider : DataAnnotationsModelMetadataProvider
{
public override ModelMetadata GetMetadataForProperty(
Func<object> modelAccessor, Type containerType, string propertyName)
{
if (containerType == null)
{
throw new ArgumentNullException("containerType");
}
if (String.IsNullOrEmpty(propertyName))
{
throw new ArgumentException(
"The property &apos;{0}&apos; cannot be null or empty", "propertyName");
}
var containerTypeToUse = containerType;
var property = GetTypeDescriptor(containerType)
.GetProperties().Find(propertyName, true);
if (property == null
&& containerType.IsInterface)
{
var foundProperty = (from t in containerType.GetInterfaces()
let p = GetTypeDescriptor(t).GetProperties()
.Find(propertyName, true)
where p != null
select (new Tuple<System.ComponentModel.PropertyDescriptor, Type>(p, t))
).FirstOrDefault();
if (foundProperty != null)
{
property = foundProperty.Item1;
containerTypeToUse = foundProperty.Item2;
}
}
if (property == null)
{
throw new ArgumentException(
String.Format(
CultureInfo.CurrentCulture,
"The property {0}.{1} could not be found",
containerType.FullName, propertyName));
}
return GetMetadataForProperty(modelAccessor, containerTypeToUse, property);
}
}

Resources