Saturday, December 7, 2019

VSCode Python: Refer global site packages inside virtual environment

Using virtual environments in Visual Studio Code is a great practise while working on python. However, I had noticed that whenever I create a virtual environment, I had to reinstall the commonly used packages like numpy again in the virtual environment folder. The typical size of all such packages for a python program using Keras packages is nearly 500 MB.

Though this is a very good - and probably essential - practise for production environment, I did not want to use up a lot of unnecessary disk place in my study laptop's hard disk. After seraching on internet I could get some solutions to this problem like setting pythonPath variable in settings.json file under the project folder.

But I was curious and wanted to look for other possible options, and finally I found a setting in the pyenv.cfg file under the virtual environment folder. This file has a setting include-system-site-packages. Setting it to true results in the virtual environment automatically refering to global package library folder, like so

home = C:\Program Files\Python36
include-system-site-packages = true
version = 3.6.8

Tuesday, December 3, 2019

SharePoint JSOM queries

Accessing SharePoint through JSOM is a very flexible and intuitive way of working with SharePoint.

Its advantages are:

  1. Data can be accessed from any browser, in any application.
  2. Javascript can be modified easily, and hence shorter deployment cycles.
  3. Can be easily embedded in SharePoint hosted apps or even a content query webpart, providing vital ability to customize sharepoint OOB elements (such as menus, styles, links, etc.) without a lot of overhead of development, testing and deployment.

To get started with JSOM the following articles are a great read.






Friday, November 29, 2019

SharePoint framework webpart - ERR_SSL_PROTOCOL_ERROR

I was unable to view a newly created sharepoint framework webpart in Chrome browser and the error shown was

This site can’t provide a secure connection
localhost sent an invalid response.
Try running Windows Network Diagnostics.
ERR_SSL_PROTOCOL_ERROR

The only solution I could find for this problem was to change protocol of the local website from https to http.

Specifially, open serve.json file under config directory and set https = false, and initialPage = http://localhost:5432/workbench


{
  "$schema": "https://developer.microsoft.com/json-schemas/core-build/serve.schema.json",
  "port": 4321,
  "https": false,
  "initialPage": "http://localhost:5432/workbench",
  "api": {
    "port": 5432,
    "entryPath": "node_modules/@microsoft/sp-webpart-workbench/lib/api/"
  }
}



If you know of any other way to fix this error without changing protocol, please do let me know.

More information can be found on this blog https://github.com/SharePoint/sp-dev-docs/issues/2343

Friday, November 22, 2019

Configure MVC app to receive and handle OPTIONS requests

Many of the frequently used http request methods are readily available from within MVC controllers. e.g, GET, PUT, POST, DELETE, etc.

However, there is also one important HTTP request method OPTIONS, which is sent before any CORS request to check if the request is allowed. This method is normally handled automatically by MVC framework, but if we ever need to handle it inside our controller, then add following tags to web.config


<system.webServer>
  <handlers>
    <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
    <remove name="OPTIONSVerbHandler" />
    <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
  </handlers>

</system.webServer>



This is explained in detail at https://docs.microsoft.com/en-us/aspnet/web-api/overview/security/enabling-cross-origin-requests-in-web-api



Wednesday, November 20, 2019

Azure Pipelines Error: Could not load file or assembly 'System.IO.FileSystem, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'

An angular project deployed through Azure DevOps pipeline would throw this error in the release pipeline:




 ##[error]System.Management.Automation.MethodInvocationException: Exception calling "Transform" with "4" argument(s): "Could not load file or assembly 'System.IO.FileSystem, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified." ---> System.IO.FileNotFoundException: Could not load file or assembly 'System.IO.FileSystem, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.



The error was thrown in a 'Config transformation' task. I tried to install file system package using
Install System.IO.FileSystem -Version 4.0.1


in a Powershell task and also tried to install dot net framework 4.6 using

Install DotNetFramework.Runtime -Version 4.6.1

in a custom Nuget task, but both these methods didn't solve the issue.

Finally after going through the task history realized that the task was auto updated to latest version which has caused this issue.

So simply changing this task's version from 2.* to 1.* solved this problem. This behavior is kind of expected since a major version includes some breaking changes by convention.

Tuesday, November 19, 2019

MVC unobstrusive - validate only a single form control

I was trying to find articles detailing how to validate only a single form control using unobstrusive validation, but could not find one. So I digged in a little and found out that the following way works nicely. It may not be the most ideal solution for this problem though, so your suggestions are welcome.



var myForm = $('form');
var validator = myForm.data('validator');
var myValidator = new $.validator(validator.settings, myForm);
myValidator.check($('#myElement')[0]);
myValidator.showErrors();


The key part here is to call check method on a validator object, this method is an internal method so is not available on the default validator object. So I have created a new validator object and passed settings of existing validator objects to it. It is important to pass existing settings since they contain final validation message for each element after combining jquery default message and any custom messages from server side attributes.


Friday, November 15, 2019

MVC validate a model using data annotation attributes without model binding

Sometimes, we need to validate a data model without model binding. For example, if the application has separate models for the actual data entity and an action method such as edit action in a view. In such cases, it would be very convenient to use data annotation attributes already specified on the model to perform validation logic.


This can be achieved by using ValidationContext and Validator objects, like so:


 var widget = new Widget
                        {
                            Id = 12,
                            Price = 15.57M
                        };

        var context = new ValidationContext(widget, null, null);
        var results = new List<ValidationResult>();
        if( Validator.TryValidateObject( widget, context, results, true ) )
        {
            //Validation Successful
        }
        else
        {
            //Validation Failed
        }


More information can be found at https://stackoverflow.com/questions/7101800/can-i-check-modelstate-without-modelbinding

Regex Email validation in c# dot net core

 Use this regex /^_?[a-zA-Z0-9]([a-zA-Z0-9]*[._+-])*[a-zA-Z0-9_]+@(?!-)[A-Za-z0-9-]{1,63}(?<!-)(\.(?!-)[A-Za-z0-9-]{1,63}(?<!-))*\.[A-...