Just a little note here that we noticed testing something (how it had not occurred before, I do not know).  We have a function that we were referencing from a referenced assembly that would format values to the slightly odd format being used by the ERP that we were talking too.  In  most cases it seamed to work OK, but for smaller values it didn't quite product the result we expected.

It turns out that the result of the Division functiod in BizTalk Server 2006 is a double.  The mapper being the way it is converts this to a string (assumably using .ToString()).  At least this is how it looks when I point ildasm.exe at Microsoft.BizTalk.BaseFunctoids.dll.

Now, rather simply put, I was being a fool and trying to convert this to a decimal and wondering what was going on.  I was passing what I though was a value of "0.00002" to my referenced method and expecting it to be formatted properly.  When we looked deeper in to the issue, we noticed that the value coming out was in fact "2E-05", which is how a float or a double would represent the value 0.00002.

 

Quick use of the double.TryParse() fixes this issue and off we trundle.


Worth keeping in mind, and also worth having a look at the base functoids using ildasm.exe when relying on their output.


Bookmark with :
Digg It! DZone StumbleUpon Technorati Reddit Del.icio.us Newsvine Furl Blinklist

When trying to access remote content in Silverlight, you need to add a clientaccespolicy.xml file to the root of the web server that you are trying to access.  I did this, my file looked like this:

<?xml version="1.0" encoding="utf-8" ?>
<access-policy>
    <cross-domain-access>
        <policy>
            <allow-from http-request-headers="*">
                <domain uri="*"/>
            </allow-from>
            <grant-to>
                <resource include-subpaths="true" path="/"/>
            </grant-to>
        </policy>
    </cross-domain-access>
</access-policy>

So essentially, anyone can access this.

I wrote my code to retrieve the information in a C# console app, just to test it out the WebClient as follows:

using System;
using System.IO;
using System.Net;
using System.Linq;
using System.Xml;
using System.Xml.Linq;

namespace GetBlogContentTest
{
    class Program
    {
        static void Main(string[] args)
        {
            WebClient w = new WebClient();

            string result = w.DownloadString(new Uri("http://blogs.interakting.co.uk/MainFeed.aspx"));
            
            if (!String.IsNullOrEmpty(result))
            {
                XDocument xDoc = XDocument.Parse(result, LoadOptions.SetBaseUri);

                var blogItems = from item in xDoc.Descendants("item")
                                select new ChannelItem
                                {
                                    Title = (string)item.Element("title"),
                                    Link = (string)item.Element("link"),
                                    PubDate = (DateTime)item.Element("pubDate"),
                                    SourceUrl = (string)item.Element("source").Attribute("url"),
                                    Description = (string)item.Element("description"),
                                    Author = (string)item.Element(XName.Get("creator","http://purl.org/dc/elements/1.1/"))
                                };

                foreach (ChannelItem item in blogItems)
                {
                    Console.WriteLine("Title: {0}", item.Title);
                    Console.WriteLine("Link : {0}", item.Link);
                    Console.WriteLine("SUrl : {0}", item.SourceUrl);
                    Console.WriteLine("Auth : {0}", item.Author);
                    Console.WriteLine("Date : {0}", item.PubDate);
                    Console.WriteLine("Content Length: {0}{1}", item.Description.Length, Environment.NewLine);
                }

            }
            else
            {
                Console.WriteLine("No Results Retrieved");
            }

            Console.ReadLine();
        }
    }
}

This also, worked fine.  However, when I put the same code in to my shiny new Silverlight application and run it through debug I get myself a nice little error.  The error message itself is a little bit on the ambiguous as we sometimes come to expect from technologies in their infancy.  Essentially the error I got was "Security error".  Gee, thanks for that?!

Digging a  little deeper in tot eh actual exception, we get the stack etc and the actual error (sort of):

SecurityException

at MS.Internal.InternalWebRequest.Send()
at System.Net.BrowserHttpWebRequest.BeginGetResponseImplementation()
at System.Net.BrowserHttpWebRequest.InternalBeginGetResponse(AsyncCallback callback, Object state)
at System.Net.AsyncHelper.BeginOnUI(BeginMethod beginMethod, AsyncCallback callback, Object state)
at System.Net.BrowserHttpWebRequest.BeginGetResponse(AsyncCallback callback, Object state)
at InteraktingMainFeed.Page.GetMainFeed()
at InteraktingMainFeed.Page..ctor()
at InteraktingMainFeed.App.Application_Startup(Object sender, StartupEventArgs e)
at System.Windows.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args)
at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, String eventName)

