Calling external web services in ASP.NET Atlas

I just received this ASP.NET Atlas question via direct email, so thought I'd answer it publically:

I have a web project that has a AutoCompleteExtender textbox. Everything works fine if my .asmx file is in the same project.

<atlas:AutoCompleteExtender runat="server" ID="CustomerList">
<atlas:AutoCompleteProperties Enabled="true"
MinimumPrefixLength="1"
ServicePath="NorthwindService.asmx"
ServiceMethod="AutoCompleteCustomerList"
TargetControlID="customer"/>
</atlas:AutoCompleteExtender>

But as soon as I move the .asmx file to another project and add a web reference to the web project the WebMethod isn't being fired.

<atlas:AutoCompleteExtender runat="server" ID="CustomerList">
<atlas:AutoCompleteProperties Enabled="true"
MinimumPrefixLength="1"
ServicePath="http://server2/App/NorthwindService.asmx"
ServiceMethod="AutoCompleteCustomerList"
TargetControlID="customer"/>
</atlas:AutoCompleteExtender>

Same Host but Different Applications

If the web service you're attempting to hit is on the same host (eg http://localhost/App1/MyPage.aspx calling http://localhost/App2/MyService.asmx) then your issue most likely revolves around the proxy generation.

One of the magic building blocks behind service references in Atlas is the generation of JavaScript proxies for them. Create a new web service in your Atlas application (eg http://localhost/MyApp/MyService.asmx) then take a look at the proxy Atlas generates for it (http://localhost/MyApp/MyService.asmx/js). If you add this "/js" suffix to a web service URL in a non-Atlas application you'll just get an error. If you need to call a non-Atlas application like this, look at the instructions below for using bridges.

Different Hosts

If the web service you're attempting to hit is not on the same host (eg http://server1/MyPage.aspx calling http://server2/MyService.asmx) then your issue is the result of a security restriction:

Other than linking to external resource files (for example, image files, CSS files, script files, and so on), browser applications are restricted to accessing resources available on their home server. However, there is a great variety of Web services available on the Web, and the restriction to a home server limits what can be returned within the scope of a browser application.

To circumvent this restriction (in a secure way) you need to use an Atlas bridge, as documented here:

http://atlas.asp.net/docs/atlas/doc/bridge/default.aspx

technorati tags: ,

Trial Balance is live!

I received an email from Paul Stovell overnight that his personal project Trial Balance is now (finally) live. Well done Paul!

Trial Balance is a simple accounting application, designed as a learning tool to teach basic double-entry accounting to high school and university students. It is created in C# on the .NET 2.0 platform. The user interface is built upon Windows Presentation Foundation in XAML.

There is a good description of the project (including some discussion about the importance of having an active personal project) on Paul's blog.

The source code is actually hosted on our SVN repository here at Fuel Advance:

http://anonymous@svn.services.fueladvance.com/hosted/trialbalance (the original codename was "Banana" until Paul actually told me what I was hosting)

Tip: Decimals in C#

I have noticed some people writing code like this to get a decimal in their code:

decimal amount = decimal.Parse("10.00");

It’s not that obvious, but you can write this which is better:

decimal amount = 10.00M;

If you don’t include the M then the compiler thinks you are typing a double which is why it won’t compile.

technorati tags:

Review: SSW Code Auditor

Over the coming weeks/months I’ll be posting a series of product reviews here. Starting that process is SSW Code Auditor. As many of you know, I have an ongoing relationship with SSW however I’m not getting paid off for this series of posts. Those who know me better would also know that even if I was getting paid … that wouldn’t stop me from publicly bashing something I don’t like.

For anyone who’s hasn’t come across SSW in their time, you might understand why this tool exists if you take a look at their almost limitless number of standards. They range from the infamous Rules to Better Email to the SQL Server Stored Procedure Naming Standard. With more writing time being spent on these standards than the average magazine publishing house it quickly becomes hard to difficult to enforce and track their implementation; thus Code Auditor, SQL Auditor and Lookout were born.

The Code Auditor sales pitch touts the product as “[using] the power of Regular Expressions to audit your code”. I’d like to rephrase that to something like this:

Code Auditor is a regex runner on steroids combining a regex editor, tester, pre-built rules databases, VS.NET integration and lots of other wacky features like being able to process MDB files.

A pre-built rules database is provided with the product that follows along the lines of SSW’s published standards, however this can be easily customized if you’d like to name your exception variable something other than ex or want to enforce standards that your organization has already developed.

So far it sounds like the dream product … and I’m sure the product homepage could do an even better job of selling it… but I still only see Code Auditor as part of a wider toolset. With me spending 99% of my time in the .NET space, I’ve always been a heavy promoter of FxCop. With the release of VS2005 we now have this functionality in the OOBE which is even better. SSW addresses this, however I’d like to rebut some of their comments:

FxCop has a good set of rules however there are some problems:

FxCop works against compiled assemblies and uses metadata to get information about the code. SSW Code Auditor on the other hand looks at the source code itself.

I never realized you could do a code review by reading the versioning or security information of an assembly. I guess they meant MSIL instead… however it helps to understand a product before comparing it. (Update: this has been fixed on their site)

FxCop only works against .NET assemblies. SSW Code Auditor on the other hand works against any plain text files.

Correct – but I don’t have a problem with this. I personally don’t use Code Auditor for anything other than my C# code. If two pieces of C# code compile to the same IL then I don’t really have a problem with what the source code was. The runtime runs IL not source code.

Many of the rules of FxCop are obscure. SSW Code Auditor on the other hand has rules that are more 'general developer' oriented.

In my opinion an FxCop rule like “Review unused parameters” is more useful than “Times New Roman should not be used”. Unused parameters generally indicate either an incomplete method implementation or a badly designed method signature. Code Auditor is unable to check for this, although can happily tell me that I’m not allowed to use Times New Roman.

Many people consider FxCop’s rules to be obscure (somewhat understandably) when they get an error thrown on something like “Static holder types should be sealed”. This is a pretty simple rule though – if all of your members are static then you have no instance members. If you have no instance members, you have nothing to override if you inherit from it and it just becomes a marker rather than a base class, which is a perfect situation for attributes. (PS: This rule is now redundant in C# 2.0 with the addition of a “static” keyword for classes.)

FxCop rules are fixed. SSW Code Auditor can easily be extended via new rules (using regular expressions).

FxCop rules are anything but “fixed”. David Kean has written and released a great set of custom FxCop rules. (Update: this has been fixed on their site)

FxCop only provides one type of report. SSW Code Auditor on the other hand provides a report that is flexible and shows the data in different views.

SSW Code Auditor is designed to be scheduled so you keep your code projects clean all the time.

Granted – this has been seen as big limitation with the .NET 1.1 version of FxCop as it would required team managers to have an updated copy of the source code on their machine before being able to run a scan. With Code Auditor you could have the scan running on a schedule on some backroom server and just get it emailed to you. For me though, this argument falls apart rapidly: you’d expect a team manager who is reviewing code to have a copy of the code anyway; FxCop can be automated by integrating it into a continuous build solution like CC.NET.

Code Auditor sits well as one part of a wider auditing and review process. If I was short of time but had to perform a review, I’d be doing things in this order:

  1. Get experienced developer to look at the code
  2. Run FxCop
  3. Run Code Auditor (with a rule set tailored to the organization, not the packaged one)

I would not be performing the following unless I was incredibly bored for an extended period of time:

  1. Run NUnit (this should be happening on a regular basis by the developer, and in some form of CI scenario)

I continue to rely on ReSharper’s extremely powerful abilities to check and fix my code formatting, and thus I disable all of Code Auditor’s rules in this area (but at least I can do that). (Update: I reported this and ReSharper integration is now on its way)

The consensus is:

  • Code Auditor is one part of a wider auditing and review process.
  • It’s in my top 3, but experienced developers and FxCop still rank higher.
  • The regex builder is great – leading many people to consider buying the product just for this functionality.
  • In comparison to other tools, heavier upfront customization is required resulting in higher implementation costs. This is a result of the rules being more specific that most – so it’s really about upfront pain for long term gain – still something that needs to be considered though.

Rocky H has also posted a review of Code Auditor on his blog which includes a screenshot based walkthrough and some interesting discussion in the comments.

Atlas Slides + Demos

I've put a copy of the slides and demos for my upcoming Atlas series at http://fueladvance.com/Downloads/AtlasTalk.zip as I usually find that if I don't put them up before the presentation I never get around to doing so afterwards. As I imagine the presentation will evolve over at least the first few talks, I'll endeavour to update the ZIP over time.

The talk dates are:

For details about exact locations (and times where not specified) your best bet is to contact the local usergroup in that city as I still don't know most of the details myself!

There's no talk scheduled for Sydney (my home base) as there have been a number of high quality presentations through the local usergroups on the technology already. If you're interested in hearing the talk anyway, either drop me a line or contact one of the usergroups.

I'll also mention now that I'm going to be presenting at Code Camp Oz this year with an introduction to source control systems. The presentation is in the introductory track, so primarily aimed at people who haven't used source control before, or have only used a basic VSS setup. It'll cover:

  • what are the two different models (checkout-modify-checkin vs. copy-modify-merge)
  • what is branching, merging and tagging?
  • what systems exist and what criteria you should evaluate before you choose one

Update: Sydney has been added to the list.

Update: Added the extra presentations in Brisbane and Canberra, as well as adding some links for the details.

Update: Added more times.

Update: Bumped Sydney to the next month.

Update: Moved Auckland back a day. 

technorati tags: , , ,

Contrasting Colours in C#

It’s still not perfect … but this is a bit of code another developer and I came up with a while ago for finding contrasting colours.


/// <summary>
/// From a given colour it works out a suitable colour that will sit on top of
/// it so that the contrast is suitable for readability.
/// </summary>
/// <param name="baseColor">Color to get the contrasting complement of</param>
/// <returns>Contrasting color</returns>
public static Color GetContrastingColor(Color baseColor)
{
HSB baseHsb = ColorToHsb(baseColor);
int newSaturation = baseHsb.Saturation;
int newBrightness = baseHsb.Brightness;

if ((baseHsb.Saturation >= 40 && baseHsb.Saturation <= 60) && (baseHsb.Brightness >= 40 && baseHsb.Brightness <= 60))
{
newSaturation = (baseHsb.Saturation <= 50 ? 100 : 0);
newBrightness = (baseHsb.Brightness <= 50 ? 100 : 0);
}
else if (baseHsb.Saturation >= 40 && baseHsb.Saturation <= 60)
{
newSaturation = (baseHsb.Saturation <= 50 ? 100 : 0);
}
else if (baseHsb.Brightness >= 40 && baseHsb.Brightness <= 60)
{
newBrightness = (baseHsb.Brightness <= 50 ? 100 : 0);
}
else
{
newSaturation = 100 - baseHsb.Saturation;
newBrightness = 100 - baseHsb.Brightness;
}

if (baseHsb.Saturation == 0)
{
newSaturation = 0;
}

HSB newHsb = new HSB(baseHsb.Hue, newSaturation, newBrightness);
return HsbToColor(newHsb);
}

Flock

I’m in love … with Flock. It’s yet another browser on the scene (just so web devs don’t get off too easily) … but it does have some really cool bits.

It’s not for the mainstream – it’s more of an experiment with the idea of social browsing. It has RSS, blogging, maps, photo sharing and favourties sharing all baked into the browser – not as extensions. Better yet, they’re integrating with existing services like Yahoo Maps, Flickr, Shadows, and del.icio.us instead of rebuilding them all for a small, isolated community of users. The blogging works with a whole range of different APIs meaning you can point it at any half decent blog site and it’ll work.

It’s a basically an awesome rich-client based mashup. Check it out!

It’s built very tightly around the Firefox codebase (and by some of the same people) so it’s not another reinvention. They are intending to maintain a continued partnership, and not a completely isolated development fork.

technorati tags: , ,