WCF Error: An error occurred while loading attribute 'XmlSerializerFormatAttribute' on method x in type y

If you are developing a WCF service with .NET 3.5 and come across the following error message:

WCF Error: An error occurred while loading attribute 'XmlSerializerFormatAttribute' on method x in type y

I have a very simple solution for you: make sure you have installed .NET Framework 3.5 Service Pack 1 on all your machines.

I have searched for hours for a solution to this error. It appeared just on one single WCF call whereas all others were working fine.

Finally I came across this MSDN forums post that pointed me in the right direction.

So If you develop your software on 3.5 SP1 systems and then deploying it to 3.5 systems without SP1, it's very likely you will fix your problem by installing SP1.

A very ugly (but working) alternative to ScriptErrorsSuppressed in WPF Browser Control

I recently worked on an application that needed to automate a website in the background. It fills out some forms, invokes some buttons and checks if all the navigation works fine.

Needless to say, the user of the application should not even notice that there is a website in the background.

I needed to use the webbrowser control as the website is a really fantastic example of how to NOT implement a web site. Everything the user does causes JavaScripts to execute and it's a nightmare (if not impossible) to solve this by sending custom POST requests to the webserver.

So I went with the webbrowser control which works really fine when used to fill out forms and invoke buttons in context of the website. It's still a lot of spaghetti code that comes together but it actually works.

For V2 of the application, i switchted from WinForms to WPF. The first thing I've learned was that it is really no good idea to use the WPF webbrowser control as it lacks some features of the WinForms control. One of them was the ScriptErrorsSuppressed-Property that prevents webpages from displaying MessageBoxes (not only caused by ScriptErros).

The solution is of course to use a WinFormsHost inside your WPF window and place a WinForms webbrowser control inside of it. But while you now have access to a much more complete API and can SET the ScriptErrorsSuppressed property, this property has actually no effect.

No matter what you are told in some dev forums (for example: it only works when the application runs in full trust) - it never works. Never ever. Don't waste your time. I spent hours trying dozens of different "solutions" to get this working, still each and every MessageBox popped up and remindet the user that there is some browser automation happening in the background.

As there is no clean solution on this and everything looks like Microsoft is not interested in changing this situation, I decided to go the ugly way with a dirty hack - I use COM Interop to the Win32 API call GetLastActivePopup that returns the window handle of the last popup a given window has created. This also works for MessageBoxes that are created by webbrowser control.

The trick now is to launch a worker thread every time you do some browsing and let this thread check frequently if your browser window raises any popups. If so, use another COM call to destroy this popup immediately.

I have very good results using this technique. That means: it works reliable and the end-user doesn't even notice that there's something going one most of the time as the check happens every 10 ms in my configuration. This short interval does not cause any performance issues or more than regular CPU load during execution.

Everything you need is here

The good news is: it is possible to use this out-of-the-box and you can use my code 1:1 in your project.

Firstly, here's the class that contains all the logic. Include it just like below in your project:

private class PopupHandler
{
    #region Interop

    [DllImport("user32.dll")]
    static extern IntPtr GetLastActivePopup(IntPtr hWnd);

    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);

    static uint WM_CLOSE = 0x10;

    #endregion

    public IntPtr OriginalWindowHandle { get; private set; }

    private bool isCanceled = false;

    public PopupHandler(IntPtr originalWindowHandle)
    {
        this.OriginalWindowHandle = originalWindowHandle;
    }

    public void Cancel()
    {
        isCanceled = true;
    }

    public void Execute(object windowHandle)
    {
        while (!isCanceled)
        {
            // Find a possible open IE popups/alert and close it
            IntPtr lastPopupHandle = GetLastActivePopup(OriginalWindowHandle);
            
            if (lastPopupHandle.ToString() != "0" & OriginalWindowHandle != lastPopupHandle)
                SendMessage(lastPopupHandle, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);   // Sends close Message to popup

            // Sleep a little to prevent full CPU load
            Thread.Sleep(10);
        }
    }
}

 

Next, here is an example how to call this class in a multithreaded way:

// Launch Popup Handler Thread
WindowInteropHelper wih = new WindowInteropHelper(this);

PopupHandler popupHandler = new PopupHandler(wih.Handle);
Thread popupHandlerThread = new Thread(popupHandler.Execute);
popupHandlerThread.Start();

// Now do your navigation
// this.browser.Navigate(@"http://www.google.at");

 

And that's it. This will do all the work.

Short explanation: this in the above context is the WPF window. To get the window handle of a WPF window, we need to use the WindowInteropHelper class. And with wih.Handle we pass the window handle to the PopupHandler class, and actually this information is all the PopupHandler class needs to do its job.

Just one important notice: a WPF window does not get a window handle until it was showed at least one time. So you *will* have to call myWindow.Show() at least once before using this class. It's ok if you call myWindow.Hide() immediately after that. Consider setting the default window state to minimized to prevent the user seeing some flickering on the screen.

What is left for you to do? Of course, you want to stop this thread after you have completed browsing. The PopupHandler class has a handy Cancel-Method for that. Just call it when you are finished.

I hope this helps some fellow developers to save many valuable hours of their lives :)

Of course if anyone out there can come up with a cleaner solution, please let me know!

New compactRSS release – Live Search is now working again

Just as I was not able to take care of bpk.cc in the last months, I was also not able to take care of compactRSS as much as I wanted.

But as bpk.cc is now up and alive again, it's also time to push out a new release of compactRSS with a few bugfixes.

The most important fix of course is that Live Search works again now (and is now rebranded to the new "Bing" brand). While Microsoft seemed to have fixed the issue with empty results for a couple of months, the problem popped up again (and was never fixed).

