Sunday, August 2, 2020

C#: Async ParallelForEach

Here's an example of using ParalleForEach Async in C#

        /// <summary>
        /// Extension method for ParallelForEachAsync
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="source"></param>
        /// <param name="dop"></param>
        /// <param name="body"></param>
        /// <returns></returns>
        public static Task ParallelForEachAsync<T>(this IEnumerable<T> source, int dop, Func<T, Task> body)
        {
            async Task AwaitPartition(IEnumerator<T> partition)
            {
                using (partition)
                {
                    while (partition.MoveNext())
                    { await body(partition.Current); }
                }
            }

            return Task.WhenAll(
                Partitioner
                    .Create(source)
                    .GetPartitions(dop)
                    .AsParallel()
                    .Select(p => AwaitPartition(p)));
        }

Usage:

#region Extension method for ParallelForEachAsync: usage
       
        //await GetDocumentsFromDatabase(session).ForEachAsync(dop: 20, body: async entry =>
        //{
        //    _logger.Info($"Processing entry '{entry.Id}'");
        //});

#endregion

Thursday, March 26, 2020

Hosting Restful services on Windows service using OWIN


Hosting Restful services on Windows service using OWIN

Code:

public partial class Service : ServiceBase
{
protected override void OnStart(string[] args)
        {
            StartOptions options = new StartOptions();
           options.Urls.Add("http://+:9889/DPE");
            _startup = WebApp.Start<OwinStartup>(options);
        }
}


Steps on server:

1.       Open command prompt in Administration mode.
2.      Run netsh show command and redirect output to another txt file
e.g.,netsh http show urlacl >"netsh_registeredurls.txt"
=>Displays/outputs the registered urls in the HTTP.sys settings
3.      If particular port present, delete all entries using netsh http delete command
e.g., netsh http delete urlacl url=http://+:9889/
4.      Uninstall service
5.      Add registered entry in HTTP.sys
e.g., netsh http add urlacl url=http://+:9889/ user=jeder (Note: jeder is Germany equivalent of Everyone)
6.      Install service
7.      Launch service

Tuesday, February 4, 2020

SlowCheetah: The TransformXml task was not found

To fix this, one just has to update the .csproj file of the particular project by adding this line - make sure you point to the correct path of "Microsoft.Web.Publishing.Tasks.dll"

<UsingTask TaskName="TransformXml" AssemblyFile="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v16.0\Web\Microsoft.Web.Publishing.Tasks.dll" />


<Target Name="BeforeBuild">
   <TransformXml Source="App.Base.config" Transform="App.$(Configuration).config" Destination="App.config" />
  </Target>

Saturday, January 11, 2020

Powershell script that will create shortcuts for client and service executables and to run all of them in one go it will also create a batch file.

Powershell script that will create shortcuts for client and service executables and to run all of them in one go it will also create a batch file.
It also takes care of launching the IIS express to host the webservice.


param(
        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [string]$SolutionFileDirectoryPath,
        [AllowNull()]
        [string]
        $ShortcutTargetLocation
)
$SolutionFileDirectoryPath=$SolutionFileDirectoryPath.TrimStart('"')
$SolutionFileDirectoryPath=$SolutionFileDirectoryPath.TrimEnd('"')
$PathExists = Test-Path $SolutionFileDirectoryPath
if ($pathexists -eq $false)
{
    write-host "missing or wrong parameters:"
    write-host "-solutionfiledirectorypath  directory path of solution file in source code(without trailing slash - \)"
    write-host "-shortcuttargetlocation     target location of services shortcut and batch file. Default location is current executable path of powershell script."
    return
}
If($ShortcutTargetLocation -eq $null -or $ShortcutTargetLocation -eq "")
{
    write-host "-ShortcutTargetLocation  Target location of services shortcut and batch file. Default location is current path"
    $ShortcutTargetLocation =$(Get-Location)
}
Else
{
    $PathExists = Test-Path $ShortcutTargetLocation
    If ($PathExists -eq $False)
    {
        write-host "missing parameter:"
        write-host "-ShortcutTargetLocation  Specified target path of services shortcut and batch file doesn't exist. Default location is current path"
        return
    }
}
$ShortcutTargetPath = Join-Path -Path $SolutionFileDirectoryPath -ChildPath "DCX.DPE.AKVService\bin\SQLExpress\DCX.DPE.Service.AKV.exe"
$ShortcutLocation = Join-Path -Path $ShortcutTargetLocation -ChildPath "DCX.DPE.Service.AKV.exe.lnk”
$WScriptShell = New-Object -ComObject WScript.Shell
$Shortcut = $WScriptShell.CreateShortcut($ShortcutLocation)
$Shortcut.TargetPath = $ShortcutTargetPath
$Shortcut.Arguments = "/c"
$Shortcut.Save()

