msdeploy and setparameters.xml - asp.net

I'm having issue with the SetParameters.xml file used by msdeploy.
In my website I've created a parameters.xml file which contains the following:
<parameters>
<parameter name="enableRule" defaultValue="DoNotDeleteRule"></parameter>
</parameters>
During the build of the deployment package, this parameter is added to the setParameters.xml file, which then looks like:
<parameters>
<setParameter name="IIS Web Application Name" value="mywebsite/myservice/>
<setParameter name="enableRule" value="DoNotDeleteRule" />
</parameters>
Yet, when I run msdeploy (using the cmd of the package), the parameter is ignored. I've tried calling the cmd by adding the -setParamFile attribute, but that didn't make any difference...
Can someone help me with this ?

You are confusing the concept of MSDeploy "parameters" with msdeploy.exe arguments. The latter contains features that cannot be specified using the former. For example "verb", "source", "dest", "enableLink", etc
Your only choice is to pass "-enableRule:DoNotDeleteRule" as an actual command line arguments to msdeploy.exe (I believe tacking it onto the end of your call to the cmd file will also suffice)

You can use the option -setParamFile I was using it like:
-setParamFile=file.xml and getting errors, but had success with -setParamFile:file.xml
Note the : instead of = like the doco says

Related

What Dotnet native directives to use for AsyncAwaitBestPractices.MVVM?

I'm using AsyncAwaitBestPractices.MVVM's IAsyncCommand and AsyncCommand in my Xamarin.Forms app. The UWP version of the app is compiled with .NET Native tool chain. When I do SomeAsyncCommand.RaiseCanExecuteChanged(), I get an exception:
System.Reflection.MissingMetadataException: 'This operation cannot be carried out because metadata for the following object was removed for performance reasons:\n\n EETypeRva:0x000976A0\n\nNo further information is available. Rebuild in debug mode for better information.\n\n'
Note that this was a debug build. When I added a local copy of the library, I was able to find the line that triggers the exception:
static bool IsLightweightMethod(this MethodBase method)
{
var typeInfoRTDynamicMethod = typeof(DynamicMethod).GetTypeInfo().GetDeclaredNestedType("RTDynamicMethod");
return ...
}
The exception is triggered by GetDeclaredNestedType("RTDynamicMethod"). So the binaries do include the metadata of DynamicMethod, but not that of it's child type RTDynamicMethod. They have been removed because of .NET Native tool chain.
Now, I read that you can whitelist classes / namespaces / assemblies in project properties -> Default.rd.xml. But I can't seem to get the right element to whitelist the nested class. Here's what I tried:
<Assembly Name="System.Private.CoreLib" Dynamic="Required All" />
<Namespace Name="System.Reflection.Emit" Dynamic="Required All" />
<Type Name="System.Reflection.Emit.DynamicMethod">
<Type Name="RTDynamicMethod" Dynamic="Required All"/>
</Type>
Here System.Private.CoreLib is the assembly of DynamicMethod, System.Reflection.Emit is the namespace of DynamicMethod and RTDynamicMethod. As far as I understand, either of the three should work, yet none of them do. Edit: the type one gives me a warning: Default.rd.xml(35): warning : ILTransform : warning ILT0027: Type 'System.Reflection.Emit.DynamicMethod' could not be found.
I also tried variations using Type Name="System.Reflection.Emit.DynamicMethod+RTDynamicMethod", using <Library>, with or without namespaces in type, etc.
At first, you can try to use the following code in the Default.rd.xml:
<Type Name="System.Reflection.Emit.DynamicMethod" Dynamic="Required All">
<Type Name="System.Reflection.Emit.RTDynamicMethod" Dynamic="Required All">
For more information, you can check the similar case and the issue on the github.
In addition, why did you add the tag .net 6.0? I have checked the package named System.Reflection.Emit, the last update time is 12/4/2019 and it does't support the .net 6.0.

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!

publish a website through MSBuild

