got net?

Kevin Hazzard's Brain Spigot

About the author

Welcome to Kevin Hazzard's blog.
E-mail me Send mail

Recent posts

Recent comments

Authors

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2010

MSDN Roadshow - What's New in Silverlight 2?

Here are my slides and sample code from the presentation I did on November 20, 2008 in Hampton, Virginia.

MSDNRoadshowSilverlightDemos20081120.zip (2.82 mb)

Whats new in Silverlight .pptx (5.80 mb)


Posted by kevin on Thursday, November 20, 2008 7:02 PM
Permalink | Comments (1) | Post RSSRSS comment feed

Enumerating the Silverlight AssemblyManifest XAML File

Go to the Silverlight home page.

From time to time, you may need to discover dynamically which .NET assemblies were packaged with the current Silverlight application. Here's a snippet that might help. It enumerates the AssemblyParts in the main XAP package and puts the names and source paths into a dictionary.

 

var assemblyParts = new Dictionary<string, string>();
try
{
   var settings = new XmlReaderSettings
      { XmlResolver = new XmlXapResolver() };
   var reader = XmlReader.Create("AppManifest.xaml", settings);
   try
   {
      reader.MoveToContent();
      while (reader.ReadToFollowing("AssemblyPart"))
      {
         var name = reader.GetAttribute("x:Name");
         var source = reader.GetAttribute("Source");
         assemblyParts.Add(name, source);
      }
   }
   finally
   {
      reader.Close();
   }
}
catch (Exception ex)
{
   Debug.WriteLine( ex.Message );
   throw;
}

 


Categories: C# | Silverlight | Software Dev
Posted by kevin on Wednesday, August 06, 2008 6:00 AM
Permalink | Comments (0) | Post RSSRSS comment feed

ApplicationDefinition Error in Silverlight

Go to the Silverlight home page.

I work on a lot of different development machines. I have a laptop with half a dozen Windows and Linux virtual machines on it. I have several virtual and physical machines at home and at the office. Keeping them all in sync is not easy. Recently, I've been doing a lot of Silverlight development. I ran into the following error when opening a Silverlight 2 Beta 2 project on one of my development servers:

Library project file cannot specify ApplicationDefinition element

I had forgotten to update the Silverlight Tools for Visual Studio to the latest Beta release. Installing the latest Silverlight beta tools for Visual Studio fixed the problem. Searching the net with this error didn't turn up enough useful information so I thought I should blog about it. Hopefully the search engines will guide others here for this potential solution. 


Posted by kevin on Tuesday, August 05, 2008 10:32 AM
Permalink | Comments (4) | Post RSSRSS comment feed

Accessing Web Services from Silverlight 2

I presented tonight (10 July 2008) to the Richmond .NET User Group. We had a pretty good turnout, I'm guessing 40 to 45 developers. I gave this same presentation at my office today as a dry run and as a training opportunity within the company. It's so good to see the developer community eager to learn. I've attached my slides and the three demonstrations projects I used in this post. I'll be giving this same presentation to the Charlottesville .NET User Group next Thursday (17 July 2008). The abstract we put on both user group websites follows:

Silverlight is a client-side technology. So it’s not really a part of your SOA strategy, right? You may want to think twice about that. SOAP and WSDL support are coming to the web desktop via Silverlight. And Silverlight has good client support for REST+ JSON/POX and RSS/ATOM-based web services, too. During this discussion, we’ll dive into data serialization, security and cross-domain access policy capabilities inside Silverlight 2 Beta 2. We also talk about the nuances and pitfalls of provisioning your web services for an Internet audience. This presentation will be heavy on coding, demonstration and interactive discussion.

Powerpoint Presentation (289KB)

Twitter solution showing how to invoke a cross-domain RESTful service by way of an in-domain SOAP service bypassing the cross-domain access policy problem. (842KB)

REST solution showing how to create RESTful services in WCF and how to consume RESTful services in Silverlight (307KB)

Silverlight syndication solution showing how to consume cross-domain RSS and Atom feeds using the SyndicationFeed class. (11KB)


Posted by kevin on Thursday, July 10, 2008 10:39 PM
Permalink | Comments (3) | Post RSSRSS comment feed

