how-to

Recovering your external hard disk data

November 15, 2013 Visual Studio , ,

I recently faced a situation that my external hard disk that I used as a backup drive started giving me bad sector issues.  I ignored the early warnings until I released that the occurrence of these issues had increased.  I used several applications to recover the files but no software could guarantee 100% recovery.  When the problem became terse, there wasn’t any easy relief!

I wasn’t able to browse my disk and I was prompted ‘Format E:\ drive’  — a solution that was unacceptable to me.

If you are facing this issue, then this article should help you!

Resolving the Format E: drive and access issue

As advised on many websites, I started with chkdsk on Command Prompt (with Run As Administrator option) on my Windows 8 laptop.  This is a time consuming task so please keep some time at hand

chkdsk e: /f /r

Immediately I received the first error after which I abruptly stopped chkdsk.  The error was 

The first NTFS boot sector is unreadable or corrupt

Chkdsk utility will not be able to fix this issue.  The Windows NT version 4.0 file system keeps a duplicate copy of the NTFS boot sector at the end of the partition.  So we still have a chance to recover the boot sector from the end and replace it at the first position.  To recover this, you need an application to do some magic for you.  After trying my luck with many promising applications, I found a very simple DOS-based utility that works wonders.

TestDisk: http://www.cgsecurity.org/wiki/TestDisk can be used to navigate to the disk, analyse partition (if any) and recover NTFS boot sector from backup. 

    run TestDisk,
    select the hard disk and the partition type
    choose Advanced
    select the partition
    choose RebuildBS

RebuildBS detected that there was a discrepancy between boot sector at beginning and at the end.  And then fixed it.  This will allow you to browse your disk from Windows Explorer

Fixing the bad sector issues

Now you re-run the following chkdsk command

chkdsk e: /f /r

Again, this is a time consuming task so please keep some time at hand.  It will fix any bad sector errors, missing file chains, etc and hopefully clean most of the issues.  However, there is no guarantee that the drive will never again have bad sectors

Data Backup

If your data is critical, it is advisable to retain another copy of it on a different drive.  There are many tools available to transfer your data from one disk to another.  But most of these tools get stuck when they face any bad sector.  I preferred to use some native tools in Windows 8 to do the initial data transfer.  Windows 8 comes with 2 handy tools – xcopy and robocopy.  I found robocopy much faster than xcopy.

The syntax for robocopy to copy contents of E: drive (the bad drive) to F: drive (new backup drive) would be

robocopy e:\ f:\ /MIR /R:0 /W:0

/MIR = Mirror entire directory structure (can use /E instead)
/R:0 = 0 retries for read/write failures
/W:0 = 0 seconds between retries

You can also use xcopy to do the same task

xcopy /C/H/R/S/Y e:\ f:\

/C = Continues copying even if errors occur
/H = Copies hidden and system files also
/R = Overwrites read-only files
/S = Copies directories and subdirectories
/Y = Overwrites existing files without asking

Hope this resolves your problems.

[Solved] App Store : Your PC is not connected to Internet

November 4, 2013 Visual Studio, Windows ,

Sometimes the Store application on Windows 8.x gives connectivity error.  If you are reading this article, it means you have tried clicking on ‘try again’ and you were unsuccessful in connecting to the Internet.  Rebooting the OS also does not always resolve this issue.

image

Check your internet connectivity on the Desktop mode.  If you are connected to the Internet on Desktop mode, then first check the date and time set on your machine.  App Store requires your date and time to be set correctly.  If the date and time are not set correctly, you need to follow these steps

  1. Press Windows key + C on your keyboard to show the Charms bar
  2. Type “Date and Time” in the Charms bar
  3. Click Change date and time button
  4. Click Apply and OK to save date and time

If date and time are set correctly, then you need to troubleshoot the network connectivity

  1. Press Windows key + C on your keyboard to show the Charms bar
  2. Type Troubleshooting and click Troubleshooting under Settings
  3. Type Network Troubleshooter in the search option
  4. Click on Network troubleshooter to run the troubleshooter