I am trying to publish an asp.net website through msbuild on command prompt, but I did not get success.
I tried by creating a new web application and execute below command, it works.
C:\Windows\system32>msbuild.exe "C:\VisualStudio 2012\Projects\HelloWorldSample\HelloWorldSample\HelloWorld\HelloWorldSample.csproj" /p:DeployOnBuild=true /p:PublishProfile="HelloDeploy"
But my problem is, in website I don't have .csproj file. That's why I am unable to execute above command for that.
So please any one help me how I can do deploy for a web site.
Thanks.
If your website has no csproj project file it most likely is a directory containing just your website files. You can convert it to a Web Application (thus getting a csproj file) or directly use msdeploy to generate a deployable package and then use msdeploy to deploy it to your website. This is wat actually would happen under the hood when you run msbuild /p:DeployOnBuild
To create a msdeploy deployable website you can use the following snippet
msdeploy.exe
-verb:sync
-source:iisApp="C:\development\mywebsite\the-website-dir"
-dest:package="C:\temp\mywebsite-package.zip"
-declareParamFile:"C:\development\mywebsite\parameters.xml"
You will need to create at a parameter for the site name in the parameters.xml file which will be packaged with the zipped website project
<?xml version="1.0" encoding="utf-8" ?>
<parameters>
<!--
This file contains (among others) references to web.config fields (xpath)
which will be 'parameterized' on package before deploy. The actual values will then be filled in based on the given deploy environment.
-->
<parameter name="IIS Web Application Name" tags="IisApp">
<parameterEntry kind="ProviderPath" scope="iisApp" match="" tags="IisApp" />
</parameter>
</parameters>
This will generate a deployable msdeploy package which you can upload to your IIS by using:
msdeploy.exe -source:package=c:\temp\mywebsite-package.zip -dest:auto,computerName=https://mysite.example.com:8172/MsDeploy.axd,userName=USERNAME,password=PASSWORD,authtype=Basic, -verb:sync -disableLink:AppPoolExtension -disableLink:ContentExtension -disableLink:CertificateExtension -setParamFile:example-setParameters.xml -allowUntrusted
Together with a setParameter file
<?xml version="1.0" encoding="utf-8" ?>
<parameters>
<setParameter name="IIS Web Application Name" value="mysite.example.com" />
</parameters>

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'"

How do I get msdeploy to create App_Data if it doesn't exist, but not delete any contents of the remote directory?

