.NET. Agile. Design. Technology.
mattcalla.com
mattcalla.com

WPF (XBAP) using WCF in a Partial Trust Environment


I've just gone through the pain of working out how to get my WPF Browser Application (XBAP) using a WCF service without any security exceptions causing me to attempt to break yet another desk.  After hours of frustration I've finally got it working:  A WPF Browser application, talking to a WCF service seamlessly and with no security issues.  In an attempt to save you the pain (although it was a good learning experience), I've put together a little walkthrough to get you up an running without the problems I went through.  The main thing to keep in mind, is that you can only have your XBAP communicate to your WCF service if they are hosted from the same server, as outlined here.  This is the only way it will work with the XBAP partial trust sandbox.

With that in mind, here are the steps I went through to get it up and running.

1. Make sure your WCF project is the WCF Service Application (under the Web tab).  Do not use the WCF Service Library (under the WCF tab).  This is a little confusing, but it is what Microsoft advised in this article.




2. Once your WCF project is created - ensure you set the service endpoint bindings to  'basicHttpBinding' (default is wsHttpBinding).  You can change this in the Web.config of your WCF project.



3. Set your WCF Service to run on a specific port all the time (this will make more sense later on).  You can choose any port, as long as it's unique.  Go to Properties->Web->Servers->SpecificPort of your WCF project.  In this case I chose port 64610.




4. Now you need to add the Service Reference to your XBAP project (I've assumed so far that you already have your WPF Browser App project created).  To do this, just right click on 'Service References' and click 'Add Service Reference'.  

                              

When the dialog opens, use the 'Discover' button to find all the WCF Services in your solution.  Then just give it a namespace and click OK.



5. Now that your WPF app can see your WCF service, it's time to configure your WPF app so that it looks like its running from the same web server (as I mentioned earlier, this is needed otherwise you will get Security Exceptions such as WebPermission exception during debug runtime).  To do this you need to go to set your debug start action to run the PresentationHost.exe (which is located in your Windows\System32 dir), and add some command line arguments which will setup your environment correctly (as to mimic that your WPF app and WCF service are being hosted from the same IP and port).  So you should end up with something similar to this:

a) Properties->Debug->StartAction->StartExternalProgram = D:\Windows\System32\PresentationHost.exe
b) Properties->Debug->Start Options->Command line arguments: 
    -debug "D:\Projects\WCFandXBAP\WpfBrowserApp\bin\Debug\WpfBrowserApp.xbap" 
    -debugSecurityZoneUrl "
http://localhost:64610"

So I'm basically telling the project, that when I debug/run the program from Visual Studio that I want it to run my .xbap file using the PresentationHost.exe, and to run it from the my localhost on port 64610 (which is the same host as the WCF service).



That's it!  You should be able to run and debug your WPF Browser Application that talks to a WCF Service with no issues.

Please let me know if you are aware of any ways to improve this process, or if you are aware of any implications from what I have done here.

Good luck with your WPF and WCF development.

 del.icio.us  Stumbleupon  Technorati  Digg 

WPF Browser Application Cache Problems

If you're having issues with your WPF Browser app's having old versions being cached - most noticeably during development, when you make a change, debug the app from Visual Studio (by pressing F5) and your changes do not appear in the version loaded in your browser - then you will need to clear your Application Cache before your new version will be loaded.

To do this simply run the following command from the Visual Studio command line:  Mage.exe -cc

This will clear the application cache, and make sure the next time you run the app, the latest version will be downloaded.

This drove me nuts for a while, but running this command solved all caching issues.

 del.icio.us  Stumbleupon  Technorati  Digg 

Register .NET COM library

If you have applications that need to create objects or call code from a .NET COM dll, then you will need to make sure you have registered the dll correctly. 

To do this type in the following from the command line:

regasm <FileToBeRegistered.dll> /codebase

I've just spent a few hours trying to work this is out, as I was ...<< MORE >>

CTRL + K,D : Auto-format your code in Visual Studio

If you are working on some existing code that's in a state of mess, then CTRL + K,D is your best friend.  In Visual Studio, hit it at any time and it will automatically format all code in your current document.  ...<< MORE >>

Planning XP - My favourite quotes


I've been re-reading the 'Planning Extreme Programming" book written by Kent Beck and Martin Fowler lately, and  I've picked out a few quotes from what I've read so far that I think highlight some key points to remember, or that simply amused me.  Either way, I think its good reading.  Here they are:

Keep the plan simple, otherwise they won't be ...<< MORE >>

BDD Spec Generator


