MSDeploy setParameter.xml not transforming web.config - asp.net

In my "myconfig" config profile transform for web.config i have this under appSettings:
<add key="my.config" xdt:Transform="SetAttributes" xdt:Locator="Match(key)" value="derp" />
When I msbuild with this transform the value is transformed correctly. Now I want to build an msdeploy package and transform this value at deploy time.
I drop this parameters.xml in my project root:
<?xml version="1.0" encoding="utf-8" ?>
<parameters>
<parameter name="my.config" description="sdfsdfsdfsd" defaultValue="fart">
<parameterEntry kind="XmlFile"
scope="\\Web\.config$"
match="/configuration/appSettings/add[#my.config]/#value/text()" />
</parameter>
</parameters>
I build my package
msbuild app.csproj /T:Package /p:Configuration=myconfigprofile;PackageLocation=mydeploy.zip
I look at mydeploy.SetParameters.xml
<?xml version="1.0" encoding="utf-8"?>
<parameters>
<setParameter name="IIS Web Application Name" value="Default Web Site/myApp_deploy" />
<setParameter name="my.config" value="fart" />
</parameters>
Then I go into parameters.xml inside of mydeploy.zip and see its there too:
<parameters>
<parameter name="my.config" description="sdkflsdjfldfj" defaultValue="fart">
<parameterEntry kind="XmlFile" scope="\\Web\.config$" match="/configuration/appSettings/add[#name='my.config']/#value/text()" />
</parameter>
</parameters>
looks good so far, then i deploy:
mydeploy.deploy.cmd /Y /M:server1
I look at web.config on the deploy server and the value is not transformed. I see no errors either, how do i debug this even?
When I run msbuild with parameters.xml present what magic happens there? How is the package preps to be able to transform web.config via parameters to web deploy?

