Wednesday, 21 August 2019

Adding an app.debug.config app.release.config transformations in .NET applications

Most applications need to run on multiple environments and with multiple configurations. At a minimum we usually have at least a local and production environment, but more often we have a local environment, a system test, UAT as well as our production and pre-production environments. Transformations allow you can have different settings for different configurations (debug or release). 

For example, the most common transformation is our connection string. For different databases , we will always have different connection strings That means a lot of different configurations are needed for each environment. Currently for ASP.NET, MVC and Web API projects, we have this functionality supported with our Web.config transformations. However for our app.config, i.e. for console or class libraries this is still not supported, out of the box. So we have to manually create our own transformations.

Previously, I have achieved these transformations manually, as you can see from this previous article I wrote: Adding an app.debug.config app.release.config transformations in .NET applications   , but now there are many different extension and Nuget packages for Visual studio, like Slow Cheetah and Configuration Transform which help you to achieve this much easily. 

Slow Cheetah 

Lately I have been using Slow Cheetah a lot and I really like it, this package allows you to automatically transform your app.config in Visual studio. You can either download it as a Visual Studio extension from the website or just add it to your project from Nuget.







Once installed ,you get a right click option to 
Add Transform, when you right click on your app.config in your project solution . 


Clicking this option will automatically create your new debug and release configs for you. 
And as you can see they are correctly nested underneath the app.config. This is because Slow Cheetah has automatically updated our project file to contain the necessary settings to tell the release and debug config that they are dependent upon the app.config. Before we had to manually make these adjustments to the project file ourselves.


If we open our project file and check the settings added, we can now see something like this, this is how we would normally have to edit our project file to look like, this basically defines the dependency between the app.config and the release and debug config.


Also notice the IsTransformFile element, this is the important setting for Slow cheetah. Instructing it to automatically transform the App.config file depending on the chosen configuration.

And that's it, a lot of manual work saved already. Now you can can add whatever customization's you like inside the transform files, for example if you want to tweak app settings and connection string.

E.G here I have just added an app setting to the app.config and the app.release.config , setting the value to true in the release config and false in the app.config. 




Note, the use of the replace transform key to prompt the replacement of this value and the locator key. 

So if we walk-through the example above, the replace transform key, sends the instruction to replace the true value from the isProd key located beneath the appSetting element. This is done using the xdt:Transform attribute. 

Also note , that all XDT documents, i.e. your app.config, and each environment config, need a namespace declaration in the root element, otherwise your transformations will not happen.





When you build your application the files are transformed and dropped into the output directory. If you are transforming the app.config then when the file is transformed it will be renamed in the output directory as usual to ensure that your application picks it up at runtime.

Preview Transform 


You can also quickly preview your transform using the Preview Transform context menu on the transform file. By selecting the release config and right clicking in the solution explorer, we are presented with the differences, which in our case, is the app setting differences we added between the app and release configs. However as you know, environment configs can become very long and detailed, so this Preview Transform tool can be a god send when you are trying to check settings or connection strings to debug an issue. 


Wednesday, 10 January 2018

How to fix: HTTP could not register URL error

Often when running a service , you might get the following annoying error :

HTTP could not register URL http://+:8000/MyWCF/. Your process does not have access rights to this namespace (see http://go.microsoft.com/fwlink/?LinkId=70353 for details).

To get around this you need to run up a command window and run the following for your service and domain :

netsh http add urlacl url=http://+:80/MyUri user=DOMAIN\user


Thanks to Brian via stackoverflow for this command and the explanation of the invalid exception text and how to find what's really going wrong.

Friday, 6 January 2017

Using FOR XML PATH(”) for String Concatenation

FOR XML PATH

Every now and then  I need to concatenate a column of strings returned from a SQL query, often I use the COALESCE function,

COALESCE





....but lately I've been using FOR XML PATH(”) a lot and am finding it easier to use. FOR XML PATH function can also be used for string concatenation when generating XML:










STUFF FOR XML PATH
Another handy function for string manipulation is STUFF. The STUFF function inserts a string

into another string. It deletes a specified length of characters in the first string at the start position and then inserts the second string into the first string at the start position.


STUFF (character_expression , start , length ,character_expression)

So applying that to the code above , we get something like this :

Thursday, 5 January 2017

Calling a action/controller with params and showing a partial view in modal dialog in MVC3/4/5

Pretty much every MVC user application you write need a modal popup or even modal form dialog containing a partial view.
Therefore we need a mechnism for calling an action and controller , passing in an Id and returning a view model with results to a partial view inside a modal dialog.

There are many different options now but i always go back to this method when i need to achieve this as it gives me so much control not only over the params i can pass in ,but dialog size and style too.

This is the jquery code , i copy this to a separate js file in the project script directory.

