Monday, November 15, 2010

Create a List Dynamically With Reflection

I wanted to do more with the Dynamic List that I put up a little while ago. I wanted a method that I could pass any type, and get a List of that type out of it. After a bit of playing around, I was able to get what I wanted using System.Reflection. Here is the code:
1:  private object GetList(object[] sourceArray, Type arrayType) {
2:        Type listType = typeof(List<>).MakeGenericType(new System.Type[] { arrayType });
3:        var list = Activator.CreateInstance(listType);
4:        MethodInfo mi = listType.GetMethod("Add");
5:        foreach (object item in sourceArray) {
6:          mi.Invoke(list, new object[] { item });
7:        }
8:        return list;
9:      }  

What I did was to first create an instance of the List. Then set the nested type to be the type of the calling type. This is all done using the Type class' MakeGenericType Method. All this does is create a List were T is the type that is passed as the parameter. I still need to instantiate this monstrosity. I do that in the next line of code, using the System.Activator class' CreateInstance method. After that I have an actual list. Now I need to populate it.
Because we don't know what types and classes we are dealing with at Design Time, we can't just call Class.Method() to make things work. I first need to find the specific method I need. To do that I call System.Reflection.Type's GetMethod method. This comes out as a System.Reflection.MethodInfo type, thus the creation of the MethodInfo variable.
From here I simply iterate through the objects in the array passed in to the method, and Add them to the List object.

There is one small issue... The return is not exactly what you would expect. It is not a List of the type you pass it, rather it is a single object, of type object. The good news is that this object can be casted as the proper List when you get back to your calling method.

For instance, here is my calling method:
1:  protected void butPeople_Click(object sender, EventArgs e) {
2:        Type peopleType = typeof(People);
3:        List<People> people = new List<People>();
4:        people = GetList(GetPeople(), peopleType) as List<People>;
5:        GridView1.DataSource = people;
6:        GridView1.DataBind();
7:        GridView1.Visible = true;
8:      }  

You can see in line 4 where I call the GetList method. I send it a custom type of People, along with an array of People. As I mentioned above, the return type of the GetList method is object, but this can be casted in to the type that we wanted it to be, as seen with the "as List" on line 4.

Monday, November 8, 2010

Network Adapter Priority Windows Server 2008 R2

Something cool that I just found out about... How does Windows choose which network to use? Does it do a network test to see which network has the best throughput? No. There may be a preference built in for wired over wireless, maybe. How do you change this? What if I have a preference for one network over another. Perhaps I want to connect via my wireless network over my wired connection. What if, I have RRAS connected to my wireless adapter, and I need that guy to be the preferred connection?
HOW??

Turns out Microsoft hid it from us, sort of... What you do is go to Network and Sharing Center and click on the change adapter settings link. From there, if you have the Desktop Experience installed, you might not have the menu bar running across the top. You need to hit the Alt button on your keyboard in order to get the menu bar to show up. From the Advanced menu option, click Advanced Settings.

What will load is a funky little window with your adapters in the top part, and the bindings associated with the adapters on the bottom. For our purposes, you can ignore the bottom part of the window. We are only concerned with the top part.
In the top part you see all of your network adapters. You will also notice two arrows on the right of the selection window. What you are seeing is the adapter connection priority. The highest priority is on the top, running to the lowest priority on the bottom.
All that you need to do to set an adapter with a higher priority is to highlight the adapter, and click the up arrow. That's it. Simple solution to a difficult problem.

Friday, November 5, 2010

Set Up Hyper-V With an Internal Network

Hyper-V is Microsoft's server virtualization product. Unlike Windows 7's Virtual PC, this product is a true vitalization suite. It allows you to create 32bit and 64bit virtual computers with any OS. This makes it very handy to create a virtual server farm for development or R&D use. Personally I use mine to host development environments for SharePoint 2010, and MOSS 2007.
If you want to create virtual servers that are available on your current network, simply attach your network card to the VM and off you go(more on this later), BUT if you want a private network separate from your public net work, i.e. you want to create a separate domain for testing and dev work, but want to keep your workstation connected to your current domain, it gets a little tricky.
I did my configuration using RRAS, not ICS. For one, RRAS is much more flexible than ICS, for another, RRAS is the professional way to do such a configuration. And I am always the professional. Like that guy in the movie Professional... Except I don't kill people... and I have friends other than a plant... and I don't have a French accent... and I am not being chased by Garry Oldman... and I don't live in New York... Other than that it is just like the movie.

