Laumania.net

A Silverlight developers blog by Mads Laumann

Dynamically changing url/endpoint for WCF service in Silverlight 2.0

I just saw Andy Beaulieu's post about dynamically chaging the endpoint for a WCF service. It's a good post, I just wanted to share how I have solved this problem in my current project.

The problem with Andy's solution is that it not that dynamic.

  • What if the schema change (https)?
  • What if the port change in the final distination environment, so you can't use the "Use specific port" like in VS?
  • What if the path/url for the WCF service is different at the distination environment?

These are the problems that Andy's solution don't solve and I actually have in my current project. Well, I have some of them, but my solution here should solve all of the above.

First, besides my solution is able to change the url/endpoint dynamically, it's also able (or actually required) to take the path to the WCF in as a InitParameter. Please note, that we are taking a relative path from the root here. Which means something like /Services/PeopleService.svc. Somethings that start with a /. This tells the browser that we want to start from the root of the current domain.

So besides solving the problems stated above, my solution also shows how you can send in the relative path for the service as a InitParameter. It's not rocket science, but good to know.

Let's start in App.cs

private void Application_Startup(object sender, StartupEventArgs e)
        {
            //Insert InitParams into Global Resources
            foreach (string key in e.InitParams.Keys)
            {
                this.Resources.Add(key, e.InitParams[key]);
            }

            this.RootVisual = new Page();
        }

We start out by taking all the potential InitParams and put them into the global Resources for the application. You could also just pass the Dictionary to the Page class, but I'll do it like this in this case as I need to access the resources from different places in the application.

If we look in the web project in the HTML we see how and what we pass in here: Default.aspx

<asp:Silverlight ID="Xaml1" runat="server" 
Source="~/ClientBin/Dummy_WCF_Application.xap" 
MinimumVersion="2.0.31005.0" Width="100%" Height="100%"
InitParameters="DummyServicePath=/Services/DummyWCFService.svc" />

Ok, so now we have the initparams inside our silverlight application inside the resources. Let's use it to set the endpoint of our application. This can be done anywhere you like, in my case I just have a DataProvider class and this is where my code is: DataProvider.cs

I applogize up front for the code looking a little wierd. I had to press the code together and on new lines where it isn't logical or correct. It's only for the code to be able to fit to the width of the page here.

public class DataProvider
    {
        public event EventHandler<DataResult> GetDataCompleted;

        private WCFServiceClassName service;
        private const string DummyServicePathKeyName = "DummyServicePath";

        public DataProvider()
        {
            BasicHttpBinding basicHttpBinding = new BasicHttpBinding();
            EndpointAddress endpointAddress;

            if (App.Current.Resources.Contains(DummyServicePathKeyName )
                && !String.IsNullOrEmpty
(App.Current.Resources[DummyServicePathKeyName ] as string)) { string relativePath =
App.Current.Resources[DummyServicePathKeyName ] as string; string completePath = App.Current.Host.Source.Scheme
+ "://" + App.Current.Host.Source.Host + ":" + App.Current.Host.Source.Port + relativePath; endpointAddress = new EndpointAddress(completePath); service = new ChannelFactory<WCFServiceClassName>
(basicHttpBinding, endpointAddress).CreateChannel(); } else throw new Exception("Missing init param named: \"" + MapServicePathKeyName
+ "\" which is a relative Url to the WCF service."); } }

This is not a completely working sample, as I have removed curtain things to keep it simple. The most important part if the code above is actually this:

string relativePath = App.Current.Resources[DummyServicePathKeyName] as string;
string completePath = App.Current.Host.Source.Scheme 
+ "://" + App.Current.Host.Source.Host + ":" + App.Current.Host.Source.Port + relativePath;

Here we create the actual complete url used to access to WCF service with.

App.Current.Host.Source.Scheme: Returns https or http

App.Current.Host.Source.Host: Returns the host ex. "laumania.net" or "silverlight.laumania.net" if you were on a subdomain.

