Error while implementing MembershipProvider - asp.net

Today i got an error while trying to implement MembershipProvider.
Error 1 The type 'System.Configuration.Provider.ProviderBase' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
The thing that i have done is i added a reference of system.web because i had to implement membershipProvider which is under System.Web.Security. and now i get this error. thanks in advance !

When implementing a custom MembershipProvider, you must also put a reference to System.Configuration (as indicated by the exception), because the MembershipProvider gets configured via settings in web.config.

Related

Referencing RoleProvider in a class library

I'm trying to create a custom role provider in a class library, and for some reason I can't reference System.Web.Security.RoleProvider.
I have added a reference to System.Web in the project, yet still no luck. Is there a reason why I can't do this?
You need to reference the assembly System.Web.ApplicationServices.dll. The RoleProvider class is not defined in System.Web.dll.
In this cases and when in doubt check the MSDN page for the associated class and at the top of the page you can see in what namespace and assembly it is defined.
For the RoleProvider class you have:
Namespace: System.Web.Security
Assembly: System.Web.ApplicationServices (in System.Web.ApplicationServices.dll)

DLL Compilation Error (ASP.NET)

We have a webserver with two versions of the AjaxControlKit on it. Im getting the following error when hitting certain pages on a specific web application.
The type
'AjaxControlToolkit.CalendarExtender'
exists in both
'c:\Windows\assembly\GAC_MSIL\AjaxControlToolkit\3.0.30930.28736__28f01b0e84b6d53e\AjaxControlToolkit.dll'
and
'c:\Windows\assembly\GAC_MSIL\AjaxControlToolkit\3.0.30512.20315__28f01b0e84b6d53e\AjaxControlToolkit.dll'
The web project Im running has a reference to the x.30930 dll, and it also has
<add assembly="AjaxControlToolkit, Version=3.0.30930.28736, Culture=neutral, PublicKeyToken=28f01b0e84b6d53e"/>
Specified in the web.config.
For the life of me I cant find out why it wants to try running x.30512.
Any suggestions?
Aaargh there was a reference to it in the 'global' web.config (in the c:\windows\microsoft.net\ folder

ASP.NET "Reference required to assembly" error

I have an ASP.NET page A that uses a data layer assembly DAL. I reference the DAL assembly with a
<%# Assembly Name="NGC.DAL" %> tag since there is no project to add the reference to. The assembly itself I copied to the pages' bin folder.
The assembly DAL is a project that references System and System.Data.
Now when A calls a method in DAL, the compiler gives the error
BC30652: Reference required to assembly 'System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=969db8053d3322ac, Retargetable=Yes' containing the type 'System.Data.IDbConnection'. Add one to your project.
I tried referencing the assembly System.Data with a <%#Import Namespace="System.Data" %> tag and I tried copying it to the bin folder and referencing it with a
<%# Assembly Name="System.Data" %> tag, both in A directly. Nothing worked.
Why would a project A that uses project B which uses project C require a reference to B and C rather than just B and why does the way above not work?
This MSDN thread covers the problem but shows no solution that works.
EDIT: As Robert Wagner suggested I added the reference to the web.config.
Now I get another error message. BC30560: 'CommandBehavior' is ambiguous in the namespace 'System.Data'. which is odd because the one in the web.config is the only one I reference inside the page.
Maybe it's worth mentioning that the System.Data referenced and used in the DAL assembly is from the compact framework. But since it's the only one I use, where does the ambiguity come from?
Most likely you have inherited from a System.Data class in your DAL dll and are using that class in your website. It requires a reference to be able to resolve the references those classes otherwise the compiler would not know what the class contract is.
You should not need to copy in System.Data as it is in the GAC.
A better way would be to add the reference in your web.config file:
<system.web>
<compilation debug="true">
<assemblies>
<add assembly="System.Data, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=B77A5C561934E089" />
</assemblies>
</compilation>
</system.web>
When you say "there is no project to add references to" are you using Visual Studio? If so, why not use the Web Application or Web Site projects?

Using ConfigurationManager to load config from an arbitrary location

I'm developing a data access component that will be used in a website that contains a mix of classic ASP and ASP.NET pages, and need a good way to manage its configuration settings.
I'd like to use a custom ConfigurationSection, and for the ASP.NET pages this works great. But when the component is called via COM interop from a classic ASP page, the component isn't running in the context of an ASP.NET request and therefore has no knowledge of web.config.
Is there a way to tell the ConfigurationManager to just load the configuration from an arbitrary path (e.g. ..\web.config if my assembly is in the /bin folder)? If there is then I'm thinking my component can fall back to that if the default ConfigurationManager.GetSection returns null for my custom section.
Any other approaches to this would be welcome!
Try this:
System.Configuration.ConfigurationFileMap fileMap = new ConfigurationFileMap(strConfigPath); //Path to your config file
System.Configuration.Configuration configuration = System.Configuration.ConfigurationManager.OpenMappedMachineConfiguration(fileMap);
Another solution is to override the default environment configuration file path.
I find it the best solution for the of non-trivial-path configuration file load, specifically the best way to attach configuration file to dll.
AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", <Full_Path_To_The_Configuration_File>);
Example:
AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", #"C:\Shared\app.config");
More details may be found at this blog.
Additionally, this other answer has an excellent solution, complete with code to refresh
the app config and an IDisposable object to reset it back to it's original state. With this
solution, you can keep the temporary app config scoped:
using(AppConfig.Change(tempFileName))
{
// tempFileName is used for the app config during this context
}
Ishmaeel's answer generally does work, however I found one issue, which is that using OpenMappedMachineConfiguration seems to lose your inherited section groups from machine.config. This means that you can access your own custom sections (which is all the OP wanted), but not the normal system sections. For example, this code will not work:
ConfigurationFileMap fileMap = new ConfigurationFileMap(strConfigPath);
Configuration configuration = ConfigurationManager.OpenMappedMachineConfiguration(fileMap);
MailSettingsSectionGroup thisMail = configuration.GetSectionGroup("system.net/mailSettings") as MailSettingsSectionGroup; // returns null
Basically, if you put a watch on the configuration.SectionGroups, you'll see that system.net is not registered as a SectionGroup, so it's pretty much inaccessible via the normal channels.
There are two ways I found to work around this. The first, which I don't like, is to re-implement the system section groups by copying them from machine.config into your own web.config e.g.
<sectionGroup name="system.net" type="System.Net.Configuration.NetSectionGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<sectionGroup name="mailSettings" type="System.Net.Configuration.MailSettingsSectionGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<section name="smtp" type="System.Net.Configuration.SmtpSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</sectionGroup>
</sectionGroup>
I'm not sure the web application itself will run correctly after that, but you can access the sectionGroups correctly.
The second solution it is instead to open your web.config as an EXE configuration, which is probably closer to its intended function anyway:
ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap() { ExeConfigFilename = strConfigPath };
Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
MailSettingsSectionGroup thisMail = configuration.GetSectionGroup("system.net/mailSettings") as MailSettingsSectionGroup; // returns valid object!
I daresay none of the answers provided here, neither mine or Ishmaeel's, are quite using these functions how the .NET designers intended. But, this seems to work for me.
The accepted answer is wrong!!
It throws the following exception on accessing the AppSettings property:
Unable to cast object of type 'System.Configuration.DefaultSection' to type 'System.Configuration.AppSettingsSection'.
Here is the correct solution:
System.Configuration.ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
fileMap.ExeConfigFilename = "YourFilePath";
System.Configuration.Configuration configuration = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
In addition to Ishmaeel's answer, the method OpenMappedMachineConfiguration() will always return a Configuration object. So to check to see if it loaded you should check the HasFile property where true means it came from a file.
I provided the configuration values to word hosted .nET Compoent as follows.
A .NET Class Library component being called/hosted in MS Word. To provide configuration values to my component, I created winword.exe.config in C:\Program Files\Microsoft Office\OFFICE11 folder. You should be able to read configurations values like You do in Traditional .NET.
string sMsg = System.Configuration.ConfigurationManager.AppSettings["WSURL"];
For ASP.NET use WebConfigurationManager:
var config = WebConfigurationManager.OpenWebConfiguration("~/Sites/" + requestDomain + "/");
(..)
config.AppSettings.Settings["xxxx"].Value;
This should do the trick :
AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", "newAppConfig.config);
Source : https://www.codeproject.com/Articles/616065/Why-Where-and-How-of-NET-Configuration-Files
Use XML processing:
var appPath = AppDomain.CurrentDomain.BaseDirectory;
var configPath = Path.Combine(appPath, baseFileName);;
var root = XElement.Load(configPath);
// can call root.Elements(...)

