INCLUDE_DATA

Category .net

Authenticating with LinkShare’s Link Locator Direct web service with C# 1

Sep6

I’ve spent the last couple hours debugging an authentication issue with LinkShare’s Link Locator Direct web service. My console app was throwing the following exception:

System.Web.Services.Protocols.SoapException: org.apache.axis.AxisFault:Pass in username/password

The error message seems straight-forward enough– “Pass in username/password.” But what do you do if you know you are passing in the username and password?

This was the code I was using:

LinkShareTextPromotion.EJBTextPromotionServiceService tp =
     new LinkShareTextPromotion.EJBTextPromotionServiceService();
tp.Credentials = new NetworkCredential(username, password);
tp.PreAuthenticate = true;
LinkShareTextPromotion.Text[] results  = tp.GetAllLinks(sid);

Anyway, I finally lucked out and stumbled onto this link on the LinkShare forums, which lead me to Gordon Weakliem’s blog that had a working solution. Apparently this problem occurs due to how .NET authenticates.

If you’re experiencing the same error and got here via Google, then I hope this helps you get on your way.

Resurrecting this blog and broadening it’s focus 1

Oct28

I started work at TELUS over 10 months ago, and since that time, I’ve barely written a stitch of Ruby, let alone learn anything more about Ruby on Rails. My TextDrive account is no longer active and the experimental websites I launched while actively learning Ruby on Rails have been shut down. Essentially, I’ve lost all connection and interest in RoR and the RoR community.

For that reason, I’m renaming my blog from “Rory on Rails” to simply, “Rory Hansen”, as I don’t foresee myself writing about Rails anytime in the near future. Who knows– maybe somewhere down the line, I’ll regain interest in RoR, but right now, my mind and my time are focused elsewhere.

At work, I program almost exclusively in ASP.NET and C#. For the type of development I do, ASP.NET is fine. I appreciate the power of the framework and, given my experience with it now, I can get new webapps up and running quickly and easily. Do I like ASP.NET? Actually, yes I do.

Outside of work, I’m constantly trying to better myself through learning new skills and improving existing abilities. As my buddy Jeff would say, I’m just trying to ”raise my NPV.” Whether it be by reading great business books like Blue Ocean Strategy or Good To Great, or by practicing my networking skills at various conferences around town, I’m consciously trying to improve myself, thus raising my “market value,” so that when I’m ready to find a great new job somewhere, I’ve positioned myself as best as I can to actually get that job.

So, with all of that said, future posts on my blog will be about many of my current interests, such as learning how to play the acoustic guitar, improving my communication and networking skills, making change happen in business, and futher developing my leadership abilities.

Stay tuned!

Rory

Capturing application-wide unhandled exceptions 0

Mar11

I’m now two months into my new job and am neck-deep in development on the two web applications that my team maintains. One thing I’ve noticed so far in the code base is that there are many areas throughout the application where try/catch blocks should have been used, but were not. This often leads to users seeing ASP.NET exception error pages when unexpected errors occur.

To quickly address this issue, I’m implementing application-wide exception handling, so that at least users see a nice error page and can continue on with their work. With ASP.NET web applications, there is a super-easy way to get this going: Use the Application_Error() method in the Global.asax file.

Global.asax.cs:

protected void Application_Error(Object sender, EventArgs e)
{
   if (Server.MachineName.ToUpper() == PRODUCTION_MACHINE)
   {
      Exception ex = Server.GetLastError().GetBaseException();

      // When an exception occurs, session is no longer available
      // in this method. Store exception at Application-level instead.
       string guid = Guid.NewGuid().ToString;
      Application.Add(guid, ex);

      Server.Transfer("./error.aspx?e=" + guid);
   }
}

So, what this code does is this:

  1. Check to see if we’re on the Production machine. (Of course, initialize the PRODUCTION_MACHINE to be the machine name of your Production web server.)
  2. If so, then get the last error and save that at the Application-level, so that we can access it on another page. Use a GUID as the key, so that we can retrieve it later.
  3. Then, transfer the user to the generic error page.

Note: You’ll notice that I did not use Session anywhere in this method. Our web applications are using the ASP.NET State Server to store our sessions and this causes the Session to temporarily become null when an unhandled exception occurs and this method executes. Once this method finishes executing and the code-behind in the error.aspx page begins to execute, we can again access Session.

If you are using the InProc session state, you may be able to store the exception in the Session instead.

Anyway, once we transfer the user to the generic error page, we can do any sort of error processing we want. My current implementation displays a friendly error message to the user, then emails our team the details of the exception, including relevant information from the user’s Session.

error.aspx.cs:

private void Page_Load(object sender, System.EventArgs e)
{
   // Get the exception
   string guid = Request.QueryString["e"];
   Exception ex = (Exception) Application[guid];
   Application.Remove(guid); // Little bit of cleanup

   // Insert any code here to display error message to user

   // Insert code to email exception to yourself
}

Using IFrames in ASP.NET 5

Aug22

