How to change app settings of Web.Config using targets.wpp - c#

I have created a wpp.targets file which does all the deployment in azure. I want to change the appsetting property of webconfig during deployment to azure. I found a sample in http://sedodream.com/PermaLink,guid,25ddd39e-59de-4e35-becc-de19dcc5e4ea.aspx , which uses the pubxml and parameters.xml. I want to use wpp.targets instead of pubxml.
<appSettings>
<!-- TODO: set this in the azure config -->
<add key="customer" value="xyz" />
</appSettings>
Need to update the customer value to "client" during deployment and web.config file should reflect the changes in the deployed folder like below
<appSettings>
<add key="customer" value="client" />
</appSettings>
ANSWER
I have achieved using the ProjectParametersXMLFile in msDeploy
msbuild Api.csproj /p:ProjectParametersXMLFile="c:\parameter.xml"/p:PublishSettingsFile=%publishFileLocation%

You can use parameter xml file
for example
<parameters >
<parameter name="customer" description="web Job queue name" defaultValue="sitetest1">
<parameterEntry kind="XmlFile"
scope="\\web.config$"
match="/configuration/appSettings/add[#key='customer']/#value" />
</parameter>
<parameter name="customer" description="web Job queue name" defaultValue="sitetest">
<parameterEntry kind="XmlFile"
scope="\\app.config$"
match="/configuration/appSettings/add[#key='customer']/#value" />
</parameter>
</parameters>
this file will change the appsettings when you deploy in azure based on default value you provide

Related

Web.Debug.config vs. Web.Release.config running web app in localhost

I have a Web.config file with some entries in there but depending on whether I am in Debug or Release mode when I execute my Web Application locally, I want to take different appSettings.
For instance, let's say that I have the following entry in my Web.Debug.config appSettings.
<add key="MyServiceUrl" value="http://my-test-site:8080/" />
And also I have this in my Web.Release.config:
<add key="MyServiceUrl" value="http://my-prod-site:80/" />
How should I configure my Web.Config, Web.Debug.Config and Web.Release.Config so depending on the mode I run my application locally (Debug - Any CPU vs. Release - Any CPU), it takes the right one?
Right now, the only pair of key and value that it takes is the one from the main Web.Config regardless I select Debug or Release in Visual Studio.
How can I configure that behavior?
EDIT
This is how I have defined Web.config
<appSettings>
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
This is how I have defined Web.Debug.config
<appSettings>
<add key="MyServiceUrl" value="http://my-test-site:8080/" xdt:Transform="SetAttributes" xdt:Locator="Match(key)"/>
</appSettings>
This is how I have defined Web.Release.config
<appSettings>
<add key="MyServiceUrl" value="http://my-prod-site:8080/" xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />
</appSettings>
Finally, in my code, I have the following method:
public static string GetAppSetting(string settingKey)
{
if (string.IsNullOrEmpty(settingKey))
throw new System.ArgumentException("Cannot fetch a setting for a null/empty setting key.");
return ConfigurationManager.AppSettings[settingKey];
}
which I call it like this:
string setting = GetAppSetting("MyServiceUrl");
However, it is null if it is not defined in the main Web.config
In the web.Release.config try this, it should work:
<appSettings>
<add key="MyServiceUrl" value="http://my-prod-site:8080/" xdt:Transform="Insert" />
</appSettings>
Read this: Web.config Transformation Syntax for Web Application Project Deployment

How to avoid commenting (out) <configuration> sections in app.config for specific deployment?

Note:
I've searched for this on Google and didn't directly find anything related to WinForms. The suggested questions while typing this question didn't apply to my situation.
I have a WinForms (.NET 3.5) application that can be published/deployed to three different locations: development, test and production. Each deployment type has its own specific settings, for example: connectionString, title(s), webservice address, directories/files to read or other settings.
The current situation in the app.config is as following:
<!-- dev-->
<configuration>
<configSections>...</configSections>
<appSettings>
<add key="Deployment" value="dev" />
<add key="SourceDir" value="\\server\foo\dev" />
</appSettings>
...
</configuration>
<!-- test -->
<configuration>
<configSections>...</configSections>
<appSettings>
<add key="Deployment" value="test" />
<add key="SourceDir" value="\\server\foo\test" />
</appSettings>
...
</configuration>
<!-- prod -->
<configuration>
<configSections>...</configSections>
<appSettings>
<add key="Deployment" value="prod" />
<add key="SourceDir" value="\\server\foo\prod" />
</appSettings>
...
</configuration>
And everytime the app needs to be published/deployed to a certain location, the other two are commented out. So if I build a DEV-release, I comment out the PROD and TEST sections. When publishing for production, ... you get the idea.
This is a useless time-consuming work. My question is: is there a way to avoid this? A way that I still have the separate deployment-specific configuration, but that I don't need to comment (out) the needed sections.

