Silverlight Journal

March 29, 2010

Dev Summit Prism Talk Source Code

Filed under: Uncategorized — bobbsmooth @ 2:51 pm

The source code from my talk can be downloaded from here.

The slides can be downloaded from here.

I’ll post a link to the video once it’s available.

EDIT:
My presentation is online here.

February 4, 2010

My First Secure Silverlight ArcGIS Application

Filed under: Uncategorized — Tags: , , , , , , — bobbsmooth @ 9:31 pm

I recently developed an application using the Silverlight API for ArcGIS Server (AGS). As with all things the requirements changed near the end of the project to require that the site be hosted offsite and be accessible via the Internet. Despite my desperate cries of, “Danger! Danger! The interwebs are unsafe.” My client had made up their collective minds and I had my orders: put it on the Internet and make it secure.

The first step was easy, secure all traffic between the Silverlight client and my web services that do all my data requests. One wildcard SSL cert later and I was all set to create as many secure websites as I could need.

Next, I had to figure out a way to make AGS keep its preverbal legs shut so it wouldn’t whore out all my client’s data over the tubes of the Internet. This is what I had to do to configure the server.

  1. Install SSL certificate on server.
  2. Install SQL Server to use as a Membership provider for AGS.
  3. Use aspnet_regsql.exe to create the membership provider. Typically, this is located somewhere like this: C:\<windows dir>\Microsoft.NET\Framework\<version dir>\aspnet_regsql.exe
  4. On AGS Manager -> Security -> Settings, change the location of my Security Stores from Windows to SQL server.
    • For SQL Express this just worked but for SQL Server I had to set up the NETWORK SERVICE account with permissions on my Aspnetdb database.
    • Also on SQL Express, for the name of the SQL database, I used the default .\SQLEXPRESS but for SQL Server, I had to use just the name of the server.
  5. On AGS Manager -> Security -> Roles, add a new role.
  6. On AGS Manager -> Security -> Users, add a new user to my role.
  7. On AGS Manager -> Services -> Manage Services
    • Add a new folder from the Manage Folders menu. (This turned out to be very important)
    • Select that folder from the drop list.
    • From the Manage Folders menu, select Permissions. Removed “Everyone” and add your new role.
    • Added your map service using the “Add New Service” link while your new folder is selected in the drop list.
  8. On AGS Manager -> Security -> Settings, enabl security.
    • To undo this, you have to modify your Server.dat file and change the <SecurityEnabled> tag from true to false.
  9. Next generate a token. For my project I knew I was going to only call the map service from my proxy so I didn’t want it to expire.
    • https://<AGSSERVERNAME>/ArcGIS/tokens?request=getToken&username=<USERNAME>&password=<PASSWORD>&clientid=<WEBURL>
    • USERNAME and PASSWORD are for the credential that you set up in step 6.
    • WEBURL is the URL or IP address of the site that will host your application’s proxy page (e.g. site.domain.com). If you use the IP address, use the one that AGS will see. For example, if AGS is on the same server as your Proxy and both are behind a firewall, then use the private IP address assigned by your firewall. However, if your proxy and AGS will be on separate networks and will be communicating across the Internet, then you will use the public IP address that your proxy will communicate through.
  10. Once you have a token, test is using this url:
    • https://<AGSSERVERNAME>/ArcGIS/rest/services/<FOLDERNAME>/<SERVICENAME>/MapServer?token=<TOKEN>

The next big chunk was modifying ESRI’s proxy to handle traffic a little smarter.

  1. In proxy.config, I added an attributes to the serverUrl elements.
    • localURL – the url that the proxy could use to find the service without going out of the firewall
  2. I modified proxy.ashx
    • Uses the localURL instead of the one passed in from the request.
    • Does error handling to verify the config file opened correctly
    • Allows for expired and incorrect SSL certificates (sometimes a legitimate SSL would fail for unknown reasons)

Next, I modified my application to use my proxy for all calls to the map service.