Now I digged deeper into the Bing API and found a solution that brings you a permanent fix. At least I hope so (seriously, Redmond!).

Here's the complete list what have been fixed in release 1.0.12.26:

  • Web search for feeds is now working again
    Searching by using Live Search returned no result over the last couple of months
  • Rebranded "Live Search" to "Bing"
  • Dropped support for Live Search 1.x API
    You cannot fallback to the 1.x API in compactRSS now by setting the relevant flag in the config file. Live Search / Bing API 2.0 is now the only supported version.
  • "Trace" menu entry is now removed completely if tracing is not enabled
    Until now, it was just disabled ("grayed out"). In fact, this menu entry was never meant to be seen by the end-user.
  • Fixed wrong header text in OPML Import explorer window
  • A few other small fixes or optimizations

As the update service is currently still not available, you will have to manually download the setup (cab) file to your phone from this website and execute it. It will update the old version automatically so you won't have to remove your current installation of compactRSS at least.

Version 1.0.12.26 will be available shortly for download from the product page and the download section of this website.

Simple Solution for ToObservableCollection() for LINQ Queries

When working with Windows Presentation Foundation and MVVM, you will have to deal a lot with ObservableCollections. And if you also love LINQ, like I do, you will soon come over a very annoying limitation:

LINQ queries always return an IEnumerable which can be very easily "converted" to a generic List:

var result = from x in y
             select x;

return result.ToList();

Sadly, there isn't anything like ToList() for an generic Observable Collection - something like ToObservableCollection() as you might expect.

Luckily, there is such cool stuff like Extension Methods in .NET and it is very easy for us to extend the type IEnumerable with an ToObservableCollection() method that just works as simple as ToList():

public static ObservableCollection<TSource> ToObservableCollection<TSource>
    (this IEnumerable<TSource> source)
{
    ObservableCollection<TSource> collection = 
        new ObservableCollection<TSource>();

    if (source != null)
    {
        foreach (var item in source)
            collection.Add(item);
    }
    else
        collection = null;

    return collection;
}

Just put this class into your project and IEnumerable will be extended with this method immediately. Now we can write:

var result = from x in y
             select x;

return result.ToObservableCollection();

If this doesn't work for you, just make sure that you put this class in the same namespace where you want to use this extension method (or that you import this namespace).

compactRSS finally available for download again

I'd like to drop a short note that compactRSS is now available for download again in the freshly unlocked download section of this website. You find also a direct link to this page in the top menu.

It's the same version as on the old website, so nothing has changed since then.

The product/info page to compactRSS will also return soon. In the meantime, this should keep you served, no matter if you're new to compactRSS or if you used it already and just need the latest update (as unfortunately the bpk Update Service went down with the shutdown of the old website, too, and won't return in the near future).

To install or update, just download the file from the download page directly with your phone.

Welcome back to bpk.cc!

For a serious amount of time, bpk.cc hasn't offered a public website. While all relevant services that you might have been consuming here (like E-Mail services) were always available, I decided to completely re-design the main web presence of this site.

The reason for this was that the formerly used software to run this site, BlogEngine.NET, didn't satisfy all my needs. While this .NET based blogging platform was absolutely fine for blogging, it lacked the power of a fully-featured CMS.

But besides blogging, bpk.cc should also serve its visitors with information's and download possibilities to products (or services) that I want to offer here, like compactRSS that some of you might still use on their good old Windows Mobile devices (although Windows Phone is now the next best thing out there, served as 7 series, please).

So for all those pretty things (and more) I searched for something more suitable out there. Namely a fully-featured CMS.

As it turned out, that is pretty hard in .NET land. There are great products available, but most of them cost a ton of money which I just can't afford. Even the lower-priced ones are not really an option for a private, non-commercial site.

For sure, there are also a ton of .NET-based, free or/and open source CMS out there. While some were never an option (I never liked DotNetNuke and its sisters which are powerful but somehow old-fashioned and over-modularized), some looked really interesting - like Umbraco.

Umbraco really shines with a fresh approach to content management. Mainly, it appealed to my because there just aren't any modules like in probably 99 % of each and every other CMS out there. It gives you a nice framework, some out-of-the-box functionality ... but at the end ... its completely up to the developer and/or designer what you pull out of it.

The flexibility is just unbelievable, but for sure you cant just click a website together in 1 or 2 hours like you might know this from hyped CMS software like Joomla.

Anyhow, as a .NET developer the decision to switch to Umbraco is hard. Because you will mainly use XSLT to inject functionality into your site. You can use ASP.NET user controls too, but it's not the best practice for most of the tasks.

While XSLT is something that you will starting to love while learning it, the tool support for writing XSLT is inferior compared to core code development. Stepping away from most of the ASP.NET concepts is also a tough step to take.

Anyhow, I'm glad my decision was Umbraco. It really redefines the way I look at CMS systems.

Modules for a CMS is just the same thing as Apps for a phone: it looks so good when you first see it, but you just can't stand that stuff anymore in a very short amount of time.

That said, I will fill up this site with content over the next weeks. Everyone who missed a download link for compactRSS over the last months will be pleased to know that also this little, free feed reader tool for your Windows Smartphone will return. And of course there are exciting announcements to make regarding the future of Windows Mobile (Phone) and the future of compactRSS.

Windows Phone 7 Series along with Silverlight and MVVM will be a big topic here even beside compactRSS. I have written a lot of code already for this toy and will share some of my code and earned knowledge here.

So stay tuned, glad to be back online in blogosphere.