If even after this the App Store is not able to connect to the Internet, you can follow these instructions

  1. Press Windows key + C on your keyboard to show the Charms bar
  2. Type WSReset.exe and press enter
  3. This clears the cache and opens the Store application again
  4. Reboot your machine to have a final clean up

This should resolve the connectivity issue.  If it still does not resolve, download the below utility and navigate through the wizard

http://download.microsoft.com/download/F/2/4/F24D0C03-4181-4E5B-A23B-5C3A6B5974E3/apps.diagcab

This wizard may ask you to reboot your machine and fix the issue.

HTTP/S WCF : Commonly faced access issues and solutions

September 5, 2013 CSharp, Visual Studio, WCF , , , ,

When running WCF services on Windows 7+ (actually, includes Vista too) operating systems when you write a simple code at the service side to open the service host, most users experience this issue

HTTP could not register URL http://+:8010/. Your process does not have access rights to this namespace (see http://go.microsoft.com/fwlink/?LinkId=70353 for details).

When you visit the link in the above error, you do not necessarily get the required information to solve this issue quickly.  So let’s see the solution to this commonly faced problem.   HTTP/S services are usually hosted on IIS or self-hosted in an application.   When registering this service on an operating system that has enhanced security (Win7+, Win 2008/2012) you need to perform some administration work such as mentioned below.   Some of you may get several other issues like endpoint not found, or SSL certificate issues. 

This article explains what is required to fix these issues

 

Namespace registration

Namespace registration grants access rights to a specific URL to a specified group/user on a domain or computer.   This one time activity ensures that only authorized users can open up endpoints on a computer/server – this is definitely more secure.

How to authorize your service/user account:

Local user account:

netsh http add urlacl url=http://+:8010/ user=ComputerName\Username

Domain user account:

netsh http add urlacl url=http://+:8010/ user=DomainName\Username

Built-in Network Service account

netsh http add urlacl url=http://+:8010/ user="NT AUTHORITY\NETWORK SERVICE"

Most likely this should directly solve your problem “HTTP could not register URL”  but I would advise going through the other steps on a production machine (actually, any restricted environment) to ensure that there are no hiccups.

 

Firewall Exception

Most organizations have Firewall Restrictions on HTTP communication on Production machines.  In that case, you would have to ensure that the port on which you are doing communication is added to the Firewall Exception list

 

SSL Certificate Store

 

HTTPS services rely on certificate exchange between server and client (in case of mutual authentication) to authenticate the the request and also to encrypt the request data.  These certificates are stored in a certificate store and it is important to configure the certificate access to a port

You can use following command to bind SSL certificate to a port (8010)

httpcfg set ssl -i 0.0.0.0:8010 –h thumprint-of-certificate-in-certificate-store

The thumbprint of a certificate can be retrieved by viewing the certificate properties in Certificate Management Console (mmc)

 

Changing these settings should help you resolve all service access issues

dI.Hook – How to ignore hook on specific method?

August 9, 2012 CSharp, Open Source, Visual Studio , , ,

 

If you are new to dI.Hook, please visit the di.Hook Product Page for Overview, Release Version and Source Code.

Some posts you might want to read would be:

  1. How to create a HookRepository?
  2. How to add hooks to HookRepository?
  3. How to add hooks via configuration file to a HookRepository?
  4. How to invoke all hooks using dI.Hook?
  5. How to invoke hooks that satisfy a condition?
  6. How to retrieve an object of hook?
  7. How to remove hooks from HookRepository?

So this is the eighth article in the series which will deal with

– Ignoring one or more hook objects when executing OnInvoke in a method
– but NOT removing the hook from the repository

Step 1 – Creating a repository and add hooks into a repository

