cancel
Showing results for 
Search instead for 
Did you mean: 

NullReferenceException when using .net Membership and Role Providers with multiple users

Former Member
0 Kudos
4,336

Hi, I am encountering NullReferenceException and other exceptions when making calls Membership and Role provider functions in a .Net MVC Web application using SQL Anywhere. Most often I am seeing the errors when calling the Roles.IsUserInRole() function however I have also seen them when calling other functions such as Membership.ValidateUser(). The errors are only occurring when the web application is being used by multiple users (i.e. it is under load) The errors are ultimately causing the IIS worker process to hang, rendering the web application unresponsive until such time as it is manually reset. I am using SQL Anywhere 11.0.1.3113 however I have also produced the problem with SA-12.0.1.3152 and SA-16.0.0.2043 Developer Edition which I downloaded a couple of days ago.

I have been able to reproduce the problem in a test environment using a simple web application and load testing software. The steps I have taken to produce the issue are: * Create a new MVC3 Web application in VS 2010 * Include a reference to iAnywhere.Web.Security * Add Connection string and membership/role provider details to web.config * Add a Call to Roles.IsUserInRole(String username, String roleName) to the existing sample pages * Deploy the application and place it under load of at least 2 concurrent users. (I am using Fiddler with the StresStimulus plugin for load testing)

Here is a typical stack trace for the errors that I am seeing:

iAnywhere.Data.SQLAnywhere.SACommand._ExecuteReader(CommandBehavior commandBehavior, Boolean isExecuteScalar, Boolean isBeginExecuteReader) +1388
iAnywhere.Data.SQLAnywhere.SACommand.ExecuteDbDataReader(CommandBehavior behavior) +129
iAnywhere.Data.SQLAnywhere.SACommand.ExecuteReader(CommandBehavior behavior) +130
iAnywhere.Web.Security.SAConnect.CallStoredProcedure(String procedureName, SAParameter[] parameters, Boolean suppressException, Hashtable outParameters, ArrayList readerArray) +1669
iAnywhere.Web.Security.SARoleProvider.IsUserInRole(String username, String roleName) +377
System.Web.Security.Roles.IsUserInRole(String username, String roleName) +794
ASP._Page_Views_Home_Index_cshtml.Execute() in c:\\web\\WebTest\\Views\\Home\\Index.cshtml:8


While the NullReferenceException is most common, I have also seen AccessViolationException, SAException (0x80004005): Connection was terminated and others, all with similar stack traces.

Can anyone tell me why these errors are occurring and how I can avoid them?

Vlad
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hello Aaron,

I have taken a look at the method. The method uses the stored procedure: aspnet_UsersInRoles_IsUserInRole with parameters: ApplicationName, UserName, UserName.

Can you please execute this procedure somehow with the code (or within Sybase Central) maybe from two threads in the loop. If it fails, then this will give us some clue what is going wrong.

In addition, this procedure looks simple:

CREATE PROCEDURE "{0}"."aspnet_UsersInRoles_IsUserInRole"( IN @ApplicationName nvarchar(256), IN @UserName nvarchar(256), IN @RoleName nvarchar(256), OUT @ReturnValue int )

BEGIN DECLARE @ApplicationId uniqueidentifier; DECLARE @UserId uniqueidentifier; DECLARE @RoleId uniqueidentifier;

SELECT ApplicationId INTO @ApplicationId FROM "{0}".aspnet_Applications WHERE LoweredApplicationName = LOWER(@ApplicationName);
SELECT UserId INTO @UserId FROM "{0}".aspnet_Users WHERE LoweredUserName = LOWER(@UserName) AND ApplicationId = @ApplicationId;
SELECT RoleId INTO @RoleId FROM "{0}".aspnet_Roles WHERE LoweredRoleName = LOWER(@RoleName) AND ApplicationId = @ApplicationId;

IF ( EXISTS(SELECT * FROM "{0}".aspnet_UsersInRoles WHERE UserId = @UserId AND RoleId = @RoleId) ) THEN
    SET @ReturnValue = 1;
ELSE
    SET @ReturnValue = 0;
END IF;


END ;

It seems that parallel requests somhow affect either DB connection, or .NET provider.

Accepted Solutions (0)

Answers (1)

Answers (1)

jack_schueler
Product and Topic Expert
Product and Topic Expert
0 Kudos

This sounds like a job for the technical support team. Have you considered contacting them?

Former Member
0 Kudos

There are a number of steps to setting up SQL Anywhere to be used as a DataSource for the management and control of memberships and roles for your ASP.Net application, and you may be suffering from a missing step or a misconfigured/misalignment of some of those.

It is possible you might just not have the schema required to back up the roles ... so make sure you have installed those into the specific database being used for this by running the .\\Assembly\\V2\\SASetupAspNet.exe -c "connection string".

It is also possible your registered connection string is just not connecting to the same server and database as used in that step. There are many ways where your connection string could be generic or not specific enough to find the same databasename or servername so you should spend time here. [Tip: If using DSNs for this do be careful to create them as System DSNs since IIS is running as a Windows service and won't normally see any user DSNs.]

It is also probable that the database is not actually running, so you might want to launch the database server as an Automatic Windows Service.

It would seem you may have registered the Roles Provider already (going by the backtrace) but revisiting that part of the setup (for all the providers you installed schema for via SASetupAspnet.exe) may help identify if you missed anything there ... and similarily review all the steps for this (ie. the links at the bottom of the SQL Anywhere ASP.NET providers page)

And finally, I don't consider myself and ASP.Net expert so (caveat utilitor) ... and if you need any more help than this do consider Jack's earlier recommendation.

HTH