by Njål

Cancelling old S-* PayPal Subscriptions from code

2015-10-26_1408

PayPal does not support cancelling old S-* subscriptions from code/api’s. This means that either you (as a merchant) or the customer has to log in to PayPal’s API and cancel these subscriptions manually. #Pain.

Luckily – it can be automated using Phantom.js. Phantom.js is basically a rock solid webbrowser without a UI which you can program (JS) to do stuff for you. I have made a PayPal.js script which lets you input Email, Password and the old S- SubscriptionID you want to cancel.

Here’s how to use it:

  1. Download Phantom.js (2.0.0) – and Unpack it to a new folder – http://phantomjs.org
  2. Download PayPal.js and place it in the same folder
  3. Run phantomjs.exe –ssl-protocol=any paypal.js paypalemail@gmail.com MySecretPass S-123455634545234
  4. While it’s running – keep an eye on the folder. PNG screenshots will appear there providing you a peepshow of the progress.
  5. When done – your website will receive a IPN Callback from PayPal.

 

by Njål

Fixing HttpException 0x80004005 / COMException 0x80070001

When getting HttpContext.Current.Request.Params in a C# .NET IIS website – we got this error several times a day:

System.Web.HttpException (0x80004005): An error occurred while communicating with the remote host. The error code is 0x80070001. —> System.Runtime.InteropServices.COMException (0x80070001): Incorrect function. (Exception from HRESULT: 0x80070001)
at System.Web.Hosting.IIS7WorkerRequest.RaiseCommunicationError(Int32 result, Boolean throwOnDisconnect)
at System.Web.Hosting.IIS7WorkerRequest.ReadEntityCoreSync(Byte[] buffer, Int32 offset, Int32 size)
at System.Web.HttpRequest.GetEntireRawContent()
at System.Web.HttpRequest.GetMultipartContent()
at System.Web.HttpRequest.FillInFormCollection()
at System.Web.HttpRequest.EnsureForm()
at System.Web.HttpRequest.get_Form()
at System.Web.HttpRequest.FillInParamsCollection()
at System.Web.HttpRequest.GetParams()

 

Turns out that this is related to various offloading done by the Network Card (NIC) – meaning that the NIC is doing stuff on its own which the OS/IIS doesn’t always like. Go to the network card properties – and disable these features:

  • Flow Control
  • Ipv4 Checksum Offload
  • Large Send Offload V2 (IPv4)
  • Large Send Offload V2 (IPv6)
  • TCP Checksum Offload (IPv4)
  • TCP Checksum Offload (IPv6)
  • UDP Checksum Offload (IPv4)
  • UDP Checksum Offload (IPv6)

 

image

 

Also – make sure you disable Power Management for the NIC.

image

 

 

by Njål

Quick row count from Microsoft SQL Database

 

Recently, I’ve been involved in a cloud provider project where I had to do a row count on a Microsoft SQL Database table with more than 50.000.000 rows in it.

Doing

SELECT COUNT(*) FROM myTable

took about 10 seconds – since it performs a full table scan.

 

Here’s an alternative way (still accurate) of doing it that takes under 1 sec:

SELECT SUM (row_count)
FROM sys.dm_db_partition_stats
WHERE object_id=OBJECT_ID('myTable')
AND (index_id=0 or index_id=1);

 

You can run this from Microsoft Entity Framework as well:

long total = dbContext.Database.SqlQuery<long>("SELECT SUM (row_count) FROM sys.dm_db_partition_stats WHERE object_id=OBJECT_ID('myTable') AND (index_id=0 or index_id=1);").First();

by Njål

The directory specified for caching compressed content is invalid. Static compression is being disabled.

image

 

After adding this in my web.config (Azure Project)…

<staticContent>
    <mimeMap fileExtension=".svg" mimeType="image/svg+xml" />
</staticContent>

…IIS refused to serve any static content – such as js and png files. Log entries in Event Viewer said:

The directory specified for caching compressed content
C:UsersxxxAppDataLocaldftmpResourcesb503ce54-3fba-4772-beaa-415d94ffc214temptempRoleTempiisexpressIIS Temporary Compressed Files9ef2ab2d-28fc-4554-a09c-5ba867eb1d19 is invalid.
Static compression is being disabled.

The problem was that .svg files were already added to the staticContent list in my applicationHost.config – but still – the error message shown in the Event Viewer is pretty useless. There is nothing wrong with the directory specified to store compressed content.

So if you suddenly get this error – undo any changes in Web.config (remove all config under staticContent) and restart IIS (Express).

by Andreas

Email validation – finally a .NET regular expression that works!

