Last month, we released PostSharp.Community.Packer, a free and open-source tool that you can use to pack your .NET Framework application into a single .exe file for distribution.

The basic idea as well as much of the code itself come from Costura.Fody, an add-in to Fody, which has the same effect. We mostly differ only in the architecture we use to apply our changes to the build process. Like Costura, Packer is released under MIT.

You can find instructions on how to add Packer to your project on the Packer GitHub page.

When I first encountered Costura.Fody, I was amazed. Before I discovered it, I needed to use an installer or complex tools like ILMerge/ILRepack, but Costura.Fody proved that you can have a self-contained .exe by adding a NuGet package, and it just works. So now we have that, too.

Why should I distribute my application as a single .exe file?

When you distribute an application, you want to make it as easy as possible for the end-user to download and start your application. The easier this process is, the more likely the user is to run your application.

Traditionally on Windows, you distribute applications using installers. But installers are heavyweight and represent a barrier themselves: users may not be as willing to install your application, especially if it’s small, but they may be more willing to just run it.

Another common way to distribute an application is as a .zip file that the user needs to extract. But in this case, the user needs to have enough computer skills to know that they must extract rather than run directly from the .zip file (in which case your project would probably silently crash) and they must be able to identify the correct .exe file to run from possibly many files that are in the .zip file. This is a problem especially because the .dll files that you depend on are normally all in the same directory as your main .exe file.

Updating is also harder with a .zip file where the user must remember where they kept your application when they extract the update... and you’d better hope that the possible extra files that are no longer required don’t mess the application up.

Finally, there’s the matter of aesthetics. A single file just looks so much cleaner than a folder with the main .exe file hiding among dozens of libraries!

Distributing your application as single .exe file allows the user to download only a small file and immediately run it. It doesn’t clutter their computer as an installer would, and it doesn’t have the risk that they won’t be able to open it as a .zip file would.

Can’t .NET Core 3 already do this on its own?

That is true. However, this would require you to use .NET Core. 

Since .NET Framework is installed on every Windows machine, you can expect your small single-file .exe program to work everywhere. (If you want to be sure about older versions of Windows, you need to choose an older version of .NET Framework.) This means that your final .exe file can be even 1 MB or 2 MB in size. What is more, our telemetry data shows that, despite the hype .NET Core and .NET 5 are receiving, a majority of developers still target .NET Framework. 

On the other hand, with .NET Core 3, you cannot usually expect the end-user to have .NET Core installed on their machine, and so you must include the runtime with your application, which means an extra 30–70 MB that the user needs to download and that can’t be shared among applications.

How does Packer work?

Packer has two components: one works at build time and one at runtime.

At build time, Packer runs in the build process after the main compilation completes. It reads from the disk all assemblies that you have set as “Copy to output folder” which includes most referenced assemblies outside the .NET Framework itself. It also includes assemblies from NuGet packages. Then it puts the content of those assemblies as resources into your .exe file, optionally compressed.

At runtime, Packer registers itself as an assembly loader so that whenever the .NET Framework runtime fails to load a required assembly, it gives a chance to Packer to load it, and Packer will then uncompress the data in your application’s resources and return that as the required assembly to the runtime.

How can I add Packer to my project?

It is very easy:

  1. Install the NuGet package PostSharp.Community.Packer.
  2. Get a free PostSharp Community license at https://www.postsharp.net/essentials
    (When you compile for the first time, you may be asked to enter the license key.)
  3. Add [assembly: Packer] somewhere in your code.

Then build your project and to test that everything is working, copy your resulting .exe file into a standalone folder and run it.

Personal note

At work, I often develop small tools to make life easier for other developers. I found that even there, among tech-savvy colleagues, distribution makes a big difference. Few colleagues would be willing to build a productivity tool from source, but many more would download and run a single .exe or .jar file (depending on the company’s preferred language). Updates – manual or automatic – get easier as well. 

Tools like PostSharp.Community.Packer truly extend your program’s reach.

 

The 'Week of PostSharp' is a joint promotion with Manning Publications where we highlight important books from their catalog each day and show how they connect to what our team is doing.

If you came late to our posts this week and missed your chance at a deep discount on one of the featured books, today is your lucky day!