Webpart registration error in event log

We created several custom web parts for SharePoint 2007. They work fine. However whenever they are loaded, we get an error in the event log saying:
error initializing safe control - Assembly: ...
The assembly actually loads fine. Additionally, it is correctly listed in the web.config and GAC.
Any ideas about how to stop these (Phantom?) errors would be appreciated.
­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
You need to add a safecontrol entry to the web,config file, have a look at the following:
<SafeControls>
<SafeControl
Assembly = "Text"
Namespace = "Text"
Safe = "TRUE" | "FALSE"
TypeName = "Text"/>
...
</SafeControls>
http://msdn.microsoft.com/en-us/library/ms413697.aspx
I was having this problem too. It turned out that there was a problem with my Manifest.xml file. In the SafeControl tag for my assembly, I had the TypeName specifically defined. When I changed the TypeName to a wildcard value, the error messages in the event log stopped.
So to recap:
This caused errors in the event log:
<SafeControl Assembly="AssemblyName, Version=1.0.0.0, Culture=neutral, PublicKeyToken=5bac12230d2e4a0a" Namespace="AssemblyName" **TypeName="AssemblyName"** Safe="True" />
This cleared them up:
<SafeControl Assembly="AssemblyName, Version=1.0.0.0, Culture=neutral, PublicKeyToken=5bac12230d2e4a0a" Namespace="AssemblyName" **TypeName="*"** Safe="True" />
It sure does sound like you have a problem with your safe control entry. I would try:
Under the NameSpace and TypeName use "*". Using wildcards in namespace and typeName will register all classes in all namespaces in your assembly as safe. (You generally wouldn't want to do this with 3rd party tools.)
This is because of the amount of list items in the lists. Your server takes to much time to migrate all the list items and it fails, try deleiting the list items or configuring the server.
Regards,
Mariano.

Resources