$ShortcutTargetPath = Join-Path -Path $SolutionFileDirectoryPath -ChildPath "DCX.DPE.AKVService\bin\SQLExpress\DCX.DPE.Service.AKV.exe"
$ShortcutLocation = Join-Path -Path $ShortcutTargetLocation -ChildPath "DCX.DPE.Service.AKV.exe.lnk”
$WScriptShell = New-Object -ComObject WScript.Shell
$Shortcut = $WScriptShell.CreateShortcut($ShortcutLocation)
$Shortcut.TargetPath = $ShortcutTargetPath
$Shortcut.Arguments = "/c"
$Shortcut.Save()

$ShortcutTargetPath = Join-Path -Path $SolutionFileDirectoryPath -ChildPath "DCX.DPE.Service.Creta\bin\SQLExpress\DCX.DPE.Service.Creta.exe"
$ShortcutLocation = Join-Path -Path $ShortcutTargetLocation -ChildPath "DCX.DPE.Service.Creta.lnk”
$WScriptShell = New-Object -ComObject WScript.Shell
$Shortcut = $WScriptShell.CreateShortcut($ShortcutLocation)
$Shortcut.TargetPath = $ShortcutTargetPath
$Shortcut.Arguments = "/c"
$Shortcut.Save()

$ShortcutTargetPath = Join-Path -Path $SolutionFileDirectoryPath -ChildPath "DCX.DPE.Service.OpenItems\bin\SQLExpress\DCX.DPE.Service.OpenItems.exe"
$ShortcutLocation = Join-Path -Path $ShortcutTargetLocation -ChildPath "DCX.DPE.Service.OpenItems.exe.lnk”
$WScriptShell = New-Object -ComObject WScript.Shell
$Shortcut = $WScriptShell.CreateShortcut($ShortcutLocation)
$Shortcut.TargetPath = $ShortcutTargetPath
$Shortcut.Arguments = "/c"
$Shortcut.Save()

$ShortcutTargetPath = Join-Path -Path $SolutionFileDirectoryPath -ChildPath "DCX.DPE.Service.Qgroup\bin\SQLExpress\DCX.DPE.Service.Qgroup.exe"
$ShortcutLocation = Join-Path -Path $ShortcutTargetLocation -ChildPath "DCX.DPE.Service.Qgroup.exe.lnk”
$WScriptShell = New-Object -ComObject WScript.Shell
$Shortcut = $WScriptShell.CreateShortcut($ShortcutLocation)
$Shortcut.TargetPath = $ShortcutTargetPath
$Shortcut.Arguments = "/c"
$Shortcut.Save()

$ShortcutTargetPath = Join-Path -Path $SolutionFileDirectoryPath -ChildPath "DCX.DPE.Service.SoftwareManagement\bin\SQLExpress\DCX.DPE.Service.SoftwareManagement.exe"
$ShortcutLocation = Join-Path -Path $ShortcutTargetLocation -ChildPath "DCX.DPE.Service.SoftwareManagement.exe.lnk”
$WScriptShell = New-Object -ComObject WScript.Shell
$Shortcut = $WScriptShell.CreateShortcut($ShortcutLocation)
$Shortcut.TargetPath = $ShortcutTargetPath
$Shortcut.Arguments = "/c"
$Shortcut.Save()

$ShortcutTargetPath = Join-Path -Path $SolutionFileDirectoryPath -ChildPath "DCX.DPE.Service.Workflow\bin\SQLExpress\DCX.DPE.Service.Workflow.exe"
$ShortcutLocation = Join-Path -Path $ShortcutTargetLocation -ChildPath "DCX.DPE.Service.Workflow.exe.lnk”
$WScriptShell = New-Object -ComObject WScript.Shell
$Shortcut = $WScriptShell.CreateShortcut($ShortcutLocation)
$Shortcut.TargetPath = $ShortcutTargetPath
$Shortcut.Arguments = "/c"
$Shortcut.Save()

