none
Generating multiple executables when building RRS feed

  • Question

  • I write a lot of windows services. To help in the debugging, I use a conditional compilation that allows me to either create a console application or a service. I however also supply both versions to the customer

    Currently I simply add a conditional compilation symbol (or 'remove' it) in the build settings. But when generating the executables for the customer, I need to build twice (and rename the executables).

    So I'm looking for a way to achieve the following:

    • When selecting active configuration 'mydebug' in the build configuration manager and building, a single executable is generated in the usual way (myservice.exe; console version) so I can debug the code.
    • When selecting active configuration 'myrelease' in the build configuration manager and building, two executables are generated (myservice.console.exe and myservice.service.exe).

    How do I go about this?

    Note:
    I'm only slightly familiar with the build configuration manager. Although there don't seem to be many options in there. And in the normal properties window of the project, I can not set different assembly names for different build configurations (?).

    • Moved by Jiayi Li Monday, November 16, 2015 6:19 AM
    Saturday, November 14, 2015 5:59 PM

Answers

  • I'd like to thank everybody for their input. It's clear to me that what I like to achieve is not possible from within VS.

    I'll rethink my strategy around this.

    • Marked as answer by sterretje_K5 Saturday, November 21, 2015 4:29 AM
    Saturday, November 21, 2015 4:29 AM

