Today’s the day!  Tuesday at DevConnections in Las Vegas, Scott Guthrie just announced the ‘launch’ of Silverlight 4.  We wanted to take the opportunity at DevConnections to let a large audience of our customers online/offline know that we’re done and shipped Silverlight 4.  As of today it’s now available for you to download/use.  Here’s some helpful quick update information for you:

What’s New in Silverlight 4?

Rather than cut/paste what I’ve already said here, I recommend the following reading:

These two posts are full of detailed information with links to tutorials and videos to help you get started.  Since the RC (which was released middle March), not much has changed from an API perspective so you should be good to go getting started using the above information.  One thing that has changed in the tools is that for XAP signing, there is a UI support for enabling the tool to help you select a certificate, etc.  You can still use my post-build method though.

Downloading Silverlight 4

The availability of Silverlight 4 will be approximately 10:00 AM PST on Thursday 15 April 2010.  I suggest you prep now by getting the Visual Studio 2010 release that was launched/available yesterday.

The goods:

That’s my own personal minimum list.  VS and SL4 tools are the minimum required to get started doing development.

Wait, what’s this RC stuff?

To be clear, Silverlight 4 has released.  This is RTW (release to web).  It is the version 4 of Silverlight.  Shipped.  Done.  Finished.

The tools (namely SL4 tools, RIA Services and Blend) are in their ‘release candidate’ mode.  I’ll spare you the gory details, but remember that these tools teams need SL4 to be *done* before they can be done.  SL4 is a dependency for them.  These tools are release quality though and I’d recommend using them.  Their final versions will come soon enough and will be a minor update.

What about RIA Services?!  You don’t consider that to be valuable?!

Actually I do.  But if you’ve installed the Silverlight 4 Tools, then you already have it!

This seems to be some confusion to many and perhaps because of how we present the information in an effort to be complete.  If you are a developer, install Silverlight 4 Tools.  After this installation completes you will have:

  • Silverlight 4 developer runtime
  • Silverlight 4 SDK
  • Visual Studio patch, debug tools and project templates
  • WCF RIA Services RC

installed.  No need to run the SDK or RIA Services installers separately.  Adding the Silverlight Toolkit to this provides you with more controls to leverage in your applications as well (charting, ContextMenu, etc.).

Hey, what about the Windows Phone 7 developer tools?

If you need to continue doing Windows Phone 7 development, stick with the Visual Studio 2010 Release Candidate for now!  The updated CTP of the Windows Phone developer tools is not quite done yet.  Information about updated tools availability will be forthcoming on these tools.  Stay tuned.

Can I keep VS2008 and VS2010 on the same machine?

Yes you can.  Visual Studio 2008 and 2010 can co-exist on the same machine.  Obviously there are some differences to what the tools can do with regard to Silverlight.  There are two distinct differences I like to call out:

  • VS2008 cannot be used for Silverlight 4 development.
  • You can only have one version of WCF RIA Services installed on the machine.

For the latter, you may be asking “huh?”  There is a version of RIA Services for VS2008.  There is also a version for VS2010.  Unfortunately RIA Services cannot co-exist in two versions.  You have to pick one.  And the VS2008 one is only for SL3 and only supported until December this year.  I recommend moving on from that…it’s not going to be developed any more…essentially the PDC09 version was the last revision there.

Other resources

If you need other resources, be sure to check the Silverlight Community Site for details on things like the stand-alone documentation file, Mac platform developer build (for debugging if needed) and other resources.  Be sure to check out the Silverlight 4 videos if you haven’t yet as well!  I’ve gotten a few questions so I’ll emit some resources here:

Hope this helps find things easier.

What about the future versions…

Man, give us a break!!!  But seriously make your opinion known on http://silverlight.mswish.net for features.  Be specifically broad ;-).  What I mean is “Fix Printing” doesn’t help, but “Enable automatic paging in printing” is better.

Hope this helps!


I recently got an inquiry to my Microsoft Translator sample on if this would work with the Silverlight in the Windows Phone 7 SDK.  I hadn’t tried it before, so I created a sample Windows Phone 7 application and copied the code over.  I used a basic UI to mock up the similarities:

