The Problem Solver

Tell me and I will forget
Show me and I will remember
Involve me and I will understand
- Confucius -

Google Ads

This Blog

Syndication

Search

Tags

News





  • View Maurice De Beijer's profile on LinkedIn

Community

Email Notifications

Explore

Archives

December 2010 - Posts

One of the sites I run for the Dutch .NET community is .NET Events in the Netherlands which lists all sorts of community events for, as the name suggests, Dutch .NET developers. The site has been around for a while but always forced people to either go to the site or use in iCalendar feed so see what is happening. Some time ago I decided that these should also be tweeted so people using Twitter would be notified of upcoming events.

There are several ways to automatically send tweets to Twitter and I choose to use TweetSharp as that seemed to be one of the more popular libraries around.

 

The first step is registering an application with Twitter. This is done using this page, make sure the access type is set to Read & Write so you can sent updates. This will give us the first two bits of information we need to send Twitter massages, the Consumer key and the Consumer secret.

image

 

Because I only needed to use a single client there is no need to implement the complete OAuth workflow to determine the authenticated access token. Instead I can just copy them from the Twitter site by clicking the My Access Token link to get to the following page where the Access Token (oauth_token) and the Access Token Secret (oauth_token_secret) are the last to pieces of information we need. These tokens don’t expire until we explicitly do so ourselves so we can keep on using these in our program

image

 

With these pieces in place sending a Twitter update is a breeze

private static void PostTweet(string twitterStatus)
{
    var clientInfo = GetClientInfo();
    var twitterStatuses = FluentTwitter.CreateRequest(clientInfo)
        .AuthenticateWith(_oauthToken, _oauthTokenSecret)
        .Statuses();
 
    var tw = twitterStatuses.Update(twitterStatus);
 
    var rsp = tw.Request();
 
    if (rsp.IsTwitterError == false)
    {
        var status = rsp.AsStatus();
        Console.WriteLine("Tweeting: {0}", status.Text);
    }
    else
    {
        var error = rsp.AsError();
        Console.ForegroundColor = ConsoleColor.Red;
        Console.WriteLine(error.ErrorMessage);
        Console.ResetColor();
    }
}
 
private static TwitterClientInfo GetClientInfo()
{
    var clientInfo = new TwitterClientInfo()
    {
        ConsumerKey = _consumerKey,
        ConsumerSecret = _consumerSecret
    };
 
    return clientInfo;
}

 

Don’t forget all required using statements as some of the methods like AuthenticateWith() and Update() called are extensions methods.

using TweetSharp;
using TweetSharp.Twitter.Extensions;
using TweetSharp.Twitter.Fluent;

Using the twitterStatuses you can also loop over the existing tweets, delete or retweet the just like you can in the user interface or Twitter.

 

Enjoy!

www.TheProblemSolver.nl
Wiki.WindowsWorkflowFoundation.eu

Posted by Maurice | 1 comment(s)
Filed under: , ,

While working on one of my WP7 applications I noticed a weird problem with the animations. The app is pretty simple with just two pages. I added the same page load animation, see below, to both pages and all seemed to work just fine.

<phone:PhoneApplicationPage.Projection>
    <PlaneProjection 
        x:Name="plane" 
        CenterOfRotationX="0"/>
</phone:PhoneApplicationPage.Projection>
<phone:PhoneApplicationPage.Triggers>
    <EventTrigger>
        <BeginStoryboard>
            <Storyboard>
                <DoubleAnimation 
                    Storyboard.TargetName="plane" 
                    Storyboard.TargetProperty="RotationY" 
                    From="-90" 
                    To="0" 
                    Duration="0:0:0.5"/>
            </Storyboard>
        </BeginStoryboard>
    </EventTrigger>
</phone:PhoneApplicationPage.Triggers>

The animation causes the page to rotate in and is something I copied from Charles Petzold eBook Programming Windows Phone 7 although I shortened the time to just half a second. While developing everything worked just fine when I ran my application in the emulator. However when I deployed my application to a real phone I noticed that the first page ran the animation just fine but the second page didn’t show the animation at all.

 

The hunt for the missing animation

After some trial and error I noticed that the animation started showing up if I increased the time to around 1.5 seconds. And it wasn’t the complete animation, only the last part would show up. The first second or so would just not show up at all. Of course I could have changed the animation to do nothing for a second and then sweep in but that felt like a wrong hack so I decided to find out what was really going on and why the two pages where behaving differently.

 

