Posts

Showing posts with the label C#

Entity Framework 6 won't save (update) an entity returned from Stored Procedure

Symptom The issue was that code that previously worked, update property then save, wasn't working any more. The application is a .NET 4.6 Web API project using Entity Framework 6.  Issue The fix isn't in the code which saved (updated) the entity, but was caused because the entity that is updated isn't correctly formed by the Stored Procedure. Before the SP was added, the entity was returned from EF directly.   Fix After the stored procedure completes, refetch the entity using straight Entity Framework code such as ` await TwitterAccounts.FirstAsync(table => table.TwitterHandle == twitterHandle); ` where TwitterAccounts is a table in the EF Context.  Step to debug issue: capture save result The original EF code to save didn't capture the returned int of number of rows changed.  Before account.IsProperty = true; _context.SaveChanges(); return OK(); After account.IsProperty = true; int result= _context.SaveChanges(); if(result==0) { throw new Exception("proper...

Fetching a Private Key From An Azure Key Vault Certificate

If you create a private certificate in Azure Key Vault and use the fancy features like auto rotation, you might like to be able to fetch the private key from the vault and rehydrate it as a X509Certificate2 class in your C# code. Here is how you do that: KeyVaultClient keyVaultClient = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(GetToken)); var certificateBundle = await keyVaultClient.GetCertificateAsync(certificateIdentifier); var certficiateSecret = await keyVaultClient.GetSecretAsync(certificateBundle.SecretIdentifier.Identifier); byte[] certificateDecoded = Convert.FromBase64String(certficiateSecret.Value); var certificate = new X509Certificate2(certificateDecoded, password: ""); The Certificate Bundle passed back from the  GetCertificateAsync call has a .Cer property, however that is just the bytes for the pubic key, if you do this: var publicCertificate = new X509Certificate2( certificateBundle.Cer ); The X509Certificate2 instance will onl...

Solving PushSharp.Apple Disconnect Issue

While doing a load test of a new Apple Passbook application, I suddenly saw some 200K transmissions errors from my WebApi application. Searching the web I found that a “high” rate of connect/disconnect to Apple Push Notification Service being reported as causing APNS to do a forced disconnect.   While Apple does have a limit (very very high) on the number of notifications before they will refuse connections for an hour, the limit for connect/disconnect is much lower. After some playing around a bit I found that if I persisted the connection via a static, I no longer have this issue.   Below is a sample of the code. Note : we disconnect and reconnect whenever an error happens (I have not seen an error yet)    using Newtonsoft.Json.Linq; using PushSharp.Apple; using System; using System.Collections.Generic; using System.Security.Cryptography.X509Certificates; using System.Text; namespace RedDwarfDogs.Passbook.Engine.Notification { ...

How to convert SVG data to a Png Image file Using InkScape

Image
Introduction The project required the need to put the visual pie chart on a web page on the client and in a Pdf file created on the server. I eventually choose to use a Telerik Kendo DataViz Chart control on the client, and use a PNG file on the server for the Pdf file. This blog post will explain the process and code to convert the client-side pie chart Svg data to a Png file, and provide a download to the working code. Prerequisites You need to have a trial version of the Telerik Kendo library installed in order to make the sample code work. You also need InkScape installed. InkScape is the application that does the actual conversion. You will need to change the Web.config appSetting of “ExeDirectoryForInkscape” to represent your own installation location. Any SVG Data Will Work The pie chart was created using the Telerik Kendo DataViz chart control. While I name a specific control, as long as you have access to the SVG data, the same conversion process on the server will...

Restyling an Html <SELECT> Element from a Telerik Kendo Panel Bar

Image
Introduction Recently, I needed to restyle an HTML <SELECT> box so that the selected element had a different background color. In all other ways, the element could be exactly like an HTML <SELECT>. Below are both the original element, and the restyled element.     This type of style change allows the novice user to immediately identify the control’s implied usage while allowing for some coordination with the site and page’s overall style.   Technology Platform The project is an Asp.net MVC site with jQuery, and Knockout.js on the front-end requesting json content on the backend. For the purposes of this blog post, I’ve stripped down the functionality so that just the required elements are visible. The project was already using Telerik Kendo controls so morphing the Kendo Panel Bar into a <SELECT> element was suggested. The main work is in the css file but the entire working sample project is available for download.   Telerik ...

Using XMLHttpRequest.getResponseHeader to get Json Response Headers in jQuery

Introduction While developing a web page with a summary graph and a detail grid below, I needed to pass a small amount of data outside of the existing Json call data sets. I also wanted to do this without another asynchronous call back to the server. I wound up stuffing the data in a custom http response header while already on the server. This article explains how I retrieved the data on the client. Existing Client Libraries Both the graph and the grid were independent jQuery libraries with very specific Json response requirements. Stuffing the small amount of data in one of those response data sets could have been done with enough tinkering but I wasn’t sure how this would affect those libraries now or in future versions. While the data was related, it was also separate and I wanted the code design to reflect that. Server Http Response Stuffing This project uses Asp.Net MVC 4. The code to add the small amount of data should be the same across the .Net platform: HttpCon...

Steps for Consuming XML data in .Net

Image
Introduction While consuming third-party RSS feeds, I found I had to relearn how to deal with XML data. This post is meant to prepare any developer who needs to consume XML which they do not control. While I used RSS feeds, any XML will apply. I wanted to change the meta data and data of the XML file into a model of data that I could control with .Net classes and conventional data storage.   This post is organized to take you from an xml file to .Net classes able to consume, serialize, and test the xml. Generating an XSD file from an XML file using Xsd.exe The first step is to make sure you have the XML Schema Definition tool, xsd.exe , installed. It is part of the .Net Framework tools . Make sure the executable location is part of the system path , user path, or command prompt path. On my computer the path is “C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin” and I added it to the system path so it is available at any command prompt, regardless of the user.   Se...

Post-Ruby High with BDD in .Net

I mentioned in my last post how much I enjoyed Ruby and wanted to continue to use it in my project. That is easier said than done.   In order to leverage all the great Ruby ( RoR ) tools and methodologies I learned, I’m attempting to find .Net equivalents. This started with finding a Behavior Driven Development ( BDD ) tool. There are several that come up but they are a hybrid to a BDD (I picked SpecFlow ) tool siting on top of a TDD tool ( NUnit or MSTest).  I also needed a web driver to make calls to a browser and verify results. I looked at WatiN but went with Selenium .   The next two tools I need to add are a mocking tool ( Moq ) and an injection tool (probably NInject ).   Since the class used GitHub and Heroku , I’ll also stick with that. My GitHub account is DFBerry .  All my example code is posted up their in public repositories.

Results of .Net Developer’s 5 weeks with Ruby (RoR) and SaaS

Introduction Several things happened in a short period of time to influence my decision to take a Ruby/SaaS class. I’m a lifer on .Net or more specifically Asp.Net and it’s precursor, Asp. First, the MVC .Net book I was reading at the time said programmers either use .Net or Ruby for MVC development but not both. Second, an online UC Berkeley class was free and gave me both a new language ( Ruby ) and backfilled any software-as-a-service (SaaS) holes in my knowledge. Third, the class used the Agile methodology which I had bits and pieces of. I wanted to see someone else’s interpretation put into practice.   Ruby on Rails (RoR)/MVC I knew MVC , web protocols, and web development so the learning curve was all Ruby. The class had a quick pace where I knew how to do it in .Net but not Ruby. Ruby, fortunately, is a very easy language to pick up. It feels very much like a script kiddie toy but  more powerful.   Interpreted versus Compiled Ruby is interpreted w...

Combining Multiple Azure Worker Roles into an Azure Web Role

Image
Introduction While working on apps.berryintl.com ’s web service, it appeared from the Windows Azure project templates that I might need several worker roles because they cycle at different times. One worker needed to cycle every day. The other needed to cycle every five minutes. Since the work and the cycle rate were very different, it felt “right” to isolate each role. In this post, I’ll show you how I combined multiple worker roles into a single, underutilized Azure web role thereby keeping the concept and design of worker roles while not having to pay extra for them. In order to verify the solution, the project uses Azure Diagnostics Trace statements. The solution file is available for download . Combining Multiple Azure Worker Roles into an Azure Web Role Currently, I use a small Azure instance and there isn’t enough traffic to justify a separate worker role. I implemented Wayne ’s process for combining one worker role with a web role a while back. Now I needed to add anot...