Thursday, December 4, 2014

Reversing a file with Powershell

Today I was tracking down the source of a deadlock and needed to reverse the SQL Server log file so that it would be easier to follow the deadlock information. A quick search for reversing a text file lead to a command called Tail which must be a *unix command. I also found a couple of Powershell scripts but nothing really simple.

As it happens, get-content returns it's results as an array.
And you can reverse an array using the static Reverse() method
And you can write an array using out-file.

A quick example:
$c = (get-content deadlock.log)
[array]::Reverse($c)
$c | out-file "rev-deadlock.log"


Saturday, October 8, 2011

Metro style apps–loading an image from content

 

I’m writing a Win8 Metro style app and I wanted to expose an image from one of my view models. The image comes from a file that I have included in the project as content. After wandering through the samples, I was finally able to piece together this solution:

image

The XAML binds to the property using the Source attribute:

image

Some things to note. The image’s Build Action is set to Content and the path delimiters in the call to ResourceLoader.GetFile() use forward slashes (“/”). Using a backslash results the exception: NamedResource Not Found (HRESULT 0x80073B17).

Wednesday, September 28, 2011

Windows Phone 7 syncing to online Exchange results in Error 85010014

I upgraded my phone to the Mango update yesterday. Everything went fine except or so I thought. This morning I noticed that my phone hadn’t synced to the company’s Exchange account (Microsoft BPOS) for over 10 hours. When I forced a sync, it resulted in the following error message:

Not Updated
There’s a problem with red001.mail.microsoftonline.com. Try again later.
Last tried xx seconds ago
Error code: 8510014

I screwed around with the configuration for more time than I should have. I tried reentering the server. I tried entering the password again. I even removed the account from the phone and recreated it.

I even went to the Mobile Devices section on OWA and removed the device and then removed the account AGAIN.

Nothing worked.

This evening I stumbled onto something that did work.

I recreated the account on the phone (for the fourth or fifth time) and received the error again. On a whim, I unchecked all the content types under the Content to Sync section (Email, Contacts, Calendar and Tasks). I then issued a sync and it worked! I then added Email and re-synced. That worked! I continued to add the other three items, one at a time and followed by a re-sync.

Now everything is good again.

Thursday, September 22, 2011

Installing SQL Server “Denali” CTP3 on Windows 8 Preview

I, and I’m sure many other developers, have been eager to begin experimenting with developing on Windows 8 and building Metro style apps. I was fortunate to attend //build and receive one of the Samsung 700T tablets with Windows 8 and Visual Studio 11 already loaded. Unfortunately my wife and daughter also enjoy the tablet so it’s not available to me for long programming sessions.

So in order to make everyone happy, I have installed the Win8 Preview in a VM under VMware Workstation 8. Here’s the configuration:

Host: Windows 7 running VMware Workstation 8
Guest: Windows 8 Preview (build 8102), 2GB RAM, 2 processors and 40 GB SCSI hard drive

I installed Visual Studio 11 without any problem other than the fact that it installed SQL Server without giving me any choice for the configuration. The easiest way to remedy that was to uninstall SQL Server and then reinstall it.

You can probably guess that the reinstall didn’t go smoothly. If it had, there’d be no reason for this post, eh?

I started by download the installer from MSDN. There are a few choices depending on whether you wanted 32bit or 64bit, with or without tools. I pulled down the 64bit package that had the tools.

Then I copied it to the VM.

I double-clicked the .EXE, it unpacked into a temporary folder, a command windows briefly flashed and then nothing. So I did what anyone would have done. I double-clicked the .EXE again and watched the same sequence of events unfold. “Ah!” I said. I must need Admin privileges! No problem, I launched a command window as Administrator, navigated to the folder and ran the .EXE.

Same results as before.

I was confused because SQL Server had already been installed on the machine. I had just uninstalled it!

