Thursday, April 26, 2012

How can you create Multiple instances of same windows service

Many times you want to create windows service with multiple instances in the same server. We must create windows service with different name for each instance.

You can add parameters for different configuration of windows service when you try to install.

Here is an example of installing windows service:
installutil -i /name=LinkDevSch2 /account=localsystem LinkDev.Timer.Service.exe
Here is an example of uninstalling windows service:
installutil /u /name=LinkDevSch1  LinkDev.Timer.Service.exe

You can use these parameters to modify server name, way of authentication. the ProjectInstaller has 4 methods you can override OnBeforeInstall, OnBeforeunInstall, Install, and Uninstall which enables us to make changes to the service installer at the at run time.

  
     public string GetContextParameter(string key)
     {
            string sValue = "";
            try
            {
                sValue = this.Context.Parameters[key].ToString();
            }
            catch
            {
                sValue = "";
            }
            return sValue;
     }


      protected override void OnBeforeInstall(IDictionary savedState)
      {
          
            base.OnBeforeInstall(savedState);

            bool isUserAccount = false;
          
            // Decode the command line switches
            string name        = GetContextParameter("name");
            serviceInstaller.ServiceName = name;

            // What type of credentials to use to run the service
            // The default is User
            string acct        = GetContextParameter("account");
          
            if (0 == acct.Length) acct = "user";

            // Decode the type of account to use
            switch (acct)
            {
                case "user":
                    processInstaller.Account = System.ServiceProcess.ServiceAccount.User;
                    isUserAccount = true;
                    break;
                case "localservice":
                    processInstaller.Account = System.ServiceProcess.ServiceAccount.LocalService;
                    break;
                case "localsystem":
                    processInstaller.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
                    break;
                case "networkservice":
                    processInstaller.Account = System.ServiceProcess.ServiceAccount.NetworkService;
                    break;
                default:
                    processInstaller.Account = System.ServiceProcess.ServiceAccount.User;
                    isUserAccount = true;
                    break;
            }

            // User name and password
            string username = GetContextParameter("user");
            string password = GetContextParameter("password");

            // Should I use a user account?
            if (isUserAccount)
            {
                // If we need to use a user account, set the user name and password
                processInstaller.Username = username;
                processInstaller.Password = password;
            }
        }


        protected override void OnBeforeUninstall(IDictionary savedState)
        {
            base.OnBeforeUninstall(savedState);

            // Set the service name based on the command line
            serviceInstaller.ServiceName = GetContextParameter("name");
        }

Now you can created NET Windows Service with multiple instance any time you want.

Tuesday, April 24, 2012

Tips Comment ASPNET Controls

Some times ASPNET Developer do mistake by commenting server controls
as html comment like below:
<!--<aspnet:TextBox runat="server" id="txt"/>

-->

This comment will affect only by rendering tags as commented html, but doesn't affect its server side events and validations. which
will cause many problems. It is displaying sensitive information, it is vulnerable to cross-site scripting,  failure in firing some scripts like script validation, unexpected behaviour of scripts, and render unwanted tags(affect the performance).

To ignore any of the above problems use server side:
<%--<aspnet:TextBox runat="server" id="txt"/>

--%>

Sunday, April 22, 2012

Allow WCF multiple Binding on IIS

IIS supports specifying multiple IIS bindings per site. A WCF service hosted under a site allows binding to only one baseAddress per scheme.
I want to host service with multiple binding without custom factories.
http://seesaudi.com, http://seesaudimor.com

I found solution:
Solution in .Net 3.5:
Set the baseAddressPrefix to one of the addresses:


        <system.serviceModel>
        <serviceHostingEnvironment>
        <baseAddressPrefixFilters>
                <add prefix="http://seesaudi.com"/>    
        </baseAddressPrefixFilters>
        </serviceHostingEnvironment>
        </system.serviceModel>

  1. Set the endpoints in your web.config file to be absolute URIs:

  2.    <services>
          <service>
            <endpoint address="http://seesaudi.com/Service1.svc/e1"  Binding="..." Contract="..." />
           <endpoint address="http://seesaudimor.com/Service1.svc/e2"  Binding="..." Contract="..." />
          </service>
       </services>
Solution in .Net 4.0: 
  In order to enable multiple site binding, you need to set multipleSiteBindingEnabled to true in your application.

   <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />  

Thursday, April 19, 2012

Silverlight Designer Error

Silverlight Problem in Design Mode:

When Created Control in Silverlight I found error in design mode:


System.Reflection.TargetInvocationException

[Async_ExceptionOccurred]Arguments: Debugging resource strings are unavailable. Often the key and arguments provide sufficient information to diagnose the problem. See http://go.microsoft.com/fwlink/?linkid=106663&Version=4.0.50401.0&File=System.dll&Key=Async_ExceptionOccurred


After many invesigation I discovered I used HtmlPage object (Need browser to execute this line of code).
To fix this didn't execute Html object in design mode example:


if (!DesignerProperties.IsInDesignTool)
{
var height = HtmlPage.Document.Body.GetProperty("clientHeight");
var width = HtmlPage.Document.Body.GetProperty("clientWidth");
this.LayoutRoot.Width = double.Parse(width.ToString());
this.LayoutRoot.Height = double.Parse(height.ToString());
}