Testing: Postman and Swagger As Additions to Your Tests

Often I need to queue up some unit tests on-the-fly. I recently came across the following helpers:

1.  CREATE POSTMAN API TESTS FROM
SWAGGER

In case anyone else did not know, it is now an option to take the swagger document from your WebAPI, import it into Postman and, voila, test cases for controllers created.   

1.      Create swagger.json from API
2.      Open Postman
3.      Navigate to “Import” button

4.      Open swagger.json file (or paste, etc)

2.  RUN POSTMAN TESTS FROM VISUAL STUDIO

Postman Runner for VSCode allows you to run Postman tests from your VSCode. It analyze the workspace folder and locate collection and environment files.
  • Export your Postman collections and environments in a folder (or subfolders) and open it with VSCode.
  • Open the Command Palette
  • Choose Postman: Run > Question Mode ( Ctrl+Q , Cmd+R )


https://marketplace.visualstudio.com/items?itemName=eridem.vscode-postman



.NET: Using A Single log4net Configuration File Across A Multi-Tier Web Application

log4net is
a tool to help the programmer output log statements to a variety of output
targets.



This
example is written in Visual Studio 2015 for a WebAPI2 solution.  The sample solution consists of three projects
that include a project for data, models, unit tests, etc.  Source code can be found here:



https://github.com/cjsander/Log4netInMultiTierWebApp

SETUP:
1.     
Create
your project and then import the following Nuget package:        
https://www.nuget.org/packages/log4net/
2.     
For
each project, add reference to log4net.dll assembly.  Though all configuration will be in the  main
API project, other projects will require the assembly reference as well.
3.     
Configuration
files to be included are:
a.       Log4net.config
b.      Web.config
c.       Settings.xml
d.      Global.asax.cs
4.     
Note
that the <appsettings> node of the web.config file has been abstracted
into a separate file — Settings.xml.  
These settings will be referenced in the web.config in the following
line of code:

         <appSettings file=c:webappslog4sampleconfigsettings.xml/>

APPLICATION:
1.       In your web.config file, enter the
following:
  <configSections>
    <section name=log4net type=log4net.Config.Log4NetConfigurationSectionHandler,
log4net
/>
  </configSections>
2.       Now, add the logging configuration
values.  These can be added in your
web.config(or app.config) but in this scenario, it has its own configuration
file – log4net.config:
<log4net>
 
<
appender name=RollingFile type=log4net.Appender.RollingFileAppender>…..
</log4net>

3.       Then,
you will want to add the path to the file in your <appSettings> node:
<add key=logPath value=c:webappslog4sampleconfiglog4net.config />
4.       In your
Global.asax file, add the following to the Application_Start() method:
//log4net setup:
var logPath = (ConfigurationManager.AppSettings[“logPath”]);

log4net.Config.XmlConfigurator.ConfigureAndWatch(new FileInfo(logPath));

EXTRA:
You can also
add a helper method to customize your logging and add details that would help
with your debugging:
public class Log4NetHelper
    {
public string LogMsg(Message m, [Optional] string exceptionMsg, [CallerMemberName] string methodName = “”)
        {
            string msg = $”{methodName} {m} {exceptionMsg};
            return msg;
        }
}

               To
reference:
private readonly Log4NetHelper _msg = new Log4NetHelper();
_logger.Debug(_msg.LogMsg(Log4NetHelper.Message.InitialRequestStart));


SAMPLE FOLDER STRUCTURE:





.NET: Customize Builds Per Environment

Two Options:
1.  Web.Config Transformations
https://msdn.microsoft.com/en-us/library/dd465326.aspx
2.  Deployment Parameters:  Useful when you have to create a package without knowing some of the values that will be needed when the package is installed.
https://msdn.microsoft.com/en-us/library/ff398068.aspx

Web.Config Transform:
1. Open Build –> Configuration Manager
2. Create new “Active solution configuration” for each version you need and then close.
3. Navigate to Solution Explorer and right-click on your web.config and select “Add Config Transform.”  This should generate the new config files you created in Configuration Manager.
4. Open the transform file for the build configuration that you want to work with.

Further Reference:
https://code.msdn.microsoft.com/ASPNET-Web-Deployment-c2d409f9

Error : Could not load file or assembly ‘Oracle.DataAccess’ or one of its dependencies.

I ran into this problem today in a ASP.NET application on deployment:

“Exception message: Could not load file or assembly ‘Oracle.DataAccess’ or one of its dependencies. An attempt was made to load a program with an incorrect format.”

Some suggest changing your solution by going into Configuration Manager and set your target platform to 64-bit.  Others suggest enabling 32-bit applications in your AppPool.

Before doing that, make sure that your reference properties for Oracle.DataAccess have NOT been inadvertently changed to “Copy Local = true”.  Otherwise, your server will be using your copy and not the one in the GAC.

NET : Create DataTable From CSV File

DataTable dt = new
DataTable();
string line = String.Empty;
int i = 0;
using (StreamReader sr = File.OpenText(@path))
{
    while ((line = sr.ReadLine()) != null)
    {
        string[] data = line.Split(‘,’);
        if (data.Length > 0)
        {
            if (i == 0)
            {
               
int j = 0;
               
foreach (var
s in data)
                {
                   
//create column
                   
dt.Columns.Add(new DataColumn());
                   
//add column name from first row
                   
dt.Columns[j].ColumnName = data[j].ToString();
                   
j++;
               
}
               
i++;
            }
            if (i > 1)
            {
               
//if not first row (column name) then create
data row
               
DataRow row = dt.NewRow();
               
row.ItemArray = data;
              
 dt.Rows.Add(row);
            }
           
i++;
        }
    }
}
return dt;