In the process of poking around on the machine, I ended up in the “Turn Windows features on or off” section of the Uninstall dialog. It was in there that I noticed that .NET 3.5 was not enabled. I enabled it, went back to the command prompt and retried the installer.

Something happen this time:

image

The installer can’t find .NET 4.0 but 4.5 is on the box! I hit Continue and the SQL Server Installation Center dialog appeared on the screen.

image

It was easy sailing from this point!

By the way, you’ll see this windows pop up a lot!

image

The installation continued without any problem and I was able to successfully install SQL Server “Denali” CTP3.

image

The unhandled exception dialog appeared again when I tried to close the SQL Server Installation Center dialog:

image

Hitting the Quit button finished closing the dialog.

If you’re curious, here are the SQL Server apps as they appear on the Win8 Start page:

image

And here’s the new SQL Server Management Studio

image

Monday, September 19, 2011

Notes from //build/ - Windows 8

I attended the Microsoft //build/ conference last week. This was the public world’s second introduction to the next version of company’s OS, Windows 8. It was also the first time that any real depth about the product was provided. The general consensus from those I spoke with was they were very pleased with the features shown.

Metro style apps

First, and most obviously, the Metro style from Windows Phone 7 has been brought forward as the primary user experience in Windows 8. Along with that, the OS has been optimized for touch but the stylus, mouse, and keyboard are all still supported.
 

image
(Attribution: Microsoft Corp.)

Note: It was pointed out that the correct capitalization is “Metro style apps” with a lowercase “S” on style and a lowercase “A” on apps.

The Start button has been morphed from a popup to an entire screen that consists of 1x1 or 2x1 “Live” tiles.
 image

The Start screen has all the apps that were pinned. There is an Apps screen that can be reached via Search. It contains all the apps that are installed:
 image
You can imagine that this screen could become quite crowded once a user starts installing apps. To help with this, Microsoft has introduced Semantic Zoom. Semantic Zoom used the “pinch zoom” gesture but instead of making the image larger, the entire view can be changed, providing something that is useful.

Using Semantic Zoom on the Apps page changes the view to:
 image
Semantic View was demonstrated on the Start page for the keynote, but it has been disabled in the Windows 8 preview that Microsoft released to the public.

Live Tiles

The content of the tiles can change to reflect information that is important to the user. The intent is to create a personal connection with the user. For example, the Stocks application will cycle the most recent information for each stock symbol that the user has added to the application. This will continue even if the application has been suspended.


 image

Suspended?

Yes. When the user navigates away from an app, the app will be suspended. The app will be notified that it is being suspended and given an opportunity to save any transient state. When the app is launched, it will be given an opportunity to restore that state. This is the Tombstone functionality that is currently used by Windows Phone 7 applications.
There is no concept of closing an app within the Metro style. Windows 8 will take of that for you.

Developing apps

A controversy was created when Windows 8 was first introduced to the public. Microsoft announced that HTML5 and JavaScript were the tools that would be used to create the new Metro style apps. At //build/ we learned the truth: Use what you know.


“Use what you know” was the tagline attached to the conference title and the message that Microsoft was delivering. At //BUILD/ Microsoft introduced a new development platform for Windows: Windows RunTime or WinRT. Using WinRT, a developer can create Metro style apps using:

-    C/C++
-    C#, VB.Net
-    HTML5 & JavaScript
 

image
(Attribution: Microsoft Corp.)

The best way to think of WinRT is as the Base Class Libraries for Metro style apps.
I plan to do another article that addresses WinRT.

And .NET?

.Net is still an applicable platform for developing both Metro style and Desktop (think traditional) apps. Not all of the .Net framework is exposed when developing a Metro style app. Where there is functionality that is duplicated with WinRT, or not allowed, those .NET APIs have been hidden.

Is Silverlight dead?


Microsoft did not deliver any message about Silverlight during the conference. Silverlight will be supported in the Desktop version of IE (The Metro style version of IE does not allow any plugins, for security and stability). I don’t expect Microsoft to make any announcement about Silverlight in the near term. This is a preview version of Windows 8. We are a long time from RTM.
There are still a lot of solutions to deliver, and since Silverlight will continue to be supported by Windows 8, I feel that Silverlight is still a valid solution platform.