This:
add[#name='my.config']
Had to be changed to this:
add[#key='my.config']
But the bigger question remains, how do I debug? I had to try a million times and just guess because I had zero errors/logs to help troubleshoot this. Is there verbose logging or some kind of validator or anything at all?
For debugging technet gave me this to try:
msbuild MyProject.proj /t:go /fl /flp:logfile=MyProjectOutput.log;verbosity=diagnostic

If you are using MSDeploy you can get the full output of the deployment by using the following:
msdeploy -verb:sync -source:dirpath=C:\WebDeployDemo\Src -dest:dirpath=C:\WebDeployDemo\Dst -setParamFile=C:\WebDeployDemo\ParameterFile.xml -verbose >msdeploysync-verbose.log
This helps with VSTS WebRM deployment debugging if you use the -verbose flag.
Sources:
https://learn.microsoft.com/en-us/iis/publish/troubleshooting-web-deploy/troubleshooting-web-deploy
https://blogs.msdn.microsoft.com/spike/2012/10/12/using-msdeploy-to-update-and-remove-sections-in-web-config-a-simple-example/

Related

TFS variables for connection string

I have continous integration in tfs project. I want to replace connection string to my production db on release, but all the information on the web is confusing. I created parameters.xml with this:
<?xml version="1.0" encoding="utf-8" ?>
<parameters>
<parameter name="connectionString" description="connectionString" defaultvalue="(localdb)\MSSQLLocalDB;InitialCatalog=BlogsPostsLocalDb;Integrated Security=true;" tags="">
<parameterentry kind="XmlFile" scope="\\web.config$" match="What to write here?" />
</parameter>
</parameters>
In TFS, in my App Deploy task I can see SetParameters File option, so I suspect that I have to use that, but I don't understand how to tell it which parameter in Web.config belongs to the parameter in parameters.xml.
In my Web.config I need to replace static path with the one in parameters.xml. My Web.config:
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
<parameters>
<parameter value="Data Source=(localdb)\MSSQLLocalDB;InitialCatalog=BlogsPostsLocalDb;Integrated Security=true;" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
EDIT:
I used a tool to create parameters.xml which now looks like this:
<parameters>
<parameter name="ConnectionString" description="Description for ConnectionString" defaultvalue="__CONNECTIONSTRING__" tags="">
<parameterentry kind="XmlFile" scope="\\web.config$" match="/configuration/appSettings/add[#key='ConnectionString']/#value" />
</parameter>
</parameters>
My web.config:
<appSettings>
<add key="ConnectionString" value="Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=BlogsPostsTestDb;Integrated Security=True" />
</appSettings>
And in my context I do this:
public BlogsPostsContext() : base(WebConfigurationManager.AppSettings["ConnectionString"]) { }
In TFS I set variable for relase
Name | Value
ConnectionString | Data Source=WIN-7ADV5BGRBE3\SQLEXPRESS;Initial Catalog=BlogsPostsDb;Integrated Security=True
Unfortunately when I do a release and look inside web.config on my server I can only see <add key="ConnectionString" value="__CONNECTIONSTRING__" />
And the parameters.xml:
<parameters>
<parameter name="ConnectionString" description="Description for ConnectionString" defaultvalue="__CONNECTIONSTRING__" tags="">
<parameterentry kind="XmlFile" scope="\\web.config$" match="/configuration/appSettings/add[#key='ConnectionString']/#value" />
</parameter>
</parameters>
MsBuild Arguments:
/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:PackageLocation="$(build.artifactstagingdirectory)\\"
A [project name].SetParameters.xml file is generated when you build a web application project.
This provides a set of parameter values to the MSDeploy.exe command.
You can update the values in this file and pass it to Web Deploy as a
command-line parameter when you deploy your web package.
Since SetParameters.xml file is dynamically generated from your web application project file and any configuration files within your project.
You could also parameterize additional settings by adding a parameters.xml file to your project. Below entry uses an XML Path Language (XPath) query to locate and parameterize the endpoint URL of the ContactService Windows Communication Foundation (WCF) service in the web.config file.
<parameters>
<parameter name="ContactService Service Endpoint Address"
description="Specify the endpoint URL for the ContactService WCF
service in the destination environment"
defaultValue="http://localhost/ContactManagerService">
<parameterEntry kind="XmlFile" scope="Web.config"
match="/configuration/system.serviceModel/client
/endpoint[#name='BasicHttpBinding_IContactService']
/#address" />
</parameter>
</parameters>
The WPP also adds a corresponding entry to the SetParameters.xml file that gets generated alongside the deployment package.
<parameters>
...
<setParameter
name="ContactService Service Endpoint Address"
value="http://localhost/ContactManagerService" />
...
</parameters>
For how to tell it which parameter in Web.config belongs to the parameter in parameters.xml, you need to use match entry to do this. More detail info please refer this tutorial: Configuring Parameters for Web Package Deployment
I had a similar problem, I am using ASP.Net and IIS application in visual studio i created a configuration for example Test for any cpu then i publish a IIS file called test.pubxml (right click on project click publish), That file will contain the connection string from your web.config file , Then you right-click on the file you have created (test.pubxml) and select add web-transform file add the following iy you want to transform the connection string.
<connectionStrings>
<add name="test" connectionString="Data Source=(localdb)\MSSQLLocalDB;InitialCatalog=BlogsPostsLocalDb;Integrated Security=true;" xdt:Transform="SetAttributes(connectionString)" xdt:Locator="Match(name)" />
</connectionStrings>
Now you have a file that will do the exact same things as parameters.xml file.
In your build defition add the following to ms arguments
/p:DeployOnBuild=true;PublishProfile=test.pubxml;Configuration=Test;
That will build a .zip package,
In your Release Definition
In your deploy step, Add the following to your Web Deploy Parameter File input
Path/Path/test.SetParameters.xml
That Will deploy the website with transformed connection string.
I know its a lot but you can follow this blog its really helpfull Colin
Following articles about parameters.xml mislead me. It turns out that all I had to do to make it work was to follow instructions inside Web.Release.Config.
This is what I added inside Web.config:
<connectionStrings>
<add name="BlogsPostsDb"
providerName="System.Data.SqlClient"
connectionString="Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=BlogsPostsTestDb;Integrated Security=True"/>
</connectionStrings>
And this is what I added inside Web.Release.Config:
<connectionStrings>
<add name="BlogsPostsDb"
connectionString="Data Source=myServerInstance;Initial Catalog=BlogsPostsDb;User ID=*********;Password=******;"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
Now it makes sense. As long as the names match in both configs, they will be replaced. I also had to tell my context which connection string I want to use:
public BlogsPostsContext() : base("BlogsPostsDb") { }

NLog does not create log files on ASP.NET Core project

I am new to using NLog with ASP.NET Core, so I have followed the guide here:
https://github.com/NLog/NLog.Web/wiki/Getting-started-with-ASP.NET-Core-(project.json)
I have created the following nlog.config file at the root of the project directory:
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
autoReload="true"
internalLogLevel="Warn"
internalLogFile="c:\temp\internal-nlog.txt">
<extensions>
<add assembly="NLog.Web.AspNetCore"/>
</extensions>
<!-- define various log targets -->
<targets>
<!-- write logs to file -->
<target xsi:type="File" name="allfile" fileName="${basedir}\nlog-all-${shortdate}.log"
layout="${longdate}|${event-properties:item=EventId.Id}|${logger}|${uppercase:${level}}|${message} ${exception}" />
<target xsi:type="File" name="ownFile-web" fileName="${basedir}\nlog-own-${shortdate}.log"
layout="${longdate}|${event-properties:item=EventId.Id}|${logger}|${uppercase:${level}}| ${message} ${exception}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}" />
<target xsi:type="Null" name="blackhole" />
</targets>
<rules>
<!--All logs, including from Microsoft-->
<logger name="*" minlevel="Trace" writeTo="allfile" />
<!--Skip Microsoft logs and so log only own logs-->
<logger name="Microsoft.*" minlevel="Trace" writeTo="blackhole" final="true" />
<logger name="*" minlevel="Trace" writeTo="ownFile-web" />
</rules>
</nlog>
Inside a controller, I call a line like this one:
_logger.LogInformation("Entered CustomerRange method");
which returns the following in the output window in Visual Studio:
CustomerMgmtAPI.Controllers.CustomerController:Information: Entered CustomerRange method
However, the actual log files are never created by NLog. I was wondering if someone can point out the error in the NLog configuration here, since I have been reviewing the documentation of NLog for ASP.NET Core project and I can't find the error myself.
So the actual fix to the problem was the remove the first line from the nlog.config file:
<?xml version="1.0" encoding="utf-8" ?>
I remove this line and everything started working as expected. I also noticed that Visual Studio was giving me these errors when that line was present:
Invalid token 'Text' at root level of document.
Unexpected XML declaration. The XML declaration must be the first node in the document and no white space characters are allowed to appear before it.
It seems in this case that the NLog tutorial is broken, as I just took over this file from the sample for ASP.NET Core. I am using VS2017, so perhaps there is an incompatibility with this version of VS?
The dirty little secret about using NLog with ASP.NET Core is that you can configure and create logs just as you did in ASP.NET and ASP.NET MVC. You just use the regular NLog Nuget package like you normally would.
Just create an NLog.config in your root, etc. You don't even have to make any extra configurations in the config or elsewhere to get it to work. You just reference NLog in your class and then create a logger with the LogManager.
What this means is that you don't have all of the wireup in Program.cs etc.

Creating a server-level Web Deploy package

I can use Web Deploy to create packages that can be imported in existing IIS sites after the IIS server administrator manually creates the site for me.
Can I use Web Deploy to create packages that can be imported as a site instead of an application?
When I try to import my existing packages, I get this ugly error.
This might not be an answer to the question as was not able to find information on how to create a site package.
But as you asked for it in the comments, here's my approach on how to use the .cmd file created by the package process to install on site level.
Step 1
I create a package in our build process with msbuild. I've just added an extra step to our normal build that creates the project files in a deploy directory.
<Target Name="CreateDeploymentPackage">
<MSBuild Projects="$(CurrentProject).csproj" Targets="Package"
properties="Platform=$(Platform);
Configuration=$(Configuration);
DeployOnBuild=false;
DeployTarget=Package;
PublishProfile=$(Environment);
PackageLocation=$(DeployDirectory)\_PublishedWebsites\DeployPackage\$(CurrentProject).zip;
PackageAsSingleFile=true;
_PackageTempDir=$(PackageOutputDir)\temp;">
</MSBuild>
</Target>
I did set a specific PublishProfile to be able to pass a Web.config transformation for everything I know at build time.
Step 2
I've created a Parameters.xml in my project to be able to change params on install time of the package.
<?xml version="1.0" encoding="utf-8" ?>
<parameters>
<parameter name="Log4net to email"
description="Please provide the email address for Log4net."
defaultValue="itsupport#ourcompany.com"
tags="">
<parameterEntry kind="XmlFile" scope="\\web.config$" match="//log4net/appender[#name='SmtpAppender']/to/#value" />
</parameter>
<parameter name="Webservice address"
description="Please provide the endpoint address for the document web service"
defaultValue="http://test.services.ourcompany.com/Service.svc"
tags="">
<parameterEntry kind="XmlFile" scope="\\web.config$" match="//system.serviceModel/client/endpoint/#address" />
</parameter>
<parameter name="Elmah error email subject"
description="Please provide the elmah errormail subject"
defaultValue="Our Portal (Production) | An unexpected error occurred"
tags="">
<parameterEntry kind="XmlFile" scope="\\web.config$" match="//elmah/errorMail/#subject" />
</parameter>
</parameters>
You might think why there is no sitename and connection string in the Parameters.xml. But these are created automatically configurable when the deploy package is created and can be set with the SetParameters.xml
Read here: Why are some Web.config transforms tokenised into SetParameters.xml and others are not?
Step 3
Then I created a SetParameters-.xml for every environmen we have (prod, staging, test, dev). Here's one example for staging:
<?xml version="1.0" encoding="utf-8"?>
<parameters>
<setParameter name="IIS Web Application Name" value="staging-sitename.ourcompany.com" />
<setParameter name="Log4net to email" value="webdev#ourcompany.com" />
<setParameter name="Webservice address" value="http://staging.services.ourcompany.com/Service.svc" />
<setParameter name="Elmah error email subject" value="Our Portal (Staging) | An unexpected error occurred" />
<setParameter name="PortalEntities-Web.config Connection String" value="metadata=res://*/Src.Entity.PortalEntities.csdl|res://*/Src.Entity.PortalEntities.ssdl|res://*/Src.Entity.PortalEntities.msl;provider=System.Data.SqlClient;provider connection string="Data Source=test.sql.ourcompany.com;Initial Catalog=Portal_Staging;User Id=<userid>;Password=<password>;Application Name ='OurPortal';Connection Timeout=180;MultipleActiveResultSets=True"" />
</parameters>
Step 4
Then I excecute the install on the server with the following command
Portal.Web.deploy.cmd /Y -setParamFile:"Portal.Web.SetParameters-STAGING.xml"
There's still a lot of room for improvement, and I would like to automate more but this is what I have right now.
Basically instead of installing at the root of the site, deploy to a folder within the site!

BizTalk ListApp command line

I used the following code in c# to get policies\rules from deployed application in BizTalk server.
BTSTask.exe ListApp -ApplicationName:"EAISolution" -ResourceSpec:"c:\EAISolution.PolicyInf
o.xml" /Server:VHYDTRBELSUP-02 /Database:BizTalkMgmtDb
From above command I got the output as below
<?xml version="1.0" encoding="utf-16" ?>
<ResourceSpec xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" ApplicationName="EAISolution" xmlns="http://schemas.microsoft.com/BizTalk/ApplicationDeployment/ResourceSpec/2004/12">
<Resources>
<Resource Type="System.BizTalk:BizTalkAssembly" Luid="EAIOrchestration, Version=1.0.0.0, Culture=neutral, PublicKeyToken=97e0f507fd7fd10d" />
<Resource Type="System.BizTalk:BizTalkAssembly" Luid="EAIServices, Version=1.0.0.0, Culture=neutral, PublicKeyToken=97e0f507fd7fd10d" />
<Resource Type="System.BizTalk:BizTalkAssembly" Luid="FFSchemasTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=97e0f507fd7fd10d" />
<Resource Type="System.BizTalk:Rules" Luid="RULE/ProcessPurchaseOrder/1.0" />
<Resource Type="System.BizTalk:BizTalkBinding" Luid="Application/EAISolution" />
</Resources>
</ResourceSpec>
and from BizTalk server I got the below output using policy export in BizTalk server administration
<?xml version="1.0" encoding="utf-8" ?>
<brl xmlns="http://schemas.microsoft.com/businessruleslanguage/2002">
<ruleset name="ProcessPurchaseOrder">
<version major="1" minor="0" description="" modifiedby="username" date="2013-05- 27T12:04:55.6121122+05:30" />
<configuration />
<bindings>
<xmldocument ref="xml_31" doctype="RuleTest.PO" instances="16" selectivity="1" instance="0">
<selector>/*[local-name()='PurchaseOrder' and namespace-uri() ='http://EAISolution.PurchaseOrder']/*[local-name()='Item' and namespace-uri()='']</selector>
<selectoralias>/PurchaseOrder/Item</selectoralias>
<schema>....\PO.xsd</schema>
</xmldocument>
<xmldocument ref="xml_32" doctype="RuleTest.PO" instances="16" selectivity="1" instance="0">
<selector>/*[local-name()='PurchaseOrder' and namespace-uri()='http://EAISolution.PurchaseOrder']
</selector>
<selectoralias>/PurchaseOrder</selectoralias>
<schema>....\PO.xsd</schema>
</xmldocument>
</bindings>
<rule name="ApprovalRule" priority="0" active="true">
<if>
<compare operator="less than or equal to">
<vocabularylink uri="3f0e9bcc-6212-4e6a-853c-e517f157a626" element="d4eb2deb-06d3-42c4-af49-ceb21331b1cc" />
<lhs>
<function>
<xmldocumentmember xmldocumentref="xml_31" type="int" sideeffects="false">
<field>*[local-name()='Quantity' and namespace-uri()='']</field>
<fieldalias>Quantity</fieldalias>
</xmldocumentmember>
</function>
</lhs>
<rhs>
<constant>
<int>500</int>
</constant>
</rhs>
</compare>
</if>
<then>
<function>
<xmldocumentmember xmldocumentref="xml_32" type="string" sideeffects="true">
<field>*[local-name()='Status' and namespace-uri()='']</field>
<fieldalias>Status</fieldalias>
<argument>
<constant>
<string>Approved</string>
</constant>
</argument>
</xmldocumentmember>
</function>
</then>
</rule>
</ruleset>
</brl>
So please let me know how to get the output of second using command line.
BTSTask will only export the policy as part of an MSI (see below).
You could then extract the MSI (see How to extract msu/msp/msi fileds from the command line) to get the policy file.
From How to Import a Policy
BTSTask does not provide a specific command for importing (or exporting) policies; however you can use the ExportApp command of BTSTask to selectively export only the policies in an application that you want, including no other application artifacts. Then you can use the ImportApp command to import the .msi file into an application in a different BizTalk group. This is the approach described in this topic. When you do this, the policy is automatically imported and published in the BizTalk group and added to the specified application.
The below steps will get export the policy, but as part of an MSI.
From How to Export a Policy
Use the BTSTask ListApp command with the /ResourceSpec option to generate an XML file that lists the artifacts in the BizTalk application from which you want to export a policy, as described in ListApp Command.
Edit the XML file generated in the previous step, deleting all of the artifacts except for the policy or policies that you want to export.
Use the BTSTask ExportApp command, and specify the modified XML file for the /ResourceSpec parameter. For more information, see ExportApp Command.
BTSTask exports the specified policies and all of their associated vocabularies into an application .msi file.

How do I target an already existing application pool with webdeploy?

I am trying to make sure that my app gets deployed to a specific application pool that already exists when using Web Deploy. The application pool should be configurable by the user using the GUI when installing app through IIS Manager or by changing the value in the .setparameters.xml file when installing via the commandline from a web package. Inserting the following parameter entry into my parameters.xml doesn't do the trick.
<parameter name="Application Pool" description="Application Pool for this site" tags="iisApp" defaultValue="ASP.NET v4.0">
<parameterEntry kind="providerPath" scope="IisApp" match="applicationPool" />
</parameter>
Is there a straightforward way to accomplish this? If not, how would I go about getting this done?
Here's what I did to set the application pool via command line or SetParameters.xml after lots of reading on SO and elsewhere:
Add a Parameters.xml file to the project.
<?xml version="1.0" encoding="utf-8" ?>
<parameters>
<parameter name="AppPool" defaultValue="ASP.NET 4.0">
<parameterEntry kind="DeploymentObjectAttribute" scope="application" match="applicationPool/#applicationPool" />
</parameter>
</parameters>
Sources:
How to specify MSDeploy parameters from MSbuild
http://vishaljoshi.blogspot.com/2010/07/web-deploy-parameterization-in-action.html
Add two parameters to msbuild when creating the package:
/P:IncludeIisSettings=true
/P:IncludeAppPool=true
Source:
https://stackoverflow.com/a/13678143/448876
Set via SetParameters.xml:
<setParameter name="AppPool" value="Some AppPoolName"/>
OR
Using command line parameter (msdeploy or *.deploy.cmd):
"-setParam:'AppPool'='Some AppPoolName'"

Resources