Thursday, November 29, 2012

How to: invoke C# generic method dynamically

Here is the sample code I figure out,

using System;

namespace X
{
    class Program
    {
        static void Main(string[] args)
        {
            string byString = "Today"; //or tomorow

            var GenericType = Type.GetType("X." + byString);
            var GenericIns = Activator.CreateInstance(GenericType);

            var genericMethod = typeof(Program).GetMethod("GetList")
                .MakeGenericMethod(GenericType);

            var output = genericMethod.Invoke(null, new object[] { GenericIns });


        }

        public static T GetList<T>(T input) where T : new()
        {
            Console.WriteLine("Invoked " + input);
            return input;
        }
    }

    class Today
    {
    }

    class Tomorrow
    {
    }
}

Wednesday, November 21, 2012

asp.net , Request.Url differs between .net versions with Load balancer

Got a weird problem, here is the quick story. I have several Web servers with the web app binding to private port 8011. the Load balancer has a Name like LB. when the page display the request Uri. it returns very weird format.  http://LB:8081 (so basically, LB name + Private Port) , no body can access this Url.

To do the simple test. I setup one test page on IIS with port8011, return the current URL.
image

then try access using the private url, it’s ok

image

However, if I setup one LB with LB_NAME, and LISTEN on Port 8888. then I try access the LB address. get unreachable url.
image

then I check the code, for .net 4.0. it has the option to follow the Url  from the user request. add one setting called aspnet:UseHostHeaderForRequestUrl
image

then we get the url as user sent in the request
image

If you check the .net 4.0 code, it has the following logic to get URL.

image

code to get new settings,
image

 

But this only applies to .net fx with latest patch. so check the code using reflector in your real case.

Wednesday, November 7, 2012

Google Tag Manager tutorial.

As the name says, it’s a tag manager offered by Google for free. Here is one basically tutorial show you what it can do, what kind of benefit it can bring to your table.

Basic Idea, you just Insert one google tag manager script to your website, then From the google tag manager console, you can push to deploy different other scripts. like anlaytics tagging, even a customized html , or javascript, img tag.

So first sign up for a account, create one container, get the script you need to inject to your website, try insert the script after <body> tag. if you insert it to blogger, parse the special tags, like & to &amp;

image

Then time to create some tags and test to see the tags on your website. 
  we create one tag to inject the firstaccess cookie to record the first time user hit the web site,

image

for the rules, we applied to all pages.

that’s for tag creating process, now we need to publish this tags.
Click the create version button,
image
it will create one new version with all the current tags,
in each version , you can click save and publish. or SaveandPreview.

savepreview give us change to make sure tags executed. so we click the save and preview mode.
you may see the msg telling you you are in the preview and debug mode. yes, we are!

Now go you your website,
a window will popoup showing you which tag get executed, for me, the inject cookie one.
image

once confirmed ok, exit the preview mode and publish it.
now, go to my web page, you can see we have the firstaccess cookie is there, we injected successfully.
here is the code for your reference,

<script>
function setCookie(c_name,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}

function getCookie(c_name)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
  x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
  y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
  x=x.replace(/^\s+|\s+$/g,"");
  if (x==c_name)
    {
    return unescape(y);
    }
  }
}

if( ! getCookie('firstaccess'))
{
setCookie('firstaccess',new Date(),100);
}

</script>

 
Locations of visitors to this page