The first decision in creating a hook repository is to think whether you want to have lazy loading or not. Once you have decided you can go through the steps – Creating a Hook Repository using dI.Hook

Be selective in adding hooks into a repository if you plan to invoke all of them at one go. If you are thinking of manually adding hooks to a repository, would suggest going through – Adding hooks manually into a repository. However, if you are planning to load them through configuration you can read – Loading a configuration set of hooks into repository

How to ignore a hook being invoked on a method?

 

The repository created in step 1 consists of 2 hooks.

Method One – Ignoring all hooks

 

Ignoring all hooks
[TestMethod] [RemoveAllHooks] public void Test_Standard_RemoveHooks()
{
    int invokedHooks = hookRepository.InvokeAll();
    Assert.AreEqual(0, invokedHooks);
}

 

Method Two – Ignoring hooks with particular name

Ignore hooks with name LogHook
[TestMethod] [RemoveHook(new[] { "LogHook" })] public void Test_Standard_RemoveHooksWithName()
{
    int invokedHooks = hookRepository.InvokeAll();
    Assert.AreEqual(1, invokedHooks);
}

or, you can have multiple hook names in the list

Ignore multiple hooks
[TestMethod] [RemoveHook(new[] { "LogHook", "DiagnosticsHook" })] public void Test_Standard_RemoveHooksWithName_MultipleHooks()
{
    int invokedHooks = hookRepository.InvokeAll();
    Assert.AreEqual(0, invokedHooks);
}

or, using multiple attributes for doing the same

Ignoring multiple hooks
[TestMethod] [RemoveHook("LogHook")] [RemoveHook("DiagnosticsHook")] public void Test_Standard_RemoveHooksWithName_MultipleAttributesInMethod()
{
    int invokedHooks = hookRepository.InvokeAll();
    Assert.AreEqual(0, invokedHooks);
}

 

Method Three – Ignore a hook of specific type

 

Ignoring a hook type
[TestMethod] [RemoveHookType(typeof(LogHook))] public void Test_Standard_RemoveHooksWithType()
{
    int invokedHooks = hookRepository.InvokeAll();
    Assert.AreEqual(hookRepository.Hooks.Length – 1, invokedHooks);
}

or

Ignoring multiple hook types
[TestMethod] [RemoveHookType(typeof(LogHook))] [RemoveHookType(typeof(DiagnosticsHook))] [RemoveHookType(typeof(LogHook))] public void Test_Standard_AddRemoveHookCombination()
{
    int invokedHooks = hookRepository.InvokeAll();
    Assert.AreEqual(0, invokedHooks);
}

 

So using these techniques, you can have a method ignore one or many hooks.  These hooks will still remain in the repository and methods without these attributes will continue to invoke their OnInvoke method.

dI.Hook – How to remove hooks from HookRepository?

August 7, 2012 CSharp, Open Source, Visual Studio , , ,

If you are new to dI.Hook, please visit the di.Hook Product Page for Overview, Release Version and Source Code.

Some posts you might want to read would be:

  1. How to create a HookRepository?
  2. How to add hooks to HookRepository?
  3. How to add hooks via configuration file to a HookRepository?
  4. How to invoke all hooks using dI.Hook?
  5. How to invoke hooks that satisfy a condition?
  6. How to retrieve an object of hook?

So this is the seventh article in the series which will deal with removing one or more hook objects from the repository.

Step 1 – Creating a repository and add hooks into a repository

The first decision in creating a hook repository is to think whether you want to have lazy loading or not. Once you have decided you can go through the steps – Creating a Hook Repository using dI.Hook

Be selective in adding hooks into a repository if you plan to invoke all of them at one go. If you are thinking of manually adding hooks to a repository, would suggest going through – Adding hooks manually into a repository. However, if you are planning to load them through configuration you can read – Loading a configuration set of hooks into repository

Step 2- How to remove an instance of a hook from a repository?

