Category: Development

TimeSpan to String Converter for XAML binding

Need to convert a TimeSpan to a sensible string for you Windows Phone or Windows Store App. With a little Bing help I created this converter. I hope you find it useful.


public class TimeSpanToStringConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        TimeSpan span = (TimeSpan)value;
        if (span == TimeSpan.MinValue)
        {
            return "0 seconds";
        }
        string formatted = string.Format("{0}{1}{2}{3}",
            span.Duration().Days > 0 ? string.Format("{0:0} day{1}, ", span.Days, span.Days == 1 ? String.Empty : "s") : string.Empty,
            span.Duration().Hours > 0 ? string.Format("{0:0} hour{1}, ", span.Hours, span.Hours == 1 ? String.Empty : "s") : string.Empty,
            span.Duration().Minutes > 0 ? string.Format("{0:0} minute{1}, ", span.Minutes, span.Minutes == 1 ? String.Empty : "s") : string.Empty,
            span.Duration().Seconds > 0 ? string.Format("{0:0} second{1}", span.Seconds, span.Seconds == 1 ? String.Empty : "s") : string.Empty);
        if (formatted.EndsWith(", ")) formatted = formatted.Substring(0, formatted.Length - 2);
        if (string.IsNullOrEmpty(formatted)) formatted = "0 seconds";
            return formatted;
    }
        
    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        string strValue = value as string;
        TimeSpan resultSpan;
        if (TimeSpan.TryParse(strValue, out resultSpan))
        {
            return resultSpan;
        }
        throw new Exception("Unable to convert string to date time");
    }
 }

IISRESET and Microsoft Azure cloud services don’t mix well!

I recently performed am IISRESET on some production Azure cloud servers and found to my horror that the website died. A restart resolved the issue, but I had just discovered a little too late that IISRESET is not an option on a Cloud Services server.

This blog (http://www.morestuffabout.net/2012/01/game-over-on-azure-dont-iisreset/) explains some of the alternative options, such as using a utility called ASPRESET which you can find info about at ASPRESET – Jon Galloway blog. This can help avoid needing to do an IISRESET.

The type or namespace name ‘xxxx’ does not exist in the namespace ‘yyyy’ (are you missing an assembly reference?)

If you suddenly find your Visual Studio solution falling to pieces with loads of error messages like the title above when you build. You also know that the project libraries are being referenced correctly and they build OK. Then check that you do not have an uninvited app.config file lurking at the root of any of your projects.

I have spent lots of annoying wasted hours trying to figure this out only to have found that a NuGet installation has added an app.config to the projects.

Delete this and do a rebuild and all should be OK.

The moral of this story is make sure you build and check-in before adding any NuGet packages. Then immediately rebuild after. If this scenario occurs, you know what to do.

WCF Test Client – Maximum message size quota exceeded

If you get

“The maximum message size quota for incoming messages (65536) has been exceeded”

in the WCF Test Client, the easy solution has been nicely documented here

http://ralessi.wordpress.com/2013/03/29/wcf-size-quota-exceeded/

Thanks

VC_RED.cab, VC_RED.MSI, install.res.1028.dll etc.. Can be removed!

If you are wondering how or what all these funny files (more listed below) are sitting on your c:\ or other drive, then you will be please to know that they were generated as temporary files by a Visual C++ redistributable install package. Unfortunately the package does not clean them up afterwards.

If they are not on your c:\ drive, that because the installer actually looks for the drive with the most available space or something like that!

Any of the following files can be safely removed.

install.exe
install.res.1028.dll
install.res.1031.dll
install.res.1033.dll
install.res.1036.dll
install.res.1040.dll
install.res.1041.dll
install.res.1042.dll
install.res.2052.dll
install.res.3082.dll
vcredist.bmp
globdata.ini
install.ini
eula.1028.txt
eula.1031.txt
eula.1033.txt
eula.1036.txt
eula.1040.txt
eula.1041.txt
eula.1042.txt
eula.2052.txt
eula.3082.txt
VC_RED.MSI
VC_RED.cab

For Microsoft’s article see http://support.microsoft.com/kb/950683

C# DateTime.ToString formats quick reference

Always useful too quickly see all the format options for DateTime.ToSring(). Republished here thanks to http://www.geekzilla.co.uk/View00FF7904-B510-468C-A2C8-F859AA20581F.htm

The quickset’s below. For the rest click the link above.

Pattern Equivalent Format string Example
d MM/dd/yyyy 08/22/2006
D dddd, dd MMMM yyyy Tuesday, 22 August 2006
f0 dddd, dd MMMM yyyy HH:mm Tuesday, 22 August 2006 06:30
f1 dddd, dd MMMM yyyy hh:mm tt Tuesday, 22 August 2006 06:30 AM
f2 dddd, dd MMMM yyyy H:mm Tuesday, 22 August 2006 6:30
f3 dddd, dd MMMM yyyy h:mm tt Tuesday, 22 August 2006 6:30 AM
F dddd, dd MMMM yyyy HH:mm:ss Tuesday, 22 August 2006 06:30:07
g0 MM/dd/yyyy HH:mm 08/22/2006 06:30
g1 MM/dd/yyyy hh:mm tt 08/22/2006 06:30 AM
g2 MM/dd/yyyy H:mm 08/22/2006 6:30
g3 MM/dd/yyyy h:mm tt 08/22/2006 6:30 AM
G MM/dd/yyyy HH:mm:ss 08/22/2006 06:30:07
m MMMM dd August 22
r ddd, dd MMM yyyy HH’:’mm’:’ss ‘GMT’ Tue, 22 Aug 2006 06:30:07 GMT
s yyyy’-‘MM’-‘dd’T’HH’:’mm’:’ss 2006-08-22T06:30:07
u yyyy’-‘MM’-‘dd HH’:’mm’:’ss’Z’ 2006-08-22 06:30:07Z
U dddd, dd MMMM yyyy HH:mm:ss Tuesday, 22 August 2006 06:30:07
y yyyy MMMM 2006 August