AppFabric Extensions Preview Release is Out

27. July 2010

Its an important day for AppFabric Extensions project. Initial setup script is finally ready and first preview is published for community review.

Please find preview release at http://appfabricextensions.codeplex.com/releases/view/49706

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

English, AppFabric Extensions

Tip : Check AppFabric Cache Status Using PowerShell

2. April 2010

It’s  a daily job of a System Administrator to check AppFabric Cache status, and it will be very usefull to have a script to do so. Please find following script which will guide you to do so;

$hostinfo = Get-CacheHost

if($hostinfo.Status -eq [Microsoft.ApplicationServer.Caching.AdminApi.ServiceStatus]::Down)){
  # Do whatever you want with a down AppFabric Cache host
}else{
  # Do whatever you want with a running AppFabric Cache host
}

 

As usual, in order to execute this PowerShell script, you should perform following commands first;

Import-Module DistributedCacheAdministration
Use-CacheCluster
Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

AppFabric, English, PowerShell ,

Tip : Is Your PowerShell Session Running With Administrative Privileges?

2. April 2010

Sometimes you may need to perform administrative stuff within your script. In such cases, it’s better to check whether your user has such privileges to execute the script.

Please find the sample below, which checks user for administrative privilege;

$user = [Security.Principal.WindowsIdentity]::GetCurrent()

if((New-Object Security.Principal.WindowsPrincipal $user).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)){
  # Keep executing your script...
}else{
  # Warn user for required privileges...
  Write-Warning "Please execute the script with administrative privileges."
}
Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

English, PowerShell

AppFabric Cache Management cmdlet – v1.1

1. April 2010

Please find below, updated version of cmdlet published on my AppFabric Cache management cmdlet post:

#################################################################
#       AppFabricCacheSetup.ps1 PowerShell Cmdlet               #
#################################################################
# Prepares your AppFabric Cache Cluster environment for         #
# development.                                                  #
#################################################################
# Fatih Boy, April 2010                                         #
# Version 1.1                                                   #
#################################################################

Import-Module DistributedCacheAdministration
Use-CacheCluster
$cacheName= Read-Host "Please enter cache name to create (press enter to use default)"

if($cacheName -ne ""){
    New-Cache $cacheName
}

if ((Get-CacheAllowedClientAccounts) -notcontains $env:username) {
    Grant-CacheAllowedClientAccount $env:username
}

Start-CacheCluster


if($cacheName -ne ""){
    Write-Host ""
    Write-Host "$cacheName cache properties : "
    Write-Host "----------------------------------"
    
    Get-CacheConfig $cacheName
}else{
    Write-Host ""
    Write-Host "default cache properties : "
    Write-Host "----------------------------------"
    Get-CacheConfig default
}
Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

AppFabric, PowerShell, English ,

AppFabric Cache Management cmdlet

17. March 2010

If you start writing your first couple of codes on AppFabric Caching, you’ve probably started to type the same PowerShell commands. If you are a lazy developer, just like me, you get bored to do the same things. So, here is my handy little PowerShell cmdlet, which will help you to prepare your developer environment:

#################################################################
#       AppFabricCacheSetup.ps1 PowerShell Cmdlet               #
#################################################################
# Prepares your AppFabric Cache Cluster environment for         #
# development.                                                  #
#################################################################
# Fatih Boy, March 2010                                         #
# Version 1.0                                                   #
#################################################################

Import-Module DistributedCacheAdministration
Use-CacheCluster
$cacheName= Read-Host "Please enter cache name to create (press enter to use default)"

if($cacheName -ne ""){
    New-Cache $cacheName
}

if ((Get-CacheAllowedClientAccounts).IndexOf($env:username) -eq -1) {
    Grant-CacheAllowedClientAccount $env:username
}

Start-CacheCluster


if($cacheName -ne ""){
    Write-Host ""
    Write-Host "$cacheName cache properties : "
    Write-Host "----------------------------------"
    
    Get-CacheConfig $cacheName
}else{
    Write-Host ""
    Write-Host "default cache properties : "
    Write-Host "----------------------------------"
    Get-CacheConfig default
}

This little cmdlet will –optionally- create a named cache, allows your user to access cache, starts cache cluster, finally display properties of created cache.

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

AppFabric, English, PowerShell ,

AppFabric Beta 2 is out!

1. March 2010

Microsoft just publish beta 2 release of its application server extensions, AppFabric. You can download this release from here.

In order to uninstall previous release of AppFabric, here is the path to follow :

Control Panel -> Programs -> Programs and Features -> Installed Updates -> Application Server Extensions for .NET 4 (KB970622)

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

AppFabric, English ,

dynamic vs object

2. February 2010

      People keep asking me what is the difference between “dynamic” and the old friend “object”. Well, answer is a little tricky; in order to show the difference, it’s better to take a look at both of them more detailed.

     Let me start with the old friend “object”, that is nothing but a shortcut for “System.Object”; it’s a part of our life since the first release of C#. Object is very powerful, cause you can assign almost any value. In order to compare them lets take a loot a example below:

object obj = 1;

 if (obj.GetType() == typeof(int)){
     Console.WriteLine("of course type is int");
 }  else {
     Console.WriteLine("Whole post is wrong :P");
}

Console.ReadKey();

One may expect "Whole post is wrong :P", since obj is declared as object not the int; but output of the code is simply "of course type is int"; cause value stored at obj is of type int. Although value of obj is of type int and code runs without any problem, as soon as you perform any int related operation, like sum, code will not even compile:

object obj = 1;

 if (obj.GetType() == typeof(int)){
     Console.WriteLine("of course type is int");
} else {
     Console.WriteLine("Whole post is wrong :P");
}

obj = obj + 1;

(error CS0019: Operator '+=' cannot be applied to operands of type 'object' and 'int')

In order to compile your code, you need to cast obj first:

object obj = 1;

 if (obj.GetType() == typeof(int)){
     Console.WriteLine("of course type is int");
} else {
     Console.WriteLine("Whole post is wrong :P");
}

obj = (int)obj + 1;

of course having a compiled code doesn’t means that everything is fine. Anyone can have a compiled code like below, which gives run-time error (InvalidCastException)

object obj = 1;

 if (obj.GetType() == typeof(int)){
     Console.WriteLine("of course type is int");
} else {
     Console.WriteLine("Whole post is wrong :P");
}

obj = (string)obj + 1;

Compiler does not let you perform any mathematical operation on obj, as long as explicitly indicated. But still can be explicitly cast to any other type, which causes run-time exceptions.

Here comes “dynamic” in. Dynamic  comes with C# 4.0 and instructs compiler not to perform additional rules on your type. You can use it just like you do with the “object”:

dynamic obj = 1;

 if (obj.GetType() == typeof(int)){
     Console.WriteLine("of course type is int");
 }  else {
     Console.WriteLine("Whole post is wrong :P");
}

Console.ReadKey();

Also, following code will compile without any compiler error:

dynamic obj = 1;

 if (obj.GetType() == typeof(int)){
     Console.WriteLine("of course type is int");
} else {
     Console.WriteLine("Whole post is wrong :P");
}

obj = obj + 1;

You don’t need to cast obj to int as you do with object. This is the main difference between object and dynamic. By using dynamic keyword, you instruct compiler not to perform additional rules on your type. It will instruct compiler that its type can be known only at run-time, so compiler doesn’t try to interfere.

By using dynamic, your code’s readability will improve, you don’t have to perform additional explicit cast.

“WITH GREAT POWER THERE MUST ALSO COME - - GREAT RESPONSIBILITY!”

Dynamic is very handy; but must use very carefully. It is still as dangerous as object is and you should apply all check-points within your code.

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

C#, English ,

Changing schema names on Sql Server 2005

25. July 2007

     I my database on Sql Server 2005, I have to change schema names from some username to dbo and I wrote the following sql statements which simply generates alter queries. All you have to do is to change 'Username Here' to actual username, it will generates alter queries for you and then just copy and execute them in query window.

SELECT 'ALTER SCHEMA dbo TRANSFER' + s.Name + '.' + p.Name FROM sys.Procedures p INNER JOIN
sys.Schemas s on p.schema_id = s.schema_id WHERE s.Name = 'Username Here'

SELECT 'ALTER SCHEMA dbo TRANSFER' + s.Name + '.' + t.Name FROM sys.Tables t INNER JOIN
sys.Schemas s on t.schema_id = s.schema_id WHERE s.Name = 'Username Here'

SELECT 'ALTER SCHEMA dbo TRANSFER' + s.Name + '.' + v.Name FROM sys.Views v INNER JOIN
sys.Schemas s on v.schema_id = s.schema_id WHERE s.Name = 'Username Here'
Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

English, Misc

Some Useful CheatSheet Links

9. February 2007

Generating Managed WMI Classes

19. August 2006

  If you are also writing code using WMI on C#, probably you've also use the following classes; ManagementScope, ManagementObjectSearcher, ManagementObjectCollection and also ManagementObject; But they're not such easy classes to deal with, espacially if you're using WMI methods. Take a look at this code:

private void List(string containerName) {
	string sorgu = String.Format("SELECT * FROM MicrosoftDNS_AType WHERE ContainerName = '{0}'", containerName);
	ManagementScope managementScope = new ManagementScope(new ManagementPath(@"\\.\root\MicrosoftDNS"));
	managementScope.Connect();

	ManagementObjectSearcher objectSearcher = new ManagementObjectSearcher(managementScope, new ObjectQuery(sQuery));
	ManagementObjectCollection objectCollection = objectSearcher.Get();

	foreach (ManagementObject managementObject in objectCollection) {
		//Business logic goes here
	}
}

   Here is the good news; "Management Strongly Typed Class Generator" (Mgmtclassgen.exe) application, placed in .Net framework tools makes it easier. "mgmtclassgen.exe" tool, placed under "\Microsoft Visual Studio 8\SDK\v2.0\Bin\" folder, generates managed classes to deal with WMI classes.

   Here is one of the simplest usage of the tool :

mgmtclassgen.exe Win32_Service

It will generate a managed class for the Win32_Service WMI class. Here is another sample :

mgmtclassgen Win32_Service /n root\cimv2 /l CS /p c:\servisler.cs

In this sample, tool will generate a managed class for the Win32_Service WMI class placed under Root\cimv2 namespace and gives its output to "c:\servisler.cs". Also output class will be placed under ROOT.CIMV2.Win32 namespace. Here is other parameters that can be used:

/L Generated Language. Followings cand be used, default is CSharp (CS) : CS, VB, JS, VJ, MC.
/M Remote computer to connect.Default is local machine.
/U Username
/PW Password

   Here is the code that makes the same thing with the code in first example. But this one uses the managed C# class called AType that generated by mgmtclassgen for AType WMI class :

private void List(string containerName) {
	foreach (AType atype in AType.GetInstances(String.Format("SELECT * FROM MicrosoftDNS_AType WHERE ContainerName = '{0}'", containerName))) {
		//Business logic goes here
	}
}
Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

C#, English ,