App.Current.Host.Source.Port: Returns the port number, typically 80, but could be anything actually. If the scheme were https the port would normally be 443 etc.

The code above would generate a url, if ran on this actual page:

http://laumania.net:80/Services/DummyWCFService.svc (The link wouldn't work here, as I don't have that service :) )

Now you should be able to make your own WCF service that you can change the endpoint of, in code.

Please let me say that Andy's solution isn't wrong and it will work just fine in most scenarios, it's just different than mine, so I wanted to share this solution too. Hope you find it usefull, maybe in combination with Andy's?

Enjoy!

Posted: Jan 06 2009, 21:13 by Mads | Comments (1) RSS comment feed |
  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Filed under: Silverlight

Farseer Physics Engine Simple Samples v4 release

I just upgraded the samples to run with the Farseer Physics Engine 2.0.1. I also removed the Demo6, which were empty at the moment and added some text different places.

Get the lastest release from CodePlex.com

I plan/hope to be able to make more samples which will get a little more advanced, still simple-advanced, but I will add a little more complexity to it. I will do it in the same way as up til now by having completely seperated projects that can run on them own. Well, this is the plan, let's see how it turns out.

Posted: Dec 31 2008, 14:27 by Mads | Comments (0) RSS comment feed |
  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Happy new year

fireworks31-12-2008, the last day of the year. It's time to look back and make status of the year that is about to end.

What have we accomplished, what have happened, any major milestones, how does the new year look like?

Silverlight 2.0

For me, technical-wise, the release of Silverlight 2.0 have been the greatest thing this year. To me, Silverlight looks like a winner on the RIA front and I think we will see many new good Silverlight applications out there in the new year, which will increase the market penetration for Silverlight. Right now the market penetration, based on data from RiaStats.com, is about 11,12% for Silverlight 2.0. Let's see what it's like at the end of the new year. My guess, around 30 %, but let's hope for more.

Read more about what Silverlight 2.0 have done in 2008 and what we can expect from Silverlight 3.0 in 2009 at ScottGu's blog.

Working with Silverlight

Another big thing for me is that I now work fulltime on a Silverlight project at my day job. Well, it's only for a short period (some months) of time, but it's very fun, interesting and I learn alot. I were giving an one-hour brief introduction to all developers at work in November and after that I were asked to join a new project where Silverlight good be used. So thats where I'm sitting now and it's great to get some "real-world" experience with Silverlight.

Let's hope 2009 will bring more Silverlight projects in my direction - both hobby and real projects.

See you in the new year - watch the fireworks! :)

Posted: Dec 31 2008, 11:38 by Mads | Comments (0) RSS comment feed |
  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Filed under: Everyday life | Silverlight

Fluent Animations in Silverlight

Today I saw this blog post over at Nikhil K's blog about a "jquery-like" framework for Silverlight to help developers build professional UI. Actually this "jquery-like" framework is an extension for the Silverlight.FX, which is created by Nikhil K. The "jQuery-like" framework is build by Daniel Crenna.

Here is some code from the sample that comes with the release.

 // Slide Script
 Animator.WithNew(horzSlideContainer)
.WithEasing(EffectEasing.QuadraticInOut)
.Slide(SlideMode.Left)
.For(1.Seconds())
.Play();

The code above "slides" this grid, defined in XAML.

<Grid x:Name="horzSlideContainer">
   <Image Source="/MS.net.png" />
   <Image Source="/Silverlight.png" />
</Grid>

If you want to know more about this, visit the blog listed below. You can also find download links for the release at the blogposts.

Resources:

Nikhil K's blogpost - Fluent Animations in Silverlight

Daniel Crenna - Composing animations with Silverlight.FX

Enjoy and happy coding :)

Posted: Dec 20 2008, 14:58 by Mads | Comments (4) RSS comment feed |
  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Filed under: Silverlight

Animation Along a Path for Silverlight