When we are referring to adding an instance of a hook in a repository, there are multiple ways of doing it

Method 1 – Removing hook instance( s ) at a time

Removing a single hook
[TestMethod] public void Test_Standard_RemoveHookObject()
{
    hookRepository.Remove(new LogHook());
    Assert.AreEqual(1, hookRepository.Hooks.Length);
}

Method 2 – Removing a List of hooks

Removing list of hooks
[TestMethod] public void Test_Standard_RemoveHookList()
{
    hookRepository.Remove(new List<IHook> { new LogHook(), new DiagnosticsHook() });
    Assert.AreEqual(0, hookRepository.Hooks.Length);
}

Method 3 – Removing hooks of a particular type

Removing hook of a type
[TestMethod] public void Test_Standard_RemoveHookType()
{
    hookRepository.Remove(typeof(LogHook));
    hookRepository.Remove(typeof(DiagnosticsHook));
    Assert.AreEqual(0, hookRepository.Hooks.Length);
}

Method 4 – Removing all hooks

Removing all hooks
[TestMethod] public void Test_Lazy_RemoveAll()
{
    hookRepository.RemoveAll();
    Assert.AreEqual(0, hookRepository.Hooks.Length);
}

 

So there are 4 ways to remove a hook from the repository so that any method using OnInvoke method on the repository will find the removed hook in the repository

dI.Hook – How to retrieve an object of hook?

August 6, 2012 CSharp, Open Source, Visual Studio , , ,

If you are new to dI.Hook, please visit the di.Hook Product Page for Overview, Release Version and Source Code.

Some posts you might want to read would be:

  1. How to create a HookRepository?
  2. How to add hooks to HookRepository?
  3. How to add hooks via configuration file to a HookRepository?
  4. How to invoke all hooks using dI.Hook?
  5. How to invoke hooks that satisfy a condition?

So this is the sixth article in the series which will deal with retrieving one or more hook objects instead of just invoking them.

Step 1 – Creating a repository and add hooks into a repository

The first decision in creating a hook repository is to think whether you want to have lazy loading or not.  Once you have decided you can go through the steps – Creating a Hook Repository using dI.Hook

Be selective in adding hooks into a repository if you plan to invoke all of them at one go.  If you are thinking of manually adding hooks to a repository, would suggest going through – Adding hooks manually into a repository.  However, if you are planning to load them through configuration you can read – Loading a configuration set of hooks into repository

Step 2 – Getting a IHook Object from repository

 

Method One – Getting hook object of a specific type

 

Getting hook by Type
[TestMethod] public void Test_Standard_GetHookByType()
{
    var hooksRetrieved = hookRepository.Get(typeof(LogHook));
    Assert.AreEqual(1, hooksRetrieved.Length);
}

 

The above method will return all the hooks that match the type of LogHook.  Now say for example, the repository was created using code below

Two hooks of same type
LogHook logHook1 = new LogHook(); /* Default Guid, Name */
LogHook logHook2 = new LogHook();
logHook2.Name = "LogHook2";

hookRepository.Add(new[] { logHook1, logHook2 });

 

The code hookRepository.Get(typeof(LogHook)); would return the value 2.

Method Two – Getting hook object based on some condition

 

Getting hook by a predicate
[TestMethod] public void Test_Standard_GetHookByName()
{
    var hooksRetrieved = hookRepository.Get(x => x.Name != null && x.Name.Contains("Log"));
    Assert.AreEqual(1, hooksRetrieved.Length);
}

Using the above method, you can query on the hook collection and returns the hook objects in an IEnumerable collection.

Method Three – Getting all hooks in a repository

This is fairly simple.  You can access the collection Hooks in the repository to get all the hooks added in the repository

Getting all hooks
[TestMethod] public void Test_Standard_GetAllHooks()
{
    var hooksRetrieved = hookRepository.Hooks;
    Assert.AreEqual(2, hooksRetrieved.Length);
}

