Category Archives: C#

Using external config files in .NET applications


The config file is place where common variables, database connection strings, web page settings and other common stuff are placed. The config file is also dynamic, so you can change the value of the variable in the config file  without compiling and deploying the .NET app. In multi tenancy environment config file can be complicate for deployment, because  for each tenant different value must be set for most of the defined variables. In such a situation you have to be careful to set right value for the right tenant.

One way of handling this is to hold separate config file for each tenant. But the problem can be variables which are the same for all tenants, and also the case where some variables can be omitted for certain tenant.

One of the solution for this can be defining external config files for only connection strings or appSettings variables, or any other custom config section. In this blog post, it will be presenting how to define connection strings as well as appSettings section in separate config file.

Lets say you have appSettings and connectionStrings config sections, similar like code below:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>

	<connectionStrings>
		<add name="SQLConnectionString01" connectionString="Data Source=sourcename01;Initial Catalog=cat01;Persist Security Info=True;Integrated Security=true;"/>
		<add name="SQLConnectionString02" connectionString="Data Source=sourcename02;Initial Catalog=cat02;Persist Security Info=True;Integrated Security=true;"/>
	</connectionStrings>

	<appSettings>
		<clear />
		<!-- Here are list of appsettings -->
		<add key="Var1" value="Var1 value from config01" />
		<add key="Var2" value="Varn value from config01"/>
		<add key="Var3" value="Var3 value from main config file"/>
	</appSettings>

</configuration>

There are three appSetting keys Var1 , Var2 and Var3  and two connectionstrings in the app.config.

The config file above can be split in such a way that variables Var1 and Var2 be defined in separated file, but the Var3 can be remain in the main cofing file. Separate config file may be unique for each tenant.

Now the main config file looks like the following:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>

	<connectionStrings configSource="config\connString01.config"/>

	<appSettings file="config\config01.config">
		
		<add key="Var3" value="Var3 value from main config file"/>
	</appSettings>

</configuration>

In the Visual Studio Solution there is config folder in which we created two config files for appSettings section and two config files for Connectionstrings section, in case we have two separate environments for deployments.

exconfigfile01
The flowing code snippet shows the appSettings section implemented in the external file:

<appSettings file="appSettings.config">

	<!-- Here are list of appsettings -->
	<add key="Var1" value="Var1 value from config02" />
	<!-- ... -->
	<add key="Varn" value="Varn value from config02"/>
</appSettings>

The external config file for connection strings looks similar like the flowing:

exconfigfile02

The simple console application shows how to use this config variables in the code:

static void Main(string[] args)
{
    var var1Value= ConfigurationManager.AppSettings["Var1"];
    var var2Value = ConfigurationManager.AppSettings["Var2"];
    var var3Value = ConfigurationManager.AppSettings["Var3"];
    var conn1 = ConfigurationManager.ConnectionStrings["SQLConnectionString01"];
    var conn2 = ConfigurationManager.ConnectionStrings["SQLConnectionString02"];

    Console.WriteLine("Values from config01.config and connString01.config files");

    Console.WriteLine("Var1={0}",var1Value);
    Console.WriteLine("Var2={0}", var2Value);
    Console.WriteLine("Var3={0}", var3Value);
    Console.WriteLine("ConnStr01={0}", conn1);
    Console.WriteLine("ConnStr01={0}", conn2);

    Console.Read();
}

The complete source code can be downloaded from this link.

Add EULA to ClickOnce Installation using Visual Studio 2015


ClickOnce technology is very smart and useful when you want simple, small and smart piece of software for deploying your Windows Forms or WFP application. This is specially useful if you deliver application which doesn’t require administrator rights during installation. ClickOnce is very powerful if you wants automatic update of your product, you can decide whenever the update appears before or after app is run. Also by using certificate you can deliver reliable and secure product to your customers. Long time ago I wrote detailed blog post  how to make ClickOnce deployment.

But one big thing is missing in ClickOnce deployment and for most of dev community is the feature which should be included by default. The feature which missing is “End User License Agreement” (EULA).  There is no simple way to implement it. Searching the internet I have found one forum post on Microsoft site describing how to implement it. Only way you can get ELUA at the beginning of the ClickOnce installation proces is by using it as prererquested component. Actually you build a redistributable component which would be seen as prerequsites dialog box under the publish window. To build custom prerequisites component you need a three files:

  1. Product.xml. – which is the file for bootstrapper and Visual Studio to show  a component in prerequisites dialog,
  2. Package.xml – which is the file containing all information about the component to be installed,
  3. EULA.txt – your ELUA text for user to accept.

Those three files must be installed in the location where all components are registered: C:\Program Files (x86)\Microsoft Visual Studio 14.0\SDK\Bootstrapper\Packages

For this blog post I have prepare demo sample which look like the following picture:

clickoncelicsl015

As can be seen, there is a two folders and one xml file. Each folder contains two files described earlier. Folders “en” and “de” means we are going to have EULA translated on two languages.

In order to successfully registered perquisites to be visible by Visual Studio we need to define proper content of the product.xml. The following xml code show content of our demo sample:

<?xml version="1.0" encoding="utf-8" ?>