Is a Metro style app written in HTML5 & JavaScript accessible via the browser?


Nope. A Metro style app is not a web page. They must be installed from the App store (consumers) or side-loaded (Enterprise).  Don’t let the HTML5 & JavaScript fool you. This is just the choice of presentation platform and language. It is no different from choosing XAML and C#.

Getting started

The keynotes, and most of the sessions, were recorded and are available on the //BUILD/ website (www.buildwindows.com).
The preview of Windows 8 can be downloaded from the Windows Dev Center (http://msdn.microsoft.com/en-us/windows/apps/br229516). One download contains a preview of both Visual Studio Express 11 and Expression Blend. If you’d rather install them yourself, you can download them separately from the Visual Studio website: (http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=27543).

Monday, June 6, 2011

Messin’ with the WCF Web API, part II – Content Negotiation & Formatters

In the last post, I briefly introduced the WCF Web API and walked through the creation of the first service that I constructed as I was experimenting with the framework. WCF Web API has a couple more tricks that it does. In this post I’d like to look at Formatters and their part in Content Negotiation.

Content Negotiation

In section 6.3.2.7 of his dissertation, Architectural Styles and the Design of Network-based Software Architectures, Roy Fielding describes Content Negotiation:

All resources map a request (consisting of method, identifier, request-header fields, and
sometimes a representation) to a response (consisting of a status code, response-header
fields, and sometimes a representation). When an HTTP request maps to multiple
representations on the server, the server may engage in content negotiation with the client in order to determine which one best meets the client’s needs. This is really more of a “content selection” process than negotiation.

So what does this mean? Let’s take a look at this in action. I’ll use the service that I built in the last post.

Using Fiddler I captured the request to retrieve the Xbox game with the ID of 12. That request looks like:

GET http://localhost:1064/MessinWebApi/games/12 HTTP/1.1
Accept: application/xml
Accept-Language: en-US
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)
Accept-Encoding: gzip, deflate
Connection: Keep-Alive
Host: localhost:1064

And the response:

HTTP/1.1 200 OK
Cache-Control: private
Content-Length: 405
Content-Type: application/xml; charset=utf-8
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?QzpcVXNlcnNcTWlrZVxEb2N1bWVudHNcVmlzdWFsIFN0dWRpbyAyMDEwXFByb2plY3RzXE1lc3NpbldlYkFwaVxNZXNzaW5XZWJBcGlPbmVcTWVzc2luV2ViQXBpT25lXGdhbWVzXDEy?=
X-Powered-By: ASP.NET
Date: Mon, 06 Jun 2011 00:51:12 GMT

<?xml version="1.0" encoding="utf-8"?><Game><Id>12</Id><Description>James Cameron's Dark Angel</Description><Developer>Radical Entertainment</Developer><Genre><Id>53</Id><Name>Action</Name></Genre><Name>James Cameron's Dark Angel</Name><Price>49.990000</Price><Publisher>Radical Entertainment</Publisher><Rating><Id>4</Id><Name>T (Teen)</Name></Rating><ReleaseDate>2002-09-01T00:00:00</ReleaseDate></Game>

And again:

GET http://localhost:1064/MessinWebApi/games/12 HTTP/1.1
Accept: application/json
Accept-Language: en-US
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)
Accept-Encoding: gzip, deflate
Connection: Keep-Alive
Host: localhost:1064

And the response:

HTTP/1.1 200 OK
Cache-Control: private
Content-Length: 293
Content-Type: application/json; charset=utf-8
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?QzpcVXNlcnNcTWlrZVxEb2N1bWVudHNcVmlzdWFsIFN0dWRpbyAyMDEwXFByb2plY3RzXE1lc3NpbldlYkFwaVxNZXNzaW5XZWJBcGlPbmVcTWVzc2luV2ViQXBpT25lXGdhbWVzXDEy?=
X-Powered-By: ASP.NET
Date: Mon, 06 Jun 2011 00:53:54 GMT

