XBAPs Part 2: Consuming WCF services from an XBAP

Introduction

One of the big improvements to XBAPs in .NET 3.5 is the ability to consume WCF services. Nowadays Service Oriented Architectures (SOAs) are very popular and used almost everywhere. This all makes sense, as SOA is very scalable, and for many cases it is the best approach to take for a project; so why not enable SOA for XBAPs as well. To tell you the truth, you could consume WCF services from XBAP in .NET 3.0, yet the XBAP had to have Full Trust, and not Partial Trust, which usually spells trouble. Anyway we don’t have to worry much about this now because .NET 3.5 enables XBAPs to consume WCF services even in Partial Trust. Read more about this and it’s limitations here.

Setting up our workspace

Ok so let’s start by creating a simple WCF service. Open Visual Studio and create a WCF service by clicking File > New > Project and then select Web > WCF Service application

NewWCF

I am not going to cover the basics of how to create a WCF service, yet by default the WCF Service Visual Studio template creates a simple service for us already (called IService). So yes, if you hit F5 and run the service you will already have a service up and running. Once we have our simple service let’s create a Simple XBAP in the same solution (in order to do this please refer to my previous post).

Now that we have both the WCF service and the XBAP how are we going to make them communicate? Well, thanks to Visual Studio this is super simple. All you have to do is Right click the XBAP project, select the “Add Service Reference” option and a Dialog will pop up:

AddServiceReference

Please Note: If you do not run the service you will get an error while expanding the Tree nodes of the service in this Dialog. If the service is not running Visual Studio cannot get the Metadata of the service, since this is just another EndPoint of the service. In order to run the service just Right Click on Service1.svc and select View in Browser.

Ok, now that we added the Service Reference, Visual Studio will go to work and generate a Proxy class for us. We will use this proxy class to communicate with the WCF service. By default the Proxy class is called <Name of service>Client so in our case it will be called Service1Client. This proxy class will have a method for every OperationContract we have in our service. You can also choose to create an Async version of the service. In order to do so just right click the Service1 (in the XBAP project that got created when we added the Service Reference) and select Configure Service Reference.

ChangeTheServiceToHaveASYNC

In this dialog check the Generate Async operations checkbox and Visual Studio will add an async version of the OperationContracts for you (Thank you VS… we love you :) )

The first error you’ll see

Ok now that we have everything set up lets do a simple button in our XBAP and in the Click event handler call a method. Something like this….

   1: private void Button_Click(object sender, RoutedEventArgs e)
   2: {
   3:     Service1Client client = new Service1Client();
   4:     MessageBox.Show(client.GetData(1));
   5: }

Let’s hit F5 and run our XBAP and see what happens.

AND BAMMMM a big ugly exception is thown.

InvalidOperationException

The WSHttpBinding with name WSHttpBinding failed validation because it contains a BindingElement with type System.ServiceModel.Channels.SymmetricSecurityBindingElement which is not supported in partial trust

which in VS looks like this

WCFError

Yet I promised you guys that WCF is supported in Partial trust XBAPs…. And it is, it’s just that the default WCF EndPoint is not :) By default a WCF Service has it’s EndPoint set up as WsHTTPBinding which is not supported in Partial trust XBAPs. In order to fix this Right click the app.config of your service and select “Edit WCF Configuration” (if this option is not available go to Tools > WCF Service Configuration Editor then select open and specify the path of your App.config. Once you have the app.config opened in the editor Expand the Bindings Node and select the node under that. Open the Security tab and choose None as security level.

ChangeSecurityLevelOfBinding

Once that’s done run the app again.

The Second error you’ll see

And this time it’s a SecurityException saying

Request for the permission of type ‘System.Net.WebPermission, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089′ failed.

SecondError

In order to fix this Go to the XBAP project Properties and select Debug tab. Select the Start External Program option and enter the following exe path “C:\WINDOWS\System32\PresentationHost.exe” and in the Command Line arguments text field enter the following

-debug <Path of XBAP> –debugSecurityZoneUrl <URL of service>

in my case it was

-debug “D:\net resources\My Blog\XBAPAndWCF\XBAPClient\bin\Debug\XBAPClient.xbap” -debugSecurityZoneUrl http://localhost:53265/Service1.svc

[Update]

Thanks to Mark Smith I discovered a new an easier way of doing this. All you have to do is

1) Open XBAP project properties