dI.Hook – How to invoke hooks that satisfy a condition?

August 1, 2012 Applications, CSharp, Open Source , , ,

If you are new to dI.Hook, please visit the di.Hook Product Page for Overview, Release Version and Source Code.

Some posts you might want to read would be:

  1. How to create a HookRepository?
  2. How to add hooks to HookRepository?
  3. How to add hooks via configuration file to a HookRepository?
  4. How to invoke all hooks using dI.Hook?

So this is the fifth article in the series which will deal with invoking all the hooks in the repository.  This article will be a deviation from the fourth article on invoking all hooks.

Step 1 – Creating a repository and add hooks into a repository

The first decision in creating a hook repository is to think whether you want to have lazy loading or not.  Once you have decided you can go through the steps – Creating a Hook Repository using dI.Hook

Be selective in adding hooks into a repository if you plan to invoke all of them at one go.  If you are thinking of manually adding hooks to a repository, would suggest going through – Adding hooks manually into a repository.  However, if you are planning to load them through configuration you can read – Loading a configuration set of hooks into repository

Step 2 – Invoking filtered hooks in the repository

 

Variation One – InvokeWhere

InvokeWhere takes 2 parameters. First – A predicate that returns Boolean value and input as IHook object. Second – Input parameters to the OnInvoke method of each hook.   The OnInvoke method of a hook is called only when the predicate returns true value.

An example of this would be,

InvokeWhere with predicate
int invokedCount = hookRepository.InvokeWhere(x =>
{
if (x is IAdditionalInterface)
{
return (x as IAdditionalInterface).IQ == “IQ.001”;
}
return false;
});

 

Variation Two – InvokeWhen with a predicate that takes no input

 

InvokeWhere takes 2 parameter. First – A predicate that returns Boolean value without any input. Second – Input parameters to the OnInvoke method of each hook.   The OnInvoke method of a hook is called only when the predicate returns true value.

An example of this would be,

InvokeWhen with predicate
int invokedCount = hookRepository.InvokeWhen(() =>
{
return false;
});

 

Variation Three – InvokeWhen with a predicate that takes no input, another that takes IHook object

InvokeWhere takes 3 parameter. First – A predicate that returns Boolean value without any input. Second – predicate that returns Boolean value and input as IHook object. Third – Input parameters to the OnInvoke method of each hook.   The OnInvoke method of a hook is called only when both the predicates return true value.

An example of this would be,

InvokeWhen with 2 predicates
int invokedCount = hookRepository.InvokeWhen(() => { return true; },
(x) =>        {
if (x is IAdditionalInterface)
{
return (x as IAdditionalInterface).IQ == “IQ.001”;
}
return false;
});

 

So using InvokeWhere and InvokeWhen you can filter the hooks to be invoked.

How to invoke all hooks using dI.Hook?

August 1, 2012 Applications, CSharp, Open Source , , ,

 

If you are new to dI.Hook, please visit the di.Hook Product Page for Overview, Release Version and Source Code.

Some posts you might want to read would be:

  1. How to create a HookRepository?
  2. How to add hooks to HookRepository?
  3. How to add hooks via configuration file to a HookRepository?

So this is the fourth article in the series which will deal with invoking all the hooks in the repository.  This article will be short and simple as there are no two ways of invoking all the hooks in a repository.

Step 1 – Creating a repository and add hooks into a repository

 

The first decision in creating a hook repository is to think whether you want to have lazy loading or not.  Once you have decided you can go through the steps – Creating a Hook Repository using dI.Hook

Be selective in adding hooks into a repository if you plan to invoke all of them at one go.  If you are thinking of manually adding hooks to a repository, would suggest going through – Adding hooks manually into a repository.  However, if you are planning to load them through configuration you can read – Loading a configuration set of hooks into repository

Step 2 – Invoking all the hooks in the repository

 

The code is fairly simple.  In fact, just one line