So, I’ve basically finished coding the “Google Suggest”-like web user control I was writing for the company I’m doing a co-op work term at. I’m now in the process of integrating it into an existing textbox control that they’ve developed, and that should be finished soon.

One part of the web user control is an iframe, which is used to counteract the much-documented z-index and select box problem in Internet Explorer. By placing the iframe directly below the div that the Google Suggest results are displayed in (as in one z-index less than the z-index of the results div), we can avoid this problem all-together.

But, to make the web user control as reusable as possible, I needed to be able to name this iframe uniquely, incase there are multiple instances of the control on the same aspx page. To do this, I needed to be able to specify the name of the iframe when it is rendered (or guarantee that it would be automagically named uniquely), and I figured out I could accomplish this by using two standard controls: PlaceHolder and HtmlGenericControl.

First, I create the HtmlGenericControl object that will eventually render as an iframe and specify whatever attributes this iframe will have:

// Create new iframe control
HtmlGenericControl searchFrame = new HtmlGenericControl("iframe");
searchFrame.ID = "searchFrame";
searchFrame.Attributes.Add("class", "searchFrame");
searchFrame.Attributes.Add("frameborder", "0");

Then, I can add it to the PlaceHolder’s controls collection:

// Add it to the Controls collection of the PlaceHolder control
searchHolder.Controls.Add(searchFrame);

Finally, I add the PlaceHolder control into my ascx document where I’d like the iframe to eventually be:

<div class="searchContainer">
   <asp:PlaceHolder id="searchHolder" runat="server" />
</div>

Now, an iframe will appear in the outputted HTML code where the placeholder once was. But more than that, since I used an ASP.NET control to actually create the iframe, the iframe will be named uniquely. We even know what it’ll be called: this.UniqueID + “_searchFrame”. This is beneficial to me since I can now reference that iframe by name throughout my JavaScript code and show it or hide it when necessary.

End of Term Co-op Project: Google Suggest-like Web Control 0

Aug11

My co-op work term is coming to an end at my current workplace and I’ve been given the opportunity to produce an ASP.NET web control that functions similar to Google Suggest which will provide me with something that I can write about for my end-of-term technical report. So, not only will I be coding all aspects of the web control, but I’ll also writing a techical paper that details the process that I followed to create the web control as well as the intimate details as to how it’s been implemented. I suppose this’ll give me a chance to practice the technical writing skills that I picked up last term in the 300-level technical writing course that I attended.

Since I’m relatively new to .NET, it should come as no surprise that I’ve never created a web control before, let alone create one that uses AJAX. But I have used AJAX in my Ruby on Rails site before, although I’ll admit that the experience won’t be all that much help unless I decide to use the Prototype javascript framework within my web control.

Anyway, I have already gently scoured the ‘Net for existing web controls that meet my requirements and, while there are numerous projects and code samples that contain bits that I’ll probably examine closer, I wasn’t able to find one that perfectly fits the bill. (I’m actually pretty sure there are some out there and that I was just not able to find them.) In any case, the most difficult part will probably not be the C# but rather the JavaScript, and with the abundance of XMLHttpRequest JavaScript snippets out there, I’m sure I’ll be able to appropriate (and credit, of course!) at least some of them into my work.

In any case, I’m excited about this project and am looking forward to starting on it this weekend. Like my other pet projects, I’ll probably document some interesting snippets of code here, so stay tuned. : )

.NET Web Services and Windows Services 0

Jun26

I’m in the process of writing my first web service in C# and through the whole process I’ve been surprised at how easy it’s been. It’s generally been the same as any sort of project, except for how it’s intially created and how it’s tested. I’m also in the process of writing my first Windows service in C# that will consume the web service, and, if possible, I’ve been even more surprised at how easy it’s been. Visual Studio.NET _really_ makes it easy to develop these kinds of projects, and I’m becoming more and more of a fan.

If you are interested in developing your own Windows service, take a look at this tutorial on CodeProject. I basically followed this tutorial from top to bottom to set up the basis of the Windows service, which includes an installer that wraps the actual Windows service.

With all this time dedicated to .NET recently, I know I’ve been neglecting my time with Ruby. But, it’s only temporary. I’m still thinking about my Ruby on Rails lyrics project all the time, and when I do get back to developing it, I hope to get a large chunk done.

Casting from MS SQL money to C# double 3

Jun21

I’m just beginning to transition to ASP.NET and C# from a largely Java / C++ / C background, and along the way, I’m finding myself often stumped with what seems like really simple problems.

For example, today, when writing a ASP.NET web service in C#, I had trouble casting a money value retrieved from a MS SQL database to a double. The following code would always report an error:

C#
double cost;
cost = (double) dr["cost"];

I also tried using a float, but that didn’t fix the problem either.

The _correct_ type to use in this case is actually Decimal, and I learned this only after a co-worker sent me a link to a document on the MSDN site that has a table showing all of the proper type conversions from one system to another. So, this code _does_ work:

C#
Decimal cost;
cost = (Decimal) dr["cost"];

You can find the table here:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconusingparameterswithdataadapters.asp

(Scroll about halfway down; you can’t miss it.)