Controller Action ambiguity even with [HttpPost] attribute? (ASP.NET MVC4) - asp.net

So I have a "Register" view (that maps to a RegisterController), which is a simple form for people to register in my service. Given that the form's method is POST, then I can name both actions of the controller (the one that shows the form, and the one that receives its submission) the same name, but discriminate them via the [HttpPost] attribute, this way:
open System
open System.Web
open System.Web.Mvc
open System.Net
open System.Threading
open System.Configuration
[<HandleError>]
type RegisterController() =
inherit Controller()
member this.Index (guid: string) =
let model = new RegisterModel(guid)
this.View("Register", model) :> ActionResult
[<HttpPost>]
member this.Index
(guid: string, password: string, passwordRetry: string) =
if not (password.Equals(passwordRetry)) then
raise(new Exception("Passwords must be the same"))
WebDbAccess.SetNewPassword guid password
RedirectResult("/") :> ActionResult
This works perfectly.
However, I just tried to use this same technique in the Home view of my app (for the Login functionality), and it doesn't work! It throws this:
The current request for action 'Index' on controller type 'HomeController' is ambiguous between the following action methods:
System.Web.Mvc.ActionResult Index(System.String, System.String) on type FsWeb.Controllers.HomeController
System.Web.Mvc.ActionResult Index() on type FsWeb.Controllers.HomeController
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.Reflection.AmbiguousMatchException: The current request for action 'Index' on controller type 'HomeController' is ambiguous between the following action methods:
System.Web.Mvc.ActionResult Index(System.String, System.String) on type FsWeb.Controllers.HomeController
System.Web.Mvc.ActionResult Index() on type FsWeb.Controllers.HomeController
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:
[AmbiguousMatchException: The current request for action 'Index' on controller type 'HomeController' is ambiguous between the following action methods:
System.Web.Mvc.ActionResult Index(System.String, System.String) on type FsWeb.Controllers.HomeController
System.Web.Mvc.ActionResult Index() on type FsWeb.Controllers.HomeController]
System.Web.Mvc.Async.AsyncActionMethodSelector.FindAction(ControllerContext controllerContext, String actionName) +495852
System.Web.Mvc.Async.ReflectedAsyncControllerDescriptor.FindAction(ControllerContext controllerContext, String actionName) +57
System.Web.Mvc.ControllerActionInvoker.FindAction(ControllerContext controllerContext, ControllerDescriptor controllerDescriptor, String actionName) +16
System.Web.Mvc.Async.AsyncControllerActionInvoker.BeginInvokeAction(ControllerContext controllerContext, String actionName, AsyncCallback callback, Object state) +114
System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__17(AsyncCallback asyncCallback, Object asyncState) +30
System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +130
System.Web.Mvc.Controller.BeginExecuteCore(AsyncCallback callback, Object state) +382
System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +130
System.Web.Mvc.Controller.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) +317
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) +15
System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__2(AsyncCallback asyncCallback, Object asyncState) +71
System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +130
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +249
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) +50
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +16
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +301
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.17929
What's the difference between the 2 cases? I don't get it.
The HomeController's implementation is:
open System
open System.Web
open System.Web.Http
[<HandleError>]
type HomeController() =
inherit Controller()
[<HttpGet>]
member this.Index () =
this.View() :> ActionResult
[<HttpPost>]
member this.Index
(id:string, password:string) =
if (UserIsValid id password) then
RedirectResult("lobby") :> ActionResult
RedirectResult("loginError") :> ActionResult

#nemesv was right, I had to fully qualify HttpPost with System.Web.Mvc.HttpPost because otherwise it would use System.Web.Http.HttpPost!!

Related

Self-built Bonobo.Git.Server Login Error: The required anti-forgery cookie "__RequestVerificationToken" is not present

