Skip to main content

Posts

Outlook AdvanceSearch API not returning latest emails in the results

Using Office Interop AdvanceSearch API method to query a Outlook folder isn't returning all the results, it's only returning results from 2 days ago! This code been running fine for over 2 years why is it failing now, and only failing on one machine? Turnsout the it's a new machine after the last one died a couple of days ago, and this lead me to the answer - Indexes... The indexes hadn't finished re-building from the last retore point, and if you use API before the Indexes are built you might not get all the results. If you dig into the Indexing Options it even tells you this, shame the API can't surface this warning.

SqlDependency - getting the permissions working...

 After much banging my head against the internet here is a canonical example of how to configure SQL Server permissions, roles to get SqlDependency working in a .Net app. This is for sceneario where the identity running the .Net code is running as a configured user account with the db_datareader & db_datawriter. -- Create Schema User CREATE USER sql_dependency_schema_owner WITHOUT LOGIN; GO -- Create Schema for SqlDependency objects CREATE SCHEMA sql_dependency AUTHORIZATION sql_dependency_schema_owner; GO -- Create Role for users of [sql_dependency] CREATE ROLE sql_dependency_user; GO -- Grant role permissions GRANT CONTROL ON SCHEMA::sql_dependency TO sql_dependency_user; GRANT IMPERSONATE ON USER::sql_dependency_schema_owner TO sql_dependency_user; GRANT CREATE PROCEDURE TO sql_dependency_user; GRANT CREATE QUEUE TO sql_dependency_user; GRANT CREATE SERVICE TO sql_dependency_user; GRANT REFERENCES ON CONTRACT::[http://schemas.microsoft.com/SQL/Notifications/PostQueryNo...

Clean app startup (WPF)

Simple easy to read code is the building blocks for complex applications, and to this end, the is how I now start all desktop apps (written in XAML / WPF). This is nothing new (and not original) but it's been refined & destilled down to a point of simpliclity :) Single line written once and forgotten about... Need to add Exception handling - write it in a Module... Need to add Heartbeat monitoring - write it in a Module... Using DI - do it in a Module... Startup XAML - guess what, done in a Module... Want only one instance of the app - do it in a Module... So lifting from my updated Simple.Wpf.Template on gitHub , I have the following set of Modules defined - this forms the basis for all apps I'm asked to develop for clients: The complexity is hidden away in the ModuleLoader class, simple put this scans & loads any type with the ModuleConfigurationAttribute, orders the Modules according to the properties of the Attribute and dynamically invokes the Modules. Check out th...

Benchmarking DateTime.ToString("...")

Looking to eek-out as much perf as I can from some code (C#) at work, and looking at all the .ToString() manipulations in the code and I came across the following: How can I quickly workout if I want to do something around this? BenchmarkDotNet to the rescue, and this library really does make benchmarking easy (and fun!). I created an overload of the extension method which internally uses a cache to avoid the repeative ToString() calls: Only small issue was wanting to test a Static methods, and this only involved creating a wrapper class with annotated methods(calling the static implementations), also preloaded the internal cache: Results below, make-of-it what you will...

Rx EventAggregator<T> for desktop apps

In my continuing (point-less) mission to avoid using anything related to MS Prism - a quick implementation of an EventAggregator using Rx - took about 5 minutes to write for a problem I currently have, normally try and avoid using such a pattern as it can lead the scattering of business logic which can hard to 'follow' when refactoring / re-visting code. Trying to kepp the interace as simple and obvious as possible (symetric design) - this will be injected by the container: Simple base event class, not thing special at the moment, might add a timestamp in the future, but more likely to do that with Rx on the event stream using the apply named Timestamp method: Included a simplifed Schedulers wrapper - idea being you can get any of the Rx Schedulers in one place, and importantly makes unit testing easier: Show me the money...

Getting the last column to fit to available space in slickGrid

I wanted a quick diagnostics page in html / js and this included grid to show server logs, decided to use slickGrid . All pretty easy to use apart from one thing - last column to fill remaining space... Nothing in their examples, so here's one for posterity... Not sure if it's the preferred way, but it works - the most obivous issue for is doing a DOM search to get the Viewport element - this is tightly coupled to the current latest release. Stuck the implmentation in a Class and not provided any logic for creating Data, Columns or other grid optnios / plugins - tried to keep it simple...

Custom AuthorizationHandler for SignalR Hubs

How to implement IAuthorizationRequirement for SignalR in Asp.Net Core v5.0 Been battling this for a couple of days, and eventually ended up raising an issue on Asp.Net Core gitHub  to find the answer. Wanting to do some custom authorization on a SignalR Hub when the client makes a connection (Hub is created) and when an endpoint (Hub method) is called:  I was assuming I could use the same Policy for both class & method attributes, but it ain't so - not because you can't, because you need the signatures to be different. Method implementation has a resource type of HubInnovationContext: I assumed class implementation would have a resource type of HubConnectionContext - client connects etc... This isn't the case, it's infact of type DefaultHttpContext . For me I don't even need that, it can be removed completely  from the inheritence signature and override implementation. Only other thing to note, and this could be a biggy, is the ordering of the statements in th...

AutoFac for SignalR using ASP.NET Core version 5.0

Putting this here, so it might help someone else in the future! Couldn't find an example for version 5 of ASP.NET Core - the number of breaking changes in ASP.NET Core is incredible, and a big negative for the framework when looking to integrate 3rd party services such as logging & IOC. Steps: 1. Add nuGet packages: Autofac Autofac.Extensions.DependencyInjection 2. Update Program.cs file to add AutoFac Factory: Highlighted the import part in Red : 3. Update Startup.cs file to add AutoFac container configuration: Highlighted the import part in  Red : IRegisterService & IRegisterHub are interfaces I created for easy registration with AutoFac.

WPF tips & tricks: Dispatcher thread performance

Not blogged for an age, and I received an email last week which provoked me back to life. It was a job spec for a WPF contract where they want help sorting out the performance of their app especially around grids and tabular data. I thought I'd shared some tips & tricks I've picked up along the way, these aren't probably going to solve any issues you might be having directly, but they might point you in the right direction when trying to find and resolve performance issues with a WPF app. First off, performance is something you shouldn't try and improve without evidence, and this means having evidence proving you've improved the performance - before & after metrics for example. Without this you're basically pissing into the wind, which can be fun from a developer point of view but bad for a project :) So, what do I mean by ' Dispatcher thread performance '? The 'dispatcher thread' or the 'UI thread' is probably the most ...

WPF anti-pattern: Docking panels

Developers love docking panels (especially Windows developers), they love the ability to 'dock' a panel to the left, top, right bottom of the window, and repeat this with windows inside windows inside windows ad infinitum... Shown below is the docking ability in Visual Studio - it works great because the target user has the required level of understanding of a complex app. The problem is the majority of users haven't got a clue about them... When they accidentally drag a tab or window and the UI suddenly show docking points like the above screen shot they haven't got a clue what to do to get back to where they were. This coupled with the look on their faces makes me think the use of docking panels should be avoid in all but the most complicated apps - the average business line app developed using WPF doesn't really need a docking panel. I call the use of docking panels in general an anti-pattern :)

Styling WPF buttons for Image Viewer

A quick post on styling the next & previous buttons for an Image Viewer written in WPF, I'm using the MahApps libraries to give the UI a modern look & feel - they are available as nuget packages, I'm using the core and resources packages. The initial version is very simple and hopefully the behaviour is obvious - the image flanked by a couple of buttons for moving to the next\previous image:   The XAML for this is very simple, using grid layout to achieve the alignment required, one thing to note is the Stretch characteristics of the Image control - the image ratio will be maintained as the app is resized. Also I'm using the default (implicit) styling for the button controls, these come from the MahApps libraries: The next iteration sees the ' cleaning up ' of the actual UI elements - the removal of border from around all the controls and the applying of the flat button style to the next & previous buttons. The other majors changes a...

UI freezing when CPU hits 100% constantly - follow up, part deux

My last post had a comment from @LordHanson about changing the thread priority via the ThreadStart parameter to see if this improved the performance. I made the following small change to include setting the thread priority: This improved the responsiveness of the UI when the number of thread is equal to or greater than the number of logical processors (tested with 2 * System.Environment.ProcessorCount), I still see the UI stuttering & stalling occasionally but it's greatly reduced - happens less than 10% of the time. I then start to wonder is it better from the point of view of total time taken to reduce the concurrency to less than the number of logical processors or to reduce the thread priority. I suppose the answer depends on which technique you want to use, for me there isn't a justifiable reason not to use the Parallel.For from the TPL - it's not a piece of time critical code.

UI freezing when CPU hits 100% constantly - follow up

I was wondering on the affects if my previous post was re-written to remove the use of TPL and just use the standard threading classes in the framework instead, would the performance be different. So the previous post was using the following code: When the limited concurrency scheduler is initialised with a number greater than or equal to the number of logical processors then the CPU hits 100% and the app stalls and stutters. If this is re-written without the use of TPL would I get the same results? Re-writing this produced the following code, using a Semaphore to control the number of concurrent threads: In answer to the question, you get exactly the same behaviour, there is no benefit in not using TPL to handle the concurrency - good :)