<Product xmlns="http://schemas.microsoft.com/developer/2004/01/bootstrapper" ProductCode="EULA.Bootstrap.Component">
  <!-- Defines list of files to be copied on build -->
  <PackageFiles>
    <PackageFile Name="en/eula.txt"/>
  </PackageFiles>
  <Commands>
    <!-- Open eula.txt without any parameters -->
    <Command PackageFile="en/eula.txt" Arguments='' >
	
	  <!-- These checks determine whether the package is to be installed -->
	  <!-- No install conditions -->
      <InstallConditions>
        
      </InstallConditions>
	  
	   <!-- Exit codes -->
	   <ExitCodes>
        <ExitCode Value="0" Result="Success" />
        <DefaultExitCode Result="Fail" FormatMessageFromSystem="false" String="GeneralFailure" />
      </ExitCodes>

    </Command>
  </Commands>
</Product>

As we can see xml content is self-described, it contains product information, files to be installed, installation conditions and exit codes.

Each folder (en, de, …) contains package.xml file which holds localized messages and list of files to copied on build. The following xml content shows content of our en demo sample:

<?xml version="1.0" encoding="utf-8" ?>
<Package xmlns="http://schemas.microsoft.com/developer/2004/01/bootstrapper" Name="ClickOnceWithLicenseAgreement" Culture="Culture" LicenseAgreement="eula.txt">
  <!-- Defines list of files to be copied on build -->
    <PackageFiles>
        <PackageFile Name="eula.txt"/>
    </PackageFiles>

  <!-- Defines a localizable string table for error messages and url's -->
  <Strings>
    <String Name="DisplayName">ClickOnceWithLicenseAgreement 1.0 (x86 and x64)</String>
    <String Name="Culture">en</String>

    <String Name="CancelFailure">User Refuse to Accept to ClickOnceWithLicenseAgreement End User License Agreement.</String>
    <String Name="GeneralFailure">A fatal error occurred during the installation of ELUA Component Execution</String>
    <String Name="AdminRequired">You do not have the permissions required to install this application.  Please contact your administrator.</String>
  </Strings>    
</Package>

The EULA.txt file contains the text user need to accept in order to install the product.

For this demo we created folder EULAPackage, put product.xml and two folders en and de because we are going to support two installation languages (see picture above).

Copy the EULAPackage folder in to : C:\Program Files (x86)\Microsoft Visual Studio 14.0\SDK\Bootstrapper\Packages

Now we have set all necessary information about ELUA component and can be included in our demo sample.

  1. Open Visual Studio 2015 and create new WFP application.Name it as “ClickOnceWithLicenseAgreement

clickoncelicsl011

2. Right-Click on the Project and select Property menu option, then select Publish tab item of the Project Property window.

clickoncelicsl016

3. Click on Prerequisites Button, then you will see your prerequisite component in the standard prerequisites lists.

clickoncelicsl013

4. Select ClickOnceWithLicenseAgreement component and click Ok button. Afterwards click Publish button to build installation package.

5. Open publish folder from the disk. DoubleClick Setup.exe and the ELUA window should be appeared:

clickoncelicsl014

6. Now user has two choises to accept or Refuse the EULA, which means install or not install the app.

Prerequested demo sample component for EULA can be downloaded from here.

Happy programming.

 

error CS0433 for ‘System.Windows.Automation’ type


vsissu2e

Few days ago I had a strange issue in one of my Visual Studio 2013 solution. The Test project required functionality of System.Windows.Automation, and I have added assembly UIAutomationClient. When tried to run tests I got the folowing compiler error:

error CS0433: The type ‘System.Windows.Automation.AutomationElement’ exists in both ‘c:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\PublicAssemblies\UIAComWrapper.dll’ and ‘c:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\UIAutomationClient.dll’

It was strange issue, because I didn’t add UiaComWrapper assembly. Tried to remove the assembly, but as soon I run test the previous error appeared.

vsissue

 

Fixing the issue

To fix this issue we have to use “Reference Alias” which is specified here. So for our assembly UIAutomatisationClient change the Alias from “global” to  “UIAutomationClientAlias”. See picture below.

vsissuesl2

Now that we defined new alias, in the following text it is shown how to use it in your cs file. Open the cs file we have error.

1. At the top opf the file put the folowing line:

extern alias UIAutomationClientAlias;

2. Define using namespace from the new alias:

using Autom = UIAutomationClientAlias::System.Windows.Automation;

3. Now we can use all members from Automation class safely, and the compiler error is despaired.

vsissuesl3

XAML Peak new feature in Visual Studio 2015


The latest version of the Visual Studio 2015 is bringing a lot of new features for WPF and XAML programming language. For this blog post I would like to share the XAML Peak Definition. Peak Definition is not new, the current version of Visual Studio 2013 supports peak definition only for C# and VB. Now we have the same functionality within XAML. Event more, in the Visual Studio 2015 peak definition is working in mixed mode, so you can reach any definition regladless of the programming languges (C#, VB or XAML).

XAML peak allows you to get definition or content of any name defined in the XAML code. For example lets see the following XAML code. Right click on the MainWindow class and choose “Peak Definition”:

peakxamlsl1

After the command is selected, the new inline window appears within xaml, and the user can easily see or change the code behind selected definition. Peak Definition works for any proper name in the XAML.

peakxamlsl2

Lets see more interesting thing. Peak Definition is very handy when you want to see or modify the defined style or control template. For Example if we right click on the Style defined resource and choose Peak Definition like the following picture:

peakxamlsl31

Inline window will appears and show the style implementation of the selected xaml element. This is very useful and long waited option of XAML editor:

 

peakxamlsl4

Inline windows are edited windows so you can edit or add new code without leaving xaml editor.

This is one of the plenty of the new features coming with the latest version of the Visual Studio 2015. More features in the coming post.

 

 

 

Follow

Get every new post delivered to your Inbox.

Join 672 other followers