Invoking all hooks
int invokedCount = hookRepository.InvokeAll();

 

The above code will call OnInvoke() of all the hooks without any parameters.  But how often would you have a scenario when you would have a hook with no parameters? I mean a system that does not take any input and gives no output. A black box! Oh, I haven’t seen such systems.  Most of the complexity in systems involve either communication between other systems in the ecosystem or in executing complex logic involving lot of workflows and rules.

So hooks with no inputs and no outputs – not useful in many scenarios!

InvokeAll with Parameters
int invokedCount = hookRepository.InvokeAll("CalledByTestWithDifferentValues", 2);

 

More parameters – not an issue!  You can add N-parameters each of different type and it would work flawlessly!

So far, so good!

But this code does not necessarily is performance centric. I mean, it does not really leverage the benefit of multi-core 64-bit processor and higher gig-RAM.  So we must optimize this to have some kind of parallel execution and spawning of threads. With .NET 4.0, we can ease this task using TPL – Read Parallel Programming in .NET 4.0 for more statistics.  So here’s the version that executes invoke of all hooks in parallel.

InvokeAllAsParallel Example
int invokedCount = hookRepository.InvokeAllAsParallel("CalledByTestWithDifferentValues", 2);

Now that works great!

OKay, one last word before we reach to the end of this article.  Now all invoke methods return an Int32 value.  This Int32 value represents the number of hooks whose OnInvoke executed successfully!

Does it mean that there are chances that not all hooks will be executed? Yes.  Let’s see that taking some examples

Invoke – With Exceptions
[TestMethod] [ExpectedException(typeof(InvalidOperationException))] public void Test_Standard_InvokeAllWithExceptionRaisedInFirstHook()
{
    // should block diagnostichook as well and throw exception
    int invokedCount = hookRepository.InvokeAll("RaiseLogException");
    Assert.AreEqual(0, invokedCount);
}

[TestMethod] [ExpectedException(typeof(InvalidOperationException))] public void Test_Standard_InvokeAllWithExceptionRaisedInSecondHook()
{
    // should not block LogHook but should throw exception
    int invokedCount = hookRepository.InvokeAll("RaiseDiagnosticException");
    Assert.AreEqual(1, invokedCount);
}

In the method InvokeAllWithExceptionRaisedInFirstHook, assume that the LogHook throws an exception.  In the above case since DiagnosticsHook was added in the repository after LogHook (refer to article on How to Add Hooks), DiagnosticsHook is not called.  Hence the invokedCount = 0

In the mehod InvokeAllWithExceptionRaisedInSecondHook, the LogHook does not throw an exception but DiagnosticsHook does.  Hence only LogHook gets executed leaving the value of invokedCount = 1

Now lets see what happens when we use InvokeAsParallel

InvokeAsParallel – Exceptions
[TestMethod] [ExpectedException(typeof(AggregateException))] public void Test_Standard_InvokeAllAsParallelWithExceptionRaisedInFirstHook()
{
    // will not block diagnostichook as it is parallel and will throw exception
    int invokedCount = hookRepository.InvokeAllAsParallel("RaiseLogException");
    Assert.AreEqual(1, invokedCount);
}

[TestMethod] [ExpectedException(typeof(AggregateException))] public void Test_Standard_InvokeAllAsParallelWithExceptionRaisedInSecondHook()
{
    // will not block LogHook as it is parallel but should throw exception
    int invokedCount = hookRepository.InvokeAllAsParallel("RaiseDiagnosticException");
    Assert.AreEqual(1, invokedCount);
}

 

In the method InvokeAllWithExceptionRaisedInFirstHook, assume that the LogHook throws an exception. In the above case since DiagnosticsHook was added in the repository after LogHook (refer to article on How to Add Hooks) but it is executed in parallel, hence DiagnosticsHook is invoked.  This definitely means that an AggregateException will be thrown to calling method. Hence the invokedCount = 1