Sometime ago I posted a topic on Silverlight.net about how to get an control to follow a Path. This resulted in many good suggestions, but not actually what I wanted.

Today I then saw this on my RSS reader:  Animation Along a Path for Silverlight, which sounded exactly like what I was looking for. I just wanted to give you, readers, an headsup on this one. I haven't actually tried this out yet, but it looks very promissing.

Enjoy!

Posted: Nov 10 2008, 20:29 by Mads | Comments (0) RSS comment feed |
  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Filed under: Silverlight

Silverlight Tools RTW released

Oh my god, it's the big release day today. Finally the RTW version of the Silverlight Tools were released. To be honest, I can't see the big changes when looking at the feature list, but there must be something, just not that clear I think.

You can see more here at this post: http://silverlight.net/blogs/msnow/archive/2008/10/28/silverlight-tools-rtw-released.aspx

Posted: Oct 28 2008, 22:22 by Mads | Comments (0) RSS comment feed |
  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Filed under: Silverlight

Fullscreen samples for Silverlight

I have now been searching for some good fullscreen Silverlight 2.0 final release samples, but with out luck. Therefore I created this thread on silverlight.net to find out. I got good responses and made this little project with two ways of doing it.

You can download the project here as a zip file

The project includes two ways of doing "fullscreen" applications.

You choose between the two different ways by commenting in the one you like in the App.xaml.cs file.

[code:c#]

this.RootVisual = new Fullscreen1();
//this.RootVisual = new Fullscreen2();

[/code]

Fullscreen1 - Shows how to make an application fullscreen/browser by setting "styles" on the controls to make them resize properly.
Fullscreen2 - Shows how to scale up your application by code.

It's two different ways of doing it as I see it and is used in different scenarios.
The way it's done in Fullscreen1 will typically be used for "application", meaning an application with a "static" layout.
The way it's done in Fullscreen2 is more for "games", as it scales the hole application up, which can constain lots of different dynamic controls at the time.
Well, this is just the way I see it at the moment please let me know if you think otherwise, I could be wrong. Laughing

Posted: Oct 28 2008, 22:01 by Mads | Comments (0) RSS comment feed |
  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Filed under: Silverlight

Silverlight Toolkit released

The Silverlight Toolkit has been released at Codeplex.com. This kit is similar to the Ajax Control Toolkit for ASP.NET, as it extends existing controls and add totally new ones to the framework.

Get Silverlight Toolkit from here

See some samples of the charts here

Or here to see samples of controls

Posted: Oct 28 2008, 20:08 by Mads | Comments (0) RSS comment feed |
  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Filed under: Silverlight

Bubbles - Source released

I have cleaned up the source for Bubbles a little and am now releasing it for you to enjoy. The source is released as-is, and some encryption/security code have been removed to keep my hiscore as correct as possible.

Get the source from here

 

Posted: Oct 22 2008, 20:51 by Mads | Comments (0) RSS comment feed |
  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Filed under: Silverlight

Silverlight game "Bubbles" - on www.silverlight.net

This Wednesday I submitted my first Silverlight 2.0 (final release) game to Silverlight.net late at night. Already the next day it was up there, great! Laughing 

The basic concept in the game is to "pop" as many bubbles as you can in 30 seconds, pretty simple. The best thing is that it also has a hi-score, which means that you can fight to become the "Bubble Master aka #1". The game is a complete rewrite of a similar game I did in Beta 1 and Beta 2.

You can play the game directly on my site here: http://silverlight.laumania.net/bubbles/

Or you can get to it through Silverlight.net here: http://silverlight.net/community/gallerydetail.aspx?cat=sl2

I'm planing to release the source code for it, I just wanted to add some minor fixes and changes to the game before that. So you should be able to find the source on my site here, and on Silverlight.net in some time.

Enjoy!

Posted: Oct 17 2008, 17:02 by Mads | Comments (0) RSS comment feed |
  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Filed under: Silverlight