I have an application setup with the following Package/Publish Web settings:
Only files needed to run this application
(unchecked) Exclude generated debug symbols
(checked) Exclude files from the App_Data folder
(checked) Include all databases configured in Package/Publish SQL tab - note I do not have any databases configured
(unchecked) include IIS settings as configured in IIS Express
In the project, I have an App_Data folder setup, primarily to handle application logs.
The behavior I'd like to see (and expect) is the following:
On initial deploy to a brand new server, the application is copied and an App_Data folder is created with write permissions assigned for the application.
On subsequent deployments, the App_Data folder is ignored because it already exists and the "Exclude files from the App_Data folder" is checked.
However, msdeploy does not appear to do step #1 (step 2 is fine if I create the folder manually). I've been unable to find any documentation on the web besides this unanswered so question that seems to confirm the behavior I see.
How do I get msdeploy to create App_Data and assign permissions on initial deployment in this scenario?
Getting App_Data deployed when starting from scratch
#tdykstra got this part right. To get App_Data out there (and ACLs set automatically), I did the following:
Adding a placeholder file in App_Data
Set the build action to content on the placeholder (my placeholder file has text in it to let people stumbling across it know why it's there).
Unchecked "Exclude files from the App_Data folder" on the Package/Publish Web tab of the project properties in VS 2010
This gets my App_Data folder created and ready for use on the server. However, it will result in all my files getting deleted whenever I republish. This is problem #2 in my question above, and pretty closely resembles this other SO question/answer.
Preventing data on the server from being deleted on subsequent publish events
There are two mechanisms in MsDeploy that can get confused (at least I confused them):
Excluding files
MsDeploy skip rules
These can both be used to solve the problem, depending on the scenario:
#tdykstra's solution will likely work if you:
Know the names of the files in App_Data in advance (e.g. a sqllite database)
Have the files included in the App_Data folder in your project
The use MsDeploy skip rules to tell MsDeploy to completely skip all deletes on the server for that directory and files in that directory. This solves the problem in all cases, but is much more involved.
Implementing MsDeploy skip rules
To implement skip rules you'll have to abandon the right-click, Deploy option in VS 2010 in favor of right-click, Package, go into a command line, re-jigger a batch file and run a command line). If you're willing to put up with this experience (I am, because I'm automating it all through a CI process), here are the details:
Edit the project file and add the following. Note that the AbsolutePath argument is a regular expression, so you can get way fancy:
<Target Name="AddCustomSkipRules">
<ItemGroup>
<MsDeploySkipRules Include="SkipDeleteAppData">
<SkipAction>Delete</SkipAction>
<ObjectName>filePath</ObjectName>
<AbsolutePath>$(_Escaped_PackageTempDir)\\App_Data\\.*</AbsolutePath>
<XPath>
</XPath>
</MsDeploySkipRules>
<MsDeploySkipRules Include="SkipDeleteAppData">
<SkipAction>Delete</SkipAction>
<ObjectName>dirPath</ObjectName>
<AbsolutePath>$(_Escaped_PackageTempDir)\\App_Data\\.*</AbsolutePath>
<XPath>
</XPath>
</MsDeploySkipRules>
</ItemGroup>
</Target>
Package, do not deploy the project. This will create a zip file and .cmd file in the target directory (defined by "Location where package will be created" on the Package/Publish Web Tab). By default, this is obj\Debug\Package (or obj\Release\Package)
Deploy the site using the the resulting command file
In my testing, you must package and run the command file. The project file tweaks will tell msbuild to put the necessary -skip rule into the command file. However, using the "publish" feature straight from VS 2010 doesn't seem to run the command file (see the warning on this walkthrough)...it calls msdeploy directly and doesn't seem to honor the project file skip rules. I believe this is the difference between VS using msbuild -T:Package and msbuild -T:MsDeployPublish to build the project, but I have not tested this.
Finally, the command file isn't quite correct, at least in VS 2010 SP1. There's a great description of what goes wrong in this SO answer, but basically, VS (or maybe the /t:Package target is a better culprit) sets up the command file to publish to the machine without specifying a site. To fix that, you'll need to somehow get "?site=sitename" (probably this is ?site=Default+Web+Site, for a full URL of https://machine:8172/MsDeploy.axd?site=Default+Web+Site) onto the end of the computerName argument.
The problem I had was that the command file (batch file) has a hard time with using site= anything on the command line since it mis-parses the command line argument (even if escaped). I don't see a way around this problem other than modifying the cmd file directly, but for testing I copied the msdeploy.exe output I saw from my failed test run and modified that to call msdeploy.exe directly without the script.
Now that it's working, my intention is to work this into my CI build processes. What I'll be doing for the final solution is:
Change my build script to use /T:Package (right now it's /T:MsDeploy)
Have a scripted search/replace routine alter the generated cmd deployment script
Run the altered deployment script
This really should be easier.
Update
Here's the scripted search/replace routine I've come up with in PowerShell:
(Get-Content "project.deploy.cmd")
-replace('^set _ArgComputerName=$'
,"set ArgComputerName=https://server:8172/MsDeploy.axd?Site=Default+Web+Site")
| Out-File -Encoding ascii deploy.cmd
Once that is run, deploy.cmd can be called (without the /M option) and it will work as expected.
Web Deploy won't create a folder if there are no files to copy to it. One workaround in your scenario would be to not use the Exclude files from the App_Data folder check box, put a dummy file in App_Data (such as a .txt file with nothing in it), and specify file exclusion rules for whatever else you have in the App_Data folder (such as your .sdf file).
On excluding individual files (you can use wildcards), see the first question in the deployment FAQ on MSDN:
http://msdn.microsoft.com/en-us/library/ee942158.aspx#can_i_exclude_specific_files_or_folders_from_deployment
On using the dummy file method for causing a folder to be created, see Making Sure that the Elmah Folder gets Deployed in this tutorial:
http://www.asp.net/web-forms/tutorials/deployment-to-a-hosting-provider/deployment-to-a-hosting-provider-configuring-project-properties-4-of-12
I managed to get it working when using the Publish Web dialog from within Visual Studio. Note: it works for any folder and not only App_Data.
This is the basic .pubxml profile:
<?xml version="1.0" encoding="utf-8"?>
<!--
This file is used by the publish/package process of your Web project. You can customize the behavior of this process
by editing this MSBuild file. In order to learn more about this please visit http://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<AfterAddIisSettingAndFileContentsToSourceManifest>AddCustomSkipRules</AfterAddIisSettingAndFileContentsToSourceManifest>
<WebPublishMethod>MSDeploy</WebPublishMethod>
<LastUsedBuildConfiguration>Local</LastUsedBuildConfiguration>
<LastUsedPlatform>Any CPU</LastUsedPlatform>
<SiteUrlToLaunchAfterPublish />
<ExcludeApp_Data>False</ExcludeApp_Data>
<MSDeployServiceURL>localhost</MSDeployServiceURL>
<DeployIisAppPath>SuperCoolAwesomeAppName</DeployIisAppPath>
<RemoteSitePhysicalPath />
<SkipExtraFilesOnServer>False</SkipExtraFilesOnServer>
<MSDeployPublishMethod>InProc</MSDeployPublishMethod>
<EnableMSDeployBackup>False</EnableMSDeployBackup>
<UserName />
<_SavePWD>False</_SavePWD>
<LaunchSiteAfterPublish>True</LaunchSiteAfterPublish>
</PropertyGroup>
<PropertyGroup>
<UseMsDeployExe>true</UseMsDeployExe>
</PropertyGroup>
<Target Name="CreateEmptyFolders">
<Message Text="Adding empty folders to Files" />
<MakeDir Directories="$(_MSDeployDirPath_FullPath)\Files\Folder 1" />
<MakeDir Directories="$(_MSDeployDirPath_FullPath)\Files\Folder 2" />
<MakeDir Directories="$(_MSDeployDirPath_FullPath)\Files\Folder 3\Test"/>
</Target>
<Target Name="AddCustomSkipRules" DependsOnTargets="CreateEmptyFolders">
<Message Text="Adding Custom Skip Rules" />
<ItemGroup>
<MsDeploySkipRules Include="SkipFilesInFilesFolder">
<SkipAction>Delete</SkipAction>
<ObjectName>filePath</ObjectName>
<AbsolutePath>$(_DestinationContentPath)\\Files\\.*</AbsolutePath>
<Apply>Destination</Apply>
</MsDeploySkipRules>
<MsDeploySkipRules Include="SkipFoldersInFilesFolders">
<SkipAction></SkipAction>
<ObjectName>dirPath</ObjectName>
<AbsolutePath>$(_DestinationContentPath)\\Files\\.*\\*</AbsolutePath>
<Apply>Destination</Apply>
</MsDeploySkipRules>
</ItemGroup>
</Target>
</Project>
Here's a detailed post explaining it:
Using MsDeploy publish profile .pubxml to create an empty folder structure on IIS and skip deleting it with MsDeploySkipRules
Summarizing and simplifying Emil and Leniel answers in a concise one, if you just want to allow App_Data deploy for adds and updates, but prevents deletes, add this to your .pubxml.
<Project>
...
<PropertyGroup>
<UseMSDeployExe>true</UseMSDeployExe>
<ExcludeApp_Data>False</ExcludeApp_Data>
</PropertyGroup>
<Target Name="AddCustomSkipRules"
AfterTargets="AddIisSettingAndFileContentsToSourceManifest">
<Message Text="Adding Custom Skip Rules" />
<ItemGroup>
<MsDeploySkipRules Include="SkipDeleteAppData">
<SkipAction>Delete</SkipAction>
<ObjectName>filePath</ObjectName>
<AbsolutePath>App_Data\\.*</AbsolutePath>
</MsDeploySkipRules>
<MsDeploySkipRules Include="SkipDeleteAppData">
<SkipAction>Delete</SkipAction>
<ObjectName>dirPath</ObjectName>
<AbsolutePath>App_Data</AbsolutePath>
</MsDeploySkipRules>
</ItemGroup>
</Target>
</Project>
<UseMSDeployExe>true</UseMSDeployExe> is really needed or it will fail complaining Unrecognized skip directive 'skipaction'.

Resources