WPF Clickonce publish with Microsoft.Net.Sdk
Asked Answered
R

3

9

I can successfully build a WPF application with the new csproj format using the Sdk="Microsoft.Net.Sdk".

However, it is a bit of a challenge to publish the said app. The option is definitely not available from the IDE. But what I find a bit puzzling is that the Publish target doesn't seem to be available when you call msbuild directly.

These are some of the top-level properties I set:

    <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
    <TargetFramework>net461</TargetFramework>
    <LanguageTargets>$(MSBuildExtensionsPath)\$(VisualStudioVersion)\Bin\Microsoft.CSharp.targets</LanguageTargets>
    <OutputType>WinExe</OutputType>
    <PlatformTarget>x86</PlatformTarget>
    <Prefer32Bit>false</Prefer32Bit>
    <!--<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>-->
</PropertyGroup></Project>

I also set the typical ones associated with the ClickOnce eg PublishUrl, etc. What can I do to get at/expose the Publish target the same way the LanguageTarget above enables "CoreBuild" for the other legacy C# build tasks outside Console, Web and plain libraries.

Further Thoughts:

So, it turns out that on further inspection, there is actually a Publish target. But it does a simple folder/xcopy deployment to a subfolder called Publish rather than creating an app.publish folder and doing the ClickOnce thing.

How does one work around this?

Ropy answered 5/5, 2017 at 16:40 Comment(7)
Are you talking about .NET Core?Athallia
@MickyD I am using this csproj format to build a WPF app which works fine (after some gymnastics). However, I cannot use the project to do this for instance: msbuild.exe /t:Publish myapp.csproj This works fine however: msbuild.exe /t:Build myapp.csprojRopy
Were you able to solve the problem somehow? I'm facing the same problem.Metaphosphate
@user544511, I am not yet able to solve the problem.Ropy
With .NET Core 3.0 (currently in preview) you are able to use the Microsoft.NET.Sdk.WindowsDesktop MSBuild project SDK instead. If that does not support publishing, please report a problem.Meathead
Thanks @DrewNoakes, I will try it out.Ropy
flexlabs.org/2020/01/… - I haven't tried it yet but this looks like a promising start (we've just encountered the same scenario today) (I've just noticed that this post is referenced in that blog post...!)Trituration
S
2

I was able to put ClickOnce in a WPF net48 project with new SDK style as before of moving to the new SDK.

It was necessary just to put this block in the end of my .csproj file:

    <PropertyGroup>
        <PublishProtocol>ClickOnce</PublishProtocol>
    </PropertyGroup>
    <Import Sdk="Microsoft.NET.Sdk.WindowsDesktop" Project="Sdk.props" />
    <Import Sdk="Microsoft.NET.Sdk.WindowsDesktop" Project="Sdk.targets" />
    <Target Name="ComputeAndCopyFilesToPublishDirectory" />

It does the following:

  1. set SDK to not skip the import of Microsoft.NET.ClickOnce.targets. That is achieved by setting <PublishProtocol>ClickOnce</PublishProtocol>

  2. set SDK to avoid bin/debug/**/* files to be copied to publish directory. That is achieved by skipping the "ComputeAndCopyFilesToPublishDirectory" target execution. To do so, we've overriden it to an empty implementation.

Supersession answered 15/10, 2021 at 11:11 Comment(0)
O
1

You can manually publish ClickOnce using the Mage.exe (command line) or MageUI.exe (gui) tools. It's not very convenient but it does seem to work if you get everything right. I'll outline what worked for me using MageUI.exe.

Choose the correct version of the utility for the .NET version you're using from:

C:\Program Files (x86)\Microsoft SDKs\Windows\

First publish your application files to a folder. Normally this would be something like:

\\server\share\MyApplication\Application Files\MyApplication_1_0_0_25\

NOTE: I had issues with the space in Application Files, where it would be converted to %20, but I don't think UNC paths support that value. I had to remove the space and renamed the folder to ApplicationFiles. (This will probably break previously published versions though.)

Then use MageUI.exe to create a new application manifest:

  • On the Name page, give it a name, version, and choose a processor architecture (x86).
  • On the Files page, enter the directory you published the files to, and then hit populate. It should load all the program files into the DataGridView below.
  • On the Permissions Required page, I was not able to get it working with anything less than FullTrust. Without FullTrust, when the application was run, nothing happened.
  • Save the manifest file as MyApplication.exe.manifest to the application folder. (You will be able to sign the manifest when you save it.)

Now create a new Deployment manifest:

  • On the Name page, enter the same name and version and choose the right processor architecture.
  • On the Description page, enter Publisher and Product.
  • On the Deployment Options page, I chose Online Only. I did not include a Start Location.
  • On the Application Reference page, choose Select Manifest and browse to the application manifest file you previously created.
  • Save the deployment manifest as \\server\share\MyApplication\MyApplication.application; (you can sign it when you save.)
  • NOTE: A glitch here seems to be that it will have inferred the wrong relative path when you select the application manifest file. After you've saved the deployment manifest the first time, go select the application manifest file again, and it will now infer the correct relative path. Then hit save again and you should be ok.

There are a lot of things that can go wrong and a lot of ways that the procedure can differ, but these are the steps that worked for me.

(Another thing I had to do during these steps was clear my ClickOnce Application Cache, by deleting the contents of c:\users\username\AppData\Local\Apps\2.0\. But that was probably just because of all the mistakes I made. I would only do this if you get stuck.)

Opinionative answered 3/10, 2019 at 18:21 Comment(1)
Thanks for this. Using Mage.exe and MageUI.exe seems error prone. When they were built, I don't think Microsoft had automation in mind. Now that DevOps has become a thing, I think those tools need some comprehensive review. But I will try all that you have suggested including the option to use the new SDKs that come with .NET Core 3 (I am leaning more toward this though - smile)...Ropy
R
1

Microsoft is finally adding ClickOnce functionality to SDK Style Winforms and WPF projects in .NET 5.

Ropy answered 19/10, 2020 at 19:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.