Thursday, July 14, 2016

Keyset does not exist

Problem:

I have some code that makes a call to an external web service that is secured using X.509 certification.  If I call the service using the application pool set to Local service then it executes.


When I set the app pool to use a domain account domain\svc.account the webservice fails with the error: Keyset does not exist.


Solution:

This certificate is installed correctly but the domain account needs to be granted permissions to that certificate.  This will allow the service account to access the private key attached to the certificate.


  1. Start -> Run -> MMC
  2. File -> Add/Remove Snapin
  3. Add the Certificates Snap In
  4. Select Computer Account, then hit next
  5. Select Local Computer (the default), then click Finish
  6. On the left panel from Console Root, navigate to Certificates (Local Computer) -> Personal -> Certificates
  7. Your certificate will most likely be here.
  8. Right click on your certificate -> All Tasks -> Manage Private Keys



       9.Add you service account to the access list.  It will need a minimum of read permissions.




      10.It is possible that you may be required to add the local IIS_USRS group to the access list.  Grant it read permissions.

Source

Wednesday, July 6, 2016

Bootstrap 3 and IE11 clash. Flushing footer to bottom of the page, twitter bootstrap

Problem:


I was having issues with my site in IE 11.  Chrome and other browsers were doing fine.
The site navigation bars would not work with the vertical scroll.  The behavior was quite ugly.  The page would render correctly and the navigation menus would work fine.  As soon as the user scrolled the fixed horizontal navigation bar would spread all over the screen.  The only way to get the browser to redraw the screen was to resize the window.  This is hardly a state I can hand over a solution to a customer.:



Solution:

This was a footer and bootstrap problem.  First issue was that the main screen was using a fixed position reference which would not work properly in ie 11. 


{
  position:fixed;
}


The fixed property forced the footer to stay in place but the navigation bar would bleed into the main work area. 


Step 1: Removed the fixed position but then the footer started to move around. 


Step 2: The final fix was to use the bootstrap css class,navbar-fixed-bottom, this forced the footer to stay in place.


<div class="navbar-fixed-bottom">
...
</div>







Source:

  1. http://stackoverflow.com/questions/10099422/flushing-footer-to-bottom-of-the-page-twitter-bootstrap
  2. https://getbootstrap.com/examples/navbar-fixed-top/
  3. https://getbootstrap.com/components/#navbar
  4. http://getbootstrap.com/components/#navbar-fixed-bottom

Tuesday, July 5, 2016

MVC 5 Razor - Compilation Error CS0246 - Type or namespace could not be found


Problem:

I ran across this issue in a vs 13 project. I was running in my dev environment with a project I had reused. The project was building and running until I removed the web service reference. I followed the prescribed method in MSDN (see reference). Then each time I built the project it compiled. However, each debug session resulted in a runtime error.

Compilation Error

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. 

Compiler Error Message: CS0246: The type or namespace name 'ExpiredNamespace' could not be found (are you missing a using directive or an assembly reference?)





Solution:

I was absolutely puzzled by the error since I had removed all references to the namespace ExpiredNamespace from the project.  It was not reference anywhere.  If I was missing a reference then I should be seeing a compiler error. 


Then I began looking at the trace information and read up on the MVC razor process.  That is when I found my rogue reference.  The error always occurred when the View attempted to run.  I spared a look at the web.config.


<pages pageBaseType="System.Web.Mvc.WebViewPage">

<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Optimization"/>
<add namespace="System.Web.Routing" />
<add namespace="ExpiredNamespace'" />
</namespaces>

</pages>


The base page type renders the on run compilation using these classes.  I had inadvertently carried over the reference in the web.config from my old project!


Note to self, scrub your test projects carefully before reusing them.






Reference:

https://msdn.microsoft.com/en-us/library/d9w023sx.aspx

Friday, July 1, 2016

LinqKit predicate builder - Multiple WHERE Clauses with LINQ extension methods


// initial "false" condition just to start "OR" clause with
var predicate = PredicateBuilder.False<YourDataClass>();
if (condition1)
{
    predicate = predicate.Or(d => d.SomeStringProperty == "Tom");
}
if (condition2)
{
    predicate = predicate.Or(d => d.SomeStringProperty == "Alex");
}
if (condition3)
{
    predicate = predicate.And(d => d.SomeIntProperty >= 4);
}
return originalCollection.Where<YourDataClass>(predicate.Compile());