2) Select Security tab

3) Advanced button

4) Check “Grant application access to site of origin” (should be checked already)

5) Type in the URL into the textbox where the web service is running

Security

We have to do this so that we set up an envirorment for the XBAP just like it is running from the same URL of the service.

Conclusion

And there you have it. Now you should be armed and ready to start doing XBAPs that consume WCF service.

Download sample application

XBAPs Part 1: An introduction to this Gem

Introduction

XBAP XBAPs (XAML Browser Application) are a really cool feature that was introduced in .NET 3.0 together with WPF. Well you can say that XBAPs are as such part of WPF because at the end of the day XBAPs are just WPF applications that can run inside the browser. One important thing to state here is that XBAPs and Silverlight have nothing to do with each other. XBAPs are full .NET enabled WPF applications, while Silverlight is a “cut down version of WPF” that runs as a browser plugin. In order to run a Silverlight enabled website you need to download the Silverlight installer(that is really really small approx 4.3 MB), if you want a client to run a XBAP the client has to have .NET installed on his machine (the Full .NET or Client Profile).

Before we continue I would like to point out that this post is just an introduction to XBAPs. I will have Part 2, Part 3 and Part x that should be much more interesting to follow…. Yet if you never heard of XBAPs you should continue reading this post (and try not to fall asleep :P).

What does your Client need?

As I said above, in order for a client to run your XBAP he/she needs to have .NET framework installed. It’s as if the client is going to run a Windows application. XBAPs got a set of improvements in .NET 3.5 such as support for Firefox (read more here) and also support for consuming WCF service in partial trust mode (with some limitations of course but still it’s good, read more here). So yea if you are planning on using XBAP’s I would seriously consider .NET 3.5.

To summarize your Client needs to have .NET installed and he can use the XBAP in Internet Explorer or if he prefers (and you are using .NET 3.5) Firefox.

Some things to keep in mind …

XBAPs run in the browser and give an impression of being a normal website (super cool website I must say :P) while instead they are just actual apps that never jump out of the browser, yet there is more to it… XBAP’s run in a “sandbox”, basically they run as Partial Trust by default. What this means is that some of the things that you would assume would work in a WPF app would not work in an XBAP, for example Reflection (and also the new 3.5 SP1 feature Effects :( ). Of course there are workarounds. You can run XBAPs as Full trust mode and we will discuss how to later on in this article.

Building a simple XBAP

So let’s start having a look at how one can build an XBAP. In order to create a new XBAP project you have to do the usual stuff, so go to File > New > Project and select WPF Browser Application

Step1

Now that we created the project, let’s have a look at what Visual Studio created for us

Step2

So as you can see by default Visual Studio created more or less the same stuff it would have created for a WPF Application yet instead of using a Window it created a Page. This is very important because a Page was designed for Navigation (the back and forward button of the browser). I will discuss Navigation more later on in this article. Besides this Visual Studio also created a pfx file. The pfx file is a way how to sign the exe, by default Visual Studio creates a pfx file that it uses to sign the Click once installer for the XBAP.

Signing

The image above shows how Visual Studio generated that pfx file. Please not that there is the Computer name and username inside the pfx files (which obviously is not something you want to give to your client). Charles Perzold discusses this in more detail here.

Ok so now that we know what Visual Studio created for us (thanks VS we love you) we can test our XBAP app (yea we can already run it :) ). In order to see something on screen lets just create a button in the Page1.xaml and press F5 to run.

   1: <Button Content="My First XBAP :) " Height="50" Width="150"/>

FirstApp

Yahhooo we have everything figured out don’t we :) well there are a couple of other things we need to have a look at.

Conclusion

If you are a WPF developer you will be developing XBAP in no time. There are a couple of things you must keep in mind, like Code Access Security and other tricks like how to consume WCF service…. In my next posts I will cover these things and make sure that I pass on my knowledge (knowledge that came from a lot of frustration and hours of head banging on the table)