All replies

  • Consider this possibility: create two projects — for service and console. They will share the same source files, placed in common locations (of one of the projects, for example). They will have some specific options and preprocessor definitions. The common code can be also put to separate static library.

    Using Configuration Manager, you can specify the projects to be compiled in Debug and Release modes.

    Using “Batch Build…” command from BUILD menu, you can select the projects and configurations to be built.

    Saturday, November 14, 2015 7:19 PM
  • Thanks for the reply

    It means that when I add a new file to one project, I also have to link it in the other project. I did consider it but it's not really what I'm looking for ;-)

    Currently the only place where the conditional symbol is used is in the method Main (in Program.cs). It looks like

    #if ( !isConsoleApp )
                ServiceBase[] ServicesToRun;
                ServicesToRun = new ServiceBase[] 
                { 
                    new scheti() 
                };
                ServiceBase.Run(ServicesToRun);
    #else
                var service = new scheti();
                service.StartThread();
                Console.WriteLine("Press the Enter key to exit the program.");
                string x = Console.ReadLine();
                service.StopThread();
    #endif
    

    Sunday, November 15, 2015 4:08 AM
  • Hi sterretje_K5,

    Your issue would be related to visual C#, I help you move this case to visual C# forum for dedicated support.

    Thank you for your understanding.

    Best Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.

    Click HERE to participate the survey.

    Monday, November 16, 2015 6:18 AM
  • Hi sterretje_K5,

    Your issue would be related to visual C#, I help you move this case to visual C# forum for dedicated support.

    Thank you for your understanding.

    Best Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.

    Click HERE to participate the survey.

    I do NOT understand, sorry.

    It's a question how to instruct Visual Studio to generate multiple executables, not related at all to C#.

    Monday, November 16, 2015 7:15 AM
  • Hi sterretje_K5,

    Maybe you could create a new project. In which you could invoke MSBuild command to build the project(It related to C#).

    In addition, MSBuild provide Choose--When element to dynamic change the project file. Code below is for your reference.

    <Choose>
        <When Condition="'$(Configuration)' == 'DEBUG'">
          <PropertyGroup>
            <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
            <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
            <ProjectGuid>{F7D17F58-0B52-451F-A5BC-27037A409520}</ProjectGuid>
            <OutputType>Exe</OutputType>
            <AppDesignerFolder>Properties</AppDesignerFolder>
            <RootNamespace>CCsharpConsoleApplication</RootNamespace>
            <AssemblyName>Name in debug mode</AssemblyName>
            <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
            <FileAlignment>512</FileAlignment>
            <TargetFrameworkProfile />
          </PropertyGroup>
        </When>
        <When Condition="'$(Configuration)' != 'DEBUG'">
          <PropertyGroup>
            <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
            <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
            <ProjectGuid>{F7D17F58-0B52-451F-A5BC-27037A409520}</ProjectGuid>
            <OutputType>Exe</OutputType>
            <AppDesignerFolder>Properties</AppDesignerFolder>
            <RootNamespace>CCsharpConsoleApplication</RootNamespace>
            <AssemblyName>Name in other mode</AssemblyName>
            <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
            <FileAlignment>512</FileAlignment>
            <TargetFrameworkProfile />
          </PropertyGroup>
        </When>
      </Choose>

    Best Regards,
    Jerry

    Tuesday, November 17, 2015 7:55 AM
  • Sorry but you're solving the wrong problem.  Do not use conditional compilation to build 2 different versions of your app.  A window service can, and should, be able to run as both a console app and service. This is how they are written.  You don't need conditional compilation and a single EXE is fine.

    You can accomplish this in a bunch of different ways. There are numerous posts on how to do this but I'll point you to a blog post that I wrote about it. Alternatively take a look at Topshelf which accomplishes the same thing. The point is that you don't need conditional compilation or multiple binaries. It complicates things for no reason.

    Michael Taylor
    http://blogs.msmvps.com/p3net

    Tuesday, November 17, 2015 5:30 PM
    Moderator
  • Hi sterretje_K5,

    Maybe you could create a new project. In which you could invoke MSBuild command to build the project(It related to C#).

    In addition, MSBuild provide Choose--When element to dynamic change the project file. Code below is for your reference.

    <Choose>
        <When Condition="'$(Configuration)' == 'DEBUG'">
          <PropertyGroup>
            <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
            <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
            <ProjectGuid>{F7D17F58-0B52-451F-A5BC-27037A409520}</ProjectGuid>
            <OutputType>Exe</OutputType>
            <AppDesignerFolder>Properties</AppDesignerFolder>
            <RootNamespace>CCsharpConsoleApplication</RootNamespace>
            <AssemblyName>Name in debug mode</AssemblyName>
            <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
            <FileAlignment>512</FileAlignment>
            <TargetFrameworkProfile />
          </PropertyGroup>
        </When>
        <When Condition="'$(Configuration)' != 'DEBUG'">
          <PropertyGroup>
            <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
            <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
            <ProjectGuid>{F7D17F58-0B52-451F-A5BC-27037A409520}</ProjectGuid>
            <OutputType>Exe</OutputType>
            <AppDesignerFolder>Properties</AppDesignerFolder>
            <RootNamespace>CCsharpConsoleApplication</RootNamespace>
            <AssemblyName>Name in other mode</AssemblyName>
            <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
            <FileAlignment>512</FileAlignment>
            <TargetFrameworkProfile />
          </PropertyGroup>
        </When>
      </Choose>

    Best Regards,
    Jerry

    That might work; lots of studying to do on the subject of MSBuild; not fond of adding another project but create a batch file with some parameters (or a powershell script) and call MSBuild twice and use that will work for me.

    But what you're saying is that VS IDE does not have support for multiple executables; one at a time, that's it?


    • Edited by sterretje_K5 Wednesday, November 18, 2015 7:36 AM
    Wednesday, November 18, 2015 6:19 AM
  • Sorry but you're solving the wrong problem.  Do not use conditional compilation to build 2 different versions of your app.  A window service can, and should, be able to run as both a console app and service. This is how they are written.  You don't need conditional compilation and a single EXE is fine.

    You can accomplish this in a bunch of different ways. There are numerous posts on how to do this but I'll point you to a blog post that I wrote about it. Alternatively take a look at Topshelf which accomplishes the same thing. The point is that you don't need conditional compilation or multiple binaries. It complicates things for no reason.

    Michael Taylor
    http://blogs.msmvps.com/p3net

    Interesting approach and there is definitely something in there that I can use in future. I only read the posted link, not the blogs before and after (to do).

    I think it's a matter of preference in this case what one wants to use, but there might be benefits in your approach that I have not realised yet (one (although maybe minor) is definitely that it does not require a separate Start method).

    But imagine that this is not about services. What if I have a need for a conditional compilation? In it's simplest form, what if I want to build a debug and a release version at the same time without selecting a different configuration (it's about the principle). I expect a full fletched IDE to give me the option to generate both with the click of one button.

    I might have missed the options in VS that allow this but seeing previous replies I doubt that.

    Wednesday, November 18, 2015 7:34 AM
  • "I might have missed the options in VS that allow this but seeing previous replies I doubt that."

    VS has supported this feature since at least the original version of VS.NET.  It's called batch build and is available by right clicking the solution (or you can create a toolbar button for it if you want).  You can specify any and all the projects that you want to build and the configuration/platforms for each one. Because projects are configured (by default, in most cases) to output the build results to a folder structure based upon the configuration/platform the builds should not collide with each other. 

    Conditional compilation isn't a scenario where I'd use this feature.  Conditional compilation isn't that useful in .NET in my experience (unlike C++).  Beyond debug settings (which I wouldn't ship to a customer) the only time I've found a need for such options is when trying to reuse the same file in multiple projects (aka WPF, WP, SL). In that case the projects are responsible for setting the directives.

    However multiple platforms are useful and therefore batch build is handy for that situation. When the build is complete you end up with the binaries in separate folders (generally x86 and x64). For deployment your best option is to keep these folder structures intact.  If you look at a lot of programs that ship with both binary formats they have separate folder for each platform. This keeps things simple and avoids any need for renaming files. 

    If you do need to rename files then you cannot do that using the project configuration as it is not a per-platform/per-configuration option. But you can use a post build event to copy/rename files. However you need to be careful about this because you may cause the debugger issues if you are moving or renaming files. The debugger normally requires files to match when loading symbols and whatnot so mucking with that can cause issues trying to debug, in my experience.

    Wednesday, November 18, 2015 2:39 PM
    Moderator
  • I'd like to thank everybody for their input. It's clear to me that what I like to achieve is not possible from within VS.

    I'll rethink my strategy around this.

    • Marked as answer by sterretje_K5 Saturday, November 21, 2015 4:29 AM
    Saturday, November 21, 2015 4:29 AM