In the mehod InvokeAllWithExceptionRaisedInSecondHook, the LogHook does not throw an exception but DiagnosticsHook does. Hence only LogHook gets executed leaving the value of invokedCount = 1

To summarize, InvokeAll() method executes all hooks sequentially.  If any one fails, the following ones do not get executed.  InvokeAllAsParallel() method executes all hooks in parallel.  If any one fails, others still get executed.  Both methods will throw different exception messages to the calling method.

In next article, we will explore how to invoke only specific hooks.

dI.Hook – Load Hooks through Configuration

July 30, 2012 Applications, CSharp, Open Source , , ,

 

If you are new to dI.Hook, please visit the product page for Overview, Release Version and Source Code.

One of the ways to adding hook(s ) in the HookRepository is to define them in Application Configuration file and then loading an appropriate configuration in repository.  The other ways are mentioned in How to Add Hooks in a Repository post

Step 1 – Defining configuration

 

Configuration – Sample
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="dIHookConfiguration" type="dIHook.Configuration.dIHookConfigurationSection, dI.Hook"/>
  </configSections>
  <dIHookConfiguration>
    <repositories>
      <repository name="default" enabled="true">
        <hooks>
          <hook name="h1" type="dIHook.UnitTests.Hooks.LogHook, dI.Hook.UnitTests" />
          <hook name="h2" type="dIHook.UnitTests.Hooks.DiagnosticsHook, dI.Hook.UnitTests" />
        </hooks>
      </repository>
      <repository name="productionRepository" enabled="true">
        <hooks>
          <hook name="h2" type="dIHook.UnitTests.Hooks.DiagnosticsHook, dI.Hook.UnitTests" />
        </hooks>
      </repository>
      <repository name="productionRepositoryDisabled" enabled="false">
        <hooks>
          <hook name="h2" type="dIHook.UnitTests.Hooks.DiagnosticsHook, dI.Hook.UnitTests" />
        </hooks>
      </repository>
    </repositories>
  </dIHookConfiguration>
</configuration>

The above configuration has an element repositories that has multiple repository elements.