{"Description":"James Cameron's Dark Angel","Developer":"Radical Entertainment","Genre":{"Id":53,"Name":"Action"},"Id":12,"Name":"James Cameron's Dark Angel","Price":49.990000,"Publisher":"Radical Entertainment","Rating":{"Id":4,"Name":"T (Teen)"},"ReleaseDate":"\/Date(1030852800000-0400)\/"}

Now I didn’t make any changes to the service between those two requests. I only changed the Accept header in the request. For the first request I used a value of application/xml and application/json was used in the second. This is Content Negotiation. The client (Fiddler in this case) is using the Accept header to indicate the response formats that it can handle.

Note: To be fair, Content Negotiation can also be used to specify other preferences like encodings and language.

The client is free to indicate more than one format, or Media Type, as the value for the Accept header. The convention is that the values are specified in order of preference. The service is then free to use any of the specified formats that it can reproduce.

The role of rendering a response into the requested media type falls to the Formatters. These are components of the pipeline that I mentioned in the first post. The reason that I’m bringing this up at this point in time, seemingly out of order, is to introduce the base functionality that you get for free: the ability to product XML and JSON straight out of the box.

Custom Formatters

Xml and JSON are covered. You’ll need a custom formatter for anything else and luckily Microsoft has made it super easy to create them. The complexity required is dependent on the representation of the response that you’re trying to create. To demonstrate this, I’m going to create a custom formatter that returns the cover image for each game. When Shawn Wildermuth put together the Xbox games sample database, he included the cover image, as a jpg, in the Games table. My custom formatter is going to take the image and stream it back to the caller.

Note: I wish I could claim this as an original idea but it’s not. The sample application that ships with the WCF Web API has similar functionality. I’m just translating the functionality as appropriate for this data set.

To start I need to have Entity Framework return the Image column when a game is retrieved. To accomplish this I just have to add a property to the Game class with the same name as the field in the table:

    public class Game
    {
        public int Id { get; set; }
        public string Description { get; set; }
        public string Developer { get; set; }
        public Genre Genre { get; set; }
        public string Name { get; set; }
        public decimal? Price { get; set; }
        public string Publisher { get; set; }
        public Rating Rating { get; set; }
        public DateTime? ReleaseDate { get; set; }
        public byte[] Image { get; set; }
    }



Next I create a new folder in the solution called Formatters and add the following new class, ImageFormatter:


    public class ImageFormatter : MediaTypeFormatter
    {
        public ImageFormatter()
        {
            this.SupportedMediaTypes.Add(new MediaTypeHeaderValue("image/jpeg"));
        }
 
        protected override bool OnCanReadType(Type type)
        {
            return false;
        }
 
        protected override bool OnCanWriteType(Type type)
        {
            return typeof (Game) == type;
        }
 
        public override object OnReadFromStream(Type type, Stream stream, HttpContentHeaders contentHeaders)
        {
            throw new NotImplementedException();
        }
 
        public override void OnWriteToStream(Type type, object value, Stream stream, HttpContentHeaders contentHeaders, TransportContext context)
        {
            var game = value as Game;
 
            // it would probably be better to write some null image - {shurg}
            if (null == game) return;
            if (null == game.Image) return;
            if (0 == game.Image.Length) return;
 
            stream.Write(game.Image, 0, game.Image.Length);
 
            contentHeaders.ContentType = new MediaTypeHeaderValue("image/jpeg");
        }
    }



I believe the code is pretty simple to follow. I just want to point out a couple of things. In the ctor, I’m adding the image/jpeg media type to the collection of media types supported by this formatter. I’ve also provided an override for the OnCanWriteType() method. With those two pieces in place, I’m instructing the WCF Web API pipeline that this formatter can create an image/jpeg representation of instances of Game.


