Thursday, February 28, 2013

Wednesday, February 27, 2013

How to create web service project in Visual Studio 2010?

1. New Project
2. Change .Net Framwork version from 4.0 to 3.5
3. Click Web

Tuesday, February 26, 2013

NoDBA


http://martinfowler.com/bliki/NoDBA.html

What is Emergent Design?

The essence of incremental design is that we start with what we know, create an initial design, and then extend the design as we learn more.

This is also called “emergent design” as the design emerges over time.
http://scaledagileframework.com/emergent-design/

Consuming ASP.NET Web API services in Windows 8 C# XAML apps

http://www.piotrwalat.net/consuming-asp-net-web-api-services-in-a-windows-8-c-xaml-app/

Monday, February 25, 2013

Friday, February 22, 2013

How to check one web site is HTTP compression enabled?

1. By this website
http://www.whatsmyip.org/http-compression-test/

2. By Chrome f12 tool/Firefox Firebug
Checking reponse headers if exists: Content-Encoding:gzip

(IE f12 tool does not display this header)

How to add GZip compression in ASP.NET?


  
    
    
      
      
      
      
    
    
      
      
      
      
    
  
  

Thursday, February 21, 2013

How to develop Android apps by C#?

http://xamarin.com/monoforandroid

Wednesday, February 20, 2013

Can you Restore SQL Server 2012 backup to a SQL Server 2008 database?

No. Error message: Specified cast is not valid. (SqlManagerUI)
You can restore 2008 to 2012, but not backward.

Tuesday, February 19, 2013

How to Cache image in IHttpHandler?

1. Check header If-Modified-Since, if found, then return Not modified
2. Set Last-Modified header in response header
    public class GetImage : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            var If_Modified_Since = context.Request.Headers["If-Modified-Since"];
            if (If_Modified_Since != null)
            {
                context.Response.StatusCode = 304;
                return;
            }
            context.Response.BinaryWrite(Image2Byte(CreateBitmapImage("Hello World")));
            context.Response.Cache.SetMaxAge(new TimeSpan(1, 0, 0)); 
            context.Response.Cache.SetLastModified(System.DateTime.Now);
            context.Response.ContentType = "image";
        }
    }

How to solve "This operation requires IIS integrated pipeline mode."

Change from:
context.Response.Headers.Add("header", "header");
To
context.Response.AddHeader("header", "header");

Friday, February 15, 2013

How to extract Javascript from html source by HtmlAgilityPack

            HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();
            htmlDoc.LoadHtml(html);
            var scriptList = htmlDoc.DocumentNode.Descendants()
                .Where(n => n.Name == "script" );

Thursday, February 14, 2013

How to cache image in httphandler?

        public void ProcessRequest(HttpContext context)
        {
            context.Response.BinaryWrite(Image2Byte(CreateBitmapImage("Hello World")));
            context.Response.Cache.SetCacheability(HttpCacheability.Public);
            context.Response.Expires = 100;
        }

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

Monday, February 4, 2013

Friday, February 1, 2013

The clearest way to express polymorphism


http://stackoverflow.com/questions/154577/polymorphism-vs-overriding-vs-overloading/154939#154939
public abstract class Human
{
   ...
   public abstract void goPee();
}

public class Male extends Human
{
...
public void goPee()
{
System.out.println("Stand Up");
}
}

public class Female extends Human
{
...
public void goPee()
{
System.out.println("Sit Down");
}
}
//Now we can tell an entire room full of Humans to go pee.

public static void main(String args)
{
ArrayList group = new ArrayList();
group.add(new Male());
group.add(new Female());
// ... add more...

// tell the class to take a pee break
for( Human person : group) person.goPee();
}