app.config assemblies - assemblies

I have the following in my app.config.
<configuration>
<configSections>
<sectionGroup name="FooGroup">
<section
name="Foo"
type="Bar.FooSection"
allowLocation="true"
allowDefinition="Everywhere"
/>
</sectionGroup>
</configSections>
....
Bar.FooSection is in another assembly however, so I get a TypeLoadException.
What do I have to do?

My tentative guess would be that you need to specify the assembly as a part of the type attribute's value, ie. make the mentioned type fully qualified, i.e. something along the lines of:
TopNamespace.SubNameSpace.ContainingClass+NestedClass, MyAssembly,
Version=1.3.0.0, Culture=neutral, PublicKeyToken=b17a5c561934e089
(Example taken from MSDN.)
You can probably omit the Version, Culture, and PublicKeyToken, and just write:
... type="Foobar.FooSection, FooAssembly" ...

Related

Xml variable substitution doesn't work with individual section key in web.config

I am working with DevOps pipeline using yaml and has deployed a .net web application to cloud. I used xml variable substitution to replace a content from configSections in web.config file.
- task: AzureRmWebAppDeployment#4
displayName: 'Service Deploy'
inputs:
appType: webApp
ConnectionType: AzureRM
ConnectedServiceName: '***'
ResourceGroupName: '***'
WebAppName: '***'
enableXmlVariableSubstitution: true
The web.config file contains multiple sections and a common key named "serviceurl" exist in both sections as below.
<configuration>
<configSections>
<section name="roombooking" type="System.Configuration.NameValueSectionHandler, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<section name="cabbooking" type="System.Configuration.NameValueSectionHandler, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</configSections>
<roombooking>
<add key="serviceurl" value="localhost" />
</roombooking>
<cabbooking>
<add key="serviceurl" value="localhost" />
</cabbooking>
</configuration>
The application has deployed to multiple client locations. I have to replace "serviceurl" in web.config during deployment. The "serviceurl" is different for "roombooking" and "cabbooking". I tried xml variable transformation during deployment as mentioned in the document but it replaces both "serviceurl"'s into one value.
The web.config file after deployment as below.
<configuration>
<configSections>
<section name="roombooking" type="System.Configuration.NameValueSectionHandler, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<section name="cabbooking" type="System.Configuration.NameValueSectionHandler, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</configSections>
<roombooking>
<add key="serviceurl" value="http://example.com/" />
</roombooking>
<cabbooking>
<add key="serviceurl" value="http://example.com/"/>
</cabbooking>
</configuration>
I tried with different variable name like "roombooking.serviceurl", "roombooking--serviceurl" etc but nothing applied on individual section key. Is there any other way to apply xml variable substitution to replace "serviceurl" individually. Any help would be appreciated.

Config Builder Not Finding Environment Variable

When I view the connection string while debugging, the value is "abc"; not what I've set the Windows environment variable to.
web.config
<configSections>
<section name="configBuilders" type="System.Configuration.ConfigurationBuildersSection, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" restartOnExternalChanges="false" requirePermission="false" />
</configSections>
<configBuilders>
<builders>
<add name="Environment" type="Microsoft.Configuration.ConfigurationBuilders.EnvironmentConfigBuilder, Microsoft.Configuration.ConfigurationBuilders.Environment, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</builders>
</configBuilders>
<connectionStrings configBuilders="Environment">
<add name="myConnection" connectionString="abc" providerName="System.Data.SqlClient"/
</connectionStrings>
I've double checked that the correct Nuget packages are installed, that the framework is correctly on 4.7.2, that my spelling is correct, and I've read the Microsoft docs on Configuration Builders several times.
In the Microsoft document linked above, Configuration builders for ASP.NET, there exists the following footnote:
Note: You might need to exit and restart Visual Studio to see changes in environment variables.
That fixed it.

Visual Studio 2015 doesn't recognize imported namespaces in web.config for Razor

