Sorted By: Month (5) and Year (2012)

Getting Started with Windows Server AppFabric Caching PowerShell Commands

I started to use Windows Server AppFabric for its distributed caching feature and I wanted to take a note of the useful PowerShell commands to manage the service configuration and administration.
2012-05-23 05:51
Tugberk Ugurlu


I started to use Windows Server AppFabric for its distributed caching feature and I wanted to take a note of the useful PowerShell commands to manage the service configuration and administration. When I write this blog post, Microsoft AppFabric 1.1 for Windows Server is available. I installed the caching service and caching management features firstly. Then, I configured it properly. See Configure AppFabric page for more information on configuration.

First of all, import necessary modules:

Import-Module DistributedCacheConfiguration
Import-Module DistributedCacheAdministration

You can view the commands with Get-Command Cmdlet. For example, we can see all commands which are available for DistributedCacheAdministration with the following one line of code.

Get-Command -Module DistributedCacheAdministration

image

Use-CacheCluster command sets the context of your PowerShell session to a particular cache cluster. Note that you must run this command before using any other Cache Administration commands in PowerShell.

Get-CacheHost command lists all cache host services that are members of the cache cluster.

Your cache host is running as a windows service and you can retrieve the status of your caching host with Get-CacheHost command. It will output a result which is similar to following:

image

My service is up and running but if the status indicates that the service is down, you can start it with the following command:

Start-CacheHost -HostName TugberkWin08R2 -CachePort 22233

You should be able to see your service running after this:

image

Get-CacheClusterInfo command from configuration module returns the cache cluster information, including details on its initialization status and its size. The following one line of code shows a sample usage.

Get-CacheClusterInfo -Provider XML -ConnectionString \\TUGBERKWIN08R2\Caching

More information on this command: http://msdn.microsoft.com/en-us/library/ff631076(v=ws.10).aspx

That’s it for now.

Resources

.NET 4.5 to Support Zip File Manipulation Out of the Box

One of the missing feature of .NET framework was a support for Zip file manipulation. In .NET 4.5, we have an extensive support for manipulating zip archives.
2012-05-17 15:19
Tugberk Ugurlu


One of the missing feature of .NET framework was a support for Zip file manipulation such as reading the zip archive, adding files, extracting files, etc. and we were using some third party libraries such as excellent the DotNetZip. In .NET 4.5, we have an extensive support for manipulating .zip files.

First thing that you should do is to add System.IO.Compression assembly as reference to your project. You may also want to reference System.IO.Compression.FileSystem assembly to access three extension methods (from the ZipFileExtensions class) for the ZipArchive class: CreateEntryFromFile, CreateEntryFromFile, and ExtractToDirectory. These extension methods enable you to compress and decompress the contents of the entry to a file.

image

Let’s cover the bits and pieces that we get from System.IO.Compression assembly at first. The below sample shows how to read a zip archive easily with ZipArchive class:

static void Main(string[] args) {

    const string zipFilePath = @"C:\apps\Sample Pictures.zip";

    using (FileStream zipFileToOpen = new FileStream(zipFilePath, FileMode.Open))
    using (ZipArchive archive = new ZipArchive(zipFileToOpen, ZipArchiveMode.Read)) {

        foreach (var zipArchiveEntry in archive.Entries)
            Console.WriteLine(
                "FullName of the Zip Archive Entry: {0}", zipArchiveEntry.FullName
            );
    }
}

In this sample, we are opening the zip archive and iterate through the collection of entries. When we run the application, we should see the list of files inside the zip archive:

image

It’s also so easy to add a new file to the zip archive:

static void Main(string[] args) {

    const string zipFilePath = @"C:\apps\Sample Pictures.zip";

    using (FileStream zipFileToOpen = new FileStream(zipFilePath, FileMode.Open))
    using (ZipArchive archive = new ZipArchive(zipFileToOpen, ZipArchiveMode.Update)) {

        ZipArchiveEntry readMeEntry = archive.CreateEntry("ReadMe.txt");
        using (StreamWriter writer = new StreamWriter(readMeEntry.Open())) {
            writer.WriteLine("Lorem ipsum dolor sit amet...");
            writer.Write("Proin rutrum, massa sed molestie porta, urna...");
        }

        foreach (var zipArchiveEntry in archive.Entries)
            Console.WriteLine(
                "FullName of the Zip Archive Entry: {0}", zipArchiveEntry.FullName
            );
    }
}

In this sample, we are adding a file named ReadMe.txt at the root of archive and then we are writing some text into that file.

Extracting files is into a folder is so easy as well. You need reference the System.IO.Compression.FileSystem assembly along with System.IO.Compression assembly as mentioned before for this sample:

static void Main(string[] args) {

    const string zipFilePath = @"C:\apps\Sample Pictures.zip";
    const string dirToExtract = @"C:\apps\Sample Pictures\";

    using (FileStream zipFileToOpen = new FileStream(zipFilePath, FileMode.Open))
    using (ZipArchive archive = new ZipArchive(zipFileToOpen, ZipArchiveMode.Update))
        archive.ExtractToDirectory(dirToExtract);
}

There are some other handy APIs as well but it is so easy to discover them by yourself. Enjoy Smile

Resources

Tags