The guilty party is data binding

It turned out the main difference between the two pages was the fact that the second used data binding while the first didn’t. And when I removed the DataContext from the second page the animation would show up just as it was supposed to. Of course the data binding was there for a good reason and removing it wasn’t an option so I had to change the way the data binding was initialized.

I was using the excellent MVVM Light Toolkit written by Laurent Bugnion and it uses the ViewModelLocator pattern to hook up the View and the ViewModel. This works really well as Blend understand the paradigm and makes it very easy to do the UI work in Blend. However it also means that the DataContext is set when the page is first created. Normally not a bad thing but in this case it meant that all data bindings where being evaluated right when the page was created, at the same time as the animation was running. So the solution was to move the runtime data binding to the page Loaded event using the following code:

void SecondPage_Loaded(object sender, RoutedEventArgs e)
{
    DataContext = ViewModelLocator.SecondStatic;
}

Of course I also needed to prevent the page from setting the DataContext when first created while still maintaining the blendability of the page. This is achieved by making a small change to the ViewModelLocator class in the property that returns the ViewModel when the page is loaded:

public SecondViewModel Second
{
    get
    {
        if (SecondStatic.IsInDesignMode)
            return SecondStatic;
        else
            return null;
    }
}

With these small changes in place the animation ran just fine and the data binding worked just fine as well.

 

Conclusion

The two tings I learned from this is not to trust the emulator as it runs a lot faster that the actual Windows Phone 7 and as a result will hide these kind of problems. And secondly to set the page DataContext in the loaded events instead of during the page construction.

 

Enjoy!

 

www.TheProblemSolver.nl
Wiki.WindowsWorkflowFoundation.eu

Posted by Maurice | with no comments
Filed under: , ,

If you are using the information in this MSDN page to get started with the ASP.NET Session State Provider to use Windows Server AppFabric Caching you might run into some problems as the steps are less than complete. First make sure you have Windows Server AppFabric Caching setup and working and add the configSections and dataCacheClient elements as specified. If you next add the sessionState element as specified you will receive the following error:

Parser Error Message: Could not load type 'Microsoft.ApplicationServer.Caching.DataCacheSessionStoreProvider'

The reason is the assembly information is not specified so the .NET runtime doesn’t know where to find the DataCacheSessionStoreProvider class. The solution is simple, either add the assemblies section to the web.config or add it to the sessionState provider like this:

<sessionState mode="Custom"
              customProvider="AppFabricCacheSessionStoreProvider">
  <providers>
    <add
      name="AppFabricCacheSessionStoreProvider"
      type="Microsoft.ApplicationServer.Caching.DataCacheSessionStoreProvider, Microsoft.ApplicationServer.Caching.Client, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
      cacheName="NamedCache1"
      sharedId="SharedApp"/>    
  </providers>
</sessionState>


We are not quite done though because if we run the application now we get a new error:

Parser Error Message: ErrorCode<ERRCA0009>:SubStatus<ES0001>:Cache referred to does not exist. Contact administrator or use the Cache administration tool to create a Cache.

This is caused by the cache name of “NamedCache1”. Chancing this to the default cache name of “default” is one way to fix the problem. The other is to create the cache named “NamedCache1” using the following PowerShell command:

New-Cache NamedCache1


Remember to run the “Caching Administration Windows PowerShell” application as administrator with elevated privileges otherwise you wont be able to manage the Windows Server AppFabric Cache and you will receive the following error:

New-Cache : ErrorCode<ERRCAdmin002>:SubStatus<ES0001>:The operation has timed out and the result is unknown. Stop the cluster and run Export-CacheClusterConfig followed by Import-CacheClusterConfig to make the cluster configuration consistent.
At line:1 char:10
+ New-Cache <<<<  NamedCache1
    + CategoryInfo          : NotSpecified: (:) [New-Cache], DataCacheExceptio
   n
    + FullyQualifiedErrorId : ERRCAdmin002,Microsoft.ApplicationServer.Caching
   .Commands.NewCacheCommand

 

See here for a complete list of PowerShell commands used to manage the Windows Server AppFabric Caching environment.

 

Enjoy!

www.TheProblemSolver.nl
Wiki.WindowsWorkflowFoundation.eu

Posted by Maurice | with no comments
Filed under: , , ,