Azure Diagnostics won't create WADLogsTable

I wanted to add logging to an existing azure cloud service using NLog and Azure Diagnostics, i went through all the steps described here: http://msdn.microsoft.com/en-us/library/azure/dn482131.aspx
But, unfortunately when testing the cloud service the WADLogsTable table storage does not created, and no information gets stored here. Not on local development storage, nor on azure.
diagnostics.wadcfg:
<?xml version="1.0" encoding="utf-8"?>
<DiagnosticMonitorConfiguration configurationChangePollInterval="PT1M" overallQuotaInMB="4096" xmlns="http://schemas.microsoft.com/ServiceHosting/2010/10/DiagnosticsConfiguration">
<DiagnosticInfrastructureLogs />
<Directories>
<IISLogs container="wad-iis-logfiles" directoryQuotaInMB="1024" />
<CrashDumps container="wad-crash-dumps" />
</Directories>
<Logs bufferQuotaInMB="1024" scheduledTransferPeriod="PT1M" scheduledTransferLogLevelFilter="Verbose" />
<PerformanceCounters bufferQuotaInMB="512">
<PerformanceCounterConfiguration counterSpecifier="\Memory\Available MBytes" sampleRate="PT3M" />
</PerformanceCounters>
<WindowsEventLog bufferQuotaInMB="1024" scheduledTransferPeriod="PT1M" scheduledTransferLogLevelFilter="Verbose">
<DataSource name="Application!*" />
</WindowsEventLog>
</DiagnosticMonitorConfiguration>
ServiceConfiguration.Local.csfg (the same at Cloud, just with the correct table storage account)
<?xml version="1.0" encoding="utf-8"?>
<ServiceConfiguration serviceName="AAService" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration" osFamily="4" osVersion="*" schemaVersion="2014-01.2.3">
<Role name="Web.Services.AAService">
<Instances count="1" />
<ConfigurationSettings>
<Setting name="Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString" value="UseDevelopmentStorage=true" />
</ConfigurationSettings>
</Role>
</ServiceConfiguration>
ServiceDefinition.csdef
<?xml version="1.0" encoding="utf-8"?>
<ServiceDefinition name="AAService" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition" schemaVersion="2014-01.2.3">
<WebRole name="Web.Services.AAService" vmsize="ExtraSmall">
<Sites>
<Site name="Web">
<Bindings>
<Binding name="Endpoint1" endpointName="Endpoint1" />
</Bindings>
</Site>
</Sites>
<Endpoints>
<InputEndpoint name="Endpoint1" protocol="http" port="80" />
</Endpoints>
<Imports>
<Import moduleName="Diagnostics" />
</Imports>
<LocalResources>
<LocalStorage name="Web.Services.AAService.svclog" sizeInMB="1000" cleanOnRoleRecycle="false" />
</LocalResources>
</WebRole>
</ServiceDefinition>
WebRole entrypoint
public override bool OnStart()
{
// To enable the AzureLocalStorageTraceListner, uncomment relevent section in the web.config
DiagnosticMonitorConfiguration diagnosticConfig = DiagnosticMonitor.GetDefaultInitialConfiguration();
diagnosticConfig.Directories.ScheduledTransferPeriod = TimeSpan.FromMinutes(1);
diagnosticConfig.Directories.DataSources.Add(AzureLocalStorageTraceListener.GetLogDirectory());
DiagnosticMonitor.Start("Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString", diagnosticConfig);
// For information on handling configuration changes
// see the MSDN topic at http://go.microsoft.com/fwlink/?LinkId=166357.
return base.OnStart();
}
cloud service web.config also has:
<system.diagnostics>
<trace>
<listeners>
<add type="Microsoft.WindowsAzure.Diagnostics.DiagnosticMonitorTraceListener, Microsoft.WindowsAzure.Diagnostics, Version=2.3.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="AzureDiagnostics">
<filter type="" />
</add>
</listeners>
</trace>
</system.diagnostics>
Does anyone have any ide why is this doesnt make any trace logs? I called Trace.WriteLine() many places where the application goes through for sure, but still no WADLogsTable.
Thanks for the help
Get rid of all the Diagnostic* code in OnStart. The <Import moduleName="Diagnostics" /> in your csdef will automatically start the diagnostics agent, and the diagnostics agent will read the settings from the .wadcfg. Your code in OnStart is unnecessary and is overriding the wadcfg settings due to the order of precedence of loading WAD configuration.
Also note that if you are trying to update an existing cloud service then you will need to first delete the XML file in blob storage. If a file already exists in blob storage then the diagnostics agent will ignore the .wadcfg settings. The file you are looking for is in wad-control-container and will be named with the deployment ID and role name.