I've written a little console application to convert NUnit test result output from BDD style test fixtures into a simple spec text file.  The spec text file output is basically a human readable file that will be useful to departments in your business other than the developers themselves, such as - QA and Project Managment.  It is something I'm starting ...<< MORE >>

Redundant comments


Code like this really frustrates me... well maybe 'frustrate' is a bit over the top, but it's something I feel the need to point out:

/// <summary>
/// Customer object
/// </summary>
class Customer
{
    /// <summary>
    /// Customer name
    /// </summary>
    private string Name;

    /// <summary>
    /// Customer age
    /// </summary>
    private int Age;

    /// <summary>
    /// Customer constructor
    /// </summary>
    /// <param name="name">the ...<< MORE >>

BDD Style Naming Macro for Visual Studio


I've really jumped on board with BDD (Behaviour Driven Development) in the past week.  I've been reading about it for awhile, but it wasn't until I started actually doing it on a side project of my own that I've really begun to realise how good it can be.  The natural language naming style of the 'specs' (not 'tests' anymore) in ...<< MORE >>

ReSharper Solution-Wide Analysis


I re-installed ReSharper today after living without it for a few months.  The reason I originally uninstalled it was sort of an experiment to see if I had become too dependent on it.  I wanted to be sure that I could work without it, for the scenarios where I jump on other people's machines to code, or ...<< MORE >>

NHibernate: Getting started


I have been reading about NHibernate for about a year now and have been thinking about converting to it for many of those months.  I've always read little bits and pieces about it, but never found a really good article or guide about setting it up and useing it in a realistic example...until today

Karl Seguin's article ...<< MORE >>

Domain Driven Data Access Layer Example


In the past few days I have been refactoring a system that had no concept of an independent Data Access Layer (DAL).  All the code to access and manipulate the database was directly in the Domain objects.  I set myself the task to extract all this code and introduce a decoupled DAL.  There are a fair few articles on this ...<< MORE >>

The ALT.NET group


In the last six months I have become increasingly interested in the discussion and ideas being generated by the ALT.NET group, and thought it would be a good idea to spread the word a bit more if you are unfamiliar with it. 
<< MORE >>

The importance of setting goals in software development


Bob Slydell: You see, what we're actually trying to do here is, we're trying to get a feel for how people spend their day at work... so, if you would, would you walk us through a typical day, for you?
Peter Gibbons: Yeah.
Bob Slydell: Great.
Peter Gibbons: Well, I generally come ...<< MORE >>

Design Patterns Quiz


Just came across this site today: http://www.vincehuston.org/dp/patterns_quiz.html

Give it a go.  If you don't go as well as expected (like me) - blame it on the Friday arvo beers...
<< MORE >>

NAnt exec error handling


If you are executing an external program in your NAnt script and want to have a bit more control over when your the task is considered a success or failure, this little config snippet should help.

In this article I assume you are already familiar with NAnt.  If it is new to you, check it out here.

First thing you ...<< MORE >>

Threading: Class locks vs Object locks


I have to go back to my university days to remember the first multi threaded programs that I wrote.  I even took entire subjects devoted to Threading in .NET, in which we created, used and understood a vast range of threading utitlities such as Mutex, Semaphore, Bounded Channel, Exchanger, Latch, Read/Write Lock and Rendevous.  Working with multi threaded programs has ...<< MORE >>

Continuous Learning: There's always a better way


One of the things that has helped me become a better software developer over the past few years is to never become complacent with the way I do things. Always be on the lookout for a better way - read blogs, read forums, talk to your peers, sign up for technical newsletters and most of all - ...<< MORE >>

Visual Studio 2008 and Blend


Lately I have been playing around with the VS2008 Beta 2 and building an interactive media application using WPF.  I have found the VS2008 designer quite buggy when dealing with WPF windows/controls at design time, which led me to start using Microsoft Expression Blend in conjunction with VS2008 - so far this has been a much better development ...<< MORE >>

Truncate problems with Foreign Key Constraints


This one is a pretty simple one, but sometimes so am I, so I thought it'd be a good idea to document it and hopefully help some others out.

I recently ran into a problem when I was using the 'TRUNCATE TABLE ...' command on a table that contains foreign key references (using a SQL Server 2005 database).  After a little ...<< MORE >>

Using .NET reflection to help test legacy code


One thing that has been bugging me a bit lately, is how to handle private methods in legacy code (or even just plain old untested code).  I've always found that when I have to work with legacy code (and 3rd party libraries) that writing tests to confirm your understanding of it is a good thing and helps you become more ...<< MORE >>