In this post I will describe how to install AppFabric Cache (AFC) and begin talking to it with a C# application. There are many useful things that can be done with AFC, but through I series of post I will discuss a scenario that I am using it for now which is to provide a scalable middle tier data service for routing events from back end systems to Silverlight clients.
But lets start with just getting this to work, which initially seems easy, but like everything else has its issues. First, we need to get a few installation packages.
Download the proper install for AFC from:
I'm installing this on a fresh Windows 2008 R2 box, so I also need to install .NET 4.0, which you can get here:
Once you have those and .NET 4 installed, start the AppFabric Cache setup, sign your life away, and press next.
You then get the customer experience improvement dialog:
Right now I don’t want to participate in the improvement program so I just press ‘Next’, which takes us to the select services dialog:
I only need caching for this demonstration, so I select them and press next, and it takes you to the confirmation screen:
Press install. When done, you get the ‘installed ok’ screen:
At this point we need to configure the cache service, so leave the check box selected and press ‘Finish’. A few moments later you’ll be presented with the send feedback page for the configuration wizard:
I select ‘no’ and press next to get to the main configuration screen:
Now with this screen I’m not sure why it is possible to not check the Set Caching Service configuration box. I tried that, and could not get things to work (in retrospect, I think it was because of the yet to be described security issue, but it’s just best to do this with a cluster [even a one node cluster] anyway). So, check it and fill out the information as needed.
For my configuration I created an account named ‘velocity’ on the local machine and use that as the login account for the cache service; the setup program does not let you use a built in account.
I am also going to use the XML configuration technique as I want to keep this lightweight without using SQL Server. To do this you must have a network share accessible, so I created a folder on this system (VELOCITYA) and shared it as ‘VelocityConfig’.
Since this is the first system installed, I select ‘New cluster’, and I’m going to use a small cluster (1-5 machines).
Select ‘Next’ and you will be taken to the page to configure the ports that the cache will use.
Leave these as the defaults. Note, I have the windows firewall disabled so those options are also disabled.
Now press ‘Finish’, and accept the confirmation dialog. After a few seconds your configuration will be completed.
But, you are not done, yet.
There are a few powershell commands that first need to be executed. The install will have installed several menu items:
Select “Caching Administrator Windows PowerShell’. This will open a powershell console where you need to run several commands. First run this command: ‘Use-CacheCluster’.
The output is pretty basic – nothing. Now run ‘Start-CacheCluster’. A teal box will show some status of starting the cluster:
And then you’ll get some status of the nodes in the cluster.
Now that we have a one node cluster running, lets try to verify we can talk to it with some C# code.
First, you’ll need the caching client assemblies. I believe you can install these on your development system with the AppFabric Cache installer, but I grabbed mine from the cache server itself in the \windows\system32\appfabric folder:
The code I have here is the simplest code you can use to simply connect to the local system’s cache and retrieve the default cache (with the caveat of some security code needed to solve a security problem that is described soon).
When running this on the VELOCITYA server, the following is the result:
What’s the deal with this? Well, that was a lot of googling on my part to figure out, so I’ll save you the trouble.
The short of it is because the AppFabric cache is not running in a domain it is not authenticating the request, even from the local box. To fix this, we must modify our cache clusters configuration file. There are a few ways to do this, but I found it simplest to just open the XML file in the network share (\\VELOCITYA\VelocityConfig\ClusterConfig.XML"). This is the default content after the configuration that we previously performed:
We need to add the following lines (26-28):
Ideally not the best for security but it is good enough for our purposes of demonstration. Also, I needed to reboot the cache server after making this change. I tried to restart the service, but that didn’t seem to get the settings. Once successful, the program output will be the following:
Note that those lines added to the configuration file also required the addition of lines 32-34 in the source code. I’ll describe other security models in later posts, but this suffices at this point in time.
Also, please note that the install be default does not set the cache service to start automatically, so you’ll need to go and change that in the services dialog (either start it manually or configure it to auto start).
Next post... Adding items to the cache and getting notifications of new items in the cache.
I'm a big fan of Linq to objects. I really like the way that you can write much more concise code with it for every day things, not just things that you would think that you normally do with Linq.
One of the things that I almost always do now is use the Linq ForEach instead of the C# foreach statement. This gives you a nominal amount of code compression. But the thing I want to write about is using Linq instead of the C# 'for' statement, which I think leads to a lot nicer code.
I came across this the other day as I actually needed to write a 'for' statement to add a specific amount of nodes to a graph. Upon writing it, I was like 'gee, there must be a more concise way of doing this', such as where I don't need to do 'for', an initializer, comparison, and incrementation sub-statements. I mean, I just need to iterate across the integers 0 to 10 and apply a function to each.
The basic model that think of for doing this is provided in Ruby with sequences / ranges. As an example, here is some ruby that generates the integers 1 to 10 and prints each to the console:
I'm not going to explain ruby in detail, but the (1..10) generates the integers 1 to 10, the '.each' says for each item in the sequence apply the code block that follows. The code block is essentially a delegage with 'i' as a parameter and which prints 'i' to the console.
So how can this be done in C#? Well, it requires a couple of steps. First, System.Linq provides extensions to the Enumerable class, notably the 'Range' method which generates the values between two parameters:
This works fine, but I have an issue with it. Enumerable.Range (and most things in Linq) returns an IEnumerable. To iterate across those, you need to use the foreach statement (or a for, or while, that uses the IEnumerable methods), which is not really better than having to use the for loop. What I'd like is something still more concise.
One thing that can be done is to convert the enumerable to a list using the ToList() Linq extension method:
This works great, but I really don't like having to convert the result of .Range() to a list just so I can apply the .ForEach() method to the result. Why the Linq extensions don't have a .ForEach() on IEnumerable I don't know; it sure would be handy. But, we can write our own:
Now that we have this, we can rewrite the code to the following, which is what I've been looking for the whole time:
Perfect, concise, and also using a fluent interface that I really like.
Last night I was porting some of my TweetZenn project over to VS2010 and came across this error in the build. It took quite a bit of googling to figure out what caused this as well as how to solve it, and many of the answers I found where also not related to the type of solution I was building (most seemed related to Azure development), so that made it a bit complicated also. But I have reduced this to a very simple scenario and solution, which I'll show here for everyone.
The problem can be reproduced very simply. Take this scenario:
1) Create a new Silverlight solution in 2010. Call it anything, but in this expositionI will call it OutputPathTest.
2) Add a C# windows library to the solution. I calle it "ALibrary"
Now build the solution. Everything works fine.
3) Now add a reference to "ALibrary" in your web project and build (not that you didn't even need to add any classes to ALibrary). You now get the OutputPath error message:
4) Remove the reference to ALibrary and build, and it builds properly.
So, this problem seems to be related to the reference to ALibrary causing the problem.
But how to solve this?
Well, it appears you need to manually edit the .csproj for ALibrary. Open the file in your favorite text editor; I'm using notepad2, and the following shows the culprits identified with red arrows:
The project is set to target x86 while the web solution is set to AnyCPU. To fix the problem, it is needed to change the mentions of x86 to AnyCPU. Do that, rebuild the solution, and everything will work perfectly.
I came across the need today to convert some CSV (excel data on the clipboard) into an array of arrays strings (string); basically a matrix of strings where each matrix element is one of the cells in Excel.
Usually I'd do this through conventional means like nested foreach statements, but I decided I'd like to try this with LINQ. This is the result I got, which I thought was so very nicely concise:
I want to give a quick introduction to something I've been working on lately, the TweetZenn client. This is a Silverlight application I've been building to provide access to twitter (and eventually many other social networks). I'm intending it to be a tool that allows you to not just construct tweets, but also to use and browse the various social networks to find information. I've also built it to hone some Silverlight skills ;-)
Far as I know, this will be the first production twitter client in Silverlight. I really think this offers many advantages over other clients in the types of features that it will be capable of providing. The first release will likely just focus on Twitter, but Facebook is in the works, as well as several other social networks. Overall I look at this not as just a Twitter client, but as an overall tool for managing collaboration within multiple social networks.
So, where is this for your access? Well, it's not quite available yet as I'm still working out a couple of kinks in the base implementation, but I'll throw a couple of screen shots out here to whet appetites...
The login screen:
If you look closely, you'll notice that this is Silverlight 2.0, in Flock, on Mac OSX, and it is rock solid. If you know me, I build and run on Mac exclusively (albeit in a Windows 7 VM on OSX).
From this screen you will be able to login using your twitter credentials (and eventually that of other networks). There is a little hint in the picture above of some of the functionality. There are the things that you would expect from a twitter client, such as a tweet stream that updates when new tweets are found, favorites support, searching, making your own tweets, reviewing your created tweets, ... I've gone with a panel / stack metaphor which seems to be the rage, but I expect to provide a bunch more "views" as time goes on.
Here are a few other snapshots of various functions, although I warn that these may change before this goes live:
The following shows the your tweets and recent tweets panel, as well as a little of the browser:
The panels can be moved around, and we also support multiple pages of panels if you would like that.
Once of the features that we have is the ability to form groups. This is quite easy using either a hot link in the tweet, or using the group panel. For example, here I assigned Jeremy Miller to the .NET group, and you can also see he is instantly added to the 'members' of .NET in the group panel.
One of the things TweetZenn has that others don't is the ability to browse the social network to review peoples tweets and potentially follow them.
Here I'm showing the use of the .NET group to show only the people I follow in that group, and to be able to on the spot review their tweets just by clicking on their picture:
If you want to see who Jeremy is following, you can click on a button in a popup (when hovering on his head), and you will be shown that information:
You can view the tweets of any of Jeremy's friends, and if you want to follow them you can just follow them by clicking the appropriate button in the popup. Notice that Jeremy now shows up at the top. It keeps track over where you've been so you can go back there at any time.
This has been just a brief introduction, and as usual I'm writing this while trying to get on a plane (seems I blog the most from airports). I'll explain more over the upcoming days, including explanations of how this works as well as showing some of the code as I'm leveraging all kinds of neat stuff like web services, LINQ to SQL, isolated storage on the client, caching on both the client and server sides, full separation of view from model with XAML, dynamic changing of visual representation through XAML templates, asyncronous everything, and much more....
If you've ever worked with Silverlight applications that accept user input, you've probably dealt with setting focus to various controls. In that case you may know this already. If you haven't then this should definitely save you some hassles.
There is an issue with Silverlight having their controls receive initial focus. As an example, take a look at the following silverlight application. This is the first page of the application and it has a text box that I would like to have the focus currently in:
I've actually called the TextBox.Focus() method in the load event of this UserControl, but it doesn't have the focus. What gives?
Well, the problem turns out to be that the Silverlight object in the HTML itself has not received the input focus from the browser, and hence the control in the Silverlight app do not receive the focus as it is somewhere else in the browser. As a matter of fact if you click anywhere on the Silverlight app (lets say in the gray areas) the browser will give the Silverlight control the focus, which then gives it to the specified text box.
What you do is add the OnPluginLoaded event property on the silverlight control, and put the script shown as in the head tag. Net time (and every time from now on), your control will get the focus when the application starts:
During the building of the WCF service mentioned in my previous post, I came across an error when trying to access the WCF service when running it within the local development fabric. It worked fine when just running the web role, and when deployed into Azure. So, this led me to investigating if the IIS as used by the local development fabric to see if it was not configured to serve .SVC files.
One of the first things that I did to investigate was to just navigate to the service in the browser with a brand new service project that I put together. When doing this, I received the following:
There should be a page showing the information about the service, instead a 403.3 error. Likewise, when stepping through the debugger, similar errors could be seen when checkin the exceptions.
After a bit of googling, the solution to this turns out to be the following. Apparently, WCF is not configured properly with IIS. This feels very similar to the problem when ASP.NET sometimes is not configured properly, in that the solution is very similar.
To solve the problem, navigate to the windows communication foundation folder as shown here, and run the "ServiceModelReg.exe -i" command.
After running that program, you will see output similar to the following and the problem will then be solved.
In my previous post I showed how to rewrite urls which where applied to .asmx web service proxies. In the last couple of days I needed to convert WCF client proxies for Silveright to do the same thing. Turns out this is very similar (if not identical), but I found a new syntax for doing it which makes things a little bit easier. Here is an example:
The ManagementServiceClient class is the proxy class generated for the WCF service. The change is to use the App.Current.Host.Source variable and append the path to the .svc file. Works like a champ and looks real nice.
When you deploy your web role to Azure (or the local development fabric), it will be assigned a url which is different from the one assigned by VS.NET, and which usually has a port of 81. This can cause problems in your Silverlight application if you call web services within the web role project. This is because VS.NET assigned a port to your web services (such as 43039). When deploying to Azure (or again the local development fabric), the app has a new (localhost:81 on the local dev fabric, and yet a different one in Azure), but the old ports (and a reference to localhost) remains in the proxy configuration(s).
So how can you solve this? The best scenario that I have found is to reconfigure your proxy URLs in the start up of the silverlight application. As an example:
What I'm doing here is calling this method during the startup of the Silverlight application object. It uses the HtmlPage.Document.DocumentUri object to determine the url to the application, and truncates it to contain just the address and port of the page that the application is served from the web server. The last two lines then create two proxy objects that I use in this app, and pass a new enpoint address object which is the base URI determined before appended with the asmx filename of the service. Note that you'll also need to pass in a binding object too; I change some parameters on mine, but using just a default BasicHttpBinding object should do fine (and it has to be a basic HTTP binding object as that is all that is supported by Silverlight).
This works for all deployments of the Silverlight application, either running locally in VS.NET, the local development fabric, or in Azure.
I'm building an application to front Twitter and provide some other services to the user around Twitter. I think it's kind of neat looking with what I've done so far, but I have a bunch of list boxes (and scrollviewers) that I think just look ugly:
I was figuring that I would want to make them look more like the scroll bars on the iPhone or in other apps I use like EventBox:
So I googled a little and came up with this, which looks to exactly fit the bill:
Mark .NET: Styling a ListBox With Silverlight 2 Beta 2 (Part 2) - Scrollbars
With some modifications, my app now looks like this....
One thing to note about the code on that site. Don't cut and paste it from the blog pages - it does not work. Get the actual solution
that is posted and use the XAML in that as it works great.