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>

Friday, October 5, 2012

Ubuntu /centos virtual machine doesn't have IP address/ can't connect to network?

you may turn on the NAT/Bridget/HOST ONly setting for your GUEST OS, but when you run the ifoncfig, there is no IP v4 address associated.
image

Basically, make sure on the HOST machine, the vmware/virtualbox DHCP service are running,
image
then run dhclient eth0, you should be able to get a IPv4 address.

for the windows host machine, the dhcp range are stored in the registry. so make sure the VMnet NIC ip address match the network address, otherwise, from the host machine, you can’t ping or access the guest OS.

image

Thursday, October 4, 2012

asp.net output cache not working? and the same code works on some servers? why

Here is one very typical issue that bring you to this blog.
Issues, I have one asp.net application with nothing special, for the performance consideration, we turn on the output cache. somehow, the same page never got cached on some servers, and It did cached on most servers. of vice versa.

short answer, It has nothing to be with the IIS Version. It’s All about HTTP Cookies and .net Framework version.  Let’s put a very basic Page.

just turn on the output cache using the most simple way,
image

for the code, we just send out some cookie,
image

Let’s start from some server that it works,
1st time you hit the url, get a initial access time.

image

  and cookie was sent to client from server side,
image

try again, this page will be cached. you are always see the page, you are 1st time here,
image

Why? because we setup the page need to be cached for 600 seconds, and cache response vary by any params, if we add something to the url parameter, we wills see refreshed page,
image

that’s the end of this cache working story, if we deploy the same code to another server, results are totally different.
1st time,
image

retry,
image

Why? I did a lot check, and find out the reason is simple. IT’S BECAUSE OF THE .NET FRAMEWORK VERSION.

In earlier verison of the FX, if you check the ouputCacheModule , it will cache response without checking the cookie value, that means even response has a server side cookie, it will still cache the response.
image

But for newer version, it will simply ignore the cache insert if response has cookie associated.
 image

my old version is, .4209, it never check cookie when cache response. that means you may see other people’s response (like login name?)
image
new version,.5456, it will check cookie which is more safe.
image

Hope it helps.

Wednesday, October 3, 2012

How to test RamDisk /Ram disk on centos

It turns out super easy to setup one RAM disk on centos, since linux 2.4 and beyond has the built-in support for ramdisk.

when you check the /dev/ram*, you may see a lot virtual RAM devices.
image

by default the ram device is very small, could be just less than 100Mb, so first step is always incresing the ramdisk size, to do so, append the ramdisk_size=numberofKBs to the boot config /etc/grub.conf

image

restart the server to make it refresh the changes.
now, let me create one fs first on /dev/ram1, mke2fs –m 0 /dev/ram1

image

then create one folder, and map the disk to this folder,
image

however, the ramdisk and data can’t survive the reboot, so you may put some script under rc.local to load the ramdisk and data.

run some basic testing,
image

Friday, September 28, 2012

How to capture network traffic through command line in C#

Wireshark is a GUI tool which enable us to click and capture network traffic. if you are IT admin guy, you may want something like tcpdump in linux. actually, bundled with the wireshark installation, there is one tool called tshark.exe

image

you can run tshark –D to list all the NIC interfaces,
image

If you want to see traffic for interface 4,using the –i command

image

for me , I just wondering to know which app is trying to send out some http traffic,
image
once found it, love it!

 
Locations of visitors to this page