Install Hyper-V
Hyper-V is a Role that you need to add to your host server. So, open your old friend Server Manager and head to the Server Roles section. Check Hyper-V and install.

Configure Networking
Now things get fun. In Hyper-V Manager, click on Virtual Network Manager. Create a new Internal network and give it a meaningful name. That name will become very important, so be sure to name it well. I called mine Hyper-V Internal Network.

What Microsoft has done with Hyper-V is to separate the physical network card completely from the OS. It then creates a virtual switch with several virtual network adapters connected to it. This is kind of complex, and you can go HERE if you want to learn more. What concerns us here is what happens with the Host's network adapters.
Open up Network Sharing Center, then click on Change Adapter Settings. You will notice that there is a new icon there. It will say something like Local Area Connection 2 or some other number. What is important is that the last line in the description, the binding information, should be the name that you gave your virtual network back in the Hyper-V manager.

Configure the Host's Virtual Network Adapter
Right click on the icon representing the Hyper-V virtual network adapter and select Properties. Double click on the v4 network settings.
Setting up the Host adapter is kind of funky compared to normal network set ups. First of all, we will not be putting in a gateway address. The virtual switch will be the gate, and if we specify a gate here, it screws everything up. In addition to this, we will not be putting in any DNS IPs either. Again, the switch will act as the DNS relay. The only thing we need to put in this area is the IP address and mask of the Host on the internal network. This address must be on a different subnet than the subnet that the host is on for the public network.
It makes sense if you think about it. Essentially, you are configuring your server to be a router. Routers need two network cards and need to have each card on a different subnet. Since you are creating a private VLAN, you will need some sort of router or switch to get you to other networks, thus the separate subnets.
So, my public network is, 192.168.107.x, I made my internal network 192.168.2.x.
To reiterate, on the HOST, the virtual adapter settings should just be the IP address and mask of the HOST in the subnet of the internal network.
In my set up my IP is 192.168.2.1, subnet 255.255.255.0.

Install RRAS
Back to the Server Manager for another role install. This time it is the Network Policies and Access Services role. Check that, then select Routing and Remote Access Service. Be sure both boxes underneath the selection are checked. Unless you are familiar with access restrictions, DO NOT install the Network Policy Server. This can mess up your installation if you don't know what you are doing. I didn't need this service, so I did not install it.

Configure RRAS
From here you will click on the new role that you installed (Network Policies and Remote Access) and right click on Routing and Remote Access. Select Configure. We are going to create a policy for NAT, so that is the guy we select. The next screen deals with which interface has the Internet. Select your public network interface and select next. Now we select the interface that we want to NAT on. Select the internal Hyper-V interface. That completes the initial configuration. We still have to configure NAT. You thought you were done with that, but you are not!

Configure NAT
Open up RRAS, then IPv4. Click on NAT. Right click on your public interface and select Properties. Click the radio button that says Public interface connected to the Internet. Check the box that said Enable NAT on this interface.
From there go to the Services and Ports tab. Check the box for the the various services that you want to use NAT for. I just want Internet, so I checked Secure Web Server(HTTPS), and Web Server(HTTP). When you check the boxes it will ask you what IP address you want to NAT from. Put in your interface IP address (for me that was 192.168.2.1). Click OK.
Done!

Install VM
From here You want to create your first Virtual Server. Set this up how ever you like it according to your needs. The network adapter that you want to use will be the name of your internal network.
When your OS is done installing, you will notice that it will have an IP address if you have enabled DHCP. This comes from the RRAS server. If you want a static, simply put in your address and mask from the internal network subnet. Your gate will be the Host interface, as will be your DNS servers.

DONE!