Translator phone sample screenshot

And then clicked the button.  The text translated fine, but no audio.  I didn’t get any warnings that the WaveMSS code sample I was using wouldn’t work.  Then I remembered about XNA.

NOTE: I actually think this is a bug in PCM audio and MediaStreamSource and have been having a dialog with the team about it.

In Windows Phone 7 your Silverlight applications can use some XNA Game Framework APIs.  A big component of games is audio!  Enter SoundEffect.  I added a reference to Microsoft.Xna.Frameowkr and changed my OnSpeakCompleted from:

   1: void OnSpeakCompleted(object sender, TimHeuer.Silverlight.SpeakCompletedEventArgs e)
   2: {
   3:     WaveMSS.WaveMediaStreamSource mss = new WaveMSS.WaveMediaStreamSource(e.AudioTranslation);
   4:     PlayMe.SetSource(mss);
   5: }

to:

   1: void OnSpeakCompleted(object sender, TimHeuer.Silverlight.SpeakCompletedEventArgs e)
   2: {
   3:     SoundEffect se = SoundEffect.FromStream(e.AudioTranslation);
   4:     se.Play();
   5: }

Notice it is still 2 lines of code :-).  I don’t need a MediaElement for the audio palyback because I can use the same libraries that XNA uses for audio (and in some instances this will be better for you for looping audio, etc.).

Very cool that Silverlight and XNA can share some libraries in a single application!

Next week on 13-April at 8:00 AM PST Scott Guthrie will deliver a keynote address for the DevConnections conference being held in Las Vegas, NV.  Scott will provide updates on the progress made in Silverlight 4 and will provide the details of availability of the developer tools, runtime and other news.

Mark your calendars and return to the Silverlight community site to tune into the LIVE event.  After the keynote, Channel 9 will be hosting interviews with Scott and other key members of the Silverlight and Expression teams.  I’ll be hosting one of those interviews as well and look forward to your submitted questions via Twitter to @ch9live.

You’ve written your service.  You’ve written your Silverlight application.  You Add Service Reference to your application and got the client proxy code.  Your app ‘works on your machine’ and you push it out. 

FAIL.

NotFound.

Crap.  You forgot that your service reference had your local URI endpoint in there and when you moved it to staging and/or production it failed.  You start cursing Microsoft and the Silverlight team and add to the threads in the forums or perhaps initiate a new wishlist item for the team and throw it out on Twitter and encourage votes.

It seems this is still a common frustration and people are trying to solve it in different ways.  I’m going to throw out what is my preferred mechanism and add some additional tips and tricks here that hopefully some are using.

The Setup

Here’s the setup.  You have a Silverlight application and a web service.  To keep it simple I started with File…New Silverlight Application – kept the web project there. I added a Silverlight-enabled WCF Service to my web project with this following code:

   1: [OperationContract]
   2: public string SayHello()
   3: {
   4:     // Add your operation implementation here
   5:     return "Hello World [DEVELOPMENT]";
   6: }

I then added 2 more empty ASP.NET Web Application projects to my solution, added a single Silverlight-enabled WCF Service to each one of them with the identical code, changing only the return string to PRODUCTION or STAGING to differentiate the response.  I called one project ProductionService and the other StagingService to simulate a production and staging environment.  I then added (because it wouldn’t work otherwise in my test setup) a clientaccesspolicy.xml file to the production/staging service projects.

NOTE: You may not have to do this clientaccesspolicy.xml setup.  This was only because I deployed the Silverlight app only to one web project, not others.  This may not be required for you.  See step below on Silverlight App in Same Web Project as Service section on why.

I’ve added some rudimentary XAML to the app to basically show the current default service that will be used (with the option to change it) and the response code:

Sample application snapshot

Now to explain what I’ve done/recommend.

The ServiceReferences.clientconfig ‘magic’

When you add a service reference via the Add Service References option in Visual Studio, you get a new file in your Silverlight application called ServiceReferences.clientconfig.  This contains the binding and endpoint configurations for the service you just referenced.  Here’s where you can change some things up.

