Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Sunday, August 28, 2016

Get IP address by MAC address in C#. Updating ARP table.

Recently I have written article about getting IP address of connected to the network device using it MAC address, you can find it using the following link Get IP address by MAC address in C#. It seemed to me that the solution was good until I figured out that it requires some time for ARP table being updated.
To reproduce such situation you just need to reboot your computer or execute command:
arp -d

Run cmd.exe as administrator, type command text and hit enter. Now the code provided in the previous article does not work as expected.

Fixing the issue


The device should be on the same sub-network as running computer. So we need to ping all the IP addresses int the sub-network, e.g. (192.168.1.1 - 192.168.1.255) to update the ARP table. I have updated IpFinder class with necessary code.

IpFinder.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace SftpDownloader.Core
{
    public class IpFinder
    {
        public string FindIpAddressByMacAddress(string macAddress, string currentIpAddress)
        {
            Parallel.ForEach(GetListOfSubnetIps(currentIpAddress), delegate (string s)
            {
                DeviceScanner.IsHostAccessible(s);
            });

            var arpEntities = new ArpHelper().GetArpResult();
            var ip = arpEntities.FirstOrDefault(
                a => string.Equals(
                    a.MacAddress,
                    macAddress,
                    StringComparison.CurrentCultureIgnoreCase))?.Ip;

            return ip;
        }

        private List<string> GetListOfSubnetIps(string currentIp)
        {
            var a = currentIp.Split('.');
            var lst = new List<string>();

            for (int i = 1; i <= 255; i++)
            {
                lst.Add($"{a[0]}.{a[1]}.{a[2]}.{i}");
            }

            lst.Remove(currentIp);

            return lst;
        }
    }
}

DeviceScanner.cs


using System.Net.NetworkInformation;

namespace SftpDownloader.Core
{
    public class DeviceScanner
    {
        public static bool IsHostAccessible(string hostNameOrAddress)
        {
            Ping ping = new Ping();
            PingReply reply = ping.Send(hostNameOrAddress, 1000);

            return reply != null && reply.Status == IPStatus.Success;
        }
    }
}

Sunday, August 21, 2016

.NET async repository using MongoDb

Intro

In this article we will learn how to create async repository for MongoDB as datasource. More info about MongoDb you can find using following link https://www.mongodb.com/. All code from this article you can find using link https://github.com/aliakseimaniuk/MongoRepository.

Repository interface

Instead of classic CRUD methods I prefer to use next repository interface. 

using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading.Tasks;
using MongoRepository.Domain.Entities;

namespace MongoRepository.Domain.Repositories
{
    public interface IRepository<TEntity, in TKey> where TEntity : IEntity<TKey>
    {
        Task<TEntity> GetByIdAsync(TKey id);

        Task<TEntity> SaveAsync(TEntity entity);

        Task DeleteAsync(TKey id);

        Task<ICollection<TEntity>> FindAllAsync(Expression<Func<TEntity, bool>> predicate);
    }
}
To be sure that entity is the good fit for the repository we introduced the IEntity interface. Repository entity should implement the specified interface.
namespace MongoRepository.Domain.Entities
{
    public interface IEntity<TKey>
    {
        TKey Id { get; set; }
    }

    public interface IEntity : IEntity<string>
    {
    }
}

Base repository implementation

As we created generic repository interface, we expect that all the repositories will work in the same way. Please find below the implementation of base repository.

using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading.Tasks;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoRepository.Domain.Entities;
using MongoRepository.Domain.Repositories;

