Wednesday 22 February 2017

404 Not Loading Woff on Azure WebApp gett

Came across this with an Umbraco site where fonts and other assets where not getting loaded as expected. The path is correct and still not getting loaded.

Add this to Web.Config under the 


       
       
     
     


Azure WebApp Quirk:  Restart the WebApp

Even though I did a deployment ,manual hack  of the web.config to test stuff it still refused find the Woff files. The deployment and the manual changing of the Web.Config should in traditional IIS restarted the app pool and reloaded the whole app.  Even hacking the web.config so it brought down the app with 500  errors and changing it back still didn't fix it.  

Only restarting the web app worked.  Burned an hour on a trivial fix.

Tuesday 17 January 2017

Umbraco 7.X Get Prevalues from NodeId and Property Alias


Prevalues are where you can store configuration details you may want to use in backend code, such as url for a particular service etc....

Well this is how to get the Prevalues in C#.

var node = ApplicationContext.Services.ContentService.GetById(nodeId);
var propertyTypeTextExtractor = node.PropertyTypes.First(x => x.PropertyEditorAlias == "MyAlias");
int DataTypeID = propertyTypeTextExtractor.DataTypeDefinitionId;
XPathNodeIterator preValueIterator = umbraco.library.GetPreValues(DataTypeID);




Monday 1 February 2016

Start and Stop Windows Services on Visual Studio Build.


Introduction

If you have a windows service it is handle to get Visual Studio build to stop the service before build then restart it after it completes the build.


Setup your service

run this from an admin dos command prompt

sc.exe create myService binpath= "C:\services\MyService.exe" DisplayName= MyService



Pre-Build Event
Its in the project properties in Visual Studio, Build Events tab.

This will stop the service.

set svcname=MyService
net stop %svcname%
EXIT 0



Post-Build
Its in the project properties in Visual Studio, Build Events tab.

This copy the files built and start the service. Will also create the c:\services directory if it doesn't exist.

IF NOT EXIST "C:\services" MD "C:\services\";
attrib -r "C:\services\*.*" /s
:while1
Copy "$(TargetDir)*.*" "C:\services";
IF ERRORLEVEL 1 goto :while1



To Debug The service

Attach to the process number (PID) in the Services Tab in Windows Task Manager I've found to be the easiest way.





Wednesday 16 December 2015

C# Handle Exceptions on TASK

Tasks are great, run stuff in the background and let the UI without any bad freezing or usual problems with calling webservices or intensive tasks.

But Handling exceptions in called Task is easy enough when you know how

The Key is looking at the Task.IsFaulted bool.


  • Task.IsFaulted  is true :    The task was ended on an exception.
  • Task.IsFaulted  is false:    The task was ended normally (no exception)


Exceptions Returned by Task

The exceptions are returned as a collection, the top exception is fairly useless and you have look at the innerException to find the real cause for the issue.


What if I don't handle Exceptions

Well!! you code will run as the Task was run successfully and your calling layer/ui may be in a state that is less than ideal and your application may stop working and it may (read will)  take a long time for you to find out or debug the exact issue.  Tasks without exception handling are black holes waiting to happen.



CODE


 // Run Stuff in BackGround
 Task.Factory.StartNew(() =>;
 {
     //Do this in the background
      SomeIntensiveJob();
                
  }).ContinueWith(task =>;
   {
     //Check to see if there was a exception on the task handle it.
     if (task.IsFaulted)
      {
        string exMessage = "unknown error";
        string exStack = "unknow Exception is null";

        if (task.Exception != null)
         {
                       
          //Log all the Exceptions (maybe lots)
          foreach (var ex in task.Exception.InnerExceptions)
          {
             exMessage = ex.Message;
             exStack = ex.StackTrace;
             string errlog = $"Stuff , Exception : {exMessage} ,  Stack :  {exStack} ";
             og(errlog);
           }

         }

       //Update Your UI here with Errors
       Label.Text = $"Error on stuff {exMessage}, with stack {exStack}";
       return; // exit on error 
  } //////////////////////////  end exception handling ///////////////////////////////////

  // do this on the UI thread once the task has finished..
  // The task has completed without any issues so do your stuff
  runPostTaksStuff();

 }, System.Threading.CancellationToken.None, TaskContinuationOptions.None, TaskScheduler.FromCurrentSynchronizationContext());

Thursday 19 November 2015

C# WPF Window to be Dialogue to Win32


The Windows Form Method

This is how its done on Windows Forms forms, fairly straight forward.

//apphandle  is the application handle for your Win32 application
IntPtr windowHandle = new IntPtr(Convert.ToInt32(apphandle));
WindowHandleWrapper windowWrapper = new WindowHandleWrapper(windowHandle);
MyWinForm = new MyForm();

MyWinForm.ShowDialog(windowWrapper);


But WPF forms don't support 

public DialogResult ShowDialog(IWin32Window owner);



Doing this for WPF Forms

You do this by setting the owner of the WPF window/form.

// apphandle  is the application handle for your Win32 application
IntPtr FormHandle = new IntPtr(Convert.ToInt32(apphandle));

// Create new WPF Form
var app = new MyWpfWindow();

// New Helper 
var helper = new WindowInteropHelper(app);

// Point the FormHandle to owner of the 
helper.Owner = FormHandle;
app.ShowDialog();




Easy eh ?  

Wednesday 24 June 2015

Getting Non Escaped Double Quotes in Strings


I discovered this today by sheer chance and trying it will trying to build some dynamic Oracle scripts I was building.

Use single quotes in a StringBuilder to enclose a double quote  SingleDoubleSingle :  '"'

   StringBuilder sb = new StringBuilder();
            var col = "NameCol";
            sb.Append('"' + col + '"');
            sb.Append(" = @NAME ");


So easy when you know how!

Friday 15 May 2015

Persist Server side Object Between PostBacks on ASP.NET

This is handy if you want a collection to persist between postback, good for general prototyping or testing.


Set the Item to static as shown below.



        private static MyData  DAL { get; set; }
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
                return;

            DAL = new MyData();
            grid1.DataSource = DAL.Customers;
            grid1.DataBind();
        }