I have a website project containing numerous .cshtml (WebPages, not MVC) files and where i've defined a common set of namespaces in my web.config file like this
<system.web.webPages.razor>
<pages pageBaseType="Composite.AspNet.Razor.RazorFunction">
<namespaces>
<add namespace="DinArv.Forms"/>
<add namespace="DinArv.Web"/>
</namespaces>
</pages>
</system.web.webPages.razor>
After installing VS 2015 RTM, i'm suddenly getting 'The type or namespace ... could not be found' errors on types that exists in namespaces defined in web.config, while Resharper doesn't seem to have a problem. I can also still browse the pages fine and there are no build errors when i build my website. In my Visual Studio 2013 Intellisense is working as it should.
If i import the namespaces explicitly in the .cshtml file instead of web.config, VS 2015 is satisfied as well, but i really don't want to do that.
What is wrong, and how can i fix it?
After hours and hours i accidentally stumbled upon what seemed to do the trick. My section group was defined like this, which VS 2013 had no problem with
<sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor">
<section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor" requirePermission="false" />
</sectionGroup>
As soon as i added version-number to the types like this, Intellisense in VS 2015 lighted up. Grrrrr.... what a waste of time, but hopefully this can save someone else time.
<sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
</sectionGroup>

web.config transform for microsoft.identitymodel configsection

How does one go about transforming the audienceUris and federatedAuthentication elements in the microsoft.identitymodel configuration section using the standard visual studio web.config transforms?
For the development environment, we have all the subdomains postfixed with a '-dev' along with special localhost port numbers, for example: realm="https://client-dev.domain.com:444". Today the main web.config reflects this but ideally we would like to move this setting to Web.Debug.config and in the Web.Release.config we would like to specify the proper audience and realms sans the '-dev' postfix and the local port settings. However, attempting to put xdt:Transform="Replace" in the microsoft.identitymodel element inside the Web.Debug.Config results in this warning:
The 'http://schemas.microsoft.com/XML-Document-Transform:transform' attribute is not declared.
and of course runtime failures (not being able to find servicecertificate for example).
Here's the microsoft.identitymodel sections:
<configuration>
<configSections>
<section name="microsoft.identityModel" type="Microsoft.IdentityModel.Configuration.MicrosoftIdentityModelSection, Microsoft.IdentityModel, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</configSections>
<microsoft.identityModel>
<service>
<applicationService>
<claimTypeRequired>
<claimType type="http://schemas.xmlsoap.org/ws/2005/05/identity/claims/privatepersonalidentifier" optional="false" />
<claimType type="http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress" optional="false" />
</claimTypeRequired>
</applicationService>
<serviceCertificate>
<certificateReference x509FindType="FindByThumbprint" findValue="NNNNNNNNNN" storeLocation="LocalMachine" storeName="My" />
</serviceCertificate>
<certificateValidation certificateValidationMode="None" />
<audienceUris>
<add value="https://url/" />
</audienceUris>
<federatedAuthentication>
<wsFederation passiveRedirectEnabled="true" issuer="https://url/" realm="https://url/" requireHttps="true" />
<cookieHandler requireSsl="true" />
</federatedAuthentication>
<issuerNameRegistry type="Microsoft.IdentityModel.Tokens.ConfigurationBasedIssuerNameRegistry, Microsoft.IdentityModel, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
<trustedIssuers>
<add thumbprint="NNNNNNNNNN" name="https://url/" />
</trustedIssuers>
</issuerNameRegistry>
</service>
</microsoft.identityModel>
<configuration>
Existing solutions that don't work:
1) Same issue was raised here but no proper response. The RemoveAll and Insert doesn't work:
Web config transform on microsoft.identityModel - 'http://schemas.microsoft.com/XML-Document-Transform' attribute is not declared
2) Also, tried to manipulate the namespace as suggested on this thread and this doesn't work either:
http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/9ededb66-3e34-4bba-8e20-a2cc6025f0f9
3) configSource looked promising but again does work when specified in the microsoft.identitymodel element.
I get:
Warning 1 The 'configSource' attribute is not declared.
http://blog.andreloker.de/post/2008/06/16/Keep-your-config-clean-with-external-config-files.aspx
Any help will be appreciated! Thanks.
After a lot of trial and error, I resorted to using XMLPreProcess by Loren Halvorson to auto-generate the correct Web.config from a TemplateWeb.config as part of the pre-Build event for each project.
Although I would have liked Visual Studio's attempt at this to work for developers, alas, time is of money and its time for me to move on. Needless to say, it's broken in the SQL Azure SDK as well because the transforms only occured when you publish a site. In the latest Azure SDK, that is no longer the case and hence transforms stopped working as well. Oh joy!
Here's a link to the excellent tool, I think, a must-have for every ASP.NET developer:
http://xmlpreprocess.codeplex.com/