((ArcGISDynamicMapServiceLayer)esriMap.Layers[0]).ProxyURL = _viewModel.MapServiceProxyURL;
((ArcGISDynamicMapServiceLayer)esriMap.Layers[0]).Url = _viewModel.MapServiceURL;

It’s very important that you set the Layer’s ProxyURL property before setting its Url property. As soon as the Url property is set, the layer object makes the calls the service.

The last thing I did was to configure IIS Authentication. For some reason AGS made everything use Windows Authenitication. I had to change everything to anonymous only so that AGS could do the authentication.

References:

Thanks to @dbouwman and @Gmapdev for pointing me in the right direction!

December 31, 2009

Static object management with file watchers

Filed under: Uncategorized — Tags: , , — bobbsmooth @ 6:15 pm

I wanted to have some objects remain in memory for use by some WCF services but still be able to refresh them without restarting the service. Basically, when certain files change, I wanted the static objects to be flushed and then re-initialized with the new values in the files.

        private static System.IO.FileSystemWatcher _configWatcher = null;
        private static void StartWatcher()
        {
            if (_configWatcher == null)
            {
//set the path to a directory that I configured in the web.config file.
                string path = HttpContext.Current.Server.MapPath(WebConfigurationManager.AppSettings["CONFIGURATION_DIRECTORY"]);

//initialize the watcher to watch any xml file in the directory identified by the path
                _configWatcher = new System.IO.FileSystemWatcher()
                {
                    Path=path,
                    EnableRaisingEvents=true,
                    NotifyFilter=System.IO.NotifyFilters.LastWrite,
                    Filter="*.xml"
                };

//handle the changed event for when files are modified and the error event
                _configWatcher.Changed += new System.IO.FileSystemEventHandler(_watcher_Changed);
                _configWatcher.Error += new System.IO.ErrorEventHandler(_watcher_Error);
            }
        }
        static void _watcher_Error(object sender, System.IO.ErrorEventArgs e)
        {
                //log error
        }
        public static void _watcher_Changed(object sender, System.IO.FileSystemEventArgs e)
        {
               //reinitialized dependent static objects
        }

December 21, 2009

Prism commanding: KeyUp example

Filed under: Uncategorized — Tags: , , , , — bobbsmooth @ 9:10 pm

Today, I had to build a simple login form using Silverlight 3 and Prism in a MVVM application.  One requirement that I always put on any login form is that the user can hit the enter key to submit her credentials for validation.  This is my solution:

KeyUpBehavior.cs

using System.Windows.Controls;
using System.Windows.Input;
using Microsoft.Practices.Composite.Presentation.Commands;

namespace Infrastructure.Commands.PasswordCommands
{
    public class KeyUpBehavior : CommandBehaviorBase<Control>
    {
        public KeyUpBehavior(Control element)
            : base(element)
        {
            element.KeyUp += new KeyEventHandler(element_KeyUp);
        }

        void element_KeyUp(object sender, KeyEventArgs e)
        {
            //only execute command if the user pressed the enter key
            if (e.Key == Key.Enter)
                base.ExecuteCommand();
        }
    }
}

KeyUp.cs

