Google+ Peter Bromberg's .NET Blog | All posts by admin

Peter Bromberg's .NET Blog All Things Programming

How to get an assembly's location on the fly

9. August 2017 13:30 by admin in

 

private string[] GetAssemblyLocation(string[] assemblyNames){string[] locations = new string[assemblyNames.Length];for (int loop = 0; loop <= assemblyNames.Length - 1; loop++){locations[loop] = AppDomain.CurrentDomain.GetAssemblies().Where(a => !a.IsDynamic && a.ManifestModule.Name == assemblyNames[loop]).Select(a => a.Location).FirstOrDefault();}return locations;}


USAGE:

string reportPath = GetAssemblyLocation(new string[] { "Reports.dll" })[0];
  System.Reflection.Assembly assembly = Assembly.LoadFrom( reportPath);

 

Rendering PDF reports as HTML for mobile devices

7. July 2017 05:58 by admin in

One of the big problems with things like Report Server and the RDLC ReportViewer control is being able to render PDF reports in mobile browsers. If you can convert these to HTML on the fly with the right library, mobile browsers will happily render the HTML.

One such library is PDF.js  It is extremely easy to install and you can get a PDF report as HTML.

You can set it up as follows:

  1. Download https://github.com/mozilla/pdf.js/archive/gh-pages.zip.

  2. Extract the ZIP file (a directory called "pdf.js-gh-pages" will be created).

  3. Copy the following directories to your website:

    • pdf.js-gh-pages/build/
    • pdf.js-gh-pages/web/
      The web/ directory contains a 1 MB PDF file called "compressed.tracemonkey-pldi-09.pdf". This file is only used as an example for the demo and can safely be removed.
  4. If you want to open a PDF from your website with PDF.js, simply link to the viewer and pass the location of the PDF file. For example:

    <a href="/web/viewer.html?file=%2Fyourpdf.pdf">Open yourpdf.pdf with PDF.js</a>

How to get the logged in user's first and last name with Auth0

13. June 2017 15:20 by admin in

Sample code:

public ActionResult Index()

        {

            var nameClaim = ClaimsPrincipal.Current.FindFirst("name");

            if (nameClaim != null && !string.IsNullOrEmpty(nameClaim.Value))

            {

                var welcomeName = ClaimsPrincipal.Current.FindFirst("given_name").Value + " " 

                    + ClaimsPrincipal.Current.FindFirst("family_name").Value;              

                ViewBag.Name = welcomeName;  

            }

            return View();

       }

Global Unhandled Exception Logging

28. November 2016 08:53 by admin in C#, EXCEPTIONS

We as developers often don't account for the fact that we may not be catching and handling all possible exceptions.This is particularly problematic in Windows Services as if something bad happens, the service may continue running but will be in a state where it isn't actually "doing anything".

Code like the following can be used to catch and handle any exception for which no handler has been wired in, and such exceptions can be logged using your preferred method:

 

namespace YourNamespace {

static class Program

{

[STAThread]

static void Main()

{

AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;

Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException); Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

}

static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)

{ HandleException(e.Exception);

}

static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)

{

HandleException((Exception)e.ExceptionObject);

}

static void HandleException(Exception e)

{

//Handle your Exception here

}

}

}