imageNote: this is only relevant for .NET 3.5 and earlier. In .NET 4 the System.Net.Mail.MailAddress class includes validation and will throw an exception if instantiated with an invalid address. See comments below. 

Validating an email address sounds simple, and it really is! Until your fancy validator is released into the real world and people’s actual email addresses start pouring in, instead of the three email addresses you’ve been testing while developing: your work address, your Gmail address and an old Yahoo address you created in 2001.

chris-@somedomain.com
!habla-hobla%wow@somedomain.com
!def!xyz%abc@example.com
joe_blow_@somedomain.com
love/hate=relationship@example.com
$ilovemoney1234@example.com

They are, believe it or not, all valid according to the RFC spec. That doesn’t mean that you’ll be allowed to actually register them everywhere. Some (most?) email providers have their own rules that are much stricter than the specifications, simply because it makes no sense to allow all sorts of rubbish.

But if you’re writing a system allowing people to register using their email address, and you confirm their ownership of the given address by sending a confirmation email you really can’t be any stricter than the RFC and this is where the problem lies. I was also under the impression that “blah, how often would THAT become an issue?!”, but recently I got feedback from three different users within just a few weeks that they were unable to register. So I thought I’d better find a regular expression that actually works.

I’ve tried them all.. examples from Microsoft, Stack Overflow, random forums, cooked up my own and also converted a couple from other languages. It wasn’t until yesterday I stumbled across an old blog post by Phil Haack called I Knew How To Validate An Email Address Until I Read The RFC. He did all the hard work (i.e. reading the #¤%&”¤ RFC specs), and came up with an expression that so far seems to be working. So I thought I’d better save it here, because I know I will need this one again:

string pattern = @"^(?!.)(""([^""r\]|\[""r\])*""|"
                + @"([-a-z0-9!#$%&'*+/=?^_`{|}~]|(?<!.).)*)(?<!.)"
                + @"@[a-z0-9][w.-]*[a-z0-9].[a-z][a-z.]*[a-z]$";

Regex regex = new Regex(pattern, RegexOptions.IgnoreCase);
return regex.IsMatch(emailAddress);

 

So thanks to Phil for his efforts! Let us know if anyone finds a flaw in this one as well…

by Andreas

.NET HTTP Handlers (.ashx) – How to get access to Session

.NET HTTP Handlers (.ashx) are really useful for jQuery / JSON communication, but if you are using these as part of a bigger web application you will find that you don’t have immediate access to the current Session data. In a recent project I needed to validate some of the current user’s session information before allowing the handler to be invoked. In order to get access to HttpContext.Current.Session from the System.Web namespace you’ll have to add the System.Web.SessionState.IReadOnlySessionState interface in the class definition:

image

This will allow you to access the current session data:

// get session data
var sessionVariable = HttpContext.Current.Session["sessionVariableName"];
by thorhalvor

NCrunch = time saved

NCrunch is an automated continuous testing tool for Visual Studio .NET. It takes responsibility for running automated tests so that you don’t have to.

Instead of going through these steps when doing Test Driven Development:

  1. Write tests
  2. Run tests
  3. Write code
  4. Run tests
  5. Refactor code
  6. Run tests

NCrunch will take care of all the “Run tests” and you can therefore concentrate on writing and refactoring your tests and code.

How does it work?

I will give some simple examples below but you can also go to NCrunch’s About-page where you can see a video.

First of all, will you get new column with red/green/black dots on every line of code.

Green = Covered by successfull test
Red =  Covered by test but failing
Black  = indicating that there are no tests for this line of code.

It will test while you write and you get feedback while coding. No need to compile or save the document.

A specific example of the most basic steps is shown below:

A: You see a green dot for every line of code. That means at least one test is testing these lines and it is also successful.
B: If I hold the mouse over the dot at line 45 I can see that there are 4 passings tests for this part of code.
C: If I comment line 43-46, the color of the dots will immediately change because some tests are now failing. And some code is untested.
D: Holding the mouse over the red dot at line 42 gives a summary of why it is red. This part of code is covered by 3 passing tests and 1 failing.

This was only the basics of NCrunch. Other things it can do is for example: Code Coveage, Performance metrics, Inline Exception Details, Paralell Execution ++

My advice is therefore: Download, Install and Enable it. Try to not focus on it in the beginning, just “let it do its job”, get familiar with the new “dots” and explore its features step by step.

by thorhalvor

Are you still writing your own TemplateEngine?

Years ago I was on a project that needed a dynamic engine for writing documents based on a template. It had to be possible to add keys like “$name$” in a text and the engines job was to replace with a Name from a given context. I ended up writing a module that used reflection on my context-object and it worked the way I wanted. – But it took some hours developing it…

NVelocity / TemplateEngine

Later I discovered NVelocity. NVelocity is an open source project, and as many others starting with a ‘N’, is based on the Java project Velocity. Castle Projects has abstracted the templating process and made it even easier to use in their TemplateEngine Component. This component gave me all I wanted and more. If I just had knew earlier..

An example of a template for Castle’s TemplateEngine is given below:

Hi $customer.FirstName,
your account has been lost..
We are sorry for that!

Regards,
The bank

The code below is reading the template, mapping the key-properties and writing the output the the variable ‘text’. As you can see, it is putting the Customer object into an Hashtable with keyword “customer” and then forwarding it to the Processor as context. More or less the same I did in “my own engine”.

string text;
var customer = GetCustomer();
using (var writer = new StringWriter())
{
    var context = new Hashtable();
    context.Add("customer", customer);
    TemplateEngine.Process(context, "yourtemplatefilename.txt", writer);
    text = writer.ToString();
}

You can also do if/else and foreach logic (see here for more examples):

#foreach($person in $people)
<table>
       <tr>
           <th>Name</th>
           <th>Age</th>
    </tr>
</table>
#end

And:

#if($order.Status == "Undefined")
  Sorry, but we don't know this order.
#elseif($order.Status == "Created")
  Your order is being processed. Hold on!
#end
RazorEngine

Most new web projects created in the “.NET World” today, at least the one I know of, is based on ASP.NET MVC 3 and the parsing technology Razor. Developers have learned to use and to like the Razor syntax. And what could be better for a new templateengine than using the same technology? Matthew Abbot has done something about this and made in late 2010 the first version of the Razor Template Engine, in v2.0 called RazorEngine. It is opensource and can be found on GitHub and as a nugetpackage, ‘Install-Package RazorEngine’.

Simple example:

var template = "Hi @Model.FirstName! Knock knock who's there?";
var result = Razor.Parse(template, new { FirstName = "KenSentMe" });

or you could offcourse read the template from a file:

var template = File.ReadAllText("yourfilename.txt");

 

A nice feature is that the template-engine support anonymous types, – as you can see in the second parameter of Razor.Parse(). (and also DynamicObject and ExpandoObject). With this loose coupling to the context/model it makes it easier for a developer to extend it. He can now just add a new property directly instead of creating a specific class just to works as a “placeholder”.

I guess there are many more template engines “out there” but these are the two I have used on my last projects. I use RazorEngine on my current project, with html-email-templates, and it works very well. The main reason I choose RazorEngine is mostly because we use MVC 3 and Razor on the websites and the email template is based on the same style and context(Viewmodels). But also the fact that it is so easily downloaded and upgraded through the NuGet Package.

So If you still write your own templateengines I hope you will take a look on one of these engines next time.

by Njål

WebClient // HttpWebResponse – Problems with Chunked Transfer encoding

 

Doing a HTTP POST to a webserver using C# is pretty easy using the System.Net.HttpWebRequest. However – if you need to retrieve the answer/reply from the webserver – you’ll often get an exception:

Exception: 'Unable to read data from the transport connection: The connection was closed.'

When looking at the HttpWebResponse in the debugger – the length of the response is -1, so something strange is going on here.

When investigating further in the VS 2010 debugger/ Fiddler, I found out that the webserver (IIS 7 in this case) is sending the response using Transfer-Encoding: Chunked – which is relatively common in HTTP 1.1. It means that the length of the message isn’t specified in the HTTP Header Content-Length, but in the message itself.

Nothing wrong with this – but apparently HTTPWebResponse does not support Chunked Transfer Encoding (and thereby not HTTP 1.1).

The easiest way to fix this is to make sure the HttpWebRequest is sent using HTTP 1.0 – this way the server will reply with a HTTP Header specifying Content-Length.

HttpWebRequest wr = (HttpWebRequest)WebRequest.Create("http://www.degree.no");
wr.ProtocolVersion = Version.Parse("1.0");

An easier/alternative way of doing HTTP Communication in the .NET framework is the System.Net.WebClient class. But since it uses HttpWebRequest under its hood,  the same problem occurs here.

There is no way (afaik) to specify WebClient to use HTTP 1.0 – so use HttpWebRequest with HTTP 1.0 if you need to POST something to a server and read the response.

by Stian

The type or namespace name 'log4net' could not be found

If you have added a reference to log4net correctly and added your usings, you may get a compile error “The type or namespace name ‘log4net’ could not be found (are you missing a using directive or an assembly reference?)”.

This is most likely due to the Target framework being set to .NET Framework X Client Profile. Change this to .NET Framwork 4 (full version) and your application will compile again!

2011-01-27_1158

If you want to read more about how to set up Log4Net, read my colleagues blog post Configure log4net from code

Tags: ,