namespace MongoRepository.Persistence.Repositories
{
    public abstract class BaseMongoRepository<TEntity>
        : IRepository<TEntity, string> where TEntity : IEntity
    {
        protected abstract IMongoCollection<TEntity> Collection { get; }

        public virtual async Task<TEntity> GetByIdAsync(string id)
        {
            return await Collection.Find(x => x.Id.Equals(id)).FirstOrDefaultAsync();
        }

        public virtual async Task<TEntity> SaveAsync(TEntity entity)
        {
            if (string.IsNullOrWhiteSpace(entity.Id))
            {
                entity.Id = ObjectId.GenerateNewId().ToString();
            }

            await Collection.ReplaceOneAsync(
                x => x.Id.Equals(entity.Id),
                entity,
                new UpdateOptions
                {
                    IsUpsert = true
                });

            return entity;
        }

        public virtual async Task DeleteAsync(string id)
        {
            await Collection.DeleteOneAsync(x => x.Id.Equals(id));
        }

        public virtual async Task<ICollection<TEntity>> FindAllAsync(
            Expression<Func<TEntity, bool>> predicate)
        {
            return await Collection.Find(predicate).ToListAsync();
        }
    }
}
You might notice, that BaseMongoRepository class has abstract property Collection. This property is representing the data collection of the entities and should be implemented in the derived classes.

Custom entity repository

Previously I mentioned that all derived from BaseMongoRepository class classes  should have Collection property. To get that I created MongoDataContext class.

using System.Configuration;
using MongoDB.Driver;

namespace MongoRepository.Persistence
{
    public class MongoDataContext
    {
        public MongoDataContext()
            : this("MongoDb")
        {
        }

        public MongoDataContext(string connectionName)
        {
            var url = 
                ConfigurationManager.ConnectionStrings[connectionName].ConnectionString;

            var mongoUrl = new MongoUrl(url);
            IMongoClient client = new MongoClient(mongoUrl);
            MongoDatabase = client.GetDatabase(mongoUrl.DatabaseName);
        }

        public IMongoDatabase MongoDatabase { get; }
    }
}

Finally we are ready to implement our custom repository class. I will create PersonRepository as example.

using MongoDB.Driver;
using MongoRepository.Domain.Entities;

namespace MongoRepository.Persistence.Repositories
{
    public class PersonRepository : BaseMongoRepository<Person>
    {
        private const string PeopleCollectionName = "People";

        private readonly MongoDataContext _dataContext;

        public PersonRepository(MongoDataContext dataContext)
        {
            _dataContext = dataContext;
        }

        protected override IMongoCollection<Person> Collection =>
            _dataContext.MongoDatabase.GetCollection<Person>(PeopleCollectionName);
    }
}

namespace MongoRepository.Domain.Entities
{
    public class Person : IEntity
    {
        public string Id { get; set; }

        public string FirstName { get; set; }

        public string LastName { get; set; }
    }
}

MongoDb settings


MongoDb version: 3.2.5
C# MongoDb driver version: 2.2.4

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="MongoDB.Bson" version="2.2.4" targetFramework="net452" />
  <package id="MongoDB.Driver" version="2.2.4" targetFramework="net452" />
  <package id="MongoDB.Driver.Core" version="2.2.4" targetFramework="net452" />
</packages>

Monday, August 15, 2016

Get IP address by MAC address in C#

Sometimes it happens that you need to access device in the network by IP address. But unfortunately, it is not every time possible, e.g the device does not have static IP. In such case you can use device's MAC address to find it in the network.

Address Resolution Protocol 

Displays and modifies entries in the Address Resolution Protocol (ARP) cache, which contains one or more tables that are used to store IP addresses and their resolved Ethernet or Token Ring physical addresses. There is a separate table for each Ethernet or Token Ring network adapter installed on your computer.
So we are going to use following arp-command to get all the entries and filter them by specified MAC address.

arp -a

For more option use following link ARP Documentation.

.NET implementation


ArpEntity.cs


namespace ArpExample
{
    public class ArpEntity
    {
        public string Ip { get; set; }

        public string MacAddress { get; set; }

        public string Type { get; set; }
    }
}

ArpHelper.cs


using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text.RegularExpressions;

namespace ArpExample
{
    public class ArpHelper
    {
        public List<ArpEntity> GetArpResult()
        {
            var p = Process.Start(new ProcessStartInfo("arp", "-a")
            {
                CreateNoWindow = true,
                UseShellExecute = false,
                RedirectStandardOutput = true
            });

            var output = p?.StandardOutput.ReadToEnd();
            p?.Close();

            return ParseArpResult(output);
        }