I got the below error while logging in with admin/admin after first open the Bonobo server:
500 Server Error
A server error occurred when processing your request. Please try again or contact the system administrator.
Bonobo version: V6.5.0
Server: Windows Server 2016 Standard
And I have tried serial ways but no luck:
Add <httpCookies httpOnlyCookies="true" requireSSL="true"/> in web.config
Move the folder out of C:\inetpub\wwwroot;
Re-open the browser;
After googled hours in internet, it seems that the root reason is code issue. could anyone can help me out of this?
Below is the log copied from Bonobo.Git.Server\App_Data\Logs:
2020-04-22 11:54:24.061 +08:00 [Error] 500 Exception caught in Global.asax
System.Web.Mvc.HttpAntiForgeryException (0x80004005): The required anti-forgery cookie "__RequestVerificationToken" is not present.
at System.Web.Helpers.AntiXsrf.TokenValidator.ValidateTokens(HttpContextBase httpContext, IIdentity identity, AntiForgeryToken sessionToken, AntiForgeryToken fieldToken)
at System.Web.Helpers.AntiXsrf.AntiForgeryWorker.Validate(HttpContextBase httpContext)
at System.Web.Mvc.ControllerActionInvoker.InvokeAuthorizationFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass21.<BeginInvokeAction>b__19(AsyncCallback asyncCallback, Object asyncState)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.Begin(AsyncCallback callback, Object state, Int32 timeout)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.BeginInvokeAction(ControllerContext controllerContext, String actionName, AsyncCallback callback, Object state)
at System.Web.Mvc.Controller.<BeginExecuteCore>b__1c(AsyncCallback asyncCallback, Object asyncState, ExecuteCoreState innerState)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallBeginDelegate(AsyncCallback callback, Object callbackState)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.Begin(AsyncCallback callback, Object state, Int32 timeout)
at System.Web.Mvc.Controller.BeginExecuteCore(AsyncCallback callback, Object state)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.Begin(AsyncCallback callback, Object state, Int32 timeout)
at System.Web.Mvc.Controller.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state)
at System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__4(AsyncCallback asyncCallback, Object asyncState, ProcessRequestState innerState)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallBeginDelegate(AsyncCallback callback, Object callbackState)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.Begin(AsyncCallback callback, Object state, Int32 timeout)
at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state)
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step)
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

Post error: Either BinaryRead, Form, Files, or InputStream was accessed before the internal storage