I was trying everything to fix this until I hit upon the idea of taking Casini (Visual Studio's web server) out of the question (just in case) and setting up a virtual directory on my local IIS to server the Silverlight web app.

That actually fixes it.  I don't know why this effects the way the Silverlight app works, but it does.  It's highly frustrating and in my opinion completely under-tested.  As developers, we generally need to be able to debug our applications but there is now a nice hurdle in the way and you can't just use F5.  Basically, I'm going to have to attach to the web process to debug.  It's not the end of the world, but it would be nice if I could just debug the lazy way.

I can see that picking up Silverlight is going to have some nice little challenges along the way.


Bookmark with :
Digg It! DZone StumbleUpon Technorati Reddit Del.icio.us Newsvine Furl Blinklist

My previous post talks about learning some Silverlight and going through some of ScottGu's articles about getting started with Silverlight.  As I have been trundling through then I have started to notice one thing that is getting particularly irritating.

When you're styling up your application, you need to be very careful about typing the Setter properties correctly.  I admit, I am using Visual Studio and not Expression <insert useful variant here>, but I would still expect a little more intelligence or when editing the XAML.

Let me explain...

Consider the following XAML from my App.xaml file in Scott's article:

<Style x:Key="ThumbNailPreview" TargetType="Image">
<Setter Property="VerticleAlignment" Value="Center" />
<
Setter Property="Margin" Value="7,7,5,5" />
<
Setter Property="Height" Value="55" />
<
Setter Property="Width" Value="55" />
</
Style>

Note how (me being me), I have spelt VerticalAlignment incorrectly.  The result of this will be that the part of the XAML that uses that Style will not render correctly and seems to cause the entire application to 'white-out'.  Now, when this is a Style that is visible to us at design-time, then the design view of the XAML will go blank and we'll get some kind of indication that something is not correct.  We don't know what is incorrect and no compilation errors appear.  In which case, get someone to check your code over for you (at the end of the day, you typed it wrong and someone else will probably pick up your mistake quicker, we all know how it is ^^).

So cool, we can fix the problem - lovely.  However, if the Style is being used by a control that is in a template for data binding, the problem does not reveal itself in design view insider Visual Studio.  It lurks somewhere in a dark corner waiting for you to run your application and bind some data to it.  At which point; BLAM!, you're application pulls a whitey and the screen goes blank.  Ye then need to go back to Visual Studio and figure out what was wrong.  Again, get someone to look over your shoulder perhaps to quickly spot your blatant typo.

I'm sure Expression Whatever is very good at all this Style stuff, and I will shortly try it out, but I really was hoping for better intellisense in Visual Studio to let me know what properties I can use in setters for the Style's TargetType.  Maybe v.Next will do this?  that would be nice.


Bookmark with :
Digg It! DZone StumbleUpon Technorati Reddit Del.icio.us Newsvine Furl Blinklist

I found myself with a little time between BizTalk Server tasks recently and decided it was high time I took a look at trying out Silverlight and seeing what it is worth.  It's still early days for me at the moment, but I thought I would share my starting point.

I was lucky enough to find a series of posts buy Scott Guthrie that give a good 8-step tutorial on building a Silverlight application in Visual Studio 2008.

I've really only just started and am taking the time to read some surrounding content on the web about each step, but I have to say that it looks quite impressive so far (as I think we all know).

One thing I did notice is that the WatermarkedTextBox control seems to have been removed from the current release of Silverlight and there at present does not seem to be a Watermark option on the standard TexBox control, but that's a pretty small issue when I'm effectively just getting to grips with it.


Bookmark with :
Digg It! DZone StumbleUpon Technorati Reddit Del.icio.us Newsvine Furl Blinklist

Summary

Quite often we want to be able to perform the same operation on some information given a differing number of arguments.  Typically, you might pass these arguments in as an array, thus avoiding the need to specify the number of arguments.  In order to call a function like this, we then need to go ahead and build and array of to pass in our arguments.  To me that seems like a little bit of an effort, at each point in my application, I know how many things I want to pass in.  So how else can I do this?

Solution

Now, consider the funtion System.String.Format().  In one form, the Format function can take a string and an array of type Objects.  A typical call to String.Format will be as follows:

string myString = System.String.Format(
    "Employee #{0} had been with the company since {1}{2}",
    currentEmployee.Number,
    currentEmployee.ContractCommenceDate.ToShortDateString(),
    Environment.NewLine
    );

Notice that our array of objects is represented as a comma separated list.  Lets see what happens if we try this and create a simple console application as follows:

using System;
using System.Text;

namespace VariadicFunctions
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(
                Concat(
                    "String 1 ",
                    "String 2 ",
                    "Stirng 3 "
                    )
                );
        }

        /// <summary>
        /// Glue a lots of strings together in to the mother of all strings.
        /// </summary>
        /// <param name="myStrings">A collection of obedient strings</param>
        /// <returns>The mother of all strings</returns>
        public static String Concat(string[] myStrings)
        {
            StringBuilder sBuild = new StringBuilder();

            foreach (String str in myStrings)
            {
                sBuild.Append(str);
            }

            return sBuild.ToString();
        }
    }
}

When we compile, we get an error telling us "No overload for method 'Concat' takes '3' arguments".  That makes sense, the method is looking for a string array, not a load of separate strings.  What we need to do is turn this in to what is know as a Variadic Function.

A variadic functions is a function that takes an variable number of arguments.  Essentially, to turn our function 'Concat' in to a variadic function we just need to use the params keyword.  What this keyword does is, in simple-speak, allows us to enter our additional arguments in a comma separated list.  Simply adding this keyword in will then allow us to comma-separate our arguments:

using System;
using System.Text;

namespace VariadicFunctions
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(
                Concat(
                    "String 1 ",
                    "String 2 ",
                    "Stirng 3 "
                    )
                );
        }

        /// <summary>
        /// Glue a lots of strings together in to the mother of all strings.
        /// </summary>
        /// <param name="myStrings">A collection of obedient strings</param>
        /// <returns>The mother of all strings</returns>
        public static String Concat(params string[] myStrings)
        {
            StringBuilder sBuild = new StringBuilder();

            foreach (String str in myStrings)
            {
                sBuild.Append(str);
            }

            return sBuild.ToString();
        }
    }
}

And there we go, no build errors, we can add as many stings as we like without issue.

References: 

Keywords:

  • Variadic, params, arguments array, variable arguments, variable parameters, parameters array

Bookmark with :
Digg It! DZone StumbleUpon Technorati Reddit Del.icio.us Newsvine Furl Blinklist