        private List<ArpEntity> ParseArpResult(string output)
        {
            var lines = output.Split('\n').Where(l => !string.IsNullOrWhiteSpace(l));

            var result =
                (from line in lines
                 select Regex.Split(line, @"\s+")
                    .Where(i => !string.IsNullOrWhiteSpace(i)).ToList()
                    into items
                 where items.Count == 3
                 select new ArpEntity()
                 {
                     Ip = items[0],
                     MacAddress = items[1],
                     Type = items[2]
                 });

            return result.ToList();
        }
    }
}

IpFinder.cs


using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace ArpExample
{
    public class IpFinder
    {
        public string FindIpAddressByMacAddress(string macAddress)
        {
            List<ArpEntity> arpEntities = new ArpHelper().GetArpResult();

            return arpEntities.FirstOrDefault(a => a.MacAddress == macAddress)?.Ip;
        }
    }
}

Troubleshooting 

Recently I figured out that the solution described in the article sometimes might fail. I wrote the article with the fix. Please find it using the link Get IP address by MAC address in C#. Updating ARP table

Saturday, December 5, 2015

StyleCop integration for .NET projects

Why it is important?

Coding conventions serve the following purposes:
  • They create a consistent look to the code, so that readers can focus on content, not layout.
  • They enable readers to understand the code more quickly by making assumptions based on previous experience.
  • They facilitate copying, changing, and maintaining the code.
  • They demonstrate C# best practices.

Code style recommendations