using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace Infrastructure.Commands.PasswordCommands
{
    public class KeyUp
    {
        #region Command  attached property
        public static ICommand GetCommand(DependencyObject obj)
        {
            return (ICommand)obj.GetValue(CommandProperty);
        }

        public static void SetCommand(DependencyObject obj, ICommand value)
        {
            obj.SetValue(CommandProperty, value);
        }

        // Using a DependencyProperty as the backing store for Command.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty CommandProperty =
            DependencyProperty.RegisterAttached("Command", typeof(ICommand), typeof(KeyUp), new PropertyMetadata(OnSetCommandCallback));

        private static void OnSetCommandCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
        {
            Control element = dependencyObject as Control;
            if (element != null)
            {
                KeyUpBehavior behavior = GetOrCreateBehavior(element);
                behavior.Command = e.NewValue as ICommand;
            }
        }
        private static KeyUpBehavior GetOrCreateBehavior(Control element)
        {
            KeyUpBehavior behavior = element.GetValue(KeyUpBehaviorProperty) as KeyUpBehavior;
            if (behavior == null)
            {
                behavior = new KeyUpBehavior(element);
                element.SetValue(KeyUpBehaviorProperty, behavior);
            }
            return behavior;
        }
        #endregion

        #region KeyUpBehavior attached property
        public static KeyUpBehavior GetKeyUpBehavior(DependencyObject obj)
        {
            return (KeyUpBehavior)obj.GetValue(KeyUpBehaviorProperty);
        }

        public static void SetKeyUpBehavior(DependencyObject obj, KeyUpBehavior value)
        {
            obj.SetValue(KeyUpBehaviorProperty, value);
        }

        public static readonly DependencyProperty KeyUpBehaviorProperty =
            DependencyProperty.RegisterAttached("KeyUpBehavior", typeof(KeyUpBehavior), typeof(KeyUp), null);
        #endregion

        #region CommandParameter attached property
        public static object GetCommandParameter(DependencyObject obj)
        {
            return (object)obj.GetValue(CommandParameterProperty);
        }

        public static void SetCommandParameter(DependencyObject obj, object value)
        {
            obj.SetValue(CommandParameterProperty, value);
        }

        public static readonly DependencyProperty CommandParameterProperty =
            DependencyProperty.RegisterAttached("CommandParameter", typeof(object), typeof(KeyUp), new PropertyMetadata(OnSetCommandParameterCallback));

        private static void OnSetCommandParameterCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
        {
            Control element = dependencyObject as Control;
            if (element != null)
            {
                KeyUpBehavior behavior = GetOrCreateBehavior(element);
                behavior.CommandParameter = e.NewValue;
            }
        }
        #endregion
    }
}

View

        <PasswordBox Grid.Row="2" 
                 Grid.Column="2" 
                 customCommands:KeyUp.Command="{Binding KeyUp_Command}" 
                 customCommands:KeyUp.CommandParameter="{Binding Password, ElementName=txtPassword}" 
                 Password="{Binding Path=User.Password, Mode=TwoWay}" 
                 x:Name="txtPassword" />

ViewModel

        public DelegateCommand<object> KeyUp_Command { get; set; }

        public void KeyUpCommand_Handler(object commandParameter)
        {
            //do authentication, commandParameter is the value stored in PasswordBox.Password.
        }

ViewModel constructor

            KeyUp_Command = new DelegateCommand<object>(KeyUpCommand_Handler);

December 8, 2009

My First Expression Encoder App

Filed under: Uncategorized — Tags: , , , , — bobbsmooth @ 8:59 pm

Problem Background

On my current project, I need to re-encode about 7000 videos into a Smooth Streaming format so that I can take advantage of the functionality in Silverlight’s MediaElement control. The last time I did a big batch of encoding, I simply scripted Windows Media Sever 9. With Expression Encoder 3, however, that’s not possible. Instead you must use the API and create an application to do the work. While not terribly difficult, this project definitely took me outside my comfort zone. There are very few references out there so I hope this helps shed some more light on the solutions that can be built.

Solution

I decided that the best thing to do first was to create a simple application that would encode one video. Then I could expand it to use something like a database or an XML file to script the encoding process.

The first step was to install Expression Encoder 3. I needed the full version because the free version doesn’t support Smooth Streaming.

Next, I started a new WPF solution and added references to all of the Microsoft.Expression.Encoder libraries.

Library References

Project References

In my Window1.asmx file, I created some simple controls for finding a file and starting the process.

