Tuesday 17 December 2013

Increasing the maximum received message size for WCF with SharePoint 2013

Today I was tasked with an issue of fixing a file upload problem. The server was returning a 413 "Request Entity Too Large" message. The file I was trying to upload was around 3MB, but the server refused anything this big. A few problems arose that are worth noting in fixing this issue.

A bit of Googling brought up the following result: http://msdn.microsoft.com/en-us/library/ff599489.aspx

Which in theory works. However it's not as straightforward as the article implies, as a few things were missed:
  • Run the code as farm administrator
  • Ensure the service name is lower case (this one caught me out..)
  • Clearing up

Run the code as farm administrator

Simply using the code in the MSDN article won't work if you try in your WCF service, you'll get an Access Denied error on SPWebService.Update(). The way I got round this was creating an event receiver on my project Feature that was scoped at the farm level, this allowed me to override FeatureActivated and FeatureDeactivating and run the code with the right permissions.

Ensure the key for the service name is lower case

There is no mention of this in the MSDN article, but it's very important as otherwise the changes won't take effect. I noticed that all the other keys in SPWebService.ContentService.WcfServiceSettings were lowercase, and it turns out mine has to be too. This should be the same name of your *.svc file under ISAPI.

Clearing up

Nothing is mentioned about clearing up either, but it needs doing and this caught me out too. Make sure you override the FeatureDeactivating and remove your settings from SPWebService.ContentService.WcfServiceSettings otherwise you will get strange behaviour (like the settings persisting). Also make sure you set the SPWebService.ContentService.MaxReceivedMessageSize back to 0 (the default setting).

The Code

   public class MyApiEventReceiver : SPFeatureReceiver  
   {  
     public override void FeatureActivated(SPFeatureReceiverProperties properties)  
     {  
       SPWebService contentService = SPWebService.ContentService;  
       contentService.ClientRequestServiceSettings.MaxReceivedMessageSize = -1;  
       SPWcfServiceSettings wcfSettings = new SPWcfServiceSettings();  
       wcfSettings.MaxReceivedMessageSize = (1024 * 1024) * 15;  
       contentService.WcfServiceSettings["wizard.svc"] = wcfSettings;  
       contentService.Update();  
     }  
     public override void FeatureDeactivating(SPFeatureReceiverProperties properties)  
     {  
       SPWebService contentService = SPWebService.ContentService;  
       contentService.WcfServiceSettings.Remove("wizard.svc");  
       contentService.ClientRequestServiceSettings.MaxReceivedMessageSize = 0;  
       contentService.Update();  
     }  
   }  

Tuesday 23 July 2013

Specifying an Assembly's Location with MEF

Today I came across a problem with dynamically loading assemblies using MEF, when the assembly being loaded has a dependency on yet another assembly. In my case this was SignalR.

Assembly Host (.exe) -> Assembly (.dll) -> SignalR (.dll)

I was typically seeing the error:
Could not load file or assembly 'AssemblyNameHere, PublicKeyToken=xxxxxxxxxxxxxxxxx' or one of its dependencies. The system cannot find the file specified.

The reason for this is because the CLR is looking for the dependency in the directory in which the assembly host is running, and not the location of the assembly MEF loaded in.

The way around this is in this MSDN article. By using the probing tag in the app.config, you can specify the directory to look for dependencies. The caveat with using probing is you can only specify sub-directories of the assembly host, so if you want to use an absolute path to another place on the disk then probing isn't the solution, but I'd argue whether you needed it in another place anyway.

The config change required should look something like this:

<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <probing privatePath="Modules\"/>
    </assemblyBinding>
  </runtime>
</configuration>

Dragging a ListView control in a fullscreen WPF touch app causes the whole window to shift

I recently had a bug with the ListView control in WPF where I had a ContentControl using a ScrollViewer with the ItemsPresenter inside of it. When using touch, and navigating through the items, the whole window would shift when you reach the end of the list, which then allowed the user to access the underlying Windows shell.

The cause of this is due to a potential bug in the ScrollViewer control, where the UIElement.ManipulationBoundaryFeedback event is fired, and is not handled by the ListView. As this is a routed event, it continues up the chain and eventually reaches the Window control, which then handles the event and results in an animation of the entire window.

