Tuesday, February 12, 2013

How to implement IoC in ASP.NET webform by HttpModule/CustomAttribute/Windsor

http://stackoverflow.com/questions/293790/how-to-use-castle-windsor-with-asp-net-web-forms

   // index.aspx.cs
    public partial class IndexPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Logger.Write("page loading");
        }

        [Inject]
        public ILogger Logger { get; set; }
    }

    // WindsorHttpModule.cs
    public class WindsorHttpModule : IHttpModule
    {
        private HttpApplication _application;
        private IoCProvider _iocProvider;

        public void Init(HttpApplication context)
        {
            _application = context;
            _iocProvider = context as IoCProvider;

            if(_iocProvider == null)
            {
                throw new InvalidOperationException("Application must implement IoCProvider");
            }

            _application.PreRequestHandlerExecute += InitiateWindsor;
        }

        private void InitiateWindsor(object sender, System.EventArgs e)
        {
            Page currentPage = _application.Context.CurrentHandler as Page;
            if(currentPage != null)
            {
                InjectPropertiesOn(currentPage);
                currentPage.InitComplete += delegate { InjectUserControls(currentPage); };
            }
        }

        private void InjectUserControls(Control parent)
        {
            if(parent.Controls != null)
            {
                foreach (Control control in parent.Controls)
                {
                    if(control is UserControl)
                    {
                        InjectPropertiesOn(control);
                    }
                    InjectUserControls(control);
                }
            }
        }

        private void InjectPropertiesOn(object currentPage)
        {
            PropertyInfo[] properties = currentPage.GetType().GetProperties();
            foreach(PropertyInfo property in properties)
            {
                object[] attributes = property.GetCustomAttributes(typeof (InjectAttribute), false);
                if(attributes != null && attributes.Length > 0)
                {
                    object valueToInject = _iocProvider.Container.Resolve(property.PropertyType);
                    property.SetValue(currentPage, valueToInject, null);
                }
            }
        }
    }

    // Global.asax.cs
    public class Global : System.Web.HttpApplication, IoCProvider
    {
        private IWindsorContainer _container;

        public override void Init()
        {
            base.Init();

            InitializeIoC();
        }

        private void InitializeIoC()
        {
            _container = new WindsorContainer();
            _container.AddComponent();
        }

        public IWindsorContainer Container
        {
            get { return _container; }
        }
    }

    public interface IoCProvider
    {
        IWindsorContainer Container { get; }
    }

Monday, February 11, 2013

How to TryParse Enum?

    public enum color
    {
        red =1, blue=2
    }

            color result;
            bool IsParsingCorrect = Enum.TryParse("red", true, out result);
            IsParsingCorrect = Enum.TryParse("green", true, out result);

Thursday, February 7, 2013

What is Reponsive UI?

Website is able to changes layout by device type.


http://www.neevtech.com/blog/2012/05/01/responsive-ui-using-css-media-query/#

Wednesday, February 6, 2013

How to fix "Hexadecimal value 0x is an invalid character"?

Because xml includes invalid character.
Add an filter following for xml content

/// 
/// Remove illegal XML characters from a string.
/// 
public string SanitizeXmlString(string xml)
{
 if (xml == null)
 {
  throw new ArgumentNullException("xml");
 }
 
 StringBuilder buffer = new StringBuilder(xml.Length);
 
 foreach (char c in xml)
 {
  if (IsLegalXmlChar(c))
  {
   buffer.Append(c);
  }
 }
  
 return buffer.ToString();
}

/// 
/// Whether a given character is allowed by XML 1.0.
/// 
public bool IsLegalXmlChar(int character)
{
 return
 (
   character == 0x9 /* == '\t' == 9   */          ||
   character == 0xA /* == '\n' == 10  */          ||
   character == 0xD /* == '\r' == 13  */          ||
  (character >= 0x20    && character <= 0xD7FF  ) ||
  (character >= 0xE000  && character <= 0xFFFD  ) ||
  (character >= 0x10000 && character <= 0x10FFFF)
 );
}



http://seattlesoftware.wordpress.com/2008/09/11/hexadecimal-value-0-is-an-invalid-character/

Tuesday, February 5, 2013

How to fix "The type or namespace name could not be found (are you missing a using directive or an assembly reference?)"

First of all, check if this assembly you already added into your project.

If yes, but still get this problem:
Go to Project Properties -> Application -> Target framework
Change this value to .NET Framework 4.0 from .NET Framework 4.0 Client Profile