UI freezing when CPU hits 100% constantly

I've been working with a team where I've been putting a WPF UI over a set of implementations of a custom interface. Each implementation is a long running process which is handled in standard manner by creating an asynchronous Task<T> and displaying the results from the continuation. The standard CPU utilisation for the majority of the interface implementations is shown below - this represent the ideal, not hogging the CPU's and behaving like a good citizen... Where as one implementation has the following CPU utilisation - all logical processors max'ed out. The app becomes unresponsive, the UI starts to stutter and freeze all because the dispatcher (UI) thread is not being scheduled frequently enough - white screen of death (WSoD). It's not only this app it starts to affect but all other apps running. Hopefully the difference between the majority and this particular instance is obvious - the majority are single-threaded long running processes and t...

Modelling units of measure

I've been looking at writing an exercise app, and this would require some kind of units of measure for distance (as well as time). I could use an enum to represent the different types of units - metres, kilometres, yards, miles etc. But this seems to be lacking from the point of view of converting between the different types - I would need a class to represent the conversions. What I want is a more integrated approach - the use of static read-only properties. What I want to be able to do is best described by the following test: The Measurement struct is very simple, a couple of properties - Amount & Unit and a ConvertTo method: As you can see in the ConvertTo method the Unit class owns the conversions - it is more than just a simple Enum - it has behaviour, how to convert between the different units of measure. So how are the units defined? A Unit instance is exposed as a read-only static property on a static Units class: What makes this approach interesting ...