Predicate Builder class

using System;
using System.Linq;
using System.Linq.Expressions;
using System.Collections.Generic;
public static class PredicateBuilder
{
  public static Expression<Func<T, bool>> True<T> ()  { return f => true;  }
  public static Expression<Func<T, bool>> False<T> () { return f => false; }
  public static Expression<Func<T, bool>> Or<T> (this Expression<Func<T, bool>> expr1,
                                                      Expression<Func<T, bool>> expr2)
  {
    var invokedExpr = Expression.Invoke (expr2, expr1.Parameters.Cast<Expression> ());
    return Expression.Lambda<Func<T, bool>>
          (Expression.OrElse (expr1.Body, invokedExpr), expr1.Parameters);
  }
  public static Expression<Func<T, bool>> And<T> (this Expression<Func<T, bool>> expr1,
                                                       Expression<Func<T, bool>> expr2)
  {
    var invokedExpr = Expression.Invoke (expr2, expr1.Parameters.Cast<Expression> ());
    return Expression.Lambda<Func<T, bool>>
          (Expression.AndAlso (expr1.Body, invokedExpr), expr1.Parameters);
  }
}






Source:

http://www.albahari.com/nutshell/predicatebuilder.aspx
http://stackoverflow.com/questions/8791540/multiple-where-clauses-with-linq-extension-methods

Forgot how to view TODOs task list in Visual Studio

If you are referring to TODOs that are defined with the // TODO comments.
Open the Task List and set it to the Comments filter.

Wednesday, June 29, 2016

Unable to create a constant value of type 'System.Object'. Only primitive types or enumeration types are supported in this context.

 Problem:

Working a dynamic linq issue.  Using the great helper class, Predicate Builder posted over on the O'Reilly site.  I had built out my query and it worked great against my EF generated class.  Then I started getting this error each time I ran the predicate builder against a data type of nullable Guid.  I searched around on line at some more in depth explanations.  But nothing seemed to really fit my problem.

Solution:

Then I took the simple advice given on stackoverflow.  I changed the following lines
 


var race = Guid.Parse(race_ID);
predicate = predicate.Or(l => l.Race_ID.Equals(race));


to


var race = Guid.Parse(race_ID);
predicate = predicate.Or(l => l.Race_ID==race);

C# interpreter at work here: 

"When == is used on an expression of type object, it'll resolve to System.Object.ReferenceEquals.
Equals is just a virtual method and behaves as such, so the overridden version will be used (which, for string type compares the contents). "


so we could also do




var race = Guid.Parse(race_ID);
predicate = predicate.Or(l => l.Race_ID.Value.Equals(race));

Source:

  1. http://www.albahari.com/nutshell/predicatebuilder.aspx
  2. http://stackoverflow.com/questions/4592432/linq-query-keeps-throwing-unable-to-create-a-constant-value-of-type-system-obje
  3. http://stackoverflow.com/questions/814878/c-sharp-difference-between-and-equals

Thursday, June 2, 2016

Unable to create a constant value of type 'System.Object'

Problem:

I had a class created using the EF tools.  It generated a field which was a nullable Guid.  The datatype declaration was:


Guid? Status_ID


Each time I ran my controller code to build out my query.  I ran the code


Guid? ms_ID = Guid.Parse(MilitaryStatus_ID);
predicate = predicate.Or(l => l.MilitaryStatus_ID.Equals(ms_ID));


This would always through the unknown system.object error.  It was driving me crazy.


Solution:

I did not have any issues against data types of Guid which lead me to think about the nullable operator.  This seemed to be the issue so I removed it for testing and was able to run the above code no problems.  Thus realized that the issue was Linq not dealing with the nullable condition of my variable correctly.  Thus the work around was  to use == instead of Equals operator.


Guid? ms_ID = Guid.Parse(MilitaryStatus_ID);
predicate = predicate.Or(l => l.MilitaryStatus_ID == ms_ID);



Source:

http://stackoverflow.com/questions/4592432/linq-query-keeps-throwing-unable-to-create-a-constant-value-of-type-system-obje