Custom Configuration Sections

I am currently trying to implement a Custom Configuration Section in a project I am busy with and no matter what I try I keep getting the error below:
{"An error occurred creating the configuration section handler for pageAppearanceGroup/pageAppearance: Could not load type 'Samples.AspNet.PageAppearanceSection' from assembly 'System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. (E:\Three Nine Developments\lastfm\msdn\msdn\bin\Debug\Samples.Aspnet.vshost.exe.config line 6)"}
I have copied the code from this MSDN Artricle:
http://msdn.microsoft.com/en-us/library/2tw134k3.aspx
I still get the same error.
I have tried the all the advice/guide in the following articles but to no avail.
http://www.evanclosson.com/devlog/bettercustomerrorsinaspnetcustomconfigurationsection
Link
This must be something stupid that I am missing.
I am running Vista, could that be a problem? some obscure security setting?
<configuration>
<!-- Configuration section-handler declaration area. -->
<configSections>
<sectionGroup name="pageAppearanceGroup">
<section
name="pageAppearance"
type="Samples.AspNet.PageAppearanceSection"
allowLocation="true"
allowDefinition="Everywhere"
/>
</sectionGroup>
<!-- Other <section> and <sectionGroup> elements. -->
</configSections>
<!-- Configuration section settings area. -->
<pageAppearanceGroup>
<pageAppearance remoteOnly="true">
<font name="TimesNewRoman" size="18"/>
<color background="000000" foreground="FFFFFF"/>
</pageAppearance>
</pageAppearanceGroup>
</configuration>
My guess is that you've copied the code, but you have different assembly names. Posting the config will help.
I would also fully quality your type in the config (something that sample doesn't show). Something like...
<section name="MySection" type="My.Assembly.Type, My.Assembly" />
You should also check out Jon Rista's three-part series on .NET 2.0 configuration up on CodeProject.
Unraveling the mysteries of .NET 2.0 configuration
Decoding the mysteries of .NET 2.0 configuration
Cracking the mysteries of .NET 2.0 configuration
Highly recommended, well written and extremely helpful!
Marc
Please try with the following code:
<configSections>
<sectionGroup name="pageAppearanceGroup">
<section name="pageAppearance" type="Samples.AspNet.PageAppearanceSection,Samples.AspNet" allowLocation="true" allowDefinition="Everywhere" />
</sectionGroup> <!-- Other <section> and <sectionGroup> elements. -->
</configSections>
Please try with this
<configSections>
<sectionGroup name="pageAppearanceGroup">
<section name="pageAppearance"
type="Samples.AspNet.PageAppearanceSection,Samples.AspNet"
allowLocation="true"
allowDefinition="Everywhere" />
</sectionGroup>
<!-- Other <section> and <sectionGroup> elements. -->
</configSections>
Thanks,
Vedi
So it turns out that when you create a project in Visual Studio, it automatically defines a root namespace (the name of the project by default) for the project. Thus you must include that root namespace in the section type as well as any custom namespaces you defined within your settings class.
For example, in the case of the original poster a working configuration for them may have looked something like this:
<section name="MySection" type="ROOT_NAMESPACE.Samples.AspNet.PageAppearanceSection, NAME_OF_ASSEMBLY" />
Where ROOT_NAMESPACE and NAME_OF_ASSEMBLY are defined in the project properties as shown in this snapshot of my project.
In my particular case, I did not explicitly define namespaces in my project. Thus my section configuration setting just had the root namespace, the name of the settings class, and the name of the assembly, as such;
<section name="programSettings" type="ShipmentImport.ProgramSettings, ShipmentImport" />
I know its a couple years late, but I hope it keeps somebody else from spending hours on this like I did.

Resources