Any post in my Asp.net MVC generate this error:
Server Error in '/' Application.
Either BinaryRead, Form, Files, or InputStream was accessed before the internal storage was filled by the caller of HttpRequest.GetBufferedInputStream.
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.InvalidOperationException: Either BinaryRead, Form, Files, or InputStream was accessed before the internal storage was filled by the caller of HttpRequest.GetBufferedInputStream.
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:
[InvalidOperationException: Either BinaryRead, Form, Files, or InputStream was accessed before the internal storage was filled by the caller of HttpRequest.GetBufferedInputStream.] System.Web.HttpRequest.GetEntireRawContent() +12673515 System.Web.HttpRequest.get_InputStream() +61 System.Web.Mvc.JsonValueProviderFactory.GetValueProvider(ControllerContext controllerContext) +132 System.Web.Mvc.<>c__DisplayClassc.<GetValueProvider>b__7(ValueProviderFactory factory) +28 System.Linq.WhereSelectEnumerableIterator`2.MoveNext() +248 System.Linq.WhereSelectEnumerableIterator`2.MoveNext() +165 System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) +536 System.Linq.Enumerable.ToList(IEnumerable`1 source) +80 System.Web.Mvc.ValueProviderFactoryCollection.GetValueProvider(ControllerContext controllerContext) +343 System.Web.Mvc.ControllerBase.get_ValueProvider() +57 System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor) +81 System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext, ActionDescriptor actionDescriptor) +153 System.Web.Mvc.Async.<>c__DisplayClass25.<BeginInvokeAction>b__1e(AsyncCallback asyncCallback, Object asyncState) +839919 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) +827009 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) +787114 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() +12622419 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +288
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18213
Full error details:
System.InvalidOperationException
Either BinaryRead, Form, Files, or InputStream was accessed before the internal storage was filled by the caller of HttpRequest.GetBufferedInputStream.
System.InvalidOperationException: Either BinaryRead, Form, Files, or InputStream was accessed before the internal storage was filled by the caller of HttpRequest.GetBufferedInputStream.
at System.Web.HttpRequest.GetEntireRawContent()
at System.Web.HttpRequest.get_InputStream()
at System.Web.HttpRequestWrapper.get_InputStream()
at System.Web.Mvc.JsonValueProviderFactory.GetDeserializedObject(ControllerContext controllerContext)
at System.Web.Mvc.JsonValueProviderFactory.GetValueProvider(ControllerContext controllerContext)
at Castle.Proxies.Invocations.ValueProviderFactory_GetValueProvider.InvokeMethodOnTarget()
at Castle.DynamicProxy.AbstractInvocation.Proceed()
at Glimpse.Core.Extensibility.CastleInvocationToAlternateMethodContextAdapter.Proceed()
at Glimpse.Core.Extensions.AlternateMethodContextExtensions.TryProceedWithTimer(IAlternateMethodContext context, TimerResult& timerResult)
at Glimpse.Core.Extensibility.AlternateMethod.NewImplementation(IAlternateMethodContext context)
at Glimpse.Core.Extensibility.AlternateTypeToCastleInterceptorAdapter.Intercept(IInvocation invocation)
at Castle.DynamicProxy.AbstractInvocation.Proceed()
at Castle.Proxies.ValueProviderFactoryProxy.GetValueProvider(ControllerContext controllerContext)
at System.Web.Mvc.ValueProviderFactoryCollection.<>c__DisplayClassc.<GetValueProvider>b__7(ValueProviderFactory factory)
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at System.Web.Mvc.ValueProviderFactoryCollection.GetValueProvider(ControllerContext controllerContext)
at System.Web.Mvc.ControllerBase.get_ValueProvider()
at System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor)
at System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext, ActionDescriptor actionDescriptor)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass25.<BeginInvokeAction>b__1e(AsyncCallback asyncCallback, Object asyncState)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.BeginInvokeAction(ControllerContext controllerContext, String actionName, AsyncCallback callback, Object state)
at System.Web.Mvc.Controller.<>c__DisplayClass1d.<BeginExecuteCore>b__17(AsyncCallback asyncCallback, Object asyncState)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout)
at System.Web.Mvc.Controller.BeginExecuteCore(AsyncCallback callback, Object state)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout)
at System.Web.Mvc.Controller.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state)
at System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state)
at System.Web.Mvc.MvcHandler.<>c__DisplayClass8.<BeginProcessRequest>b__2(AsyncCallback asyncCallback, Object asyncState)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout)
at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state)
at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state)
at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData)
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
Route config:
Default
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "home", action = "index", id = UrlParameter.Optional },
namespaces: new string[] { "CreditoImobiliarioBB.Web.Controllers" }
);
}
Areas
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Usuario_default"
,"usuario/{controller}/{action}/{id}"
,new { controller="home", action = "index", id = UrlParameter.Optional }
, namespaces: new string[] { "CreditoImobiliarioBB.Web.Areas.Usuario.Controllers" }
);
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
name: "Relatorios",
url: "{controller}/{id}/relatorio/{action}",
defaults: new { id = 1 },
constraints: new { id = #"^\d+$" },
namespaces: new string[] { "CreditoImobiliarioBB.Web.Areas.Relatorios" }
);
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
name: "Documentos",
url: "{controller}/{id}/documento/{action}",
defaults: null,
constraints: new { id = #"^\d+$" }
);
}
Just posting the findings here - as a summary of the chat - since others may get this error. Don't hand out bounty, since I didn't actually find the solution. ;-)
The error itself is a result of trying to access BinaryRead, Form, Files or InputStream after something else has already accessed the request entity body stream (in other words, the content of the request) using a different method.
These "different methods" seem to include GetBufferedInputStream and GetBufferlessInputStream. These methods appear (not sure) to all be mutually exclusive. In other words, for a single request, you can only use one of:
BinaryRead(), Form, Files, InputStream (these will "cooperate" with each other),
GetBufferedInputStream(), or
GetBufferlessInputStream()
GetBufferedInputStream should cooperate with the "classic" access methods, according to the documentation - in fact, that's its only difference from GetBufferlessInputStream. Nonetheless, in this case, the error stems from GetBufferedInputStream() being used, followed by InputStream.
The different methods are (inadequately) described in the documentation for the ReadEntityBodyMode property and its enum type.
A recent commit to the ASP.NET WebAPI changed its method of reading the body entity to GetBufferedInputStream() where before it used request.InputStream.
This appears to conflict with MVC's JsonValueProviderFactory.GetDeserializedObject which uses the "classic" InputStream property to read the entity body. If we take the error message at face value, GetBufferedInputStream() has already been called, but it isn't done reading when InputStream is accessed, so InputStream hasn't been filled yet.
Need to do more testing, but a temporary fix (found by #KiranChalla) is to revert to a build from before the above-mentioned commit.
We've reverted to using request.InputStream in Web API due to this problem (commit 9c76bb9090490541a453fcead75485ea50b88022).
JimmiTh's answer correctly notes a discrepancy between the initial release notes and the implementation. As implied in the later documentation mentioned above (though it is not particularly clear), request.GetBufferlessInputStream(), request.GetBufferedInputStream() and request.InputStream (along with Form, Files, etc.) are three mutually exclusive methods of request body access.
It appears that, contrary to the initial release notes, request.GetBufferedInputStream() is not currently usable unless no other callers for the same request use any of InputStream, Form, Files, etc. I'm also following up to see if something better can be done compatibility between these two methods.
David
An update after talking with the product team:
Calling request.GetBufferedInputStream() or request.GetBufferlessInputStream() means "I'm handling the stream explicitly myself; take me into advanced mode where automatic stream processing is disabled." However, in the case of buffered only, the members that normally rely on automatic stream processing (Form, Files, InputStream, etc.) are re-enabled after the buffered stream has been fully read.
So, as the exception message hints, touching any of those properties before finishing reading the stream will fail:
public ActionResult Index()
{
Request.GetBufferedInputStream();
var ignore = Request.InputStream;
return View();
}
But doing so after finishing reading the stream will succeed:
public ActionResult Index()
{
var stream = Request.GetBufferedInputStream();
new StreamReader(stream).ReadToEnd();
var ignore = Request.InputStream;
return View();
}
The documentation isn't particularly clear here; I'll ask the product team if they can improve it.
We're planning on switching back to GetBufferedInputStream in Web API; we'll just make sure it plays better with MVC in terms of when this call happens relative to consuming the stream and MVC executing.
I had the same issue when uploading files via Web API. Apparently "maximum request length exceeded" was hiding beneath this error, so increasing maxRequestLength in web.config solved it in my case.
Today I learned that if you are using WebApi and your stream is a System.Net.Http.StreamContent.ReadOnlyStream and you try and read 0 bytes from it (stream.Read(buffer, 0, 0)), it will throw a System.InvalidOperationException, which when caught in a try/catch will show up as an exception with the message "Either BinaryRead, Form, Files, or InputStream was accessed before the internal storage was filled by the caller of HttpRequest.GetBufferedInputStream."
I realize this seems silly, but you can do the same thing on a memory stream or file stream and no exception is thrown and in my case it was coming from MsgPackCli, hopefully this saves someone 4 hours.

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.

What does a stacktrace mean when there are two of them?

I have an MVC application and it has essentially two stacktraces, im wondering why there are two of them though?..
[MissingMethodException: No parameterless constructor defined for this object.]
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) +98
System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean skipCheckThis, Boolean fillCache) +241
System.Activator.CreateInstance(Type type, Boolean nonPublic) +69
System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +67
[InvalidOperationException: An error occurred when trying to create a controller of type 'PteDotNet.Web.Controllers.BlogController'. Make sure that the controller has a parameterless public constructor.]
System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +182
System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType) +80
System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName) +74
System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory) +232
System.Web.Mvc.<>c__DisplayClass6.<BeginProcessRequest>b__2() +49
System.Web.Mvc.<>c__DisplayClassb`1.<ProcessInApplicationTrust>b__a() +13
System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f) +7
System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +22
System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Func`1 func) +124
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +98
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) +50
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +16
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8970356
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184
One is a stacktrace of an inner exception.
It looks like you first got a MissingMethodException, with reason
No parameterless constructor defined for this object.
That caused an InvalidOperationException, with reason
An error occurred when trying to create a controller of type 'PteDotNet.Web.Controllers.BlogController'. Make sure that the controller has a parameterless public constructor.
So apparently your BlogController needs a parameterless constructor.
I'am not completely sure, but It looks like the Root Cause:
The MissingMethodException is fired first, then the InvalidOperationException gets fired, because of the first Exceptin
The InvalidOperationException is the exception thrown, but it is thrown as a result of a MissingMethodException and this exception is included as the InnerException of the InvalidOperationException. The InvalidOperationException explains what went wrong but looking at the InnerException you can see where the failure was triggered in the first place.
When you evaluate ToString() on an exception with an inner exception you get stack traces of all nested inner exceptions to help you understand the problem. A common mistake is to use ex.Message + ex.StackTrace for diagnostics purposes but you loose information about inner exceptions. Unless you want to hide this valuable information you should always use ex.ToString() when logging exceptions.
In the MVC framework there is code similar to this:
try {
// Will throw MissingMethodException if there is no parameterless constructor.
var controller = Activator.CreateInstance(type);
...
}
catch (MissingMethodException ex) {
var message = String.Format("An error occurred when trying to create a controller of type '{0}'. Make sure that the controller has a parameterless public constructor.", type);
throw new InvalidOperationException(message, ex);
}
So instead of getting a MissingMethodException which might confuse you, you get an InvalidOperationException with a nice explanation. This is an example of best practice when it comes to exception handling.

Error Updating Dynamic Entity When Setting Property Value of Type PickList

I've added a custom attribute ("custom_contacttype") to the Contact entity. This attribute is of type picklist which is comprised of seven values. I'm developing using Advanced Developer Extensions for Microsoft Dynamics CRM against CRM 4.0. After I set the value for this attribute and call SaveChanges() I get "Object reference not set to an instance of an object." error. I've been battling this one for a while. What am I doing wrong? Below is my code:
var crm = new CrmDataContext(context.Connection);
var saveContact = crm.GetEntities("contact").Where(p => p.GetPropertyValue<Guid> ("contactid") == contact.Id.Value).Single();
saveContact.SetPropertyValue("custom_contacttype", 2, typeof(Picklist));
crm.UpdateObject(saveContact);
crm.SaveChanges();
Trace Log from CRM Server:
[2011-07-01 16:39:33.7] Process: w3wp |Organization:f827deb3-c6cc-df11-bc07-005056887b79 |Thread: 8 |Category: Platform.Sdk |User: 822138f1-c574-e011-9dca-005056887b79 |Level: Error | PluginStep.Execute
at PluginStep.Execute(PipelineExecutionContext context)
at Pipeline.Execute(PipelineExecutionContext context)
at MessageProcessor.Execute(PipelineExecutionContext context)
at InternalMessageDispatcher.Execute(PipelineExecutionContext context)
at ExternalMessageDispatcher.Execute(String messageName, Int32 primaryObjectTypeCode, Int32 secondaryObjectTypeCode, PropertyBag fields, CorrelationToken correlationToken, CallerOriginToken originToken, UserAuth userAuth, Guid callerId)
at CrmServiceInternal.Update(String namespaceName, BusinessEntityBase entity, CorrelationToken correlationToken, CallerOriginToken originToken, UserAuth userAuth, Guid callerId)
at CrmService.Update(BusinessEntity entity)
at RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
at RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
at RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at LogicalMethodInfo.Invoke(Object target, Object[] values)
at WebServiceHandler.Invoke()
at WebServiceHandler.CoreProcessRequest()
at SyncSessionlessHandler.ProcessRequest(HttpContext context)
at CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
at ApplicationStepManager.ResumeSteps(Exception error)
at HttpApplication.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData)
at HttpRuntime.ProcessRequestInternal(HttpWorkerRequest wr)
at HttpRuntime.ProcessRequestNoDemand(HttpWorkerRequest wr)
at ISAPIRuntime.ProcessRequest(IntPtr ecb, Int32 iWRType)
Web Service Plug-in failed in SdkMessageProcessingStepId: {27DF4121-19BC-DF11-A90E-005056887B79}; EntityName: contact; Stage: 10; MessageName: Update; AssemblyName: AccessCRM.ChangeLogContactData, AccessCRM, Version=1.0.0.0, Culture=neutral, PublicKeyToken=a030c130976783ab; ClassName: AccessCRM.ChangeLogContactData; Exception: Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.
at AccessCRM.PluginUtilities.GetStringValueFromProperty(Property p)
at AccessCRM.ChangeLogContactData.Execute(IPluginExecutionContext context)
at Microsoft.Crm.Extensibility.PluginStep.Execute(PipelineExecutionContext context)
.
[2011-07-01 16:39:33.7] Process: w3wp |Organization:f827deb3-c6cc-df11-bc07-005056887b79 |Thread: 8 |Category: Platform.Sdk |User: 822138f1-c574-e011-9dca-005056887b79 |Level: Error | PluginExecutionExceptionHandler.Handle
at PluginExecutionExceptionHandler.Handle(Stream from, Stream to, Exception exception)
at CompositeSoapExtensionExceptionHandler.Handle(Stream to, Stream from, Exception exception)
at CrmAuthenticationSoapExtensionBase.ProcessMessage(SoapMessage message)
at SoapMessage.RunExtensions(SoapExtension[] extensions, Boolean throwOnException)
at SoapServerProtocol.WriteException(Exception e, Stream outputStream)
at WebServiceHandler.WriteException(Exception e)
at WebServiceHandler.Invoke()
at WebServiceHandler.CoreProcessRequest()
at SyncSessionlessHandler.ProcessRequest(HttpContext context)
at CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
at ApplicationStepManager.ResumeSteps(Exception error)
at HttpApplication.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData)
at HttpRuntime.ProcessRequestInternal(HttpWorkerRequest wr)
at HttpRuntime.ProcessRequestNoDemand(HttpWorkerRequest wr)
at ISAPIRuntime.ProcessRequest(IntPtr ecb, Int32 iWRType)
CrmSoapExtension detected InvalidPluginExecutionException:
System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> Microsoft.Crm.Sdk.InvalidPluginExecutionException: Object reference not set to an instance of an object. ---> System.NullReferenceException: Object reference not set to an instance of an object.
at AccessCRM.PluginUtilities.GetStringValueFromProperty(Property p)
at AccessCRM.ChangeLogContactData.Execute(IPluginExecutionContext context)
at Microsoft.Crm.Extensibility.PluginStep.Execute(PipelineExecutionContext context)
--- End of inner exception stack trace ---
at Microsoft.Crm.Extensibility.PluginStep.Execute(PipelineExecutionContext context)
at Microsoft.Crm.Extensibility.Pipeline.Execute(PipelineExecutionContext context)
at Microsoft.Crm.Extensibility.MessageProcessor.Execute(PipelineExecutionContext context)
at Microsoft.Crm.Extensibility.InternalMessageDispatcher.Execute(PipelineExecutionContext context)
at Microsoft.Crm.Extensibility.ExternalMessageDispatcher.Execute(String messageName, Int32 primaryObjectTypeCode, Int32 secondaryObjectTypeCode, PropertyBag fields, CorrelationToken correlationToken, CallerOriginToken originToken, UserAuth userAuth, Guid callerId)
at Microsoft.Crm.Sdk.CrmServiceInternal.Update(String namespaceName, BusinessEntityBase entity, CorrelationToken correlationToken, CallerOriginToken originToken, UserAuth userAuth, Guid callerId)
at Microsoft.Crm.Sdk.Crm2007.CrmService.Update(BusinessEntity entity)
--- End of inner exception stack trace ---
Looking at the stack trace, the error is actually being thrown in a plugin, not in the code you have pasted above. Look at this line in particular:
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.
at AccessCRM.PluginUtilities.GetStringValueFromProperty(Property p)
at AccessCRM.ChangeLogContactData.Execute(IPluginExecutionContext context)
You'd need to post up the code for that plugin for us to have another look.
Alternatively use Remote Debugging to debug this yourself.

Resources