Skip to content
October 21, 2013 / pauldundon

Running Visual Studio with Admin Privileges in Windows 8

The problem: You have Visual Studio solutions which you want to run with admin privileges, and want to launch them by double-clicking on the solution file in Explorer.

Possible approaches: It is possible to run Visual Studio with admin privileges from the Start screen, but the solution file then has to be opened from within Visual Studio. It is also possible to disable UAC so that VS will run with admin privileges if you are logged in under an admin account, but this disables Metro apps.

Solution: Back up your registry, and then find the key

HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers\

Under this key, add a string value

C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\devenv.exe

Set the data for this to RUNASADMIN

This value relates to Visual Studio 2012. For VS 2010, substitute 10.0 for 11.0; for VS 2013, substitute 12.0 for 11.0.

Advertisements
October 9, 2013 / pauldundon

Why you can’t have a RESTful API

One of the questions which arises in discussion of REST is “what is a resource?” The difficulty with this question is that we have an instinctive grasp of what a resource is but it’s hard to pin down exactly what we mean. A resource is whatever flows over the wire; whatever we name with our URIs; the thing the user wants to work with. The question of how we might decide whether a given thing constitutes a resource or not is somewhat open and this is a problem if we are trying to use a process of articulating resources and resource types as an early step in our application design process.

This article by Roy Fielding implies a more specific answer. The “hypertext constraint” on RESTful APIs requires that the possibility of transition between resources be captured in the data for those resources in a way which is defined by the media type, and not the resource type. If we have an “order” resource type and a “customer” resource type, and it’s possible to transition from an order to a customer (ie, view the details of the customer who placed the order) then this fact must be captured in the details of the order in such a way that the client can present this possibility to the user without the client knowing that it is displaying an order entity as opposed to any other entity.

In his PhD, Fielding puts it like this: “Representational State Transfer is intended to evoke an image of how a well-designed Web application behaves: presented with a network of Web pages (a virtual state-machine), the user progresses through an application by selecting links (state transitions), resulting in the next page (representing the next state of the application) being transferred to the user and rendered for their use.”

This means that every resource delivered by the server has to contain a list of all the application state transitions which are possible from that resource: the links between application states just are the links between resources. And it follows that resources are application states.

This brings a number of challenges. To help articulate these, we introduce the notion of a desideratum. If we use Google to search for “REST”, we get back some search results (which is what we wanted) along with some ads, the Google logo, and other sundry items which aren’t particularly what we wanted. The search results are the desideratum: the thing we wanted. It’s also the thing we would want to get access to if we bookmarked the page and used the bookmark later – at this point, we wouldn’t care if the logo had changed or we saw different ads. Similarly, it’s what we want someone else to see if we share a link with them.

We might be tempted to think that desiderata are resources. However:

  1. An application state might permit transitions which are nothing to do with the content of the desiderata being viewed. A top-level menu, for example, has links which are largely independent of the item on display.
  2. An application state might forbid transitions which are part of the content of a desideratum in order to encourage users to take a particular path during a process
  3. The same desideratum might appear in more than one location in an application state graph. For example, a shopping cart might be visible in a view which is designed to allow searching for products in a catalogue, with a “checkout” link leading to another view of the shopping cart with a “proceed to payment” link

Thus, if resources are application states, then resources are not desiderata. This is significant because the temptation in writing a RESTful API is to provide HTTP access to a database of desiderata, which is, I take it, the main frustration Fielding raises in his article.

It’s useful to distinguish, at this point, the difference between a desideratum which is an HTML document and a desideratum such as a customer record which is expressed as an HTML document. The distinction becomes clear if we consider what would happen if the user wanted to modify the desideratum in question. We can imagine a browser with an edit mode, which allowed the user to edit HTML (perhaps in a WYSIWIG sort of way) and post the results back to the server. If someone were part of a team publishing research findings, for example, then they might work with HTML documents in this way, and would expect, when entering “edit” mode, to see all the relevant markup, links and the like. On the other hand, if they were working in an office maintaining a list of contacts, and wanted to edit a phone number, when entering edit mode they would be surprised to see, and probably not able to understand, the markup around the number.

If the desiderata are HTML documents, then the issue we have identified is not a problem. As a document author, I determine which other documents might be of interest to my reader, and embed links to these in the document as I write it. Thus, there are no transitions which are not content-determined (point 1). Similarly, I have no interest in the process my reader follows; on the contrary, I want them to be able to move around as freely as possible. Thus, no transitions are possible but sometimes forbidden (point 2) and no document needs to appear more than once in the application graph (point 3). All my links are in the data and the application graph is simply an emergent property of those links; there is no “application design process” as such because the application graph is fully determined by the data.

This is really the beauty of the web; we see it when we realise that the web is not a collection of applications, but one big application, engineered concurrently by millions of people, able to work independently. You can search Google and find this article; you can read it and then go and read Fielding’s comments about the hypertext constraint. The whole journey will involve multiple servers, and software engineered by possibly hundreds of people, yet all this is completely transparent – the web can work in this way precisely because no-one designs it; the shape of the web emerges from the bottom up, not the top down.