Visual Studio Express 2013 WPF warning: "The 'configuration' element is not defined" [duplicate]

I'm doing some work in Visual Studio 2012 Express Edition. I have added an App.config XML file as follows:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
</configuration>
The first thing that happens is a warning comes up that says "The 'configuration' element is not declared". Does anyone know why this is happening? It looks like elements can not be declared inside of until this is resolved.
Thanks!
This is the entire XML:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="Version" value="779" />
<add key="TimeOut" value="60000" />
<add key="LogFileName" value="Log.txt" />
<!-- your Developer Id with eBay -->
<add key="Environment.DevId" value="" />
<!-- your Application Id with eBay -->
<add key="Environment.AppId" value="" />
<!-- your Application Certificate with eBay -->
<add key="Environment.CertId" value="" />
<!-- API Server URL -->
<!-- For production site use: https://api.ebay.com/wsapi -->
<!-- For Sandbox use: https://api.sandbox.ebay.com/wsapi -->
<add key="Environment.ApiServerUrl" value="https://api.sandbox.ebay.com/wsapi" />
<!-- EPS Server URL -->
<!-- For production site use: https://api.ebay.com/ws/api.dll"/-->
<add key="Environment.EpsServerUrl" value="https://api.sandbox.ebay.com/ws/api.dll" />
<!-- eBay Signin URL -->
<!-- For production site use: https://signin.ebay.com/ws/eBayISAPI.dll?SignIn -->
<!-- https://signin.sandbox.ebay.com/ws/eBayISAPI.dll?SignIn -->
<add key="Environment.SignInUrl" value="https://signin.sandbox.ebay.com/ws/eBayISAPI.dll?SignIn" />
<!-- ViewItem URL -->
<!-- For production site use: http://cgi.ebay.com/ws/eBayISAPI.dll?ViewItem&item={0} -->
<add key="Environment.ViewItemUrl" value="http://cgi.sandbox.ebay.com/ws/eBayISAPI.dll?ViewItem&item={0}" />
<!-- token is for both API server and EPS server -->
<add key="UserAccount.ApiToken" value="" />
<!-- eBay site ID -->
<add key="UserAccount.eBayUserSiteId" value="0" />
<add key="logexception" value="true"/>
<add key="logmessages" value="true"/>
<add key="logsdkmessages" value="true"/>
<add key="logsdk" value="true"/>
<add key="logfile" value="Log.txt"/>
<!-- Rule Name-->
<add key="RuName" value=""/>
<!-- Set this if you access eBay API server behind a proxy server-->
<add key="Proxy.Host" value =""/>
<add key="Proxy.Port" value =""/>
<!-- set proxy server username/password if necessary-->
<add key="Proxy.Username" value=""/>
<add key="Proxy.Password" value=""/>
Go to XML menu (visual studio top menu item) choose schemas and find for DotNetConfig.xsd and choose Use this schema.
Your problem will resolve for sure
<configuration xmlns="schema URL">
<!-- configuration settings -->
</configuration>
do changes,like above & try
I had the same issue. It is not an error, it is simply a warning; so your application should still compile. I used the following simple config file and the warning is still produced.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime
version="v4.0"sku=".NETFramework,
Version=v4.5"/>
</startup>
</configuration>
It is an issue that has been raised on the MSDN website, but it does not seem to have been satisfactorily resolved. See link below:
http://social.msdn.microsoft.com/Forums/en-US/Vsexpressvcs/thread/18a1074f-668f-4fe3-a8d9-4440db797439
I had to
-> Go to XML menu (visual studio top menu item) choose schemas and select DotNetConfig.xsd AND RazorCustomSchema.xsd AND EntityFrameworkConfig_6_1_0.xsd
I just had this warning popup inside an autogenerated xml file while working on a xaml project.
Using Debug->Clean Solution and Debug->Rebuild Solution fixed it. Might want to try that before getting fancy with the schemas.
Visual Studio 2013 Express Edition is missing the DotNetConfig.xsd (https://connect.microsoft.com/VisualStudio/feedback/details/817322/dotnetconfig-xsd-files-not-present-in-vs-2013-express-for-desktop).
So to get rid of the warning in VS 2013 Express:
get a copy of DotNetConfig.xsd from another system or from the web (I used https://gist.github.com/eed3si9n/5dd7dd98ad2b3f668928b23477de35a3)
download to C:\Program Files (x86)\Microsoft Visual Studio 12.0\Xml\Schemas
add the schema following Ramakrishna's answer
The warning should be gone.
Choose use this schema. DotNetConfig.xsd
XLM Menu..... Visual Studio
Works perfectly.
I was having less space on my drive which might have resulted in incomplete loading of my application solution. This "the-configuration-element-is-not-declared" problem got solved after i created some space on my drive.
I also got the same warning. After thinking about for some time I realized my error working with SQL (MS SQL).
Warning: the 'configuration' element is not declared
Using C#
App.Config code:
<connectionStrings>
<add name="dbx" connectionString="Data Source=ServerNameHere;Initial Catalog=DatabaseNameHere;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
*this calls out the database name in the connectionStrings, when I plugged in my SQL code as a practice I always use the database name, schema, then table. This practice didn't carry over well in Visual Studio as I am a beginner. I removed the db name from my SQL syntax and only called from the schema, data table. This resolved the issue for me.
Form.CS:
using (SqlCommand cmd = new SqlCommand("SELECT * FROM [DatabaseName].[Schema].[TableName] WHERE [MEPeriod] = '2020-06-01'", con))
Updated to:
using (SqlCommand cmd = new SqlCommand("SELECT * FROM [Schema].[TableName] WHERE [MEPeriod] = '2020-06-01'", con))
This worked for me, I hope this is found as useful.

Mono webservice and basic authentication

I'm trying to add basic authenthication to my webservice. I followed steps from this article and ended up with this in my web.config file:
<configuration>
<httpModules>
<add name="BasicAuthenticationModule"
type="Mono.Http.Modules.BasicAuthenticationModule, Mono.Http, Version=2.0.0.0, PublicKeyToken=0738eb9f132ed756"/>
</httpModules>
<appSettings>
<add key="Authentication" value="Basic" />
<add key="Basic.Users" value="/home/vadmin/Projects/TestService/TestService/users.xml" />
<add key="Basic.Realm" value="My Realm" />
</appSettings>
</configuration>
My users.xml looks like this:
<?xml version="1.0" encoding="utf-8"?>
<users>
<user name="adrian" password="adrian">
<role name="user" />
</user>
</users>
When I run xsp2 and then go to
http://localhost:8080/TestService.asmx
user and password prompt appears. But after I enter correct user and password it asks me again and again. I'm pretty sure that path to users.xml file is correct, tried running xsp2 with --verbose options hoping for some error messages with no luck.
Can anyone help me debug this situation?
If you specify a path starting with / in config file, this will be interpreted not as a root directory of your filesystem, but as a root directory of your website - i.e. /home/vadmin/Projects/TestService/TestService.
So the path starting with / should be relative to website root folder - in your case this will be "/users.xml" if users.xml file is in project folder.

Categories