MSDN states:
The ManipulationBoundaryFeedback event enables applications or components to provide visual feedback when an object hits a boundary. For example, the Window class handles the ManipulationBoundaryFeedback event to cause the window to slightly move when its edge is encountered.

The fix (or hack) for this, is to hook in to the event and handle the event, so that it does not bubble up to the Window control.

Friday 7 June 2013

Telltale Games on Windows 8

I recently got a pack of games by Telltale Games on the Humble Bundle Weekly Sale, however I was unable to play any of the games using Steam. The game would just begin to start and then exit. Googling didn't help much, but the support forum did. According to member Septarius: "Somewhere in the TTG engine there is a possible incompatibility with the Windows 8 version, either their fault or Microsoft's. This fault causes their games to crash when it detects a controller is attached and it tries to use it. The Windows 7 version does not have this problem."

Anyway, the solution is to download dinput8.dll (can be found in the forum link) and place it into the folder of the game you want to play. So in my case, for The Walking Dead it was "D:\Program Files (x86)\Steam\steamapps\common\The Walking Dead".

Telltale Games Support Forum: http://www.telltalegames.com/forums/showthread.php?p=682670#post682670

Although this appears to be the same for their other games, such as Hector, Wallace and Gromit, Back to the Future, and others.

Hope that helps someone else having the same issue!

Saturday 13 April 2013

Setting up xrdp for Ubuntu 12.10 (Quantal Quetzal)

I decided to give Ubuntu a go on an old computer today, but I've not got a spare monitor/keyboard/mouse to hook up to it and decided I'd like to remote in to it. I could either use VNC (which actually has the same issues as xrdp anyway) or use xrdp so I can use a familiar environment like Windows Remote Desktop.

However it wasn't as straight forward as installing xrdp, as I had a couple of issues, namely:

  • Blank (plain with just the desktop) screen when connecting
  • The 'd' key caused all windows to be minimized.

Installing xrdp

This is the easy bit. Open up the terminal (Ctrl+T) and type in:
sudo apt-get install xrdp

Fixing the plain screen

The problem seems to lay with the Unity interface, so unfortunately if you are wanting to use that then this fix isn't for you. However, you can use the older Gnome interface by first checking you have it installed:sudo apt-get install gnome-fallback

and then creating an xsession file that specifies what interface to use:
cd /home/adrian
echo "gnome-session --session=ubuntu-2d" > .xsession
sudo /etc/init.d/xrdp restart

Fixing the 'd' key problem

This problem appears to be due to one of xrdp's dependencies (as I had the same issue using VNC too), and I didn't have the problem when using the machine with a keyboard directly.
It seems that the <super> key is ignored when remoting in, and so any key combinations that require the super key are simply ignored. Clearly this is quite frustrating and so I just disabled them.

Open the dconf Editor:
dconf-editor

and navigate to:
org > gnome > desktop > wm > keybindings

Find anything with the "<super>" in it and either remove it all together (leaving behind square brackets "[]"), or replace it with another key.

After these fixes it's all up and running great!

Thursday 28 February 2013

Starters guide to web scraping with the HTML Agility Pack for .NET

I recently wanted to get a rough average MPG for each car available on the website fuelly.com, yet unfortunately there was no API for me to access the values, so I turned to Google and came across the NuGet package HTML Agility Pack. This post will get you up to speed on using HTML Agility Pack, basic XPath and some LINQ.

Before we start, please make sure to check the terms and conditions and any possible copyright terms that may be applicable to the data you are retrieving. You should be able to view this on the website, however it may vary from country to country. Please also keep in mind that you will effectively be accessing the site at a rapid rate, and so it would be sensible to save any communications to your local disk for later usage, and adding a delay between page downloads.

Getting Started

Identifying the data