To wrap up the week, we're offering a discount on every book title mentioned during the Week of Postsharp. Purchase any of the books highlighted in this post with a 50% discount - but hurry, the offering is valid only until August 26th, 12 midnight EST.

Enjoy the deals!

Windows Store App Development introduces the Windows 8.x app model to readers familiar with traditional desktop development. You'll explore dozens of carefully crafted examples as you master Windows features, the Windows Runtime, and the best practices of app design. Along the way, you'll pick up tips for deploying apps, including selling through the Windows Store.

Download Free Chapter (PDF)   |   Purchase with 50% discount using the following code: pswkd7

 

Windows 8 Apps with HTML5 and JavaScript provides a comprehensive tutorial for web developers who want to build Windows 8 apps using development tools familiar to them. It's also a great way for a C#/XAML developer to add a few new tools to his or her belt. This practical book provides expert guidance on application design, so you'll learn the emerging patterns and practices for making apps that will work equally well on phones, tablets, and standard screens. Along the way you'll pick up tips for monetizing your apps and deploying to the Windows Store.

Download Free Chapter (PDF)   |  Purchase with 50% discount using the following code: pswkd7

 

Brownfield Application Development in .NET starts by laying out the unique characteristics of brownfield apps—both negative and positive. It then systematically explores how you can employ well-established techniques like continuous integration, version control, and automated testing, even if they weren't part of the original dev process. You'll learn techniques to identify logical layers and tease apart dependencies to effectively separate concerns for improved flexibility. This practical book will help you improve not only your application's maintainability, but also the team's confidence in the project.

Download Free Chapter (PDF)   |   Purchase with 50% discount using the following code: pswkd7

 

Continuous Integration in .NET shows you how to reimagine your development strategies by creating a consistent continuous integration process. You'll use Visual Studio along with tools like Subversion, MSBuild, TFS, TeamCity, NUnit, and Selenium. And because CI is as much about the culture of your shop as the tooling, this book provides clear guidelines for starting and maintaining projects, along with metrics for measuring project success.

Download Free Chapter (PDF)   |   Purchase with 50% discount using the following code: pswkd7

 

DSLs in Boo shows you how to design, extend, and evolve DSLs for .NET by focusing on approaches and patterns. You learn to define an app in terms that match the domain, and to use Boo to build DSLs that generate efficient executables. And you won't deal with the awkward XML-laden syntax many DSLs require. The book concentrates on writing internal (textual) DSLs that allow easy extensibility of the application and framework. And if you don't know Boo, don't worry—you'll learn right here all the techniques you need.

Download Free Chapter (PDF)   |   Purchase with 50% discount using the code pswkd7

 

ASP.NET MVC 4 in Action is a hands-on guide that shows you how to apply ASP.NET MVC effectively. After a high-speed ramp up, this thoroughly revised new edition explores each key topic with a self-contained example so you can jump right to the parts you need. Based on thousands of hours of real-world experience, the authors show you valuable high-end techniques you won't find anywhere else. Written for developers, the book arms you with the next-level skills and practical guidance to create compelling web applications. You need some knowledge of ASP.NET and C#, but no prior ASP.NET MVC experience is assumed.

Download Free Chapter (PDF)   |   Purchase with 50% discount using the following discount code: pswkd7

 

ASP.NET in Practice distills over 100 practical ASP.NET techniques from the experience of a team of MVPs, and puts them right at your fingertips. The techniques are tested and selected for their usefulness, and they are all presented in a simple problem-solution-discussion format. You'll discover methods for key new subjects like data integration with Entity Framework and ASP.NET MVC. Along the way, you'll also find ways to make your applications fast and secure.

Download Free Chapter (PDF)   |   Purchase with 50% discount using the following discount code: pswkd7

 

AOP in .NET introduces aspect-oriented programming and provides guidance on how to get the most practical benefit from this technique. The book's many examples concentrate on modularizing non-functional requirements that often sprawl throughout object-oriented projects. You'll appreciate its straightforward introduction using familiar C#-based examples.

Download Free Chapter (PDF)   |   Purchase with 50% discount using the discount code pswkd7

 

