Samstag, 21. Januar 2017

Check for usb-sticks in windows

Hi,

I wanted to check in my app whether usb sticks are connected or not (automatic recognition)...

Some research later I found the following article:

https://www.codeproject.com/Articles/63878/Enumerate-and-Auto-Detect-USB-Drives

works quite straight forward ...

things to mention:
- for different use-cases it might be enough to check Win32_LogicalDisk where drivetype=2

- in wpf you might use something like:

1
2
3
4
5
6
        protected override void OnSourceInitialized(EventArgs e)
        {
            base.OnSourceInitialized(e);
            HwndSource source = PresentationSource.FromVisual(this) as HwndSource;
            source.AddHook(WndProc);
        }

- when you disconnect a stick and call the wmi to show you data Win32_DiskDrive / Win32_LogicalDisk you get exception ... sleep and retry works after about 5 seconds.

- you can check the result in command line using "wmic logicaldisk get"

kr,
Daniel

Mittwoch, 18. Januar 2017

C# code generation for databases

Hi,

today I found SQLMetal.exe... https://msdn.microsoft.com/en-us/library/bb386987(v=vs.110).aspx

It is part of the visual studio installation and can create c# and vb code for different kind of db artefacts... I am curious how this works in comparison to Entity Framework (= T4).

kr,
Daniel

Dienstag, 3. Januar 2017

vim as a command line util

For quick find, edit, search and/or replace actions vi | vim | gvim is a perfect tool. A research question for me was: is it also a tool for automation? I wanted to search and replace strings with vi-syntax through a batch file (yeah... using windows makes the thing even more special) which can be scheduled or called on demand.

... and yes... it works. see: http://stackoverflow.com/questions/6945558/calling-search-and-replace-functions-for-vim-from-within-a-bash-script ... the trick is to start in "Ex-Mode" ... (see: documentation), but unfortunately it did not worked for me... I was not able to make it work in my script... no idea why... but:


then I found the -c option which is simple and works perfectly... -c executes a command like substitutions or other stuff and can be used in a chain of "-c"s.



my test-environment:
echo. >> data.txt
del data.txt
echo data data data > data.txt
cls

type data.txt
gvim -c %%s/data/0101/g -c wq data.txt
echo _____________________________________________________
type data.txt

it outputs data before the substitution and 0101 after it... perfect!

kr,
Daniel

Freitag, 30. Dezember 2016

stateless - workflows in .NET

Hi,

I found some frameworks for handling workflows:

- ApprovaFlow
- simple state machine
- objectflow
- jazz
- nstate 

... but best of all alternatives seems to be "stateless" (see: https://github.com/dotnet-state-machine/stateless ).

The following features / best practices are made (following the examples-folder of the github repo):
  • choose or build a class for State
  • choose or build a class for Triggers
  • create an object for a StateMachine<State,Trigger>
    • initialization / get-set possibility to variables outside)
  • configuring phase
  • stateMachineObject.IsInState(stateItem)
  • stateMachineObject.PermittedTriggers
  • stateMachineObject.CanFire(triggerItem)
  • stateMachineObject.Fire(triggerItem)
  • stateMachineObject.FireAsync(triggerItem)
Configuration Options fluid-api style:
  • stateMachineObject.Configure(stateItem)
    • SubstateOf(stateItem) // Hierarchical State
    • PermitReentry(Trigger)
    • Permit(Trigger, State)
    • PermitIf(Trigger, State, Guard)
    • Ignore(Trigger)
    • InternalTransition
    • OnEntry(() => action());
    • OnEntryAsync(async () => await action());
    • OnEntryFrom(trigger, x => action()); // Parameterized Trigger
    • OnExit(() => action());
Support for DOT-graph - visualization (see: Graphviz): .ToDotGraph()

kr,
Daniel

Donnerstag, 29. Dezember 2016

Assigning Certificates to applications (without IIS)

Hi,

it is hard work to implement a web page using HttpListener! It is easy to turn the HttpListener to listen to https (by changing the listening prefix) but to get this up and running is still a challenge!

You need to:


I followed the instructions of http://sunshaking.blogspot.co.at/2012/11/https-communication-httplistener-based.html , but still needed more than 4 hours to get it up and running. Important is to work with the generated pfx file! It is clear but it is not mentioned explicitly in the article... also keep an eye on the icons. if you have the pfx file, you need to see a lock symbol in the mmc.

kr,
Daniel

Donnerstag, 22. Dezember 2016

SQL Server - Checks

Hi,

try to start a list of most important things (not in order) to check as a DBA in operation:


  • Services Running
    (see: sys.dm_server_services)
  • FileSystem Space
    (see: sp_spaceused and sp_MSforeachtable / sys.master_files + sys.databases / msdb.sys.dm_io_virtual_file_stats)
  • Error Log (Agent and DB)
    (see: xp_readerrorlog)
  • Locks and Waits
    (see: sys.dm_exec_requests (wait_type), sys.dm_exec_connections, sys.dm_exec_sql_text, trace-flags 1204, 1222)
  • Backups
    (see: msdb.dbo.backupmediafamily, msdb..backupset)
  • expensive queries
    (see: dm_exec_query_stats, dm_exec_sql_text, dm_exec_query_plan)
  • index fragmentation
    (see: sys.dm_db_index_physical_stats)
  • statistics
    (see: dbcc show_statistics)
  • consistency
    (see: dbcc checkdb / progress: SYS.DM_EXEC_REQUESTS, SYS.DM_EXEC_SQL_TEXT)
  • overview of sessions (users)
    (see: sys.dm_exec_sessions, kill)
  • Shrink
    (see: dbcc shrinkfile / don't do that if not edge case)
  • User Handling
    (see: create user/login, sp_change_users_login)
  • Update Process
  • Logging Data-Changes
    (see: Change Data Capture (msdn), Trigger)
  • Firewall Security
  • Automation
    (see: SQL Server Agent, Powershell, SSIS, ETL)
Supporting Tools: Management Studio, Profiler, SSDT / BIDS, Configuration Manager, PowerShell, Nagios, IIS Crypto, sqlcmd, bcp, tsqlt 

kr,
Daniel

C# WCF WebOperationContext - best practice in error handling

Hi,

WebOperationContext is typically used in a WCF REST method so that method can access the incoming request and outgoing response.

(see: http://stackoverflow.com/questions/18747581/difference-between-weboperationcontext-current-and-httpcontext-current )


... in my projects I wrap OperationContract-ed methods with error handling. You can imagine that with a standard method like MessageWrapper which uses an Action or Function and implements the code in lambda-style in-line. The MessageWrapper handles the Exception handling and sets the Response ErrorCode (WebOperationContext.Current.OutgoingResponse.StatusCode) to 200 if everything is fine or to error if an exception occurred.

public T MessageWrapper(Func<T> action) where T : class{
  try 
    SetStatusOK(); 
    return action(); 
  } catch (Exception exc){
    SetStatusError(exc);
    return null;
}

public ReturnObject Operation() {
  MessageWrapper(()=> return new ReturnObject("some", "parameters"));
}

... here I would love to mention, that with Castle you can create interceptors where you don't have to put the MessageWrapper inside the business logic code. You can keep this as an aspect (AOP) which can be configured at startup.

And here one more thing that helped a lot me during development time. You can set additionally to the StatusCode also a StatusDescription which e.g.: is displayed in your browser of choice as an explanation for an error. So StatusDescription = exc.Message is simply gold!

kr,
Daniel