<Window x:Class="SingleFileEncoder.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="557">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="25"></RowDefinition>
            <RowDefinition Height="25"></RowDefinition>
            <RowDefinition Height="25"></RowDefinition>

        </Grid.RowDefinitions>
        <StackPanel Orientation="Horizontal" Grid.Row="0">
            <TextBlock Text="Source Video:" VerticalAlignment="Center">
                <TextBlock.Margin>
                    <Thickness>
                        <Thickness.Left>5</Thickness.Left>
                    </Thickness>
                </TextBlock.Margin>
            </TextBlock>
            <TextBox Height="23" Name="txtFileName" VerticalAlignment="Center" Width="300" BorderThickness="1">
                
                <TextBox.BorderBrush>
                    <SolidColorBrush Color="Blue" ></SolidColorBrush>
                </TextBox.BorderBrush>
                <TextBox.Background>
                    <LinearGradientBrush StartPoint=".75,0" EndPoint="0,.5">
                        <GradientStop Color="#CCCCFF" Offset="1" />
                        <GradientStop Color="White" Offset=".25" />
                    </LinearGradientBrush>
                </TextBox.Background>
                <TextBox.Margin>
                    <Thickness>
                        <Thickness.Left>5</Thickness.Left>
                    </Thickness>
                </TextBox.Margin>
            </TextBox>
            <Button Height="23" HorizontalAlignment="Right" Name="btnBrowse" VerticalAlignment="Center" Width="75" Click="btnBrowse_Click">Browse
                <Button.Margin>
                    <Thickness>
                        <Thickness.Left>5</Thickness.Left>
                    </Thickness>
                </Button.Margin>
            </Button>
            <Button Name="btnEncode" Click="btnEncode_Click">
                Encode
                <Button.Margin>
                    <Thickness>
                        <Thickness.Left>5</Thickness.Left>
                    </Thickness>
                </Button.Margin>
            </Button>
        </StackPanel>
        <ProgressBar Grid.Row="1" Name="progressBar" Minimum="0" Maximum="100"></ProgressBar>
        <TextBlock Name="txtMessage" Grid.Row="2"></TextBlock>
    </Grid>
</Window>

In the code behind file, I added using statements for the Encoder namespaces.

 using System;
 using System.Windows;
 using Microsoft.Expression.Encoder;
 using Microsoft.Expression.Encoder.Profiles;
 using Microsoft.Win32;
 

I used the Win32 OpenFileDialog to find a file when the user clicks the Browse button.

private void btnBrowse_Click(object sender, RoutedEventArgs e)
 {
 OpenFileDialog dialog = new OpenFileDialog();
 dialog.ShowDialog();
 txtFileName.Text = dialog.FileName;
 }

In the encode button’s even handler, I assigned that file to a new MediaItem.
MediaItem mediaItem;
mediaItem = new MediaItem(txtFileName.Text);

Next, I created the output profile for my MediaItem. This technique is new to Encoder 3.

 AdvancedVC1VideoProfile videoProfile = new AdvancedVC1VideoProfile
 {
 Bitrate = new VariableConstrainedBitrate(403, 600),
 Complexity = VideoComplexity.Fastest,
 SmoothStreaming = true,
 Size = new System.Drawing.Size(236, 176),
 KeyFrameDistance = new TimeSpan(0, 0, 2),
 InLoopFilter = true
 };
mediaItem.OutputFormat = new WindowsMediaOutputFormat()
 {
 VideoProfile = videoProfile
 };

Next, I created a Job and assigned the MediaItem to it.

 Microsoft.Expression.Encoder.Job job = new Job();
 job.MediaItems.Add(mediaItem);
 job.OutputDirectory = @"C:\EncodedContent\";
 job.CreateSubfolder = false;

I assigned a couple of event handlers that would let me see progress as it was made and notify me when the encoding was complete.

job.EncodeProgress += new EventHandler<EncodeProgressEventArgs>(job_EncodeProgress);
 job.EncodeCompleted += new EventHandler<EncodeCompletedEventArgs>(job_EncodeCompleted);

Finally, I fire the encode process for the Job.

job.Encode();

December 4, 2009

Silverlight Journal

Filed under: Uncategorized — Tags: , , , — bobbsmooth @ 11:42 pm

This will be the home of my musings on Silverlight, MVVM, Prism, Twitter, ArcGIS Server, JavaScript, Agile methodologies and all manner of programming minutia.

You can follow me on twitter at http://twitter.com/jrockers.

Theme: Shocking Blue Green. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.