Wednesday, 4 July 2012

Split unity configuration file

For maintenance reasons, it is better to split a 'BIG' unity configuration into multiple files. One such way to do this is

1. Let us imagine we need to split into two files, so your root config file will look like below

<configSections>
    <section name="unitySection1" 
          type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, 
          Microsoft.Practices.Unity.Configuration"/>
    <section name="unitySection2" 
          type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, 
          Microsoft.Practices.Unity.Configuration"/>
</configSections>

<unitySection1 configSource="unity1.config" />
<unitySection2 configSource="unity2.config" /> 
 
2. And your individual unity files will look like below(see the highlighted section that 'unity' tag becomes 'unitySection1')
 
   <unitySection1>
       <container /> 
   </unitySection1>
 
   <unitySection2> 
       <container />
   </unitySection2>
 
3. Load the separate configuration files into Container


    UnityContainer.LoadConfiguration - will adds up the container details, so we just need to call it twice


 private void Initialise()
 {
   string[] unitySections = new string[] {"unitySection1", "unitySection2"};
  
   IUnityContainer container = new UnityContainer(); 
 
   foreach(var section in unitySections)
   {
      var configuration = ConfigurationManager.OpenExeConfiguration(path);
 
      var unitySection = (UnityConfigurationSection)configuration.
                          GetSection(section);
        
      container.LoadConfiguration(unitySection);
    }
 }
 
reference link 

CmdLet.ShouldContinue

Please refer this

Below example code

protected override void ProcessRecord()
{
    if (this.ShouldContinue("Are you sure you want to uninstall", "Confirm Uninstall"))
    {
        //your script here
    }
}

Thursday, 21 June 2012

IRequestChannel - Demo 1

Let's assume below ServiceContract

[ServiceContract]
public interface IDemo1
{
    [OperationContract]
    string HelloWorld(string name);
}

so how can we invoke above using IRequestChannel

var httpsBasicBinding = new BasicHttpBinding(BasicHttpSecurityMode.Transport);
var endpoint = "https://host/service.svc";
var channelFactory = new ChannelFactory<IRequestChannel>(httpsBasicBinding, endpoint);
var channel = null;
 
try
{
    channelFactory.Open();
 
    channel = channelFactory.CreateChannel();
 
    channel.Open(); 
    
    string body = "<HelloWorld xmlns="http://tempuri.org/"><name>Suresh</name></HelloWorld>";
    var messageBody = XmlReader.Create(new MemoryStream(Encoding.UTF8.GetBytes(body)
    Message message = Message.CreateMessage(MessageVersion.Soap11,  
                       "http://tempuri.org/IDemo1/HelloWorld");
 
    Message response = channel.Request(message);
    var result = response.GetReaderAtBodyContents().ReadOuterXml(); //parse based on the requirments
    
    Console.WriteLine(result);
    
    channel.Close();
}
finally
{
    channel.close(); //abort if faulted 
    channelFactory.Close(); //abort if faulted
}
 
reference : http://www.codeproject.com/Articles/34632/How-to-pass-arbitrary-data-in-a-Message-object-usi 

Wednesday, 13 June 2012

XSD: restriction on type to not be empty or blank

I wanted to create a SimpleType called 'ValidInput', basically i don't want to allow empty spaces. I started with below but for some reasons it worked on XML SPY, but not thru c#.

  <xs:simpleType name="ValidInput">
    <xs:restriction base="xs:string">
      <xs:minLength value="1"/>
      <xs:whiteSpace value="collapse"/>
    </xs:restriction>
  </xs:simpleType>

But below worked, can't figure out why


<xs:simpleType name="ValidInput">
    <xs:restriction base = "xs:string">
       <xs:minLength value="1" />
       <xs:pattern value=".*[^\s].*" />
   </xs:restriction>
</xs:simpleType>

Monday, 21 May 2012

Any CPU vs x64 vs x86

When we compile .net applications/libraries we get IL's, not Machine readable codes. So when we say 'Any CPU'(in a way architecture-neutral PE files) we create neutral IL's, and as part of JIT compilation, output will be either x64 or x86 PE's based on executing machine or calling process.

On a 32-bit machine:
  • Any CPU: runs as a 32-bit process, can load Any CPU and x86 assemblies, will get BadImageFormatException if it tries to load an x64 assembly.
  • x86: same as Any CPU.
  • x64: BadImageFormatException always.
On a 64-bit machine:
  • Any CPU: runs as a 64-bit process, can load Any CPU and x64 assemblies, will get BadImageFormatException if it tries to load an x86 assembly.
  • x86: runs as a 32-bit process, can load Any CPU and x86 assemblies, will get BadImageFormatException if it tries to load an x64 assembly.
  • x64: same as Any CPU.
It is the JIT compiler that generates an assembly code that's compatible with the requested target based on this flag.

Also CorFlags.exe assembly.dll|assembly.exe will tell us whether it is targeted x86 or Any CPU
Just for clarification, CorFlags.exe is part of the .NET Framework SDK. I have the development tools on my machine, and the simplest way for me determine whether a DLL is 32-bit only is to:
  1. Open the Visual Studio Command Prompt (In Windows: menu Start/Programs/Microsoft Visual Studio/Visual Studio Tools/Visual Studio 2008 Command Prompt)
  2. CD to the directory containing the DLL in question
  3. Run corflags like this: corflags MyAssembly.dll
You will get output something like this:
    Microsoft (R) .NET Framework CorFlags Conversion Tool.  Version  3.5.21022.8
Copyright (c) Microsoft Corporation.  All rights reserved.
Version   : v2.0.50727
CLR Header: 2.5
PE        : PE32CorFlags  : 3
ILONLY    : 1
32BIT     : 1
Signed    : 0
The key is the "32BIT" flag as documented above: 1 = x86; 0 = Any CPU.
·        Any CPU: PE = PE32    and  32BIT = 0
·         x86:         PE = PE32    and  32BIT = 1
      ·         x64:         PE = PE32+  and  32BIT = 0



Compilation and Deployment in ASP.NET 2.0

Just for my reference, good article from Rick Strahl

Wednesday, 16 May 2012

500.0 Internal Server Error - aspnet_isapi.dll failed in IIS 7.5

If you experience '500.0 Internal Server Error - aspnet_isapi.dll failed' even after trying all possibilities like 'aspnet_regiis -i' etc..., just uninstall IIS and Re-install again, it just works. It is Microsoft:)

I faced this when i installed Framework 4.0, so uninstalled IIS 7.5/Framework4.0 and re-installed both.