By default it adds the configuration for the literal endpoint you just referenced (think absolute URI here…most of the time in development this may be localhost).  The file isn’t fixed though and you can add your other configurations there as well.  Here’s my initial updated config file with the addition of the production and staging URI endpoints.

   1: <configuration>
   2:     <system.serviceModel>
   3:         <bindings>
   4:             <customBinding>
   5:                 <binding name="CustomBinding_HelloWorldService">
   6:                     <binaryMessageEncoding />
   7:                     <httpTransport maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" />
   8:                 </binding>
   9:                 <binding name="StagingServiceBinding">
  10:                     <binaryMessageEncoding />
  11:                     <httpTransport maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" />
  12:                 </binding>
  13:                 <binding name="ProductionServiceBinding">
  14:                     <binaryMessageEncoding />
  15:                     <httpTransport maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" />
  16:                 </binding>
  17:             </customBinding>
  18:         </bindings>
  19:         <client>
  20:             <endpoint address="http://localhost:40473/HelloWorldService.svc"
  21:                 binding="customBinding" bindingConfiguration="CustomBinding_HelloWorldService"
  22:                 contract="HelloServices.HelloWorldService" name="CustomBinding_HelloWorldService" />
  23:             <endpoint address="http://localhost:40848/HelloWorldService.svc"
  24:                 binding="customBinding" bindingConfiguration="StagingServiceBinding"
  25:                 contract="HelloServices.HelloWorldService" name="StagingServiceBinding" />
  26:             <endpoint address="http://localhost:40849/HelloWorldService.svc"
  27:                 binding="customBinding" bindingConfiguration="ProductionServiceBinding"
  28:                 contract="HelloServices.HelloWorldService" name="ProductionServiceBinding" />
  29:         </client>
  30:     </system.serviceModel>
  31: </configuration>

Notice the endpoint names: CustomBinding_HelloWorldService, StagingServiceBinding, ProductionServiceBinding.  The first was created for me by VS – hence the awesome hugely unhelpful name :-).  By default, if you added this code in your Silverlight application:

   1: HelloWorldServiceClient client = new HelloWorldServiceClient();

Then it will be using the default endpoint it creates (which would only be one of them unless you add custom ones like I did above).

Initializing the Service with different endpoints

Now that you know that the client config file can have multiple configuration endpoints, how would you use them?  Simple.  If you take a look at the proxy code that gets generated for you when you Add Service Reference (this is in the Reference.cs file when you use the ‘show all files’ option in VS) you’ll notice that the constructor for HelloWorldService is overloaded to allow and endpoint configuration, or optionally explicit binding/endpoint address information.  It’s the former that will make this easier for you.

Take our above ServiceReferences.clientconfig modifictation.  Let’s say we wanted to work with the staging service, we could now simply instantiate with:

   1: HelloWorldServiceClient client = new HelloWorldServiceClient("StagingServiceBinding");

In fact, I might argue that you should never use the default constructor.  Being more explicit leads to easier code to read/track in my opinion.  You don’t expect to be on this project for the rest of your life do you?  I didn’t think so…make it easier on the next developer that has to come behind you and fix your bugs enhance the code to add functionality and be explicit.

Being Dynamic about your Endpoint Initialization

Obviously you don’t want to hard-code various endpoint names in your instantiation of the service proxies.  In fact, you may be struggling because you may push your code out via automated build servers and you don’t want to have to build the XAP, then change something, blah blah.  This is where conditional compilation can help.  For instance, here’s how I have the code in this project:

   1: string _endpointName = "RelativeBinding";
   2:  
   3: #if PRODUCTION
   4:     _endpointName = "ProductionServiceBinding";
   5: #endif
   6:  
   7: #if STAGING
   8:     _endpointName = "StagingServiceBinding";
   9: #endif
  10:  
  11: HelloWorldServiceClient client = new HelloWorldServiceClient(_endpointName);

Now I just have to make sure that my compile tasks add those conditional flags.  This is relatively simple.  You could use Visual Studio’s Configuration Build Manager to create new profiles, or you could also customize an MSBuild task to append those constants.  It is simple and there are plenty of resources to help you here.  For my project I simply customized (added) new configuration profiles in Visual Studio and have been manually switching them to test.

Silverlight App in Same Web Project as Service