Silverlight Assembly Handling

Hanu Kommalapati is blogging again! Awesome. In his latest post, he addresses the issue of assembly handling by Silverlight. With security at the forefront of everyone's mind these days, you need to be able to relate to others how Silverlight handles XAP files and their contents on the web desktop. In his post, Hanu explains how this is done. Scroll down and read his reply to a reader's question to fully understand. In essence, he says, "As of beta1, [the XAP files] won't leave any footprint on disk." This is good to know when you are selling the idea of using Silverlight to your clients or your management team.

Posted by kevin on Tuesday, May 20, 2008 9:16 PM
Permalink | Comments (0) | Post RSSRSS comment feed

Silverlight Web Services Slides and Sample Code from NOVA 2008.1 Code Camp

This was the first time I gave this talk but the attendees seemed to be very receptive. The concerns about security in Silverlight, especially related to the protection of Intellectual Property (IP) and account access are really on peoples' minds. Based on the thoughtful question I received here and in Roanoke 2 weeks ago, I have decided to dedicate a whole chapter in my new book to Silverlight security principles.

In this talk, I discussed the various methods by which a Silverlight application can access remote web services. We didn't have time to get into RSS/Atom syndication but I'll be sure to cover it in a future talk. We also discussed cross-domain policy, another hot security topic, as it turns out. I showed how to enabled a WCF web service for RESTful delivery and then showed how to consume SOAP-based services from Silverlight. We closed by looking at the use of an in-domain, SOAP-based WCF service to act as a proxy for a cross-domain RESTful service that does not allow cross-domain access by policy. Here are the slides and sample code:

WCFRESTDEMO20080517.zip (18.89 kb) - sample code that shows how to make a WCF service RESTful; a Silverlight control is included that demonstrates how to use it; there is also a Digg.com downloader that demonstrates cross-domain functionality from Silverlight.

Twitter20080517.zip (25.59 kb) - sample code that shows how to consume an in-domain SOAP-based service from Silverlight; that SOAP-based service is really a proxy to a RESTful service at Twitter.com. And since Twitter.com's cross-domain policy doesn't allow access from my domain, this example shows how the server-side WebClient class can be used to circumvent the policy limitation.

Silverlight and WCF - NOVA Code Camp 2008.1.pptx (199.39 kb) - my PowerPoint slides from this discussion.


Posted by kevin on Saturday, May 17, 2008 4:42 PM
Permalink | Comments (0) | Post RSSRSS comment feed

Excellent Post from Hanu Kommalapati on Silverlight Deployment

Hanu Kommalapati has a fantastic blog post on Silverlight deployment from 5/11/2008 at http://blogs.msdn.com/hanuk/archive/2008/05/11/silverlight-for-the-enterprises-fundamentals.aspx. Hanu doesn't blog often but his stuff is very good. I think I'll write to him and encourage him to share more often.

Categories: Silverlight
Posted by kevin on Friday, May 16, 2008 12:22 PM
Permalink | Comments (0) | Post RSSRSS comment feed

Flow Diagram for Silverlight Cross-Domain Policy Checks

Last week, I blogged about Overcoming Cross-Domain Issues in Silverlight which recommended the use of a proxy service for making cross-domain calls. I'll be writing more about the security considerations of using that method in the coming days. For now, here's a flow diagram I created to better understand what the WebClient class in Silverlight is doing when making cross-domain calls. If you see any problems with my interpretation, I invite corrections by commenting on this blog post.


Categories: Silverlight
Posted by kevin on Friday, May 09, 2008 10:48 AM
Permalink | Comments (2) | Post RSSRSS comment feed

Silverlight Tip - Loading XAML Fragments Dynamically

Being able to instantiate XAML controls from text fragments at runtime is very convenient, especially when you have a lot of properties to set. Using a block of text to hydrate a new XAML control is often a much more compact and efficient alternative to dozens of lines of code that would be required in C#, for example. However, when you load XAML text up at runtime in Silverlight 2, there are a couple of things of which you should be wary. First of all, the string containing the XAML fragment must contain only one root element. And that node must include a namespace reference to http://schemas.microsoft.com/client/2007. If you forget to reference that namespace, you'll get a XamlParseException when you attempt to load the XAML.  As an example, look at the following C# code which creates a TextBlock object:

