Custom role provider web config error - c#

I have written custom roleProvider for my Silverlight application.
Unfortunately I'm getting an error while loading:
Configuration Error
Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately.
Parser Error Message: Exception has been thrown by the target of an invocation.
Source Error:
Line 46: <providers>
Line 47: <clear />
Line 48: <add name="LanosRoleProvider" type="LANOS.Web.Security.LanosRoleProvider" applicationName="/" />
Line 49: </providers>
Line 50: </roleManager>
Source File: C:\path\LANOS\LANOS.Web\web.config Line: 48
This is definition of my role provider (class is inside Lanos.Web project):
namespace LANOS.Web.Security
{
public class LanosRoleProvider : RoleProvider
I have really no clue why this does not work. Before adding it to my application I've tested it on some sample projects and it worked fine. Any ideas?
Or at least could you tell me how to show that exception info which is being thrown at the time of config load?

What version of ASP.NET are you running?
I would suggest adding the assembly to the element like so if your assembly is LANOS.Web.Security:
<add name="LanosRoleProvider" type="LANOS.Web.Security.LanosRoleProvider, LANOS.Web.Security" applicationName="/" connectionStringName="abcConnectionString" />
UPDATED
connectionStringName is a required attribute which is why the message is a parse error. This also means you will have to define a connection string in the section.
<connectionStrings>
<add name="abcConnectionString" connectionString="blah" />
</connectionStrings>
Hope this helps.

Just ran into this problem and my problem was that I was using property injection for a service used by the Custom Roles Provider. This service also required its dependencies to be injected by constructor. I missed a binding of one of these constructor dependencies which caused this ambiguous error to be thrown. I tried explaining this on here.
I hope it helps people coming from google like I did.

This happened to me because an exception was thrown in the constructor of my RoleProvider.
Check your constructors call chain and and private member default assignments.
My particular issue was caused by a problem with EntityFramework in the constructor.
Clear out any constructor code in the provider and test to see if the error goes away and you'll know you are on the right track.

Related

The type initializer for System.Data.Entity.MigrateDatabaseToLatestVersion threw an exception

I am getting error trying to hit a MVC controller that constructs an EF derived context using EF 6.0.0 in an ASP.NET MVC5 project.
The type initializer for System.Data.Entity.MigrateDatabaseToLatestVersion threw an exception.
the type initializer for 'system.data.entity.internal.appconfig'
threw an exception configuration system failed to initialize entity
framework the type initializer for threw an exception could not load
file or assembly unrecognized element 'providers'. entity framework
Removing EF from all the shared projects as well as this project does not make a difference. The current context used to be used on an MVC4 site and then used when migrated to MVC5, but this new project was added as a new MVC5 project and the context is used in other MVC5 (migrated from MVC4 classes.) We're doing code based config using DbConfiguration...
Our config entry is defined:
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
But I think that entry is not even required anymore with EF 6. I could be wrong, I am just gonna leave it in there for now. I know connection to the DB is working because that was the first thing I fixed (couldn't find DefaultConnection) when setting up init of the SimpleMembership database.
I also confirmed configuration is arranged correctly according to the other projects which jives with MS topics thrown in the internal exceptions: https://msdn.microsoft.com/en-us/data/jj680699?f=255&MSPPError=-2147217396
Well... I see what I missed... In the global.asax there was a few lines to init the DB in the other projects... and well... it was missing:
using (var context = new Data.FooContext())
{
context.Database.Initialize(false);
}
I guess my followup question is why is this required here. This is a smell no?
Maybe its not. But init was it... for me.

requiresQuestionAndAnswer set to false in web.config but RequiresQuestionAndAnswer is true at runtime

In an ASP.net web application I have defined the following Membership provider in the web.config:
<membership>
<providers>
<add connectionStringName="MyServer" name="MyServer" type="System.Web.Security.SqlMembershipProvider"
enablePasswordReset="true"
requiresQuestionAndAnswer="false"
enablePasswordRetrieval="false" />
</providers>
</membership>
When I run the application in the debugger, the property Membership.Provider.RequiresQuestionAndAnswer is true.
Why? And how can I fix this?
Update: Ar tuntime, the Membership.Providers collection contains two instances of Provider that are almost identical. The differences are:
The first Provider has Name=="AspNetSqlMembershipProvider" and RequiresQuestionAndAnswer==true
The second Provider has Name=="MyServer" and RequiresQuestionAndAnswer==false.
Now trying to figure out where the first one is coming from.
Per the follow up question, the mystery providers is defined on a lower level config file, named machine.config, which is the server-wide parent config file for all asp.net sites running on the machine. See some more details at the MSDN page for asp.net config hierarchy.
The fix for my problem is to specify in web.config:
<membership defaultProvider="MyServer>
The reason why there's an "extra" Provider at run-time is that it's defined in my machine.config file. Here's how to find yours.
Credit for fix goes to #meidan-alon and credit for root cause goes to #ken-egozi :)

The type or namespace name 'Optimization' does not exist in the namespace 'System.Web'

I am deploying a new website as my main site and it works beautifully.
All of my applications under the root url work as well, except for one. It is a legacy system (c#.net) that is heavily used and unfortunately, I do not have access to the source code.
When I run the legacy application www.mysite.com/crm I get the following error:
Server Error in '/crm' Application.
Compilation Error
Description: An error occurred during the compilation of a resource required to
service this request. Please review the following specific error details and modify
your source code appropriately.
Compiler Error Message: CS0234: The type or namespace name 'Optimization' does not
exist in the namespace 'System.Web' (are you missing an assembly reference?)
Source Error:
Line 17: <pages>
Line 18: <namespaces>
Line 19: <add namespace="System.Web.Optimization" />
Line 20: </namespaces>
Line 21: <controls>
Source File: e:\WebSites\newsite\Web.config Line: 19
Show Detailed Compiler Output:
Show Complete Compilation Source:
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18446
The error is listing the web.config for the main site, which works. However, it happens only when I run the application under the main site.
This is running on Server 2008 R2 and IIS7
How do I correct this? What else can I try?
UPDATE
I don't know if it makes a difference, but the Web.Config it references in the error is for the new website, not for the legacy application. I am not sure why the main site config is a factor at all.
CLARIFICATION
I don't have access to the source code of the legacy application so I cannot adjust the properties for that application. Pasting the Optimization dll into the site didn't fix it either. It may work for other users, but not for me.
WORK AROUND
I setup the new site under another URL and redirect traffic to the root URL to the new URL and allow the legacy application to run as is until we can replace it. Very sloppy solution, but it works for now.
I just faced this issue now:
I reinstalled the optimization framework and everything worked fine:
Install-Package Microsoft.AspNet.Web.Optimization
I faced same problem after create a new area in MVC5. There is auto generate web.config file. Check your web.config file under Views folder there is a line
<add namespace="System.Web.Optimization" />
if you need this then install it using following command.
Install-Package Microsoft.AspNet.Web.Optimization
at Package Manager Console like below
If you think no need then simply remove <add namespace="System.Web.Optimization" /> line from following section in web.config file
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
<!--<add namespace="System.Web.Optimization" />-->
<add namespace="AutoParts.Web" />
</namespaces>
Check the Bin folder for the legacy application. It may simply need you to drop in that DLL. The System.Web.Optimization namespace exists as of ASP.NET 4.5 so it may not even be supported. The legacy App might be older than that.
In Visual Studio .. Go to your project and see the References tree.
right click System.Web.Optimization and choose Properties
look for the path , you will find it empty
if that is the case then
look in your PC for the file names System.Web.Optimization.dll and copy it in the bin folder of your project and that will resolve it
I was just experiencing: "CS0234 C# The type or namespace name 'Optimization' does not exist in the namespace 'System.Web' (are you missing an assembly reference?)" and found this stackoverflow page. -- The cause for me was due to me renaming the web form (aspx page) I was working with, I also wanted to rename the public partial class behind that form. Doing so resulted in the mentioned error code. I added "using System.Web.Optimization;" to the code behind (C#), "cleaned" the solution, then "built" then solution, then "rebuilt" the solution and now the error is gone and that using statement wasn't being used, so I got rid of it. My solution no longer has that or any errors so I figured I'd share this weird story. I probably didn't need to use that "using" statement, but whatever.
Main to Area
#Html.ActionLink("My Area Home", "Index", "Home", new { area = "MyArea" }, null)
Area to Main
#Html.ActionLink("Home", "Index", "Home", new { area = "" }, null)

Compilation error with missing Microsoft.WindowsAzure.ServiceRuntime.RoleEntryPoint

I am trying to extend the default webrole template. I have added a new entity model and created a skeleton framework for it. When the loading of the index page is attempted, a server error is displayed.
"The type 'Microsoft.WindowsAzure.ServiceRuntime.RoleEntryPoint' is defined in an assembly that is not referenced. You must add a reference to assembly 'Microsoft.WindowsAzure.ServiceRuntime, Version=2.3.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'."
I have double checked that the WebRole includes this reference and it does. I can fix this error by setting this reference Copy Local property to true.
If I do this, the error changes to "The type name 'Models' does not exist in the type 'WebRole.WebRole'"
Both errors are thrown from the Index.cshtml file, specificially on the line:
public class _Page_Views_BankAccount_Index_cshtml : System.Web.Mvc.WebViewPage<IEnumerable<WebRole.Models.BankAccount>> {
What is causing this?
You are missing references in your view compilation.
The view compiler uses a different set of references, and to make your model work you will need to add the references in your web.config
For example:
<configuration>
<system.web>
<assemblies>
<add assembly="YourAssembly, Version=4.0.0.0, Culture=neutral, PublicKeyToken=TOKENKEY" />
</assemblies>
</system.web>
</configuration>
I know it's old but might help anyone else.
This error occurs due to a conflict in the default project for Windows Azure Cloud Service when the web project is created with the name WebRole (Which puts the project in the same namespace as the Microsoft.WindowsAzure.ServiceRuntime dll).
To fix there are two easy options:
replace this code
#model WebRole.Models.RegisterViewModel
and add the using statement and your model without the namesapce
#using WebRole.Models
#model RegisterViewModel
Or instead one can add a namespace in the web.config inside the views folder (not the one in the root)
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
-----
<add namespace="WebRole.Models" />
</namespaces>
</pages>
And on your views just replace the existing code with
#model RegisterViewModel
Hope it helps.
Do you have the correct version of Azure Tools installed? I know for me, I only get this error when I'm missing the Azure Tools library.

Unable to retrieve metadata - MVC application

I was following this to create a simple MVC application. After creating the model, when I tried to add controller, I got the following error :
Unable to retrieve metadata for "MvcApplication.Models.Movie". Invalid value for key "attachdbfilename".
Can someone tell why I am getting this error.
Update : I saw this wherein solution is provided by changing the providerName. But in my case it is already System.Data.SqlClient. Following are my connectionStrings :
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=aspnet-MvcApplication-20130708120940;Integrated Security=SSPI" providerName="System.Data.SqlClient" />
<add name="MovieDBContext" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Movies.mdf;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
The error message is saying invalid value for AttachDbFilename which is part of your MovieDbContext connection string.
The connection string currently shows this AttachDbFilename=|DataDirectory|\Movies.mdf so it appears the mdf file is now missing from the App_Data folder.
I found the solution. the problem is the "|DataDirectory|". I Changed "|" to "\"
I just solved this problem. Even though I am new to MVC, I am bit embarrassed to say, I was trying a work around for another problem I was having and moved my SetInitializer into my classname : DbContext class.
The app worked just fine and the database was created fine as I changed the models but I couldn't add new controllers or modify current controllers using the scaffolding tool.
I began receiving errors. One of them is listed above and another is:
Unable to retrieve meta data for 'namespace.Models.class'. A file
activation error occurred. The physical file name '\name.mdf'
may be incorrect. Diagnose and correct additional errors, and retry the
operation.
CREATE DATABASE failed. Some file names listed could not be created.
Check related errors.
Moving the SetInitializer back into my Global.asax fixed the problem.

Categories