AspectJ in Action, Second Edition is a fully updated, major revision of Ramnivas Laddad's best-selling first edition. It's a hands-on guide for Java developers. After introducing the core principles of AOP, it shows you how to create reusable solutions using AspectJ 6 and Spring 3. You'll master key features including annotation-based syntax, load-time weaver, annotation-based crosscutting, and Spring-AspectJ integration. Building on familiar technologies such as JDBC, Hibernate, JPA, Spring Security, Spring MVC, and Swing, you'll apply AOP to common problems encountered in enterprise applications.

Download Free Chapter (PDF)   |   Purchase with 50% discount using the code pswkd7

 

C# in Depth, Third Edition updates the best selling second edition to cover the new features of C# 5, including the challenges of writing maintainable asynchronous code. It preserves the uniquely-insightful look into the tricky areas and dusty corners of C# that only expert Jon Skeet can provide. In this third edition, you'll see the subtleties of C# programming in action, learning how to work with high-value features that you'll be glad to have in your toolkit. The book helps readers avoid hidden pitfalls of C# programming by providing crystal-clear explanations of "behind the scenes" issues.

Download Free Chapter (PDF)   |   Purchase with 50% discount using the following code: pswkd7

 

The book Real-World Functional Programming teaches the ideas and techniques of functional programming applied to real-world problems. You'll see how the functional way of thinking changes the game for .NET developers. Then, you'll tackle common issues using a functional approach. The book will also teach you the basics of the F# language and extend your C# skills into the functional domain. No prior experience with functional programming or F# is required.

Download Free Chapter (PDF)   |   Purchase with 50% discount using the following code: pswkd7

 

 

 

The 'Week of PostSharp' is a joint promotion with Manning Publications where we highlight a new book each day that is special to us and hand-picked by the PostSharp team – with a small introduction about how it connects to what we are doing. We also collaborated with Manning to select a second book for each day of the week, and will be offering our choices at a 50% discount on their site! You can even read excerpts from some of the books online for free by clicking on the links below.

We hope you enjoy the deals! We’ll be adding new posts with the daily discount code throughout the week, so stay tuned.

My favorite feature of XAML is data binding: just bind a XAML property to a property of your model of view-model objects, and they will keep synchronized. That works wonderfully. No code involved – at least if your classes implement INotifyPropertyChanged. And this is where the fairy tale turns into a horror story.

If your object model has anything more than a dozen of classes with trivial dependencies, implementing INotifyPropertyChanged is daunting task that will only result in a large amount of boilerplate code. As any boilerplate code, it will be full of bugs and omissions, and will rarely be subject to systematic unit testing. You may think that this code is a necessary evil, but it is not: it can be reduced to zero – or almost.

PostSharp Model Pattern Library includes a ready-made implementation of the NotifyPropertyChanged pattern. Just add the [NotifyPropertyChanged] custom attribute to your class and you can forget the rest. Designed to cover 90% of all cases, the feature covers properties backed by any number of fields/properties of the current object or of another object, without any boilerplate. The default implementation can be customized by custom code to cover the remaining 10%.

You can use our NotifyPropertyChanged aspect with or without XAML and on any platform as long as you’re using C# or Visual Basic. We’re proposing two books today: Windows App Store Development: C# and XAML and Windows 8 Apps with HTML5 and JavaScript. If you’re going the first way, you can count PostSharp in. But with the second, you’ll be on your own to implement change notification.

Today, we're recommending the following books that you can purchase with a 50% discount - but hurry, the offering is valid only until August 25th, 12 midnight EST:

Windows Store App Development introduces the Windows 8.x app model to readers familiar with traditional desktop development. You'll explore dozens of carefully crafted examples as you master Windows features, the Windows Runtime, and the best practices of app design. Along the way, you'll pick up tips for deploying apps, including selling through the Windows Store.

Download Free Chapter (PDF)   |   Purchase with 50% discount using the following code: pswkd6

 

Windows 8 Apps with HTML5 and JavaScript provides a comprehensive tutorial for web developers who want to build Windows 8 apps using development tools familiar to them. It's also a great way for a C#/XAML developer to add a few new tools to his or her belt. This practical book provides expert guidance on application design, so you'll learn the emerging patterns and practices for making apps that will work equally well on phones, tablets, and standard screens. Along the way you'll pick up tips for monetizing your apps and deploying to the Windows Store.

Download Free Chapter (PDF)   |  Purchase with 50% discount using the following code: pswkd6