In this code, the XAML text fragment contains the necessary namespace reference and an attribute setting the Text property to "Hello world!" The XamlReader class in the System.Windows.Markup namespace is then used to load up the XAML text using the Load method which returns a System.Object. Since the root node of the XAML text was a <TextBlock/>, the resultant object is actually an instance of the System.Windows.Controls.TextBlock type. So the cast to that type succeeds, yielding a disconnected TextBlock.

I say that the new TextBlock is "disconnected" because just after it is created, it is not associated with any other UIElement. Controls are not really usable until they are connected to a canvas and ultimately to an application. Most XAML controls have a property called Children which is a UIElementCollection. That collection's Add method can be use to attach the new TextBlock and render it. Of course, casting the result of the Load method to a TextBlock isn't necessary. But to be attached via the UIElementCollection.Add method, you'll have to cast the Load result to the UIElement base class at a minimum.


Categories: C# | Silverlight
Posted by kevin on Monday, May 05, 2008 10:32 AM
Permalink | Comments (0) | Post RSSRSS comment feed

Overcoming Cross-Domain Issues in Silverlight

One of the easiest ways to deal with cross-domain issues in Silverlight is to write a WCF service that acts as a proxy for the calls you want to make. For example, in my Twitter control, I want to be able to load XML from the RESTful Twitter service at the following address:

http://twitter.com/statuses/user_timeline/wkh.xml

However, when invoking the RESTful Twitter service from a WebClient in Silverlight, I get the dreaded "Download Failure" error when I call DownloadStringAsync. This is because the Twitter.com site doesn't have an entry in its clientaccesspolicy.xml file for my test domain. And that's never going to happen, right? To solve this problem, consider exposing a WCF service from the site from which the Silverlight control emanated. The WebClient class can be used on the server side without the same cross-domain control being applied by Twitter.com. For the example outlined above, I called my service TwitterProxy. The ITwitterProxy couldn't be simpler:

using System.ServiceModel;

[ServiceContract( Namespace = "urn:gotnet:biz:Services:2008:05" )]
public interface ITwitterProxy
{
    [OperationContract]
    string GetUserTimeline( string user, int count );
}

The service implementation is also quite simple. It uses the WebClient just as I would from Silverlight (if I could):

using System;
using System.Net;

public class TwitterProxy : ITwitterProxy
{
    public string GetUserTimeline(string user, int count)
    {
        user = (user != null) ? user.Trim() : String.Empty;
        count = (count < 1) ? 1 : (count > 20) ? 20 : count;
        WebClient client = new WebClient();
        Uri uri = new Uri( String.Format("http://twitter.com/statuses" +
            "/user_timeline/{0}.xml?count={1}", user, count ) );
        return client.DownloadString(uri);
    }
}

Expose an SVC file on the server to host the new service. In this example, the URL to invoke the new service is:

http://localhost/WebSite/TwitterProxy.svc

So, to invoke this service from Silverlight, add a proxy to the Silverlight control, using the service address and invoke it like this:

private void OnLayoutRootLoaded(object sender, RoutedEventArgs e)
{
    TwitterProxyClient client = new TwitterProxyClient(
        new BasicHttpBinding(), new EndpointAddress(
        "http://localhost/Website/TwitterProxy.svc"));
    client.GetUserTimelineCompleted += OnGetUserTimelineCompleted;
    client.GetUserTimelineAsync("wkh", 10);
}

void OnGetUserTimelineCompleted(object sender,
    GetUserTimelineCompletedEventArgs e)
{
    // open a Twitter Status dialog, passing the XML string
    NavigationHelper.Navigate(new FadeTransition(
        TimeSpan.FromMilliseconds(750.0d)), new Status(e.Result));
}

The dreaded "Download failure" error is gone. Nice. of course, the potentially bad side effect of this technique is that all of the Twitter service traffic will be flowing through the server that's hosting the TwitterProxy. Think about that before using this technique.


Posted by kevin on Friday, May 02, 2008 9:24 AM
Permalink | Comments (1) | Post RSSRSS comment feed