2024-10-13

ASP.NET Route results in 404 when the route ends with a filename

The Problem 

I had a custom route set on an API method that was to be used to delete a file from a ZIP created for an expense.

[HttpDelete]
[Route("api/reimbursements/expense/{expenseId}/receipt/{receiptFileName}")]
public async Task<IHttpActionResult> RemoveReceiptFromExpense(string expenseId, string receiptFileName) 
{
    // code to perform the remove here...
}

Everytime I tried to hit this endpoing using JavaScript's "fetch" API using the DELETE verb, it would always result in a 404. I spent more than an hour banging my head against the wall trying various different route changes, but could not understand why other endpoints on the same API controller worked except for this one.

The Reason

It finally dawned on me that ASP.NET has handlers that are filename-based, and they were intercepting my request and not finding a match that could fill it.  It wouldn't pass it to the routing handler that would go find my template and handle it.

The Solution

I don't have time today to research the more elegant solution, which would be to get the routing system to ignore the filename checks for this route and handle it with the filename at the end of the route, so I chose to reverse the template and put the receipt filename first and the expenseId last.  Now it looks like this:

[HttpDelete]
[Route("api/reimbursements/receipt/{receiptFileName}/expense/{expenseId}")]
public async Task<IHttpActionResult> RemoveReceiptFromExpense(string expenseId, string receiptFileName) 
{
    // code to perform remove here...
}

This made it work with no issues.  This is a quick solution for others experiencing the same issue.  

If you want to keep your route with the filename at the end, you will need to research how to force the "Extensionless" handlers to intercept your request for that template.  As I mentioned before, I don't have time today to research that, but needed to document this with the little time I have, so I can remember why next time I run into this issue.


2020-02-19

Access ASP.NET website hosted in IIS Express from another machine on the local network (VS 2019)

I needed to test Mac Safari against a website I was developing on a Windows machine. Instead of being required to publish for every change, I wanted to just expose IIS Express across the LAN and hit the website that way. In order to do so, I had to do the following:

  1. Run Visual Studio 2019 as Administrator
  2. Edit {ProjectRoot}/.vs/{WebProjectName}/config/applicationhost.config
  3. In the <sites> section, add a binding under the website project's <site> definition (see below):
  4. Allow the desired port through your firewall
<site name="FocusAndExecute" id="1">
  <application path="/" applicationPool="Clr4IntegratedAppPool">
    <virtualDirectory path="/" physicalPath="{path_to_site}" />
  </application>
  <bindings>
    <binding protocol="http" bindingInformation="*:51427:localhost" />
    <binding protocol="http" bindingInformation="*:51428:192.168.1.123" />
  </bindings>
</site>


After doing this and launching IIS Express from Visual Studio, I was able to hit the site from the Mac by going to:
http://192.168.1.123:51428

NOTE: You may be able to put *:51428:* instead of specifying the IP, so you can hit it by host name, etc. but I didn't play with that yet.  Feel free to leave a comment if you have a better way!

2019-11-04

[SOLVED] Visual Studio 2019 keeps rebuilding my ASP.NET Core project when I refresh my browser

