Thursday, December 4, 2008

ASP.NET Convert a Relative URL to an Absolute URL

My ASP.NET web application needed to send a notification e-mail to a user that included a dynamically generated link. To create this link, I wanted to pass a relative url including the tilde i.e ("~/DirectoryA/Webform.aspx?querystring=1") and have an absolute url returned ("http://www.domain.com/DirectoryA/Webform.aspx?querystring=1")

So I wrote this:

public string ResolveAbsoluteUrl(string url)
{
// Get the site root
string rawURL = Request.Url.ToString();

// Find the end of the root
int rootEnd = rawURL.IndexOf('/', rawURL.IndexOf("//") + 2);
if (rootEnd < 0) { rootEnd = rawURL.Length; }

// Combine the root with the resolved url
string rootURL = rawURL.Substring(0, rootEnd);

return string.Format("{0}{1}", rootURL, ResolveUrl(url));
}

Thursday, November 13, 2008

Operation could destabilize the runtime.

Problem:
I had an ASP.NET web application that worked fine in development, but on the test server threw the error:

Operation could destabilize the runtime.

Solution:

After attempting a myriad of solutions that other people used to resolve their problems, I decided to look a little deeper into my code and found the following:

if (credentials == null)
{
if (!IsDomainUser)
{
// throw access error
throw new System.Security.VerificationException();

Basically, I was throwing this error myself. The vagueness of the error didn't help, but in my own defense, I was trying to share libraries between a web application and a desktop application, and well... code that should ownly be called on a desktop or server application won't always fun so well in a web application.

Wednesday, November 5, 2008

Where's the ADO Entity Framework?

So I'm using Visual Studio 2008 and install the .NET 3.5 Framework SP1 and I do not have an option use the Entity Framework Model designer or any of the associated types. "What's up?" I think!

So I look around on google and can't find anything. So, I finally figured it out and I'm going to share. So here goes:

YOU HAVE TO INSTALL THE VS2008 SP1 that comes with the .NET 3.5 Framework SP1 to get that stuff!!!

And there you have it!

Thank you ladies and gentlemen.

Monday, November 3, 2008

Installing .NET Framework 3.5 SP 1


"Disconnect from the internet!" Are you crazy!

Saturday, November 1, 2008

Copying items to the clipboard in VS 2005 caused snippets to appear in the Toolbox

Problem:

When I would copy code to the clipboard while in Visual Studio 2005 and 2008, the text that copied would appear as a snippet in the Toolbox. The Toolbox would eventually fill-up.


Solution:

Delete local Toolbox files and the problem goes away.


Thanks to the poster NoiseMaker at http://forums.asp.net/p/993239/1339348.aspx, I was able to get past this problem. In his entry, he deleted the following files:

C:\Documents and Settings\MyUser\Local Settings\Application Data\Microsoft\VisualStudio\8.0\toolbox.tbd
C:\Documents and Settings\MyUser\Local Settings\Application Data\Microsoft\VisualStudio\8.0\toolbox_reset.tbd
C:\Documents and Settings\MyUser\Local Settings\Application Data\Microsoft\VisualStudio\8.0\toolboxIndex.tbd
C:\Documents and Settings\MyUser\Local Settings\Application Data\Microsoft\VisualStudio\8.0\toolboxIndex_reset.tbd


My files were in a different place because I'm on Vista, but the solution still worked for me. Btw, my (tbd) files were in the following location:

C:\Users\jaclimer.CXP\AppData\Local\Microsoft\VisualStudio\8.0\toolbox.tbd

A socket operation was attempted to an unreachable host

Problem:

After joining a newly loaded domain, I received the following error when trying to use Remote Desktop: "A socket operation was attempted to an unreachable host"


Cause:

After playing around I found that my routing entries did not have a gateway added. This was because I had recently loaded a Windows Server 2003 Domain Controller and had just setup DHCP.


Solution:

Add a gateway.


I did it manually, though I wouldn't recommend it. In this case, I was loading a temporary network on a 192.168.3.x network where my router was 192.168.3.1 and my domain controller was 192.168.3.5.

When I ran (from the command prompt) : c:\route print

I noticed no line existed for 0.0.0.0 (no gateway). So I added the router as a gateway as follows:

c:\route add 0.0.0.0 mask 0.0.0.0 192.168.3.1

This is, however, a temporary route and will be removed when the machine reboots. I could have used the '-P' flag to make it permanent, but in this case, this was all temporary.

I should add, that I also got an error TFS Error TF50621 which also had to do with me messing around with the domain controller.

The problem was that I started with one IP Address, 192.168.1.X and setup the domain controller. Then I changed the IP to 192.168.3.X and I got the error mentioned above.

The fix was to go in to the Domain Controller, goto DHCP settings, and add 192.168.3.x to the Reverse Lookup Zones.

Group Field in Excel 2007 was disabled

Problem:

The Group Field button in MS Excel 2007 was disabled when I selected a date column in my Pivot Table. At first glance, my data looked good, and I could not figure out why the button would be disabled. I added other columns that contained date information and the button would become enabled for them.

Solution:

At a closer inspection of my spreadsheet data, I had an invalid date in my source table. It was hardly noticeable as it was an additional slash in the date, but it was enough for the pivot wizard to not recognize the column as a date column. I fixed this bad date and all was well.

Note:

I'm logging this here because I spent about 45 minutes searching around for "Group Field button disabled" and the like and could not find anything. While this was entirely my fault, it would have been nice to have a hint to inspect my data closer.

Problem upgrading ComponentArt

Problem:

After upgrading a component I began getting the following error:

Could not load file or assembly 'ComponentArt.Web.UI, Version=2006.2.1507.2, Culture=neutral, PublicKeyToken=9bc9f846553156bb' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

Solution:

The problem was with the manifest reporting I needed version ...1507.2 of the file when in fact I had version Version ...1507.3 of the file in the bin directory. The ...1507.3 version of the file was both in the Main projects references correctly and in the dependent project references correctly. Nowhere did it show as the old version--though the day before I had the previous version in stalled and I overwrote the dll with the newer one. I assumed something was cached somewhere.

I used advice from the following two urls to help:

http://blogs.msdn.com/suzcook/archive/2003/05/29/57120.aspx
http://blogs.msdn.com/junfeng/archive/2004/03/25/95826.aspx
I did the following the following to solve my problem:
  1. I updated the registry key [HKLM\Software\Microsoft\Fusion\LogFailures] (DWORD) to 1 -- This required me setting permissions on the Fusion key so that I could even see the LogFailures value (which was already there and set to 1).
  2. I ran my application in debug mode and got a much more helpful error message: Which pointed to a dependency project even though the error was reported in the main project.
  3. Though the dependent project appeared to have the dll registered correctly, I had to remove the 'obj' directory from it and from the main project and recompile to get rid of all of the cached pointers to the old dll.
This fixed my problem.

ComponentArt upgrade issue

Problem:

After upgrading the ComponentArt.Web.UI.dll from 2006.1.1283 to 2006.2.1507.2 I was getting the following error:

... 2006_2_1507/A573G988.js not found


My first thought was that I needed to copy a client side library to this location. So I searched and searched and couldn't find a 2006_2_1507 client library. Nor could I find the error in the ComponentArt forum.

Solution:

In the aspx page, where I create my ComponentArt control, there was an attribute on the control called ClientScriptLocation which we had set to the location of our client library for the previous version of ComponentArt.

The new version does not need a client library, but still has the functionality. So I removed the value from the ClientScriptLocation attribute and all is well. The component will generate the required client script as needed.

Using STA COM components in a HTTPHandler

Problem:

So I had this good idea that I would use an HTTPHandler to for our multi file upload project. I have a Java Applet that allows the user to drag and drop or select from a dialog multiple files to upload.

Before I tried to integrate any of this into the main project, I created a proof-of-concept to test with and had no problem with it.

I have a HTTPHandler that implements the System.Web.SessionState.IRequiresSessionState interface which insures I get the session and all was well in my proof-of-concept project. When I moved it, I had to add the functionality that I thought would work without any modifications, but I could not have been more wrong.

First of all, the applet has some sort of java based browser that renders html. This browser apparently starts a new session with the web server so I had to post my session variables in URL parameters (not too big a deal). Then, I tried to implement some code that worked just fine on our single file upload aspx page and suddenly I was in trouble.

The code used COM components from the old ASP days and worked just fine on a WebForm with the ASPCompat page directive set to "true". But HTTPHandlers have no page directives and do not support ASPCompat, so now what?


Solution:

Well after a lot of searching I came across the following article that helped me out: http://www.epocalipse.com/blog/2006/03/04/aspnet-web-services-and-sta-com-objects/

Basically I implemented the System.Web.IHttpAsyncHandler and inherited the System.Web.UI.Page. Then I added the following functions:

Public Function BeginProcessRequest(ByVal context As System.Web.HttpContext, ByVal cb As System.AsyncCallback, ByVal extraData As Object) As System.IAsyncResult Implements System.Web.IHttpAsyncHandler.BeginProcessRequest
Return AspCompatBeginProcessRequest(context, cb, extraData)
End Function

Public Sub EndProcessRequest(ByVal result As System.IAsyncResult) Implements System.Web.IHttpAsyncHandler.EndProcessRequest
AspCompatEndProcessRequest(result)
End Sub

Private Sub FileUpload_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
'The code that used to be in ProcessRequest goes here
End Sub

Now I know what you are thinking. Hey what's up? Why use a HTTPHandler if you are just going to inherit System.Web.UI.Page anyway? Why not just use a webform?

Well, the problem is that I needed a quick fix so that I could move on. This allows me to run STA COM components in my HTTPHandler and hey, I still have a little more control than if this was a webform. And I am able to auto generate a config file used by my Java applet, that is hard coded in the applet (the applet uses a file called thinupload.properties and I wanted to auto generate that file, this allows me to handle .properties files and create the config file on the fly).

Object moved to here

Problem:

Yesterday I spent about half of the day working on a problem where I was using Thin Upload Pro to upload multiple files to our web server. Everything worked fine in the development environment but when I moved it to test, I received the message "Object moved to here" in the applet.

I found several articles on this, but they seemed to all refer to using the 1.1 .NET framework. This project was using the 2.0 framework.

The applet was showing the page just fine on first load. But when I would drag files in to the applet to upload them (which posted back to the same page) I would get the error.

Solution:

We are using forms authentication with our site and I found that when I gave unauthenticated users rights to the upload page, the error went away. I suspect this is because the applet does not post information the page is expecting to allow the authenticated user to the page. The page itself still has access to the session and verifies the user's credentials, but this had to be done manually.

Introduction

Like many developers, I rely heavily on the internet and other developer's experiences to solve problems I encounter. Occasionally I come across problems that I cannot easily fix either because the solution is no posted anywhere or it was not easily found. I have decided to start this blog to publish these problems and solutions, in hopes to give back to the developer community. I encourage readers to comment because many times it is in the comments that we find our solutions.

I might even post my experiences and thoughts on technologies as I feel so compelled.

You can find examples of my code on my website: Climers.com.
You can hear my podcast at: Cubertainment.com