As the reference for your coding conventions, please use Microsoft guidelines (https://msdn.microsoft.com/en-us/library/ff926074.aspx).  If they will conflict with this document, please use rules from the document.

Naming convention 


EntityPattern
Types and namespacesUpperCamelCase
InterfacesIUpperCamelCase
Type parametersTUpperCamelCase
Methods, properties and eventsUpperCamelCase
Local variableslowerCamelCase
Local constantslowerCamelCase
ParameterslowerCamelCase
Fields (not private)UpperCamelCase
Instance fields (private)_lowerCamelCase
Static fields (private)_lowerCamelCase
Constants fields (not private)UpperCamelCase
Constants fields (private)UpperCamelCase
Static read-only (not private)UpperCamelCase
Static read-only (private)UpperCamelCase
Enum membersUpperCamelCase
All other entities UpperCamelCase

Try to give names for entities without any abbreviations. Name should describe entity’s purposes. 

Using statement


1. Please put “using statement” outside of namespace.
using System;  
using System.Text;  
using System.Web.Mvc; 
 
namespace SampleProject.Controllers  
{  
}
2. System usings should be placed first, then all others
3. Usings should be sorted alphabetically.

Return statement 

1. Please separate return statement from other code with single empty line.
public ActionResult DashBoard()  
{  
    DoSomething();

    return View();  
}  

Curved brackets

1. Curved brackets should be places for “foreach”, “if”, “for”, “while”, etc. all the time.
if (someVal)  
{  
   DoSomething();  
}  

How to implement rules if you are at the beginning of the project?

We assume that all team members should follow the code guidelines without any exceptions. So, know we have to think how we can achieve that statement. In general, all the cases will lead you to the following two cases.
  1. Rely on people
  2. Rely on tools
What does it mean to “Rely on people”? By this phrase, we assume that all team members will be responsible for following code style rules. There are some procs and cons of this approach. Although it is easy to start using it, there are some disadvantages of such approach. Among them – additional time for code style review and non-strict control of following the rules by all team members.
In such situation, tools can help the team to follow code style guidelines. Therefore, in this article, we will look at StyleCop tool (https://stylecop.codeplex.com/).

StyleCop

Installation

To install StyleCop on your machine you need to download installer from the https://stylecop.codeplex.com/ and run. Alternatively, you can open “Extensions and Updates” window in Visual Studio and install any appropriate extension, e.g. “Visual StyleCop”.

Create settings file

To create StyleCop settings file just right click on project and select “StyleCop settings”.
Select rules you want to follow and click “Apply”. After that, Settings.StyleCop file will appear in project folder.To download settings file click on following link Settings.StyleCop.


Enable StyleCop on build

1. Open NuGet Package Manager for project you would like to validate.
2. Search for “StyleCop.MSBuild” package and install it.



3. Create file with rules, which are acceptable for your team. 
4. Import that file to the project.


5. Build the project. As the result, you will see warning in “Error List” window.


Just described behavior is kind of soft behavior, because code compiled and application would work. To fix such situation you can do following things:
  • In project’s properties change “Treat warning as errors” property
  • Install “StyleCop.Error.MSBuild” NuGet package

After implementation all the steps above, you can be sure that everyone will follow the code style rules.

How to start using styling rules if you already have “billion” files?

It is no secret that majority projects does not have styling standards and we have to live with it. Let’s think how we can improve such situation. 

Assumptions:
  • At the begging do not allow to fail build on style settings validation
  • After some period build should fail on code styling issue
I would like to recommend following steps to rewrite all code according style standard:
  1. Create styling rules for your project and introduce Settings.StyleCop  with them
  2. Install on each developer’s machine StyleCop or another appropriate tool
  3. Create milestones with percentage of cleaned files
  4. Ask developers to change code according style rules in files, which they working on during current iteration
  5. Allocate time for code style fixes as technical debt
  6. Track progress
  7. Start failing build on style issues

Approximate roadmap for code style changes

Step 1

The step's approximate time frame is 0 - 6 week. During step 1 team concentrates on following:
  1. All team members have installed StyleCop tool on their machines
  2. While working on some area teammates do code style refactoring
  3. While code review first step to run StyleCop against created rules
  4. Team will concentrate on “main” libraries
  5. Team will allocate time for style fixes as part of technical debt stories

Step 2

The step's approximate time frame is  6 - 12 week. During step 2 team concentrates on following:
  1. We will enable code style checks on build. “StyleCop.MSBuild” package will be installed to libraries we would like to validate
  2. Code style warnings will appear
  3. Steps 1, 2, 3, 5 from first row
  4. Libraries coverage increased
  5. We will track number of style warnings

Step 3

The step's approximate time frame is  from 12 week. During step 3 team concentrates on following:
  1. Warnings replaced with build failures. By this moment, we assume that all files will have appropriate style. 
    1. In project’s properties change “Treat warning as errors” property
    2. Install “StyleCop.Error.MSBuild” NuGet package
  2. Deprecate style review on code review sessions

FAQ

How to apply settings for solution?

It is easy when you have only one file with rules and all projects in solution have reference to that file. StyleCop allow us to do that. Put file with rules in the root folder of the project. Then add to each projects’ folder Settings.StyleCop file with following lines.

<StyleCopSettings Version="105">  
  <GlobalSettings>  
   <StringProperty Name="LinkedSettingsFile">..\Settings.StyleCop</StringProperty>  
   <StringProperty Name="MergeSettingsFiles">Linked</StringProperty>  
  </GlobalSettings>  
</StyleCopSettings>  

LinkedSettingsFile – path to the main settings file.

How can I extend existing rules?

First, you can try to find some already written rule extensions. E.g. StyleCop+ (StyleCop+ is a plug-in that extends original StyleCop features. It offers you a variety of rules for building C# code style that best suits your needs.) (https://stylecopplus.codeplex.com/)
Second, you can write your own rule. 


Wednesday, September 10, 2014

Использование StyleCop

Начало

Многие программисты за свою карьеру успели поработать на проектах, которые разрабатываются не один год. И все представляют какие страшные вещи можно найти в проекте, процесс написания которого был плохо поставлен.

Все началось, что к нам на проект, очень похожий на вышеупомянутые, пришел Женя. Он уже работал со StyleCop, поэтому идея интеграции StyleCop в проект принадлежала ему.

Шаги для интеграции

1) Мы начали с того, что разработали руководство по написанию кода, в которое попытались включить лучшие практики, которые существуют в мире разработки .NET приложений. За основу были взяты C# Coding Conventions (C# Programming Guide), немного доработаны в связи со спецификой нашего проекта и утверждены большими дядями типа Principal Solution Developer =).

2) Установка и использование StyleCop. Скачать и установить StyleCop можно пройдя по ссылке http://stylecop.codeplex.com/



Установка завершилась успешно, теперь можно открыть Visual Studio и попробовать поработать со StyleCop. Для начала нам необходимо создать файл с настройками, по которым будет проходить валидация существующего кода. Выбираем "StyleCop Settings" нажав правой кнопкой по проекту.


После того, как мы выбрали "StyleCop Settings" перед нами появится окно, в котором можно будет выбрать правила, которые подходят для нашего проекта.



После того, как правила были выбраны, можно запусить StyleCop и проверить, удовлетворяет ли проект, выбранным правилам. Для этого необходимо выбрать файлы, которые вы желаете провалидировать и запустить StyleCop.



3) Более продвинутая настройка StyleCop в соответсвии с правилами написания кода, принятых на нашем проекте. 
Нам повезло, потому что нам не пришлось добавлять правила, которых не было в "коробке", поэтому для того, чтобы StyleCop валидация для нашего проекта проходила в соответсвии с нашими договоренностями по форматированию кода, мы удалили некоторые правила из стандартного набора. Файл с нашими настройками, можно скачать по ссылке Settings.StyleCop
Для того, чтобы применить данный файл к проекту, необходимо скопировать его в директорию, в которой находится файл проекта (*.sln)
Если ваш проект состоит из нескольких сборок и вы хотите, чтобы у вас был один файл со StyleCop настройками, в папку с проектом необходимо добавить файл Child Project Settings. В данном файле нужно указать путь к "Settings.StyleCop" файлу, который вы добавили в корень проекта.
Если случилось так, что набор предустановленных правил для вас не работает, StyleCop имеет возможность расширить набор правил. Узнать как это сделать можно по пройдя по ссылке: http://scottwhite.blogspot.com/2008/11/creating-custom-stylecop-rules-in-c.html