$(document).ready(function () {
      $(".openDialog").on("click"function (e) {
          e.preventDefault();
          $("<div></div>")
              .addClass("dialog")
              .attr("id", $(this)
              .attr("data-dialog-id"))
              .appendTo("body")
              .dialog({
                  title: $(this).attr("data-dialog-title"),
                  close: function (ev, ui) { window.location.reload() }, //function () { $(this).remove() },
                  width: 600,
                  modal: true,
                  height: 'auto',
                  show: 'fade',
                  hide: 'fade',
                  resizable: 'false',
 
                  //buttons: {
                  //    "Close": function ()
                  //    { $(this).dialog("close"); }
                  //}
              })
 
 
          .load(this.href);
       
 
      });
 
   
 
      $(".close").on("@Html.ActionLink("Edit""EditDocumentDetails""Document",
             new { id = item.DocumentId }, 
             new 
             {     @class = "openDialog", 
                   data_dialog_id = "interestDialog", 
                   data_dialog_title = "Edit Document Details" 
             })click"function (e) {
          e.preventDefault();
          $(this).closest(".dialog").dialog("close");
      });
   
  });


An example of where this is called from a view is :

@Html.ActionLink("Edit""EditDocumentDetails""Document",
             new { id = item.DocumentId }, 
             new 
             {     @class = "openDialog", 
                   data_dialog_id = "interestDialog", 
                   data_dialog_title = "Edit Document Details" 
             })

And an example of your controller code :

public ActionResult EditDocumentDetails(int id)
      {
          var doc = this.context.ApplicantDocuments.Find(id);
          if (doc == null)
          {
              return HttpNotFound();
          }
 
          IEnumerable<SelectListItem> docTypes = this.GetReferenceList(Constants.ReferenceDocumentTypeList);
          ViewBag.DocumentTypes = new SelectList(docTypes, "Value""Text", doc.DocumentTypeRef);
          
          return PartialView("_EditDocumentDetails", doc);
      }

Saturday, 17 October 2015

CTE , ROW_NUMBER() PARTITION BY , useful tools when face with duplicate data

I recently came across an issue where I was retrieving data for a UI and dealing with potential dupes. for my purposes I used CTEs and row_number function to solve my particular issue.

 A CTE is essentially a kind of temporary table or a named result set off of a physical table (sort of like an in-line view).