But wait, there’s more!

If you have a simpler solution where your service is being served up in the same web application/site as your Silverlight application, then you have a simpler solution using Silverlight 4.  Just to clarify, what I mean by this is that your app - let’s say http://foo.com/clientbin/myapp.xap is a part of the same web application http://foo.com which serves up the service you are calling http://foo.com/helloworldservice.svc. 

Good news for you if you are using Silverlight 4.  You can now use relative path information for service references!  Let’s say your XAP is in /ClientBin/MyApp.xap and your service is in the same root location as the ClientBin folder at /HelloWorldService.svc.  This means that “../HelloWorldService.svc” will work!  I’d recommend still being explicit in your code so that it helps those come behind you.  In my client config file I added a “RelativeBinding” configuration:

   1: <endpoint address="../HelloWorldService.svc"
   2:                 binding="customBinding" bindingConfiguration="RelativeBinding"
   3:                 contract="HelloServices.HelloWorldService" name="RelativeBinding" />

and then in my code I can initiate it like I’ve done above using the named configuration.  But notice in the configuration I used ../HelloWorldService.svc as the URI for the service endpoint.  Assuming my staging and production follow the same paths, I don’t have to worry about conditional compilation, etc. and can push these out in the various environments.

Code Specific Endpoints to hide your configurations

In theory if your solution can use the relative binding scenario described above there is not too much to worry about as far as revealing too much.  However if you are using the other method about adding multiple configurations to your ServiceReferences.clientconfig file you should be aware that this file is compiled as a resource and could show others your staging/dev URIs that you may not want.  This might be a subtle by-product of getting added benefit to making the process easier though but you should be aware of these capabilities since tools like Silverlight Spy and Reflector could enable someone to look at your resource files.

You could by pass the client config named endpoints mechanism and use conditional compilation with explicit code-created endpoints.  Using the conditional statements as shown above, only the code that meets the condition will be compiled into IL.  Thus decompilation doesn’t show the other #if options.  So in Reflector or other tools you’d only see what was output.  Given this you could be even more aboslutely explicit and do something like this:

   1: BasicHttpBinding binding = new BasicHttpBinding();
   2: EndpointAddress endpoint;
   3:  
   4: #if PRODUCTION
   5:     _endpointName = "ProductionServiceBinding";
   6:     endpoint = new EndpointAddress("http://myproductionserver.com/HelloWorldService.svc");
   7: #endif
   8:  
   9: #if STAGING
  10:     _endpointName = "StagingServiceBinding";
  11:     endpoint = new EndpointAddress("http://mySTAGING.com/HelloWorldService.svc");
  12: #endif
  13:  
  14: HelloWorldServiceClient client = new HelloWorldServiceClient(binding, endpoint);

Using this conditional compilation method you’re doing a bit more ‘hard-coding’ than relying on changing configuration and not code, but it might be more suitable to your liking.  Sure, this can still be decompiled, but it would only reveal the endpoint you already will be using (which anyone could see anyway just watching HTTP traffic).

Summary

Managing your endpoints doesn’t have to be difficult.  Hopefully these simple ways give you an idea of the options you can use.  Can we do better in helping manage this easier?  I think so.  And I know we’re thinking about it as well.

You can download my complete code sample for this application here: ManagingServiceEndpoints.zip.

Hope this helps!  (oh and hat-tip to Shawn who wrote about this for SL2…I’ve just expanded on the same idea going deeper and providing some updates for Silverlight 4)

Microsoft MVP Logo

Congratulations to all the new/returning MVPs from all competencies, but I wanted to call out the newly awarded Silverlight MVPs for this latest round.  Please join me in congratulating them:

And a specific call out also to Colin Blair (@SLColinBlair).  Colin’s work in the WCF RIA Services space gained him recognition from the connected systems division.  Since Colin is around our Silverlight circles as well, I wanted to give him a shout out!  He also had one of the best free upgraded suites at MIX10…which he aptly named the RIASuite – and an impromptu design review for WCF RIA Services took place one night with Brad Abrams, Nikhil Kothari and others.

Congratulations again to all these folks for your deserving work in your local and broad communities.  The Silverlight community expects high things of you!