$ShortcutTargetPath = Join-Path -Path $SolutionFileDirectoryPath -ChildPath "DCX.DPE.Client\bin\SQLExpress\DCX.DPE.Client.exe"
$ShortcutLocation = Join-Path -Path $ShortcutTargetLocation -ChildPath "DCX.DPE.Client.exe.lnk”
$WScriptShell = New-Object -ComObject WScript.Shell
$Shortcut = $WScriptShell.CreateShortcut($ShortcutLocation)
$Shortcut.TargetPath = $ShortcutTargetPath
$Shortcut.Save()

$ShortcutTargetPath = Join-Path -Path $SolutionFileDirectoryPath -ChildPath "DCX.DPE.Client\bin\SQLExpress\DCX.DPE.Client.exe"
$ShortcutLocation = Join-Path -Path $ShortcutTargetLocation -ChildPath "DCX.DPE.Client.exe - PMULLAN.lnk”
$WScriptShell = New-Object -ComObject WScript.Shell
$Shortcut = $WScriptShell.CreateShortcut($ShortcutLocation)
$Shortcut.TargetPath = $ShortcutTargetPath
$Shortcut.Arguments = "APAC\PMULLAN"
$Shortcut.Save()

$BatFile = Join-Path -Path $ShortcutTargetLocation -ChildPath "LaunchDPEServices.bat"
If (Test-Path $BatFile) { Del $BatFile } #> #>
$BatchFileContent ="cd `"C:\Program Files (x86)\IIS Express\`"
start iisexpress /config:`"$(Join-Path -Path $SolutionFileDirectoryPath -ChildPath ".vs\DPE\config\applicationhost.config")`" /site:DCX.DPE.Web
cd /d `"$ShortcutTargetLocation`"
start `"DPE-AKVService`" /b `"DCX.DPE.Service.AKV.exe.lnk`"
start `"DPE-OpenItemService`" /b `"DCX.DPE.Service.OpenItems.exe.lnk`"
start `"DPE-WorkflowService`" /b `"DCX.DPE.Service.Workflow.exe.lnk`"
start `"DPE-QGroupService`" /b `"DCX.DPE.Service.Qgroup.exe.lnk`"
start `"DPE-SwMgmntService`" /b `"DCX.DPE.Service.SoftwareManagement.exe.lnk`""
Set-Content -Path $BatFile -Value $BatchFileContent -Encoding ASCII
write-host "Output files generated at `"$ShortcutTargetLocation`""

Wednesday, October 16, 2019

Logoff from a remote machine


Remote Logoff
  1. Open command prompt using remote machine login credentials
  2. Run QUser command to get list of remote sessions active for current logged in user
C:\Users\XYZ>QUser /server:servername
USERNAME              SESSIONNAME        ID  STATE   IDLE TIME  LOGON TIME
user1                                                            2  Disc     13+22:24  9/26/2019 9:48 AM
user2                          rdp-tcp#0                3  Active          .  9/26/2019 9:54 AM
user3 console                                             4  Active       2:14  10/7/2019 7:27 PM

  1. Run Loggoff command
C:\Users\XYZ>Logoff 3 /server:servername

Thursday, January 31, 2019

NPM config proxy behind corporate wall

NPM config proxy behind corporate wall

Edit C:\Users\Username\.npmrc as below:

strict-ssl = false
ca = null


Reference
https://fak3r.com/2015/07/31/howto-use-npm-behind-a-corporate-proxy/

Wednesday, January 2, 2019

Windows Docker Issue "Deny write access to fixed drive not protected by Bitlocker" con Win10 machine: Solved


Anyone facing this annoying issue in Win10 machine while running Windows Docker in Windows container can be solved by following the below steps:



One needs Admin access though.

Registry Settings
·         [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Policies\Microsoft\FVE]
"FDVDenyWriteAccess"=dword:00000000

Group Edit Policy
·         [Computer Configuration\Administrative Templates\Windows Components\BitLocker Drive Encryption\Fixed Data Drives]
“Deny write access to fixed drives not protected by BitLocker”=Disabled


This perhaps has been fixed in Win 10 17074 Build or higher.