The last piece is to adjust the configuration so that WCF Web API will include the formatter in the pipeline. This is done when the routes are registered in Global.asax:


        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
 
            var config = HttpHostConfiguration.Create()
                .AddFormatters(new ImageFormatter());
 
            routes.MapServiceRoute<GamesResource>("games", config);
 
            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );
        }



To retrieve the cover image, I make a request for a specific game, just as in the previous two examples, but use the value "image/jpeg” for the Accept header:


GET http://localhost:1064/MessinWebApi/games/12 HTTP/1.1
Accept: image/jpeg
Accept-Language: en-US
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)
Accept-Encoding: gzip, deflate
Connection: Keep-Alive
Host: localhost:1064


Fiddler is kind enough to render the image for me:


image



Note: the art of the cover image is probably copy written by someone. All rights belong to them.


Formatters are a key component of the pipeline. A formatter will be responsible for creating a representation of a resource and the WCF Web API will handle engaging the correct formatter based on the Accept header in the request.

Sunday, June 5, 2011

Messin’ with the WCF Web API

Back in April, Microsoft released the fourth preview of WCF Web API on CodePlex. The intent of the WCF Web API is to make it easier to expose application data and services over HTTP. I’ve seen a lot of people throw in the REST descriptor but I’m not ready to go there yet.

I’ve spent the last couple of weeks playing with the WCF Web API and I’ve decided to put what I’ve learned out there for others. I’m imagining a series of posts but we’ll have to see where my schedule allows me to go.

In this first post, I want to basically put down the steps that I used to get started; consider this a step above the traditional Hello World program.

It’s all about the data

To get started, I first needed something to serve up. There is always the typical Northwind or AdventureWorks database that everyone is probably already familiar with. Instead, I went for something different. Shawn Wildermuth has been kind enough to to make available a database of XBox games. I’m running SQL Server 2008 so I grabbed the appropriate zip file and attached the database that is contained within it.

Next I need a host. A WCF service can be hosted within a number of application. I going to use an ASP.NET MVC 3 Application.

As long as I’m playing with new tech, I’m going to use the Code First features of Entity Framework 4. If you’re following along at home and Entity Framework isn’t already installed, you can get it via NuGet. The package name is EntityFramework.

The next thing is to create some classes that EF will use to expose the data in the Xbox games database. I’ll create three: Game, Genre and Rating and put them in the Models folder. This seemed like a good enough place to store them for now. I can always move them later.

    public class Game
    {
        public int Id { get; set; }
        public string Description { get; set; }
        public string Developer { get; set; }
        public Genre Genre { get; set; }
        public string Name { get; set; }
        public decimal? Price { get; set; }
        public string Publisher { get; set; }
        public Rating Rating { get; set; }
        public DateTime? ReleaseDate { get; set; }
    }



    public class Genre
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }



    public class Rating
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }



The data is accessed through an instance of DbContext. Mine looks like this:


    public class XBoxGames : DbContext
    {
        public DbSet<Game> Games { get; set; }
        public DbSet<Genre> Genres { get; set; }
        public DbSet<Rating> Ratings { get; set; }
 
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
 
            modelBuilder.Entity<Genre>().ToTable("Genres", "SimpleGames");
            modelBuilder.Entity<Genre>().HasKey(g => g.Id);
            modelBuilder.Entity<Genre>().Property(g => g.Id).HasColumnName("GenreID");
 
            modelBuilder.Entity<Rating>().ToTable("Ratings", "SimpleGames");
            modelBuilder.Entity<Rating>().HasKey(g => g.Id);
            modelBuilder.Entity<Rating>().Property(r => r.Id).HasColumnName("RatingID");
 
            modelBuilder.Entity<Game>().ToTable("Games", "SimpleGames");
            modelBuilder.Entity<Game>().HasKey(g => g.Id);
            modelBuilder.Entity<Game>().Property(g => g.Id).HasColumnName("GameID");
 
            modelBuilder.Entity<Game>()
                .HasOptional(g => g.Genre)
                .WithMany()
                .Map(m => m.MapKey("Genre"));
 
            modelBuilder.Entity<Game>()
                .HasOptional(g => g.Rating)
                .WithMany()
                .Map(m=>m.MapKey("Rating"));
        }
    }



