Tuesday 7 October 2014

The certificate for the signer of the message is invalid or not found

A Colleuge of Mine was running the new Microsoft CRM SDK however when he tried to execute the Application it kept failing in:
Microsoft.IdentityModel accompanied with this message in Visual Studio: 'Could not load file or assembly ‘Microsoft.IdentityModel Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35’ or one of its dependencie. The system cannot fine the find specified.'

When I downloaded the WIF from Microsoft and Installed Its Application then I seen this:

SOLUTION : After doing a little research, I discovered that Windows Identity Foundation is built into Windows 8.1 that you can add via Add Programs and Features

After selecting Windows Identity Foundation 3.5 from the Add Roles and Features Wizard, I clicked Next and then Install. Once the installation wizard completed I run my application and it worked without any Issue. Well I did need to restart Visual Studio.

Friday 12 September 2014

Updating Images on the Fly

Streaming RAW Images to a Page. If you wish to stream an image to a page the easiest method is to convert it to a Binary 64 data type. Red dot This will enable IE to render the image as if it was stored on the server. Intrestingly this concept could work on the premise of playing a silent video clip at 1 frame per second in that if we store the Binary in a database we could in theory poll AJAX to and stream the image every second to the browsers image control updating it on the fly. In a company where I previously worked we used this approach to enable the user to scroll through footage as we took Keyframes every so many seconds that they could in essence watch the feed.

Monday 1 September 2014

Force File Download

Sometimes instead of letting a user view an Image or PDF I would rather them download it, This method below Forces the File (Doc / MP3 / PDF / Images) etc.. to be downloaded rather than rendered. This can save time and bandwidth on a website rather than the user continaully streaming the data on a frequent basis.
namespace FieldOfSheep
{
public class FileDownloadExtensions
{
 public static void ForceFileDownload(MemoryStream s, string fileName)
        {
            if (HttpContext.Current != null)
            {
                var responseObject = HttpContext.Current.Response;

                if (responseObject.IsClientConnected && s != null)
                {
                    responseObject.Clear();
                    responseObject.ContentType = "application/octet-stream";
                    responseObject.AddHeader("Content-Disposition",
                        "attachment; filename=" + fileName);

                    responseObject.BinaryWrite(s.ToArray());

                    responseObject.Flush();

                    responseObject.Close();
                }
            }
        }
 }
}       

Saturday 16 August 2014

ServiceStack Object Dump

There are times when you may need to dump an entire object to JSON for storing or for viewing, One example is to run an Object Dump on an Object or class at each step of a registraion page to see what the user is entering rather than getting final data at the end. This would help some people to see why users are dropping off the site for example. This is where 'ServiceStack.NET' comes into play - It has methods that support this functionality and one method is the 'Dump' method. Its rather simple and easy to use here is a working example.
  
  using ServiceStack.Text;
namespace FieldOfSheep
{  
  public static void Main(String[] args)
  {
     object myClass;
     var str = myClass.Dump();
    
     //Str now contains the JSON Values inside 'Object'
     
  }
}

Friday 25 July 2014

Integer Extension

When using Linq & Lambda expressions I find it easier to write a word based approach than writing long hand. Take for example this query
var something = (from myObject in myTable where myObject.Number > 1 && myObject.Number < 10 select myObject);
We could simply make this code cleaner and easier to read by writing
var something = (from myObject in myTable where myObject.Number.IsWithin(1,10) select myObject);
And the brains behind this method is this little extension method.
namespace FieldOfSheep
{
public static class IntegerExtensions
    {
        public static bool IsWithin(this int value, int minimum, int maximum)
        {
            return value >= minimum && value <= maximum;
        }
    }
}

Thursday 24 July 2014

Sanity Checker

Some times I need to run RegEx Scripts on text boxes. Here is a simple RegEx Sanity Checker for ASP.NET - You can download this from my SourceForge Page
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace FieldOfSheep
{
    [ToolboxData("<{0}:SanityChecker runat=server>")]
    public class SanityChecker : TextBox
    {
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            Page.ClientScript.RegisterClientScriptBlock(typeof(string), "SanityChecker.js", JScript.SanityChecker, true);

            this.Attributes.Add("onkeyup", string.Format("return SanityChecker(event,'{0}');", this.ClientID));

       }
    }
}
And the JavaScript code for this is
function SanityChecker(evt, element)
{
    var charCode = (evt.which) ? evt.which : event.keyCode;
    if(charCode > 93 && charCode < 112 || charCode > 46 && charCode < 91 || charCode > 185 && charCode < 223)
    {
        var rex = /^[^<>&\"\';]*$/;
        var value = document.getElementById(element).value;
        if(rex.test(str))
        {
            return true;
        }
        else
        {
            
            value = str.substring(0,value.length -1);
            document.getElementById(element).value = value;
        }
    }
}

Thursday 12 June 2014

Printing a Remote Document Using Aspose.Words to a Network Printer.

Sometimes clients need to print documents from Internal non public facing websites. At times there are also user access considerations in that we do not want to give them direct access to the file. For this we allow them the option to print the document to a designated printer on their network. We can do this by creating a simple printing method that takes a URL / Downloads It on the Server / Renders It Through Aspose and Prints it to a Network printer. Step 1. Identify the Network Printer we can display printers running this command
public static void Main(string[] args)
{
     DropDownList list = new DropDownList();
     
     System.Drawing.Printing.PrinterSettings.InstalledPrinters.Cast().ForEach
     (
         y => this.ddlPrinters.Items.Add(y)            
     );
}
Step 2. Run this method
var couldPrint = PrintDocument("http://www.somesite.com/somedocument.doc", this.ddlPrinters.SelectedValue);
Which in turn runs this block of code. Step 3.
public static bool PrintDocument(string uriToDocument, string printerName)
        {
            try
            {
                HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(uriToDocument);
                myReq.Credentials = new NetworkCredential(ConfigurationManager.AppSettings["Username"], ConfigurationManager.AppSettings["Password"]);
                WebResponse myResp = myReq.GetResponse();

                StreamReader reader = new StreamReader(myResp.GetResponseStream());

                MemoryStream ms = new MemoryStream();

                reader.BaseStream.CopyTo(ms);

                if (Path.GetExtension(uriToDocument) == ".doc" || Path.GetExtension(uriToDocument) == ".docx")
                {
 
                    Aspose.Words.Document document = new Aspose.Words.Document(ms);

                    document.Print(printerName);

                }
                return true;
            }
            catch (Exception ex)
            {
                //Log to your database here.
            }

            return false;
        }