Simple F# REPL in WPF - part 4

This is the final post in this mini series, I'm going to show the finished UI user control and how simple it is to host inside a WPF app. The code is available on gitHub  and the published binaries are available via nuGet  (supports .Net version 4.0+). Before I show the finished UI, lets look at the F# Interactive in Visual Studio, this loads, queries & displays the results from an external assembly: What does this look like in my implementation? It's looks pretty familiar right? :) The major difference is I've output the working folder for the F# interactive process when the process starts: I've made this configurable in code, so I thought it would be a good idea to tell the end user where they could put any assemblies they want to reference. How can the user access the working folder? Simply, a right click menu: All the menu options should be obvious, the working folder for the above example is shown below, you can see it has the assembly re...

Managing themes in WPF

Having just published Simple.Wpf.Terminal as a nuget package I wanted the ability to skin the user control - the more flexibility in the way it looks the more likely it will be used by other devs :) Before getting into the detail, lets look at the solution working in the test harness for my WPF F# REPL Engine , shown below are four different themes applied dynamically at run-time - no recompilation required to change the theme: Previously in larger WPF apps I've relied upon third-party control vendors like Telerik to provide theming support. Typically they provide good solutions to the problem with multiple themes supported as standard, and when you're already using their third party controls the use of their themes makes sense. But when you're developing a small UI component you don't want to add (& support) such a large footprint just to add themes to your niche control. A theme is a collection of Styles grouped into a ResourceDictionary in WPF. A ...

Simple F# REPL in WPF - part 3

In this post I'm going to talk about the WPF user control I'm going to use to display the output from the F# Interactive executable, the previous posts ( here & here ) talked about manipulating the standard input & output streams of the executable to provide the following interface: Loading gist .... The control will render any values generated by the Output property on the interface, the property is an Rx stream which generates a new value when ever the F# Interactive executable outputs a line. The control has basic REPL semantics - user enters a line of text, the line is executed and the output is printed to the screen. The F# Interactive window in Visual Studio is the implementation I'll be copying. I had a look around and found 'WPF Terminal' on CodePlex  it looked promising - fulfills the REPL requirements, until I realised it's using a TextBox  for the rendeing. With a TextBox the text can only be one colour and I need the ability to s...