Posts

Hosting a WCF Service in IIS / C#

Image
Step 1: Create a new Visual Studio project Choose the WCF installed template and create a new WCF Service Application: Step 2: Create your web service code Update the IService1.cs and Service1.svc.cs classes that are automatically created. You can of course delete these and create your own but I am re-using here for the sake of brevity. For ease of demonstration I will create an extremely simple “Hello world!” example. IService1.cs [code language="csharp"] using System.ServiceModel; using System.ServiceModel.Web; namespace WcfService { [ServiceContract] public interface IService1 { [OperationContract] [WebGet(UriTemplate = "/HelloWorld/")] string HelloWorld(); } } [/code] Service1.svc.cs [code language="csharp"] namespace WcfService { public class Service1 : IService1 { public string HelloWorld() { return "Hello world!"; } } } [/code] Re-build the visu...

Creating and consuming a web service in WCF

Image
Creating the WCF web service in Visual Studio In this post I am using the WCF Web Service as an example, see this post for an example using using asmx: https://www.technical-recipes.com/2017/creating-and-consuming-a-web-service-in-c-net/ A useful C# Corner post: http://www.c-sharpcorner.com/article/create-wcf-web-service-in-visual-studio-2015/ Create a new Visual Studio project In Visual Studio select the WCF installed template and create a new WCF Service Application: Observe that a number of files are automatically created: IService1.cs, Service1.cs and Web.config, amongst others., We will modify the content of these for the purpose of this example: Update the code in IService.cs file, as highlighted below. Notice that is does not do much since I like to keep these recipes extremely simple, understanding the process is more important. IService1.cs [code language="csharp"] using System.Runtime.Serialization; using System.ServiceModel; using Sys...

How to join multiple tables with multiple keys / columns in SQL

Image
Some short instructions on how to accomplish a join on three or more SQL database tables but also using multiple keys/columns as well. Good StackOverflow reference: https://stackoverflow.com/questions/24639504/mysql-join-3-tables-using-multiple-columns-keys Three different tables used: 1. UserDivisions An old legacy table containing Division and it's ID: 2. MediaCategoryDivisionMap This table maps the Division ID of the first table to the Category ID of a newer MediaCategories table: 3. MediaCategories Finally the newer table that uses a Category ID: I wish to use the Division IDs in table UserDivisions in order to extract the equivalent Category IDs they are mapped to in MediaCategoryDivisionMap and therefore obtain the new Name contained in the MediaCategories table. As the StackOverflow post states "INNER JOIN creates a new result table by combining column values of two tables (A and B) based upon the join-predicate. The query compares each row ...

Adding tab items dynamically in WPF / MVVM

Image
Some instructions on how to add/remove tab items within a WPF / MVVM setting. Step 1: Create a new WPF application Step 2: Add classes to implement ICommand RelayCommand.cs [code language="csharp"] using System; using System.Windows.Input; namespace Tabs { public class RelayCommand<T> : ICommand { private readonly Predicate<T> _canExecute; private readonly Action<T> _execute; public RelayCommand(Action<T> execute) : this(execute, null) { _execute = execute; } public RelayCommand(Action<T> execute, Predicate<T> canExecute) { if (execute == null) { throw new ArgumentNullException("execute"); } _execute = execute; _canExecute = canExecute; } public bool CanExecute(object parameter) { return _canExecute == null || _canExecute((T)parameter); } public vo...

How to stop C# async methods

Image
Scenario: a WPF application which when the user presses a button goes off and runs something asynchronously , so that the user is still able to interact with the user interface without it freezing. Not only that, I want to be able to stop the asynchronous operation on receipt of another button press so that it exits graciously. The is where CancellationToken structure comes in, that is used to propagates notifications that operations should be cancelled. Some useful StackOverflow resources on this subject can be found here: https://stackoverflow.com/questions/15614991/simply-stop-an-async-method https://stackoverflow.com/questions/7343211/cancelling-a-task-is-throwing-an-exception Some instructions to implement this in a WPF / MVVM kind of environment are given: Step 1: Create a WPF application Step 2: Include event-handling classes Specifically this means classes to implement ICommand RelayCommand.cs [code language="csharp"] using System; using Sy...

Binding the visibility of DataGridColumn in WPF / MVVM

Image
See this StackOverflow answer for the original inspiration. Notable quote: "First of all DataGridTextColumn or any other supported dataGrid columns doesn't lie in Visual tree of DataGrid. Hence, by default it doesn't inherit DataContext of DataGrid. But, it works for Binding DP only and for not other DP's on DataGridColumn." Here is an example of how to achieve this in a Visual Studio WPF application. Step 1: Create a Visual Studio WPF application: Step 2: Create a Freezable class "Freezable objects can inherit the DataContext even when they’re not in the visual or logical tree. So, we can take advantage of that to our use. First create class inheriting from Freezable and Data DP which we can use to bind in XAML". BindingProxy.cs [code language="csharp"] using System.Windows; namespace DataGridColumnVisibility { public class BindingProxy : Freezable { public static readonly DependencyProperty DataProperty = ...

Using Expander controls in C# / WPF

Image
A short post demonstrating how to use expander controls in WPF, to create a list of collapsible / expandable menu items in your C# WPF application Create a new Visual Studio application Create a View Model class for your main window MainWindowViewModel.cs [code language="csharp"] using System.Collections.Generic; namespace ExpanderExample { public class ApplicationListItem { public ApplicationListItem(string name, string openButtonText, string updateButtonText, bool updateButtonVisibility = false, bool progressVisibility = true) { Name = name; OpenButtonText = openButtonText; UpdateButtonText = updateButtonText; UpdateButtonVisibility = updateButtonVisibility; ProgressVisibility = progressVisibility; } public string Name { get; set; } public string OpenButtonText { get; set; } public string UpdateButtonText { get; set; } public bool UpdateButtonVisibility {...