With this function, you can number rows in your result set.  Even better, you can PARTITION BY to split your result set up into groups.  I might not want to see 1-10 as my row numbers, I might want to see 1-5 and 1-5 based on some column that I decide to partition the data by.  Note, this is a horizontal partition of rows.




 

  •  

    Thursday, 4 June 2015

    A closer look -Enumerable.Range (System.Linq)

    Enumerable.Range is a small static method in the  Enumerable class from System.Linq

    The Enumerable.Range() method performs a very simple function, but it’s results can be used to drive much more complex LINQ expressions.  In it's simplest form it can be used to generate a sequence of consecutive integers. 

    It accepts two int parameters, start and count, and constructs a sequence of count consecutive integers, starting at start:


    • Range(int start, int count)
    • Returns a sequence of int from start to (start + count – 1).


    So, for example, the code snippet below will create an List<int> of the numbers from 1 to 10:
       1: var numbers = Enumerable.Range(1, 10).ToList();
       2: string nums= string.Join(",", numbers); 
      3: // result will be : "1,2,3,4,5,6,7,8,9,10"

    Not just for numbers !

    Or it can be used to  to generate a string containing the English alphabet
       1: IEnumerable<char> letters = Enumerable
       2:  .Range(0, 26)
       3:  .Select(x => (char)(x + 'a'));
       4: string alphabet = string.Join("", letters); 
       5: // result will be : "abcdefghijklmnopqrstuvwxyz"
    Handy eh ?

    Getting a bit more complicated now, if we wanted a list of the first 5 even numbers, we could start with the number of expected items and use the .Select() clause in conjunction with the mod operator to bring back only the even numbers :
       1: var evens = Enumerable.Range(0, 5).Select(n => n*2).ToList();
       2: string ans = string.Join(",", evens ); 
       3: // result will be : "0,2,4,6,8"
    Alternatively , you can achieve the same result using the .Where() clause against the total results to filter it down:
       1: var another= Enumerable.Range(0, 10).Where(n => (n % 2) == 0).ToList();
       2: string ans = string.Join(",", another); 
       3: // result again will be : "0,2,4,6,8"
    Or if you want to provide a min value, max value, and a number of steps, as demonstrated in this question on StackOverflow and answered by by John Skeet:

    i.e. basically what was needed here was to return something like this :

    0, 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000

    Were the min was 0 , the max 1000 , and he wanted to get there in 11 steps.
       1: int numberOfSteps = 11, max = 1000, min = 0;
       2: var example = Enumerable.Range(0, numberOfSteps)
    .Select(i => min + (max - min) * ((double)i / (numberOfSteps - 1)));
       3: string ans = string.Join(",", example); 
       4: // result will be : "0,100,200,300,400,500,600,700,800,900,1000"
    
    

    Conclusion

    The Enumerable.Range() method performs a very simple function, but it’s results can be used to drive much more complex LINQ expressions.

    Friday, 29 May 2015

    Displaying SSRS Report in MVC using MvcReportViewer -Part II

    Embedding a Local Report


    In my previous post we discussed how to install the MVCReportViewer package via Nuget into your application to avail of the helpers to embed SSRS reports into your MVC views.

    In this article we will discuss using the MVCReportViewer  package and helpers to embed local reports.

    Most often people want to embed SSRS reports from the server into thier views, but sometimes we just want to create a simple local report in Visual Studio and display that in our view. This is the scenario we will discuss below.

    Creating reports in Visual Studio 2012/2013

    Visual Studio 2012/2013 comes with reporting capibilites out of the box. Simply right click in your solution explorer in Visual Studio and Add Item/ Reporting and you can see you have the option to add a Report or use the Report Wizard.



















    This provides you with good basic reports, it's like a pared down version of the tools and controls you get with real SSRS. If this suits your needs you need go no further. However if you need more capilibilites than whats on offer here you will need to install the SQL Server Data Tools – Business Intelligence tools  for Visual Studio 2012/2013.

    SQL Server Data Tools – Business Intelligence for Visual Studio 

    Currently the URL to download Microsoft Visual Studio Tools for Applications component for Visual Studio 2012 can be found here:

    http://www.microsoft.com/en-ca/download/details.aspx?id=38807

    Once you have install it, you will be able to create and work with SSRS reports within Visual Studio. Here are the basic steps for setting up a new SSRS report project:

    Open up Visual Studio

    • Select File –> New –> Project
    • You will now see a new project template option called Business Intelligence
    • Expand this project template and select the Reporting Services option.
    • You can now either choose to use the setup wizard or just a plain project template.


    Regardless of whether your requirements necessitate the use of the Business Intelligence tools or the standard out of the box reporting tools in Visual Studio, lets now disucss adding a basic report to your project so you can embed it in your MVC view.

    DataSets

    Before creating your report, you need to define your dataset. Lets create a Report folder in our project. It is a good idea to organise your folder structure within your application in a logical manner , so we will use this folder for all our Datasets and reports.

    Using the standard out of the box reporting functionality in Visual Studio 2012/2013, right-click in your Report folder and Add New item.

    Go to Data-> and select Dataset. Call it 

    Displaying SSRS Report in MVC using MvcReportViewer -Part I

    In this article we will discuss installing the MVCReportViewer package into your MVC application to get it ready to embed reports. 

    In Part 2 , we will discusss embedding local reports into your MVC application and in Part 3 we will discuss the changes needed to embed reports from yourn SSRS server.

    Embedding SSRS or indeed local RDLC reports into a  view in an ASP.NET MVC application is easy using the MVCReportViewer package , and the HTML Helpers it comes with.

    The first step to get the MVCReportViewer package installed into your project , to do this you can use the Package manager console in Visual Studio , using the following command:

    PM> Install-Package MvcReportViewer
    This will add the relevant scripts, Web.Config app settings and report viewer appx to your MVC Application which are needed to embed your reports into your views.

    After sucessfully installing the MVCReportViewer package you will notice some changes to your application. 

    The following App settings for the report viewer have been added to your Web.Config:


    <add key="MvcReportViewer.AspxViewer" value="/MvcReportViewer.aspx" />
    <add key="MvcReportViewer.AspxViewerJavaScript" value="~/Scripts/MvcReportViewer.js" />
    <add key="MvcReportViewer.ErrorPage" value="~/MvcReportViewerErrorPage.html" />
    <add key="MvcReportViewer.ShowErrorPage" value="true" />

    <add key="MvcReportViewer.ReportServerUrl" value="http://localhost/Reportserver_SQLEXPRESS" />
    <add key="MvcReportViewer.Username" value="" />
    <add key="MvcReportViewer.Password" value="" />
    <add key="MvcReportViewer.EncryptParameters" value="false" />
    <add key="MvcReportViewer.IsAzureSSRS" value="false" />

    <add key="MvcReportViewer.LocalDataSourceProvider" value="MvcReportViewer.SessionLocalDataSourceProvider, MvcReportViewer" />


    Key settings to note here , are the AspxViewer setting, which points to the aspx page which has also been installed by the NuGet package . It is this page that will be used to display our report in an iframe. 

    The ReportServerUrl and LocalDataSourceProvider settings are important depending on wehther you want to embed a server report or a local one. I will use both later on when i give examples of both scenarios.

    And that's it, now you are ready to embed a server or local report into your MVC views. See the following articles for examples of both.


    Monday, 16 February 2015

    Know It, Prove It

    Anyone else doing the "Know It, Prove It" challenge on the Microsoft Virtual Academy site this February ? I'm half way through the Web Development challenge and really enjoying it.

    Image result for know it prove it challenge

    http://www.microsoftvirtualacademy.com/LearningPaths/Dashboard.aspx

    Even if you're not taking part in the challenge, all the courses on the Virtual Academy are free and i highly recommend watching Learn-html5-with-javascript-css3-jumpstart-training with Michael Palermo and Jeremy Foster if you have a hour or two to spare. Its also recommended viewing for the Microsoft Exam 70-480 Programming in HTML5 with JavaScript and CSS3 if you're taking this Certification.

    Thursday, 8 January 2015

    Output Caching in ASP.NET MVC4

    You can dramatically improve the performance of your ASP.NET MVC application very easily by taking advantage of the built-in output caching attributes. If you have an application that gets alot of traffic on a specific page , or returns a large list of records from a database you can use the output caching mechanism to cache the content returned by a controller action. And in doing so the same content does not need to be generated each and every time the same controller action is invoked causing unnecessary database hits and slowing down your page.

    Most Output caching can be implemented very easily in MVC4 by using the standard OutputCacheAttribute class without having to go down the route of building a custom output cache provider. By decorating your controller actions with the outputcache attributes , you can maintain control over the duration of the cache of your actions.

    First off , it's important to note that by default actions in asp.NET MVC have a cache-control of private.Which you can see by creating a simple action like the one below :

    public JsonResult Details(int id)
    {


    ////code here to get your results for your id


    return Json(result, JsonRequestBehavior.AllowGet);

    }


    And then checking the header in the response in Fiddler or your tool of choice , and  you will see the cache-control :

    Cache-Control:private
    Connection:Close
    Content-Length:81836
    Content-Type:application/json; charset=utf-8
    Date:Mon, 29 Oct 2012 08:08:44 GMT
    Server:ASP.NET Development Server/11.0.0.0
    X-AspNet-Version:4.0.30319
    X-AspNetMvc-Version:3.0

    This means that the browser might cache such ajax request made by a JavaScript client, which most of the time might not be desired. And can result in unexpected behaviour , especially in IE which is cache hungry. So often it is desirable to control what is cached and when yourself just to have some confidence in what is going on.


    ASP.NET MVC Build-in caching mechanism

     You can control output caching by using the build-in output cache in asp.NET MVC. To do this you can decoration your controller actions with the outputcache attribute as follows.

    [OutputCache(Duration = 6)]


    public ActionResult Search(string searchText)
    {


    return Json(SearchWebService.MyWebServiceInstance.AutocompleteSearchClient(searchText),JsonRequestBehavior.AllowGet);

    }

    Now if we check our headers again , we can see our cache-control change :

    Cache-Control: public, max-age=6
    Expires: Mon, 29 Oct 2012 09:18:37 GMT
    Last-Modified: Mon, 29 Oct 2012 09:18:34 GMT
    Connection:Close
    Content-Length:81836
    Content-Type:application/json; charset=utf-8
    Date:Mon, 29 Oct 2012 08:08:44 GMT
    Server:ASP.NET Development Server/11.0.0.0
    X-AspNet-Version:4.0.30319
    X-AspNetMvc-Version:3.0

    To see it in action for yourself , create a simple action like the one above , at first without the caching attribute and put a breakpoint in your controller action , you will note when you F5 the page , that it will hit the breakpoint every time.After decorating your action with the atttribute above , if you then run up your application , and F5 your page again , you can see that after the initial breakpoint being hit , the contents are cached and the breakpoint isnt hit again until your cache duration times out.

    VaryByParam

    The VaryByParam allows you to have different cached versions of the very same content. This property enables you to create different cached versions of the very same content for example when a query string parameter varies. By setting the VaryByParam property to “none” ,when the action is invoked, the same cached version of the action is returned i.e. any query string parameters are ignored , so usually if we have an action for a particular id , we'd want this to be cached based on the id , so we dont get the same results back.

    If we have a number of params , and we want them all to be noted in the cache , we can either specific them semi-colon seperated list, or use the * in the VaryByParam value. E.G in the action which populates a grid below , we dont want the paging , sort and id queryparams to be ignored.

    [OutputCache(Duration = 1, VaryByParam = "*")]
    public ActionResult Index(int accountId, string page, string sort, string sortdir)
    {

    ///code to get all accounts



    return PartialView("_AccountsList", data);

    }