4) Как заставить программистов следовать правилам
StyleCop предоставляет возможность генерировать "Warning" или "Error" в случае, если код не соответсвует правилам. Мы долго спорили на тему, что же все таки лучше для нашего проекта и пришли к выводу, что "Warning" это хорошо, но данный подход не работает в большинстве случаев. Да и свалившийся билд подстегивает писать код красиво =). 
Для того, чтобы интегрировать StyleCop в процесс сборки проекта необходимо:
  1. Установить StyleCop.MSBuild. Данную операцию можно проделать через "Manage Nuget Pacakges" в Visual Studio.
  2. Добавить в файл проекта (*.csproj) свойство <StyleCopTreatErrorsAsWarnings>false</StyleCopTreatErrorsAsWarnings>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    ...    
    <AssemblyName>SampleProject</AssemblyName>
    <StyleCopTreatErrorsAsWarnings>false</StyleCopTreatErrorsAsWarnings>
  </PropertyGroup>

Валидация существующих файлов

В нашем проекте было около 10 тысяч файлов, которые не удовлетворяли разработанным нами правилам. Приведение всех файлов к определенному формату могло занять кучу времени не только программистов, но и тестировщиков. Поэтому нам была необходима возможность постепенного изменения существующих файлов.
StyleCop предоставляет такую возможность. Более подробно можно прочитать в документации Using StyleCop on Legacy Projects. Для того, чтобы игнорировать валидацию для конкретного файла необходимо нажать правой кнопкой мыши по файлу и выбрать в меню "Exclude From StyleCop".


К сожаление данное решение работает только для выбранный файлов и является крайне неудобным, если в проекте их много.
Именно для такой ситуации команда StyleCop предоставляет небольшое приложение, которое может обновить файлы из указанного проекта. Скачать данное приложение можно по ссылке ExcludeFromStyleCop.

Заключение

StyleCop отличное средство для того, чтобы привести стиль написания исходного кода в порядок. На мой взгляд данное средство должно интегрироваться в проект со старта. Потом будет сложнее все поправить.