A repository element has following attributes

  • Name: This represents a unique name of the repository.  A repository is fetched from configuration using this name.
  • Enabled: If the value is ‘false’ then that repository configuration is not considered when loading the configuration.  In the above configuration, when repository ‘productionRepositoryDisabled’ is loaded into a HookRepository it will not have any hooks.

    Each repository element represents a collection or container of hooks  elements.  Each hooks element has multiple hook elements.

    A hook element has multiple attributes

    • Name: Name of the hook
    • Type: Fully qualified type of the hook to be added to the repository.  This hook should inherit from IHook interface

      This relation can be summarized as:

      repositoryConfig

       

      Step 2 – Loading Configuration into Repository

       

      To load configuration of a repository with name ‘default’, the code is fairly simple

      Loading ‘default’ repository
      1. hookRepository.LoadConfiguration();

       

      To load a specific repository. Say, productionRepository from configuration from a section named ‘dIHookConfiguration’ in the ApplicationConfiguration the code should be

      Loading custom repository
      1. hookRepository.LoadConfiguration("dIHookConfiguration", "productionRepository");

      If the ‘enabled’ property of a repository is false, there will be no hooks added in the repository.  To verify this, you can try the code below

      Loading disabled repository
      [TestMethod] public void Test_Standard_LoadFromConfigFileWithRepositoryNameDisabled()
      {
          hookRepository.RemoveAll();
          hookRepository.LoadConfiguration("dIHookConfiguration", "productionRepositoryDisabled");

          Assert.AreEqual(0, hookRepository.Hooks.Length);
      }

      dI.Hook – How to add hooks to HookRepository?

      July 29, 2012 Applications, CSharp, Open Source , , ,

       

      If you are new to dI.Hook, please visit the product page for Overview, Release Version and Source Code.

      Hook – A hook is a .NET object derived from the interface IHook.  If you already have classes that have business/technical functionalities, implementing IHook will not impact their functionality.  If your classes have similar function names, it is advisable to explicitly implement interface IHook.

      To illustrate this, let’s create two hooks LogHook and DiagnosticsHook.  We will not focus on their implementation and limit our scope to their definition and adding them to HookRepository

      LogHook
      1. public class LogHook : IHook
      2.     {
      3.         public string Name { get; set; }
      4.         public Guid Id { get; set; }
      5.         public LogHook()
      6.         {
      7.             Name = "LogHook";
      8.             Id = new Guid("B3D75F63-F7DA-4939-8777-1A354202B9D2");
      9.         }
      10.  
      11.         public void OnInvoke(params object[] inputParams)
      12.         {
      13.             Console.WriteLine("Hook Called");
      14.         }
      15.  
      16.         public void Dispose()
      17.         {
      18.             Name = null;
      19.             Console.WriteLine("Woah! Hook Disposed!");
      20.         }
      21.     }

       

      DiagnosticsHook – Implements multiple interfaces (IHook, required for HookRepository) and IAdditionalInterface

      DiagnosticsHook
      1. public class DiagnosticsHook : IHook,
      2.         IAdditionalInterface
      3. {
      4.     public string Name { get; set; }
      5.     public Guid Id { get; set; }
      6.  
      7.     public DiagnosticsHook()
      8.     {
      9.         Name = "DiagnosticsHook";
      10.         Id = new Guid("CB75FCCF-593B-4BCF-871B-298087CDE741");
      11.     }
      12.  
      13.     public void OnInvoke(params object[] inputParams)
      14.     {
      15.         Console.WriteLine("Hook Called");
      16.     }
      17.  
      18.     public void Dispose()
      19.     {
      20.         Name = null;
      21.         Console.WriteLine("Woah! Hook Disposed!");
      22.     }
      23.  
      24.     public string IQ
      25.     {
      26.         get { return "IQ.001"; }
      27.     }
      28. }

       

       

      How to add an instance of a hook in a repository?

      When we are referring to adding an instance of a hook in a repository, there are multiple ways of doing it

      Method 1 – Adding one hook instance at a time

      Adding one hook at a time
      1. hookRepository.Add(new LogHook());
      2. hookRepository.Add(new DiagnosticsHook());

      Method 2 – Adding a List of hooks

      Adding a List of hooks
      1. hookRepository.Add(new List<IHook> { new LogHook(), new DiagnosticsHook()}) ;

      Method 3 – Adding two different instances of same hook type

      Adding 2 LogHooks
      1. LogHook logHook1 = new LogHook(); /* Default Guid */
      2. LogHook logHook2 = new LogHook();
      3. logHook2.Id = new Guid("B9D2D1E3-6BAD-47C3-AEFC-3ABFAAB210F8");
      4. hookRepository.Add(new[] { logHook1, logHook2 });

       

      Method 4 – Adding by searching within an Assembly

      Searching within an Assembly
      1. hookRepository.Add(SearchScope.CallingAssembly, SearchBy.Name, Operator.Like, "Log");

       

      Method 5 – Adding by searching within a directory

      Searching within a directory
      1. hookRepository.Add(SearchScope.CurrentDirectory, SearchBy.Name, Operator.Like, "Log");

       

      How to add a type of a hook in a repository?

       

      If your repository supports lazy loading, then adding a type of hook in a repository is preferred over adding an instance of a hook.   By adding a type in the repository, the instance of hook is not created until it is invoked or searched for.

      However, if your repository does not support lazy loading you can choose any mechanism of adding hook into repository

      Add a Hook Type in Repository
      1. hookRepository.Add(typeof(LogHook));
      2. hookRepository.Add(typeof(DiagnosticsHook));

       

      So you can add a hook by six different ways in a Hook Repository

      Follow on Feedly