How To Set Up Windows Server 2008 R2 As a Kick Ass Workstation

Everybody knows that I am a Microsoft Guy... I make the majority of my money using and developing for their products. That said, they have massively screwed up at times. So badly that I have refused to work on several of their platforms (Windows Me, Windows Vista, Windows 2008). I have to admit Windows Server 2008 R2 is the bees knees. It is everything I have asked for in a modern operating system. This is R2, mind you, not Windows 2008. While a good start that OS wanted to BSOD every few moments... not fun.

Anyway, I like the OS so much that I wanted to use it as the OS on my workstation. Why? You get all of the benefits of 64bit with none of the hangups that Windows XP and Windows 7 seem to have. You get all of the features of a Windows 7 workstation, PLUS IIS 7.5, PLUS Hyper-V, PLUS RRAS, PLUS PLUS PLUS. It is awesome. In the past I have attempted to use a Server OS as my workstation OS, and I always ran in to some sort of hang up, I couldn't game on it, or It wasn't as user friendly, or it lacked some aspect of the workstation OS. Windows Server 2008 has none of those hang ups. It handles hardware, even advanced graphics cards, like a champ, and gives all of the nice to have perks that you are used to in a workstation OS. BUT... You have to work at it. There are lots of stuff you need to do to get the OS like you want it. I am going to go through what I did to get my Windows Server 2008 R2 working on my IBM Lenovo T410.

Install Desktop Experience
First things first... You look at Windows Server 2008 after install, and it looks like well... It looks like a server OS. Where is all of the pretty??? We want themes, and that cool graphical active program selector thing, and rotating background images, and stuff!!! We also want the ability to download and use Active X components as well as other WebDAV and whizbang web stuff. We get all of that by going in to the Server Manager and installing the Desktop Experience feature. It is easy, just check the box.
After installing the Desktop Experience we have all of the stuff right? Nope... That would be too easy. If you want Microsoft's whole new Windows 7 "glass" stuff, they call it "Aero", you need to open up the Services area in the Server Manager and find the Themes service.
After that, simply download the Windows 7 theme of your choice, and you are off to the races... So to speak...

Configure Wireless
A very annoying aspect of installing Windows Server 2008 on a laptop is that you have to jump through hoops to get your wireless network card to work. I must admit that I wasted a lot of time on this issue. I kept thinking that the OS had a problem with the driver. Nope, it liked the driver just fine, you have to install the Wireless LAN Service in order for it to work. Back to the Server Manager and open up the Features area and add the Wireless LAN Service Feature. If you card is installed correctly, immediately after installation is complete, you will see the OS looking for wireless networks.

Configure IE and Web Client Service
IE on Windows Server 2008 R2 is a pain in the neck. Even if you change your security and privacy options, it still complains to you that you are not as secure as you should be. There is a fix for that! You need to change the policy that is complaining.
First turn off IE Enhanced Security by going in to Server Manager. On the very first page is the Server Summary. On that page there is a link that says Configure IE ESC. It is on the right hand side in a box under the Security Information section. Set what you want for your IE annoyances just for users or just for admins. I set both to off. After that you are free to change your security and privacy settings to however you like them. Depending on your settings in IE, you may get a banner warning telling you that your security configuration has left your computer vulnerable. I hate that... So, let's get that changed.

Disable Security Warnings in IE
You will need to open up the Group Policy Editor (gpedit.msc, from run in the start menu, or somewhere else). From there go to:
Local Computer Policy
Computer Configuration
Administrative Templates
Windows Components
Internet Explorer
Double-click on “Turn off the Security Settings Check feature” and set it to Enabled. Done!

Monday, November 1, 2010

Dynamic List

I needed to create a method that would take any type, and return a List of that type. The catch was I didn't know what type T would be. How to get that done?

Like this:
1:  private object CreateList(Type type){
  
2:   System.Collections.IList lst = (System.Collections.IList)Activator.CreateInstance((typeof(List<>).MakeGenericType(type)));
  
3:  return lst;
  
4:  }  

Your calling code will need to cast the return correctly, but that one line will get you your dynamic typed list.