The difficulty begins because the driver for having an API is presumably that we want to write multiple applications, designed from the top down, which manipulate the same desiderata; and those desiderata are not HTML documents, but contact records, product details, tallies of who has made tea in the office etc. Thus, there must be multiple application designs in play, and multiple application state graphs. Each of those graphs defines a set of application states. Since these are resources, the server must be able to respond to a request for any application state we define. That is, the server owns the definition of possible application states. And since each application state encodes the possibilities of its outbound transitions, the server owns the definition of these as well. Thus, the interface to the server defines the application’s state graph.

The point of an API is, of course, that it is independent of the state graph – it abstracts the data storage and processing operations which are common across applications, without prejudicing how the application uses those operations. A RESTful API which satisfies the hypertext constraint simply cannot do this, so is not, in fact, an API.

August 4, 2013 / pauldundon

Android Chrome <audio> Element Doesn’t Play from Javascript

The problem: You are calling play() on an audio element, but playback doesn’t start on Chrome on Android.

The cause: There appears to be a feature in Chrome on Android which restricts <audio> tags from playing unless “authorised” by a user click. Calls to “play” will have no effect until the user clicks on the play button of the element, or a call to play is made inside a click handler.

Possible solution: No solution appears to be possible unless there is a click of some sort between creating the element and your first required play() call. However, if there is a such an event, calling play and then immediately calling pause will “authorise” the element.

December 22, 2012 / pauldundon

Running Startup Code in .NET Libraries

The problem: You have written a .NET Class Library for use in the context of a web application and want to run some code at startup time to initialise state within the library.

Possible solution: use the System.Web.PreApplicationStartMethod attribute

Limitation: This will only take effect when the library is loaded by IIS.

Example:

<Assembly: System.Web.PreApplicationStartMethod(GetType(Startup), "DoStartup")> 
Public Class Startup
  	Public Shared Sub DoStartup()
   		' Startup code goes here
  	End Sub
End Class 
September 20, 2011 / pauldundon

Inserting XML into the DOM using Ajax and jQuery

The problem: You want to use Ajax to retrieve some XML from the server, process is (eg to insert user content) and then insert it into the DOM.

The difficulty:If you retrieve it using ‘dataType’:’xml’, the browser won’t allow you to insert nodes from the resulting object into the DOM. Accessing the XML markup proves difficult as .html() returns an empty string and .text() returns only text nodes. Retrieving the data as html, or as text and parsing as html using $(result), makes changes to the content.

Possible solutions: Retrieve the xml using ‘dataType’:’text’ and parse it using $.parseXML(). Nodes from the resulting object will be insertable.

The following code will allow you to access the markup from a jQuery object wrapping an xml document. This means you can retrieve the data as XML, process it and then extract the markup for insertion as a string:

function extractXML(jq) {
    if (jq[0].xml) {
        return jq[0].xml;
    } else {
        var serializer = new XMLSerializer();
        var xml = serializer.serializeToString(jq[0]);
        return xml;
    }
}
September 11, 2011 / pauldundon

Using Images in an MVC WebGrid

The Problem: You wish to include images, or other content using HTML, in a column in a WebGrid. For example, you want to use an image for an “edit” link in each row. The HTML encoding of web grid contents makes this difficult.

Possible approach: insert placeholders where you want your images and substitute the actual HTML just before writing to the output.

Possible solution:

Add the following class to your project

Public Class WebGridFormatWrapper
    Private m_substitutions As New Dictionary(Of Guid, String)

    Public Function Format(rawFormat As Func(Of Object, String)) As Func(Of Object, String)
        Return Function(item)
                   Dim key As Guid = Guid.NewGuid
                   m_substitutions(key) = rawFormat(item)
                   Return key.ToString
               End Function
    End Function

    Public Function PostProcess(gridhtml As HtmlString) As String
        Dim result As String = gridhtml.ToString
        For Each key As Guid In m_substitutions.Keys
            result = result.Replace(key.ToString, m_substitutions(key))
        Next
        Return result
    End Function
End Class

In your view, construct an instance of the class and wrap your WebGrid format functions with calls to Format. For example, if you have a function GetImageHTML which returns an img tag, and you are using it like this:

cols.Add(grid.Column(format:=Function(currentItem) GetImageHTML(currentItem)))

Add

Dim wrapper As New WebGridFormatWrapper

and replace your code with

cols.Add(grid.Column(format:=wrapper.Format(Function(currentItem) GetImageHTML(currentItem))))

To output the grid as HTML, use

@Html.Raw(wrapper.PostProcess(grid.GetHtml(columns:=cols)))
September 11, 2011 / pauldundon

Avoiding Nested If…Then…Else Blocks for Mutually Exclusive Conditions

The problem: You want to run a series of mutually exclusive tests but the nature of the tests precludes using a Select…Case statement. For example, you wish to test the type of an object against several candidate types. You arrive at code such as the following:

        
If TypeOf (x) Is String Then
	DoStringStuff(x)
Else
	If TypeOf (x) Is Integer Then
		DoIntegerStuff(x)
	Else
		If TypeOf (x) Is Double Then
			DoDoubleStuff(x)
		End If
	End If
End If

However, you wish to find a clearer way to set out the code.

Possible solution: Refactor the code as follows

Do
    If TypeOf (x) Is String Then
        DoStringStuff(x)
        Exit Do
    End If
    If TypeOf (x) Is Integer Then
        DoIntegerStuff(x)
        Exit Do
    End If
    If TypeOf (x) Is Double Then
        DoDoubleStuff(x)
        Exit Do
    End If
Loop Until True