The three properties Games, Genres and Ratings are used to access and manipulate the data using Linq. The OnModelCreating() override is the key to the Code First implementation. This method contains the mapping of the tables to the classes and defines the relationships between the tables, in terms of the classes.


The last item is to tell Entity Framework where to find the database. I’ve added the following connection string to the web.config file:


  <connectionStrings>
    <add name="XBoxGames"
         connectionString="data source=.\SQLEXPRESS; Integrated Security=SSPI; database=XBoxGames"
          providerName="System.Data.SqlClient"/>
  </connectionStrings>



 


Resources


The next step is to expose the game data over HTTP. If you were wondering when I’d get to WCF Web API, well, that’s now.


Like EntityFramework, WCF Web API can be added to the project using NuGet. The package to install is WebApi.All


image


The nice thing about using NuGet to add libraries to your solution is that it will automatically pickup any dependencies that are required. I needed four.


I’ve decided to create a resource called GamesResource and expose it over HTTP.


    [ServiceContract]
    public class GamesResource
    {
        [WebGet(UriTemplate = "")]
        public List<Game> GetGames()
        {
 
            using (var gamesRepository = new XBoxGames())
            {
                return gamesRepository
                    .Games
                    .AsNoTracking()
                    .OrderBy(g => g.Id)
                    .Include("Genre")
                    .Include("Rating")
                    .ToList();
            }
        }
    }



Accessing this resource will return a list of all the games, sorted by the game ID.



Note: Returning the entire table via HTTP is probably a very unlikely scenario, if for no other reason than you are stressing your network and server resources. The most common mitigation is to provide either filtering or paging capabilities. I plan to add paging in a future post.


The last thing that I have to do is add some configuration that exposes the new GamesResource. Within Global.asax.cs, I’ve changed the RegisterRoutes() method to:


        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
 
            routes.MapServiceRoute<GamesResource>("games");
 
            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );
        }



The call to MapServiceRoute() exposes the GamesResource as an Uri whose path starts with “games”. For example, the Uri http://localhost/games would be directed to our GetGames() method within GamesResource.


Starting the site with F5 and navigating to the games resource (for me is: http://localhost:1064/MessinWebApi/games) displays the list of games, encoded using Xml!


image


Returning a single game


I now want to allow the consumer of my service to fetch a single game using an Uri similar to http://localhost/games/12. This is easy and built into the WCF Web API via the UriTemplate parameter of the WebGet Attribute. This will look familiar if you’ve worked with ASP.Net MVC routing.


I’m going to add a new method to GamesResource called GetSingleGame():


        [WebGet(UriTemplate = "{id}")]
        public Game GetSingleGame(string id)
        {
            using (var gamesRepository = new XBoxGames())
            {
                int idToFind = int.Parse(id);
 
                return gamesRepository
                    .Games
                    .AsNoTracking()
                    .Include("Genre")
                    .Include("Rating")
                    .Where(g => g.Id == idToFind)
                    .FirstOrDefault();
 
            }
        }

 

 

GetSingleGame() is very similar to GetGames() with the exception that I have defined a token called {id} within the UriTemplate property of the WebGet attribute and I’ve added a parameter to GetSingleGame() to receive the value. The WCF Web API infrastructure will handle parsing the incoming request to get the ID and making the appropriate call to GetSingleGame().


When put into motion, a request for the game with an ID of 12 (http://localhost:1064/MessinWebApi/games/12) results in the following Xml:


image


This is just the “tip of the iceberg” for WCF Web Api. There is a very extensible pipeline built into the framework. I plan to explore some of these features in future posts.