Accessing Embedded Resources

on Friday, May 15, 2009

Accessing Embedded Resources using GetManifestResourceStream
How do you access resource files (bmp, mp3, etc) that are compiled into your Windows Forms executable? After googling the subject, the Assembly object's GetManifestResourceStream method seemed to be the solution. MSDN's entry on this subject makes it seem relatively simple. However, it took me over an hour to get it to work in my latest project. There are two things you must do in other to access an executable's resources:

1.You must know the exact name of resource (mynamespace.resource.resourcename).

2.You must embed the resource into your executable.

Accessing the resources means that you must have access to the executable's Assembly object.

Example

Assembly resourceAssembly = Assembly.GetAssembly(typeof(Program));
string[] names=resourceAssembly.GetManifestResourceNames();
foreach (string str in names)
{
Console.WriteLine(str);
}
using (StreamReader stream = new StreamReader(resourceAssembly.GetManifestResourceStream("ConsoleApplication2.NewFolder1.Embed.xml")))
{
string str=stream.ReadToEnd();
}

How to enable remote debugging when you don't have Visual Studio on Servers

on Thursday, May 14, 2009

Remote Debugging is a great feature to use, especially when you work with virtual machines. It allows you to develop and debug locally but have the code running on another machine, virtual or physical. Microsoft SharePoint can't be installed on a Windows Vista or XP workstation, but needs to be installed on Windows Server 2003 or 2008, so the general recommendations has been for developers to have either Windows Server as their main OS or have a virtual machine with Windows Server. None of these work that well; either you have problems debugging your components and you have to rely on traces or message boxes or you have to have a virtual machine with a full development environment, which will not resemble a production machine.

So, Remote Debugging, is my primary way when working with SharePoint development. It allows you to have a smaller virtual machine, and it will allow you to develop in your main OS. But, Remote Debugging has been quite problematic to set up and configure, so here is a guide that you can follow to get it work in a few minutes.

This guide uses Microsoft Visual Studio 2008 and Windows Vista with UAC on the client and a Windows Server 2003 with WSS 3.0 running in a Virtual PC on the client.

Prepare your remote host
First of all you need to prepare your remote host for accepting incoming debugging requests, this is done by running the Visual Studio Remote Debugging Monitor on the remote machine, which is found under C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\Remote Debugger\. Copy that folder to your remote host and place a shortcut to the msvsmon.exe on the desktop, so you have fast access to it whenever you need to debug.

The Remote Debugging Monitor is the one accepting your debug calls and the talking back to Visual Studio on your client, so to get these things to work you have to start the client as the user running Visual Studio (or have administrative permissions on the client). You can either log in to the remote host using that user or, as I prefer, right-click on the msvsmon.exe and choose Run As... Make sure that the user you are using to run the monitor with is member of the local Administrators group.


Now the Remote Debugging Monitor has started to waiting for new debugging connections. The debugging server was named with the username that is running the application and the server name, separated with an @-character. You can rename it using Tools->Options.

Prepare your client
Now it's time to prepare your client. First of all you have to run the Visual Studio 2008 Remote Debugger Configuration Wizard, which will open up the correct ports in your firewall. You will find the wizard under the Visual Studio Tools in your start menu. The wizard also allows you to run the Remote Debugger as a service on the machine which the wizard is run on, skip this step for this guide. When the wizard asks you for how you would like to configure your firewall, choose the Allow only computers on the local network... option and the finish the wizard.

Start Debugging
Now it's time to start Visual Studio 2008 and load up your solution and hook up the debugger to the remote machine. Prior to this you need to deploy the application to be debugged on the remote machine, including the .pdb files.

In Visual Studio choose Debug->Attach to process. In the Qualifier you have to enter the name that was given to the Remote Debugger Monitor and hit Enter, then all you need to do is attach to the process you would like to debug and set some break points!

That wasn't to hard?

Problems
Here are some problems that I have stumbled upon when trying to get these things to work.

Unable to connect to the Microsoft Visual Studio Remote Debugging Monitor named 'XXX. The Visual Studio Remote Debugger on the target computer cannot connect back to this computer. Authentication failed. Please see Help for assistance.

This one is due to the fact that the user running the debugging monitor are not allowed to access the client machine, make sure that the user running the monitor is either the same user running Visual Studio or the member of the Administrators group on your client.

You only see a few processes in the Attach to Process dialog.

First of all make sure you have checked the Show processes from all users check box, then make sure that the user running the monitor has access to the process on the machine, that is you have to make the user member of the local Administrators group. After adding the account to the Administrators group you have to restart the monitor.

Unable to connect to the Microsoft Visual Studio Remote Debugging Monitor named 'ibvsretail'. Logon Failure: The target account name is incorrect.

This one is pretty uncommon, but still I have had it. Somehow the server account in Active Directory had gone wrong so I hade to remove the machine from the domain and add it back.

Code behind variable in aspx pages

on Tuesday, May 5, 2009

For asp.net Server Controls if you want to access code behind variable in aspx pages. There are 3 things to remember:
1. Scope of the variable has to be protected or Public
2. The variable can be accessed only using DataBinding Syntax like below.
<asp:TextBox ID="TextBox1" runat="server" Text='<%# DateTime.Now.ToString()%>'></asp:TextBox>
3.You have to Call Page.DataBind() or TextBox1.DataBind() method in the Page_Load event or any other event before Page_Render event's base.Render(writer) method call.

For HTML controls you could just use the traditional syntax like below.
<input type='text' value='<%=DateTime.Now.ToString()%>' />