A couple of weeks ago, I started noticing that Visual Studio would rebuild my code whenever I would force a refresh in my browser, even if I had just rebuilt the solution in Visual Studio before executing said refresh.  Sometimes, Visual Studio would rebuild more than once during this process, and eventually would end up with a "Cannot copy the **** files because it is being used by another process" (which I imagine was the dotnet process handling the request.

I assume the multiple builds were because I was reloading a page that also turned around and made AJAX calls back to the same site.

It got beyond super annoying, and I searched the internet and dug through settings and could not find a workaround, until today.

Under "Tools" | "Options" | "Projects and Solutions" | "ASP.NET Core" you will find an option under the "General" category called "Auto restart Kestrel server after build".  It was set to "True" in my case, and I set it to "False" and it stopped the constant rebuilding for me.

Visual Studio 2019
Tools | Options | Projects and Solutions | ASP.NET Core
Notice the description of that option states:
"Automatically build modified ASP.NET Core projects hosted in IIS Express when a browser request is received."

Unfortunately this was happening to me even after I explicitly did a full rebuild in Visual Studio, then switched to my browser to refresh the page.  So I have no idea why VS thought it was "modified" but it wasn't.  I don't have time to track down the cause of this right now, so I consider this really more of a workaround than a solution, but I hope it will help someone else avoid this madness.

I would also like to know when this option was introduced... has it been there this whole time and always defaulted to "True" and I just happened to not be bit by it until a couple of weeks ago?  Perhaps the detection mechanism for determining an ASP.NET Core project was "modified" has been broken in a recent Visual Studio release?  (I'm currently on 16.3.7, but it was happening to me on at 16.3.6 as well.)

I'd love to hear from you if you have encountered the same thing and found a better explanation and/or solution.

2018-12-30

[SOLVED] Using Bootstrap-Select and Vue together

I ran into some trouble when trying to use Vue bindings and Bootstrap-Select together that kept the visual controls from updating when options were changed via a Vue model update. 

I wrote up an article on my JavaScriptJedi.com website with both broken and working examples and an explanation about what happens.

The quick answers for making sure bootstrap-select works with Vue:

  • Prevent bootstrap-select from automatically hooking itself up to your controls by avoiding using the "selectpicker" class on the <select> element.  Use a different class like "select-picker" instead that you can manually hook up the selectpicker after your Vue model is finished mounting.
    • (Use Vue's $nextTick function inside your "mounted" handler function to ensure all components/child-components are mounted as well before setting up bootstrap-select)
  • Only use the "title" HTML attribute on your <select> when you don't have a default selected value on your <select>
  • ALWAYS call bootstrap-select's "refresh" after changing the <select>'s options via an update to the Vue model.
Please see my write-up on JavaScript Jedi and leave feedback here if you can help me enhance the article or find it helpful.  I hope this saves someone some time and frustration! 

2018-10-18

[Azure DevOps] Pending Changes gives "you are not authorized to access ..." error during Check-In

For some reason lately I have been periodically getting "... you are not authorized to access [filename.extension] ..." after clicking on the Check-In button in the "Pending Changes" section of the "Team Explorer" tab in Visual Studio.

I can't explain why this happens, but the workaround I've discovered is to click the "Refresh" button at the top of the dialog.  Once the refresh is complete, the check-in process seems to work fine.  I hope it works for you as well.


If you have run into the same and can explain why, please share in the comments.

Happy coding!

2018-06-27

ASP.NET MVC: Controlling order when using Scripts.Render

The Problem

I like concise, so when I realized I could change the following:

@Scripts.Render("~/bundles/bundle1")
@Scripts.Render("~/bundles/bundle2")
@Scripts.Render("~/bundles/bundle3")
@Scripts.Render("~/bundles/bundle4")


... to ...

@Scripts.Render("~/bundles/bundle1", "~/bundles/bundle2", "~/bundles/bundle3", "~/bundles/bundle4")

... naturally I got excited.  And it worked really well, that is, until I needed bundle3 to come before bundle1.

For a more realistic example, I have my own bundle that is dependent on KnockoutJS and requires Knockout to be loaded before my bundle is.  It would look like this:

@Scripts.Render("~/bundles/something", "~/bundles/knockout", "~/bundles/adminView")

I assumed that the bundles would be rendered in the same order I specified in that call, but it doesn't seem to play nice that way. My "~/bundles/adminView" was rendered before Knockout which broke my script.

To fix it, I removed my bundle from the rest of the list and moved it out into a separate Scripts.Render call. Now it looks like:

@Scripts.Render("~/bundles/something", "~/bundles/knockout")
@Scripts.Render("~/bundles/adminView")


Doing this forced my "adminView" bundle to be rendered after the other two because it was a separate call.

Moral of the Story

You can only "bundle" multiple bundle paths in one Scripts.Render call when none of them depend on each other, because you can't count on what order they will be rendered in. I don't know how Microsoft decides the order to render them in, but it's not necessarily the order you provide.

If order matters, use separate calls and order them the way you need to.

2018-06-13

NUnit 3.x: Asserting Equal Decimal Values with a Precision Threshold

With the newer style of doing assertions in NUnit 3.x, I was trying to figure out how to assert a decimal whose value was 1 digit precision on the right-side of the decimal, but only allowing a variance of 0.0001.

I believe this is the best practice, but will be happy to be corrected if someone knows a better way.

Assert.That(actualObject.SomeDecimalValue, Is.EqualTo(42.6m).Within(0.0001m));

2015-08-21

[SOLVED] Visual Studio "Attach to Process": Available Processes list appears empty

The Quick Answer:


  • Open the "Attach to Process" dialog (For VS2012: Alt-D | P)
  • Open Task Manager (right-click taskbar | Start Task Manager)
  • Find msvsmon.exe, highlight it, then click End Process button
  • Go back to "Attach to Process" and click "Refresh" button

The Story Behind the Answer:

I don't know if this will apply to more than Visual Studio 2012, but if it does, fantastic; please let us know in the comments.

A few days ago, my Visual Studio 2012 went wonky and decided to not show me the Available Processes in the "Attach to Process" dialog in most cases.

Here is what it should look like:

Here is what it looks like when the problem occurs.  Notice how, if I hover inside it, it will show me the tooltip for the process I'm currently over.  This suggests the list is actually populated, and double-clicking on that spot will actually make the debugger try to attach to it.  The information is there; the questions are, "Is it white font on white background?" or "Is there some other kind of painting/rendering issue with the dialog box?"

Looking around the internet, I could not seem to find the right phrase to search to find help with this issue; until I finally stumbled upon this StackOverflow question that was as close as I could get.  One answer suggested renaming MSVSMON.EXE in the x64 folder and copying the x86 version to it.

Not feeling comfortable with that, I decided to experiment with a similar concept.  I opened Task Manager and saw that msvsmon.exe was a running process.  

I executed an "End Task" on the process, and was met with this error:
Going back to my already open "Attach to Process" dialog, I clicked "Refresh" and BAM! ... there appeared the visible list.

I have done this several times, each time resulting in success.  I hope it works for you!

2015-07-03

Data Dynamics Reports - Enabling sorting on column headers causes abnormal height growth when CanGrow is True

I ran into a weird one today when working with a Data Dynamics Report (DDR) that needed sorting enabled on the column headers of a table.  Each of the TextBox controls in the column header cells were set to CanGrow=True, which was good to make sure each could fit their content.  Width was forced, so the only way for it to grow was in height.  It worked great until I enabled sorting.

Each column cell required sorting, so I added the SortExpression to each accordingly.  The instant I brought up the report in the WebReportViewer, it looked like the heights of the header cells were twice as tall as needed to fill the largest content of any of the given cells.

I then reverted my changes and the height went back to normal, making the height of the table row equal to the cell with the most content in it.  I deduced that DDR was computing height based on the extra sorting control being added to each cell, but somehow was calculating too much.  As I changed strings, the height would grow relative to the amount of content in the cell... so if the string was longer, the cell would grow all the more tall; looking double what the content in the cell was taking in height.

As I was about to give up and disable sorting on the report because the headers were taking up 1/3 of the page, I decided to try an experiment.

I turned CanGrow=False on every one of the column headers' TextBox controls (leaving the sorting enabled.)  The height went down to minimal, where only one line of text would show, and the rest was hidden because the row wasn't tall enough.  Great.  I have a baseline now!

The next step was to try changing CanGrow=True only on the cell I knew would have the most content in it.
Win!

So if you run into a similar issue, try the following:

  • Change CanGrow=False on all TextBox controls in your column headers, except the cell you know will have the largest content.

2014-07-25

[SOLVED] GrapeCity/ComponentOne ActiveReports 8 - Excel Export using WebViewer is broken

The ActiveReports engine is a great engine for rendering reports for an application.  Sometimes, though, errors will occur that have multiple reasons for why they show up; some known, others not.

It's very hard to troubleshoot those or to find solutions for them, even if you have incredible internet search skills.  That said, this solution may not work for you, but hopefully will give you a quick test to see if you can move on to another suggestion.

I was puzzled while trying to solve a problem we had in our web application where exporting via ActiveReports 8 WebViewer to PDF or DOC worked fine, but XLS for some reason returned a 404 error.

The problem turned out to be with mine that we missed laying down a DLL required by the Excel exporter (GrapeCity.ActiveReports.Export.Excel.v8.dll) called DocumentFormat.OpenXml.dll, which didn't seem to be referenced by the other Export DLLs.  

As soon as I put this in place, it started working again.

This file could be easily missed as it is not named like the other export related DLLs:
GrapeCity.ActiveReports.Export.*.v8.dll

Make sure to include that and you'll have one less reason to see a 404 error when you try exporting to Excel from ActiveReports!

On a side note, the following are all of the referenced DLLs (Microsoft DLLs excluded) for GrapeCity.ActiveReports.Export.Excel.dll in case you need to check for others also missing:
  • DocumentFormat.OpenXml.dll
  • GrapeCity.ActiveReports.Diagnostics.v8.dll
  • GrapeCity.ActiveReports.Document.v8.dll
  • GrapeCity.ActiveReports.Export.Rdf.v8.dll
  • GrapeCity.ActiveReports.Extensibility.v8.dll
  • GrapeCity.ActiveReports.v8.dll
Best of luck!

2013-08-28

MSBuild - Copy Task using Items with Includes and Excludes

I struggled for a couple of hours on this one... until finally finding this useful answer from stackoverflow.com! My scenario was needing to copy some files in a pre-build step in a Visual Studio C# project. For every file I needed to copy, there was a counterpart for each of our supported languages. The naming convention for the files was like this:

MyFile.properties
MyFile_de_DE.properties
MyFile_fr_FR.properties

etc.

Here is what my csproj looked like:
<PropertyGroup>
    <ReportingEnglishLocFolder>$(SolutionDir)..\path\to\destination\</ReportingEnglishLocFolder>
</PropertyGroup>
<ItemGroup>
    <EnglishLocPropertiesFiles
        Include="$(ReportingBaseFolder)master\*.properties"
        Exclude="*_de_DE.properties"/>
</ItemGroup>
<Copy
        SourceFiles="@(EnglishLocPropertiesFiles)"
        DestinationFolder="$(ReportingEnglishLocFolder)" />

After attempting multiple combinations of wildcard patterns and having no success, I changed the search phrases I used to search the internet and found the stackoverflow thread.
The most important part from the thread, and the most important thing to remember about using Include and Exclude is:
If you use an absolute path for Include, you must use an absolute path for Exclude. If you use a relative path for Include, you must use a relative path for both.
So I changed to this and it worked like a champ!
<ItemGroup>
    <EnglishLocPropertiesFiles
        Include="$(ReportingBaseFolder)master\*.properties"
        Exclude="$(ReportingBaseFolder)master\*_de_DE.properties"/>
</ItemGroup>

So remember, whichever type of path you use for Include, you need to do the same for Exclude.

2013-08-09

Silverlight: ObservableCollection.CollectionChanged Event

The ObservableCollection<T>.CollectionChanged event will only fire if the collection itself changes (items are added, removed or rearranged/moved.), NOT if an item in the collection changes (meaning one of the item's in the collection has one or more properties modified.)

It's now around 2:30am, and I've been going the rounds for a couple of hours with a Silverlight project at work; fighting a seemingly simple change that ended up not being so simple because of a major misunderstanding of the CollectionChanged event on the ObservableCollection<T>.  My ObservableCollection<T>.CollectionChanged event was not firing like I thought it should.

I imagine this will be a common mistake for others, and I'm sure I myself am doomed to repeat it again in the future; so I'm documenting it for a quick reminder later.

My thought process was this... "I need to watch my ObservableCollection for one of its items to be changed.  Once the item is changed, I need to flip a boolean flag which happens to be bound to a command which will disable a button after the change has been made."

Simple, right?
I should just be able to handle the ObservableCollection<T>.CollectionChanged event, because if one of those items in the collection gets changed, that counts... 

Or not.

See, the misconception is, the collection itself hasn't changed.  It has the exact same items it had before, it's just that the item within the collection changed.  So there are two things that can happen to your collection:

  • The collection itself is modified (Items are added, removed or moved.)
  • An item in the collection is modified (its individual properties are changed.)

Let me illustrate:
For this example we will have a class called VideoGame to keep track of titles and costs.

public class VideoGame : INotifyPropertyChanged
{
 private string _title;
 public string Title 
 { 
  get { return _title; } 
  set
  {
   _title = value;
   RaisePropertyChanged("Title");
  }
 }
 
 private decimal _cost;
 public decimal Cost 
 { 
  get { return _cost; } 
  set
  {
   _cost = value;
   RaisePropertyChanged("Cost");
  }
 }
 
 public event PropertyChangedEventHandler PropertyChanged;
 public void RaisePropertyChanged(string propertyName)
 {
  if(PropertyChanged != null)
   PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
 }
}

And now some code to use it...

ObservableCollection<VideoGame> videoGames = new ObservableCollection<VideoGame>
{
 new VideoGame{ Title = "TrackMania 2: Stadium", Cost = 9.99M },
 new VideoGame{ Title = "TrackMania 2: Canyon", Cost = 19.99M },
 new VideoGame{ Title = "TrackMania 2: Valley", Cost = 19.99M },
};

videoGames.CollectionChanged += (sender, eventArgs) => {
 
 Debug.WriteLine("--- The collection changed");
};

Debug.WriteLine("Changing the cost of one of the video games in the collection...");
// The CollectionChanged event will not fire because it was just one of the collection's items that changed...
// not the collection itself.
videoGames[0].Cost = 7.99M; // the game went on sale.

Debug.WriteLine("Adding a video game to the collection...");
// The CollectionChanged event WILL fire because the collection of items changed.
videoGames.Add(new VideoGame{ Title ="ShootMania: Storm", Cost = 19.99M });

And the output...
Changing the cost of one of the video games in the collection...
Adding a video game to the collection...
--- The collection changed
When it dawned on me, at first I was frustrated; but then clarity set in.  It makes sense to me that we only want the CollectionChanged event to fire when the collection itself is modified, not its individual parts.  With it, the NotifyCollectionChangedEventArgs that are passed with the event give us a lot of good information about what happened with the change.

  • Action - Tells which action effected the change in the collection (Add, Remove, Replace, Reset)
  • NewItems - The items affected by the action
  • OldItems - The items removed or replaced in the collection
  • NewStartingIndex - Tells the index where the change occurred.
  • OldStartingIndex - Tells where a Replace or Remove action took place.
While all this is cool, what do I need to have happen so I can track when an item in the collection is changed?

I need to make sure I handle PropertyChanged on the individual items of the collection.  Make note, this can be tricky because I still need to watch for the Collection to change so I can subscribe to any new items' PropertyChanged events as they are added to the collection, and clean up after myself when the items leave the collection.

I'll let you decide how you're going to approach that; it's a discussion for another day.  But I want to drive home the concept that the CollectionChanged event is just that... when the collection itself changes, not its individual items.  Know that, and you'll save yourself a lot of time.

2013-04-02

[SOLVED] "Attaching the Script debugger to process iexplore.exe failed" message after upgrading to Internet Explorer 10

HUGE thanks going out to Dmitri Leonov - MSFT in a stackoverflow article about the same message.
After updating to Internet Explorer 10, I started receiving the error when debugging with Visual Studio 2010:

"Attaching the Script debugger to process [####] iexplore.exe failed.  Another debugger may already be attached to the process."

The fix:

  • Close all instances of Internet Explorer.  (I will add you can leave Visual Studio open)
  • Run a command prompt as Administrator and paste this little gem into the command line:

 
It worked like a champ for me (and apparently others from comments on the stackoverflow post.)

Good riddance to an annoying message!  Happy coding.

2013-01-08

JavaScript String.Replace: Replacement Pattern for Full Match Reminder

This is my quick reminder to self to use
$&
instead of
$0
in JavaScript for Regular Expression replacement patterns when wishing to use the entire matched text.

Some browsers allow $0, but $& is officially documented (table 22, page 147) and supposed to work in all of them.

Example:
document.write("Hello World".replace(/^Hello/, "$& to the whole");

Output:
Hello to the whole World

2012-09-29

LINQ to Objects: Querying Nested Collections

I love LINQ.  LINQ is just fun to use.  I love the fact that I can query the same way across databases, XML  documents and objects (alright XML is slightly different, but...)

Today I had the task of querying down a complex set of objects that each own a collection data member.  I needed to get to some data 4-layers deep and thought the LINQ query would get pretty nasty.  That is until I learned that I can use the "from" keyword multiple times in the same LINQ query.

My example will be very contrived to save having to look at a lot of extra code for different collections, so I'll go ahead and just nest the same collection several layers down, just so we get the concept.

I have a Person class:
public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }


    public int IQ { get; set; }

    private List<Person> _children;
    public List<Person> Children
    {
        get{ return _children ?? (_children = new List<Person>()); } 
        set{ _children = value; }
    }
}
If I set up a new List<Person> like so:
  List<Person> grandpas = new List<Person>
  {
    new Person{
      FirstName = "John", LastName = "Doe", IQ = 91,
      Children = new List<Person>{ // children
        new Person{ FirstName = "Meh", LastName = "Doe", IQ = 103 },
        new Person{
          FirstName = "Tae Kwon", LastName = "Doe", IQ = 125,
          Children = new List<Person>{ // grandchildren
            new Person{ FirstName = "Karate", LastName = "Kid", IQ = 68, },
            new Person{ FirstName = "Karate", LastName = "Kid2", IQ = 117 },
          }
        },
      }
    },
 
    new Person{
      FirstName = "John", LastName = "Doh", IQ = 135,
      Children = new List<Person>{ // children
        new Person{ FirstName = "Meh", LastName = "Doh", IQ = 42 },
        new Person{
          FirstName = "Tae Kwon", LastName = "Doh", IQ = 140,
          Children = new List<Person>{ // grandchildren
            new Person{ FirstName = "Doh", LastName = "Boy", IQ = 111 },
            new Person{ FirstName = "Doh", LastName = "Nut", IQ = 98 },
          }
        },
      }
    },
  };
Check out how easy it is to get to data that is below the top layer:
IEnumerable<Person> grandChildrenWithHighIQ = from grandpa in grandpas from parent in grandpa.Children from grandkid in parent.Children where grandkid.IQ > 100 select grandkid;
EDIT: I was asked to provide a version with the fluent API as well:

IEnumerable grandChildrenWithHighIQ = grandpas .SelectMany(grandpa => grandpa.Children.SelectMany(parent => parent.Children)) .Where(grandchild => grandchild.IQ > 100);

Now we know longer need be afraid of querying complex objects and getting the data we want.  Write your queries in the bLINQ of an eye, and get your data back even faster!

And I'll put a shameless plug out there for LINQPad since I took a screenshot from its results above.  It is a fantastic tool for crafting LINQ queries and is a wonderful sandbox for small .NET proof-of-concept projects because you can write small programs without creating a Visual Studio project or solution.

Enjoy!

2012-08-29

Telerik RadNumericUpDown for Silverlight does not update binding when value is blanked out

Note: This may be fixed in a later version of Telerik controls; this issue was discovered in the Telerik.Windows.Controls library; version 2011.1.315.1040

Working on a Silverlight application, we found ourselves with some dialogs that utilize the Telerik RadNumericUpDown control.  We discovered a defect when binding the Value property of the control to a ViewModel behind the scenes.

The original binding works as expected, and if the spinner controls are used to change the value, the ViewModel updates as we expect.  If a user types a new value into the control it also updated the binding on the ViewModel correctly.

The problem arises when we blank out the value by clicking into the textbox portion of the control, highlighting it and hitting the Backspace or Delete key.  Removing focus from the control or not, the ViewModel property bound to that control does not get updated.

To work around this, we created this Event Handler:
void RadNumericUpDown_ValueChanged(object sender, 
                                   RadRangeBaseValueChangedEventArgs e)
{
    if (sender is RadNumericUpDown)
    {
        RadNumericUpDown upDownControl = sender as RadNumericUpDown;
        if (!upDownControl.Value.HasValue)
            upDownControl.Value = upDownControl.Minimum;
    }
}
Now we apply this event handler to our XAML:
<telerik:RadNumericUpDown ValueChanged="RadNumericUpDown_ValueChanged" ... />
This makes the behavior act the same as if the user had used the down spinner on the control to modify the value down to the minimum.

This may not be the ideal solution for you, but understanding this will give you direction on how you can create your own workaround.  We started down the path of handling the KeyUp event, but ValueChanged seems much more appropriate.

Telerik makes fantastic controls; and I'm not mad by any means.  Being a developer myself, I realize these things happen; but I do hope they have handled this better in a later release.  And as far as I know this is the only control in the 2011.1.315.1040 version that has this issue.

2012-07-04

PayPal Instant Payment Notification (IPN) Simulator Requires Port 80

Someone please correct me if I'm wrong, but I fought for an hour or more trying to get PayPal's Instant Payment Notification (IPN) Simulator to post test transactions to my test site running on IIS Express which listens on a port different than 80; and it just doesn't work.

I asked friends on external networks to bring my site up in their browsers and they could hit it fine on the non-standard port, so I knew my firewall and IIS Express were configured correctly.

I cannot find anywhere in PayPal's documentation where it states that port 80 is required but it seems their own firewall is pretty strict with outbound traffic.  Once I forced my router to listen on port 80, and forward the request to the non-standard port my site listens on, the simulator worked like a champ.

So, if you're having problems with the IPN simulator not sending to your listener and you are listening on a non-standard port, make sure to do one of the following:

  1. Force your test site to listen on port 80 and configure your firewall accordingly.
  2. Force your router to listen on port 80, but forward to the port your test site is set up on.
That's what we like to see!
As a side note, if I remember correctly... the standard ASP.NET Development Server (Cassini) that comes with Visual Studio, does not accept requests from an external network.  I believe it can only accept them from the same machine you are doing the development on.  This is why I opted to use IIS Express.

Hope this saves someone else some time and head-banging on your desk.  :)
Happy coding!

2011-11-02

Silverlight Unit Testing: TestInitialize/TestCleanup methods do not fire, tests do not run, nor pass/fail, and no details given

I had the classic bang-head-against-the-keyboard for three hours scenario today while doing Silverlight Unit Testing.

I had a test class with two unit tests in them which worked fine, until I started refactoring to get common setup/teardown functionality moved into methods with TestInitialize/TestCleanup attributes applied.

Then the weirdness started.  I started seeing this:
The tests don't run; don't pass or fail; and no details as to why.
Check out the yellow highlighted areas.
As you can see, there wasn't a whole lot of information present to assist me in my predicament.  After removing the TestInitialize/TestCleanup attributes, the tests worked fine.

I began searching the internet trying to find someone who ran into the same problem.  I found many results where developers complained of the TestInitialize methods swallowing exceptions and moving on, so I started down that path in my own code, trying to discover if there was an exception I was missing that was being eaten and causing the entire class to fail.

The only problem was, when I commented all code within the bodies of the TestInitialize/TestCleanup methods, I still ended up with the same results.
???
Digging deeper, it finally dawned on me that the test class in question was inheriting from a base test class that also happened to have methods dressed with the TestInitialize/TestCleanup attributes.  Looking at the declaration of TestInitializeAttribute revealed what I suspected:

[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)] public sealed class TestInitializeAttribute : Attribute

AllowMultiple = false... which makes sense. So my code had two problems:

  1. My base class' method for TestInitialize needed to be made virtual so it could be overridden in classes that inherit from it to allow for overriding (and calling back to the base so both get their jobs done.)
  2. My child class' method was named different, so I needed to go make it match and override.
Once I fixed these problems for both the init and cleanup methods, the tests went back to working accurately.

Bottom Line: When doing unit testing with base test classes that perform common setup functionality, make sure to have your Setup/Teardown or Initialize/Cleanup methods marked virtual in the base class, so they can be overridden in the sub-classes.  Then override those methods in the sub-class, making sure to call back to the base appropriately.

Example (MSTest):

public class CommonTestSetupBase { [TestInitialize] public virtual void TestSetup() { // do common setup work for all that inherit } [TestCleanup] public virtual void TestTeardown() { // do common cleanup work for all that inherit } } //================= [TestClass] public class UnitTestedClassTests : CommonTestSetupBase { [TestInitialize] public override void TestSetup() { base.TestSetup(); // do common setup for this class } [TestCleanup] public override void TestTeardown() { // do common cleanup for this class base.TestTeardown(); } } I knew I wouldn't remember this if I bump into it again, so I had to document it here... I wish I had more time to go into more detail, but I hope this will be enough to help you and I in the future when we meet this problem again.

2011-08-30

Silverlight throws InvalidOperationException with Common_MethodFailed error when calling HtmlElement.SetAttribute for the "type" attribute

I was working on a Silverlight application the other day and needed to create and submit an HTML form to another ASPX page inside the website hosting the app.

To accomplish this, I was using the HtmlDocument.CreateElement, HtmlElement.SetAttribute and HtmlElement.AppendChild methods to set the whole thing up.

After deploying the application having success with Internet Explorer 9 (IE9), Chrome, etc. we tested on two virtual machines that had IE7 and IE8 and were met with errors and no form submission.

It turns out it was because I was trying to set the "type" attribute of the <input /> elements I was creating with the Silverlight methods after I'd called the AppendChild method.

IE7 and IE8 don't like a developer setting the "type" attribute after the element has been appended to the document and it will throw a JavaScript error which bubbles up to the Silverlight application and the user is shown an obscure InvalidOperationException error whose key is "Common_MethodFailed".

It made no sense to me, but I dug deeper until I could find the offending line.  I then tried an experiment with a XHTML/JavaScript page in the offending browsers to see if it was a Silverlight problem or a problem with the JavaScript engines of said browsers.

I ran into the same problem without Silverlight in the equation.  I wrote up a detailed article explaining it and have the experiment to try in different browsers yourself on my JavaScript Jedi website.

The problem can be solved by making sure you call AppendChild AFTER you set up all the properties/attributes on the HtmlElement, not before.

I hope this helps you avoid several hours of frustration.  :)
Happy Coding!

2011-06-17

WCF "Gotcha" When Sending an Invalid Enum Value Across the Boundary

The Silverlight application I'm working on for my employer at the time of this writing involves WCF services that provide data for the app.

It was working fine until a coworker ran a SQL query that changed an int field (which maps to an Enum in our code) to an invalid value on several records in a common test database a few of us were pointing to.

All of the sudden we were getting a FaultException (I can't remember the exact error - but it was one of those unrelated ones that mask the real exception message that caused the problem to begin with.) while trying to load a graph.

Further investigation revealed that our asynchronous calls to the WCF service client were retrying the same call three times and coming back with a failure.  Stepping down deeper into the code, we saw that our resultset seemed to be coming back fine from our data layer, leaving us wondering why it was behaving so strange.

My coworker put a breakpoint where our data layer returns the IEnumerable to the service so it can serialize/return the data to our app, and changed the resultset to an empty array of the given type instead of allowing the real resultset through.

Only a single attempt was made on the service instead of three and the data was received fine by the application...  However, when we reverted to allow the regular resultset through, it again made three attempts and returned the exception.

On the next run, we decided to look at the resultset closer in the QuickWatch window.  We didn't look carefully enough because everything looked correct... so we started removing one object at a time until we could prove that the culprit was corrupt data in our resultset.

Finally we expanded two objects in our collection at the same time in the QuickWatch window and started comparing.  We noticed that one instance's enum property was retuning the name of the assigned enum and another showed an integer value.

After asking why that would happen, it dawned on me that it must be an invalid enum value and therefore couldn't resolve to a name when ToString() was called.  I assume to fail gracefully, Microsoft had their Enum.ToString() try to do name first and just fall back to the value of the Enum's base type if it couldn't resolve the name, rather than throwing an Exception.

Examining the implementation of the enum quickly confirmed the suspicion.  After probing other coworkers, we discovered the erroneous query that had been run against the database.  :)

I assume what happens is an exception occurs when the WCF tries to serialize the object to send across the boundary... but the exception is swallowed up and the Fault Exception with the less-than-helpful error message is returned to the consumer of the service.

Moral of the story: Be careful when using Enums that map to data in your database.  Make sure they always match the allowed values in the database (which is easy to do if you have look-up tables and use an ORM (like PLINQO, etc.) to generate the enums for you. 

Also, if you use "switch" statements in your code on the enum value, make sure your default case handles an invalid enum value scenario.  For instance, you've made a switch statement that contains a case block for all valid enum values.  Your default case can throw an ArgumentOutOfRangeException so you can quickly detect when something like that happens.

This will be harder to detect in scenarios like the one I described above, because the code is still "valid" at compile and runtime until the sucker is serialized.  I don't know what the best practice solution is for that scenario, but I do know I want a better FaultException passed to me if serialization breaks before being passed from the WCF service.

Guess I have my work cut out for me tomorrow.

For those like me that have a hard time visualizing what I'm talking about... here's a contrived example.

public enum ReportType
{
  Html = 1,
  Excel = 2,
  Pdf = 3
}

The query to the database changed the offending records to 0, 4 or some other number that wasn't the valid values.  (Incidentally, my coworker really meant to change another int field's value... but ended up typing the wrong column name.)   Too funny.

And now you know... go forth and make sure you aren't bit by this problem.