The first thing you need to do is find where in the HTML the data is you want to download. Let's try going to fuelly and browsing all the cars. As you can see there are a large variety of cars available, and if you click on one of the models it takes you to a page that displays the year of the model and the average MPG for it. For my personal project, I wanted to obtain four values: Manufacturer, Model, Year and Average MPG. With this data I can then perform queries such as what vehicles between 2003 and 2008 give an MPG figure of above 50? - However, for the purpose of this blog post and simplicity, lets simply just retrieve a list of some models from a manufacturer.

So, lets start on the browse all cars page, and view the page source. If we look at the first manufacturer header on the page, we see Abarth, followed by AC, etc.

Open the page source (for Google Chrome: Settings > Tools > View Source), and find "Abarth":


You will see that the Manufacturer is wrapped in a <h3> tag. Below this, is a <div> that contains each model under the Abarth name: 500, Grande Punto and Punto Evo. Let's try and get hold of these models, but first, we need to setup the project.

Setting up the project

Create a new project in Visual Studio, a simple console project should suffice for this blog post. Add the HTML Agility Pack to the project via NuGet and add the following code:
1:    class Program  
2:    {  
3:      static void Main(string[] args)  
4:      {  
5:        const string WEBSITE_LOCATION = @"http://www.fuelly.com/car/";  
6:        var htmlDocument = new HtmlAgilityPack.HtmlDocument();  
7:        using (var webClient = new System.Net.WebClient())  
8:        {  
9:          using (var stream = webClient.OpenRead(WEBSITE_LOCATION))  
10:         {  
11:            htmlDocument.Load(stream);  
12:         }  
13:        }  
14:      }  
15:    }  

This simply loads the HTML page in to the HtmlDocument type so that we can run XPath queries against it to eventually get the value we are looking for.

Navigating Nodes with XPath

So, as noted earlier, we want to get a load of models from a manufacturer. Each <div> tag has a specific ID which we can use in our query (see "inline-list" below).
1:  <h3><a href="/car/abarth" style="text-decoration:none;color:#000;">Abarth</a></h3>  
2:  <div id="inline-list">  
3:       <ul>  
4:            <li><nobr><a href="/car/abarth/500">500</a> <span class="smallcopy">(34)</span> &nbsp; </nobr></li>  
5:            <li><nobr><a href="/car/abarth/grande punto">Grande Punto</a> <span class="smallcopy">(1)</span> &nbsp;</nobr></li>  
6:            <li><nobr><a href="/car/abarth/punto evo">Punto EVO</a> <span class="smallcopy">(3)</span> &nbsp; </nobr></li>  
7:       </ul>  
8:  </div>  

We can use this specific hook to get the values we want. So lets add the code:
1:  HtmlAgilityPack.HtmlNodeCollection divTags = htmlDocument.DocumentNode.SelectNodes("//div[@id='inline-list']");  

The above code returns a collection of <div> tags that represent each Manufacturer listed on the page. 

Let's break up the XPath syntax to make sense of it:
// - Selects nodes in the document from the current node that match the selection no matter where they are.
div - The specific nodes we are interested in.
[@id] - Predicate that defines a specific node.
[@id='inline-list'] - Predicate that defines a specific node with a specific value.

We can now dig deeper into each div tag, using a little more XPath and some LINQ to get the values we want.

Accessing the data using LINQ

OK, so within each item of the <div> tags we still have a load of rubbish we don't really need. All we want is the car models. Well we know each car model is between <a> (hyperlink) tags, with a href value. So using XPath and a little LINQ we can extract the data we need:


1:  HtmlAgilityPack.HtmlNode aTags = divTags.FirstOrDefault();  
2:  var manufacturerList = from hyperlink in aTags.SelectNodes(".//a[@href]")  
3:                         where hyperlink != null  
4:                         select hyperlink.InnerText;  

Line 1: Get the first manufacturer in the list 
Line 2: for each hyperlink inside the div, select all <a> tags with a 'href' node
Line 3: where the hyperlink isn't null (i.e. a href node was found), then:
Line 4: select the text inside of the hyperlink.

Conclusion

So, there you have it. You learned how to get specific values within a piece of HTML, a little XPath and some LINQ. Although the end data in this example probably isn't too useful, hopefully you can now see how you would expand upon this to find specific values and URLs to build a more complex system.