European ASP.NET 4.5 Hosting BLOG

BLOG about ASP.NET 4, ASP.NET 4.5 Hosting and Its Technology - Dedicated to European Windows Hosting Customer

ASP.NET Core 2.2.1 Hosting - HostForLIFE.eu :: Command Design Pattern In C# .NET

clock July 25, 2018 09:42 by author Peter

Command Design Pattern is a type of Behavioral Design Pattern.

Behavioral Design Pattern
It's about object communication, their responsibilities, and how they communicate to each other.

There might be a situation where we want to encapsulate the required information in an Object to perform some task and the task can be performed many times or whenever it's required. The command design pattern is the solution. It also gives you an easy way to implement Undo() that can just undo multiple commands.

  • Implementation - Typically, Implementation of Command Pattern is divided into 4 parts.
  • Command - That executes an action.
  • Receiver - Objects that receive the action from the command.

Invoker: Invoke the Commands to execute their actions. The Invoker may be a queue that holds commands for future execution, or holds such commands which can be used by different applications. The machine can be used to execute commands multiple times or can be used to undo the command.

Client
Client is the main program that asks for a command to be executed.
Consider the case of a banking application which is capable of making transactions i.e. Transfer, Deposit, Withdraw etc.
Let's identify each part of the command design pattern we discussed above.
Account of a customer ? Think, what it should be ?
Command ? Read command's definition again.......It says that it executes an action, but what action Account will it execute? Actions such as increment in account balance or decrements in account balance can be executed on Account by the Commands Deposit/Withdraw . So, if it receives actions, it means account is a Receiver.

So, Receiver is Account, and Command Deposit will add money from Account Balance and Withdraw command will subtract money from Account Balance.
/// <summary> 
/// Reciever of Command 
/// </summary> 
public class Account { 
    public string CustomerName { 
        get; 
        set; 
    } 
    public double AccountBalance { 
        get; 
        set; 
    } 
    public Account(string customerName, double accountBalance) { 
        CustomerName = customerName; 
        AccountBalance = accountBalance; 
    } 

/// <summary> 
/// Defines the action of Command those can be executed - will be called by Invoker 
/// IsCommandCompleted signals the command is completed and can be removed from Invoker 
/// </summary> 
public interface ITransaction { 
    void ExecuteCommand(); 
    bool IsCommandCompleted { 
        get; 
        set; 
    } 

/// <summary> 
/// Deposit Command 
/// </summary> 
public class Deposit: ITransaction { 
    private readonly Account _account; 
    private readonly double _amount; 
    public bool IsCommandCompleted { 
        get; 
        set; 
    } 
    public Deposit(Account account, double amount) { 
        _account = account; 
        _amount = amount; 
        IsCommandCompleted = false; 
    } 
    public void ExecuteCommand() { 
        _account.AccountBalance += _amount; 
        IsCommandCompleted = true; 
    } 

/// <summary> 
/// Withdraw Command 
/// </summary> 
public class Withdraw: ITransaction { 
    private readonly Account _account; 
    private readonly double _amount; 
    public bool IsCommandCompleted { 
        get; 
        set; 
    } 
    public Withdraw(Account account, double amount) { 
        _account = account; 
        _amount = amount; 
        IsCommandCompleted = false; 
    } 
    public void ExecuteCommand() { 
        if (_account.AccountBalance >= _amount) { 
            _account.AccountBalance -= _amount; 
            IsCommandCompleted = true; 
        } 
    } 

/// <summary> 
/// Transfer Command 
/// </summary> 
public class Transfer: ITransaction { 
        private readonly Account _fromAccount; 
        private readonly Account _toAccount; 
        private readonly double _amount; 
        public bool IsCommandCompleted { 
            get; 
            set; 
        } 
        public Transfer(Account fromAccount, Account toAccount, double amount) { 
            _fromAccount = fromAccount; 
            _toAccount = toAccount; 
            IsCommandCompleted = false; 
        } 
        public void ExecuteCommand() { 
            _fromAccount.AccountBalance -= _amount; 
            _toAccount.AccountBalance += _amount; 
            IsCommandCompleted = true; 
        } 


Lets write Invoker,
public class TransactionManager { 
    private readonly IList < ITransaction > _transactions = new List < ITransaction > (); 
    public bool HasInCompleteTransactions { 
        get { 
            return _transactions.Any(x => !x.IsCommandCompleted); 
        } 
    } 
    public IList < ITransaction > GetPendingTransactions() { 
        return _transactions ? .Where(x => !x.IsCommandCompleted) ? .ToList(); 
    } 
    public void AddTransaction(ITransaction transaction) { 
        _transactions.Add(transaction); 
    } 
    public void ProcessPendingTransactions() { 
        foreach(var transaction in _transactions.Where(x => !x.IsCommandCompleted)) { 
            transaction.ExecuteCommand(); 
        } 
    } 



The Client is responsible to create commands and pass them to the Invoker. The Commands will be held in the _transactions list, until the Client calls ProcessInCompleteTransactions. Then, the Invoker will try to execute each incomplete Command.

Invoker should not be aware of anything about what the Command can do, or what inputs it needs. All it needs to know is that the Command should be executed.

We will here simulate the client using our Console application to demonstrate.
class Program { 
    static void Main(string[] args) { 
        //Add 100 to the account - there should not be any pending job 
        TransactionManager manager = new CommandPattern.TransactionManager(); 
        Account accountAshish = new CommandPattern.Account("Ashish", 0); 
        ITransaction depositTransaction = new Deposit(accountAshish, 100); 
        manager.AddTransaction(depositTransaction); 
        manager.ProcessPendingTransactions(); 
        //try to withdraw 200 - transction will be pending since the balance is account is low 
        ITransaction withdrawTransaction = new Withdraw(accountAshish, 200); 
        manager.AddTransaction(withdrawTransaction); 
        manager.ProcessPendingTransactions(); 
        var pendingTransaction = manager.HasInCompleteTransactions; 
        Console.WriteLine(pendingTransaction); 
        Console.ReadKey(); 
        //add 200- still withdraw trasaction would be pending since we are adding money after withdraw failed attempt, 
        //we would need to execute failed transacction again 
        ITransaction anotherDepositTransaction = new Deposit(accountAshish, 200); 
        manager.AddTransaction(anotherDepositTransaction); 
        manager.ProcessPendingTransactions(); 
        Console.WriteLine(manager.HasInCompleteTransactions); 
        Console.ReadKey(); 
        if (manager.HasInCompleteTransactions) { 
            //reattempt failed transactions 
            ReattemptPendingTransactions(manager); 
        } 
        Console.WriteLine(manager.HasInCompleteTransactions); 
        Console.ReadKey(); 
        //Try Transfer 
        Account accountAvinash = new Account("Avinash", 10); 
        ITransaction transferTransaction = new Transfer(accountAshish, accountAvinash, 10); 
        manager.AddTransaction(transferTransaction); 
        manager.ProcessPendingTransactions(); 
        Console.WriteLine("Ashish account balance:" + accountAshish.AccountBalance); 
        Console.WriteLine("Anjali account balance:" + accountAvinash.AccountBalance); 
        Console.ReadKey(); 
    } 
    private static void ReattemptPendingTransactions(TransactionManager manager) { 
        var pendingTransactions = manager.GetPendingTransactions(); 
        foreach(var item in pendingTransactions) { 
            item.ExecuteCommand(); 
        } 
    } 


Enhancement Undo
Suppose you want to undo the command. Modify your code, add Undo in your command.

You should facilitate your program with the undo all command and undo a particular command. In case of a particular command undo, you would need some kind of identifier which can uniquely identify the command (i.e. Id) and perform undo on it. We should be able to Undo successful commands also, we can have some status of command which tells us if command is executed successfully or not, unprocessed, Undo Successful, Undo Failed etc.

Transaction would look like,
public interface ITransaction { 
    int Id { 
        get; 
        set; 
    } 
    void ExecuteCommand(); 
    bool IsCommandCompleted { 
        get; 
        set; 
    } 
    void Undo(); 


Implement modified interface in all the Commands Deposit, Withdraw and Transfer

Create an enum to set Command state
/// <summary> 
/// Command sate enum 
/// </summary> 
public enum CommandState { 
    UnProcessed, 
    ExecutionFailed, 
    ExecutionSuccessed, 
    UndoDone, 
    UndoFailed 

public interface ITransaction { 
    int Id { 
        get; 
        set; 
    } 
    void ExecuteCommand(); 
    bool IsCommandCompleted { 
        get; 
        set; 
    } 
    CommandState Status { 
        get; 
        set; 
    } 
    void Undo(); 
} === === === === === == Other Updated Classes === === === === === === === === === === === == private static void ReattemptPendingTransactions(TransactionManager manager) { 
    var pendingTransactions = manager.GetPendingTransactions(); 
    foreach(var item in pendingTransactions) { 
        item.ExecuteCommand(); 
    } 


/// <summary> 
/// Reciever of Command 
/// </summary> 
public class Account { 
    public string CustomerName { 
        get; 
        set; 
    } 
    public double AccountBalance { 
        get; 
        set; 
    } 
    public Account(string customerName, double accountBalance) { 
        CustomerName = customerName; 
        AccountBalance = accountBalance; 
    } 

/// <summary> 
/// Defines the action of Command those can be executed - will be called by Invoker 
/// IsCommandCompleted signals the command is completed and can be removed from Invoker 
/// </summary> 
public interface ITransaction { 
    int Id { 
        get; 
        set; 
    } 
    void ExecuteCommand(); 
    bool IsCommandCompleted { 
        get; 
        set; 
    } 
    CommandState Status { 
        get; 
        set; 
    } 
    void Undo(); 

/// <summary> 
/// Command sate enum 
/// </summary> 
public enum CommandState { 
    UnProcessed, 
    ExecutionFailed, 
    ExecutionSuccessed, 
    UndoDone, 
    UndoFailed 

/// <summary> 
/// Deposit Command 
/// </summary> 
public class Deposit: ITransaction { 
    private readonly Account _account; 
    private readonly double _amount; 
    public bool IsCommandCompleted { 
        get; 
        set; 
    } 
    public int Id { 
        get; 
        set; 
    } 
    public CommandState Status { 
        get { 
            throw new NotImplementedException(); 
        } 
        set { 
            throw new NotImplementedException(); 
        } 
    } 
    public Deposit(int Id, Account account, double amount) { 
        this.Id = Id; 
        _account = account; 
        _amount = amount; 
        IsCommandCompleted = false; 
        Status = CommandState.UnProcessed; 
    } 
    public void ExecuteCommand() { 
        _account.AccountBalance += _amount; 
        IsCommandCompleted = true; 
        Status = CommandState.ExecutionSuccessed; 
    } 
    public void Undo() { 
        if (_account.AccountBalance >= _amount) { 
            _account.AccountBalance -= _amount; 
            Status = CommandState.UndoDone; 
        } else { 
            Status = CommandState.UndoFailed; 
        } 
    } 

/// <summary> 
/// Withdraw Command 
/// </summary> 
public class Withdraw: ITransaction { 
    private readonly Account _account; 
    private readonly double _amount; 
    public bool IsCommandCompleted { 
        get; 
        set; 
    } 
    public int Id { 
        get; 
        set; 
    } 
    public CommandState Status { 
        get; 
        set; 
    } 
    public Withdraw(int Id, Account account, double amount) { 
        _account = account; 
        _amount = amount; 
        IsCommandCompleted = false; 
        this.Id = Id; 
        Status = CommandState.UnProcessed; 
    } 
    public void ExecuteCommand() { 
        if (_account.AccountBalance >= _amount) { 
            _account.AccountBalance -= _amount; 
            IsCommandCompleted = true; 
            Status = CommandState.ExecutionSuccessed; 
        } else { 
            Status = CommandState.ExecutionFailed; 
        } 
    } 
    public void Undo() { 
        _account.AccountBalance += _amount; 
        Status = CommandState.UndoDone; 
    } 

/// <summary> 
/// Transfer Command 
/// </summary> 
public class Transfer: ITransaction { 
    private readonly Account _fromAccount; 
    private readonly Account _toAccount; 
    private readonly double _amount; 
    public bool IsCommandCompleted { 
        get; 
        set; 
    } 
    public int Id { 
        get; 
        set; 
    } 
    public CommandState Status { 
        get; 
        set; 
    } 
    public Transfer(int Id, Account fromAccount, Account toAccount, double amount) { 
        _fromAccount = fromAccount; 
        _toAccount = toAccount; 
        IsCommandCompleted = false; 
        _amount = amount; 
        this.Id = Id; 
        Status = CommandState.UnProcessed; 
    } 
    public void ExecuteCommand() { 
        if (_fromAccount.AccountBalance >= +_amount) { 
            _fromAccount.AccountBalance -= _amount; 
            _toAccount.AccountBalance += _amount; 
            IsCommandCompleted = true; 
            Status = CommandState.ExecutionSuccessed; 
        } else { 
            Status = CommandState.ExecutionFailed; 
        } 
    } 
    public void Undo() { 
        if (_toAccount.AccountBalance >= _amount) { 
            _toAccount.AccountBalance -= _amount; 
            _fromAccount.AccountBalance += _amount; 
            Status = CommandState.UndoDone; 
        } else { 
            Status = CommandState.UndoFailed; 
        } 
    } 

public class TransactionManager { 
    private readonly IList < ITransaction > _transactions = new List < ITransaction > (); 
    public bool HasInCompleteTransactions { 
        get { 
            return _transactions.Any(x => !x.IsCommandCompleted); 
        } 
    } 
    public IList < ITransaction > GetPendingTransactions() { 
        return _transactions ? .Where(x => !x.IsCommandCompleted) ? .ToList(); 
    } 
    public void AddTransaction(ITransaction transaction) { 
        _transactions.Add(transaction); 
    } 
    public void ProcessPendingTransactions() { 
        foreach(var transaction in _transactions.Where(x => !x.IsCommandCompleted)) { 
            transaction.ExecuteCommand(); 
        } 
    } 


Note
I won't create a client for these extended functionalities, I want you to try this. If you find any difficulty, please reach out to me through the Contact Us Page.

Command Design Pattern is often used with message queue applications such as, logging. In case of sudden system shut down/ crash, our system would be able read the incomplete commands from the queue, and resume without any data loss. Another scenario is if you want to interact with some service and that service is not available, this pattern will help you in reattempting the operation once service is up again.

Since it adds the complexity to the system, it is recommended to use this pattern in the big system where reliability is important.

HostForLIFE.eu ASP.NET Core 2.2.1 Hosting
European best, cheap and reliable ASP.NET hosting with instant activation. HostForLIFE.eu is #1 Recommended Windows and ASP.NET hosting in European Continent. With 99.99% Uptime Guaranteed of Relibility, Stability and Performace. HostForLIFE.eu security team is constantly monitoring the entire network for unusual behaviour. We deliver hosting solution including Shared hosting, Cloud hosting, Reseller hosting, Dedicated Servers, and IT as Service for companies of all size.

 



ASP.NET Core 2.2.1 Hosting - HostForLIFE.eu :: Quick Micro Services Development Using MicroService4Net (No Containers)

clock July 23, 2018 08:57 by author Peter
I won't be discussing Microservices here because I specifically need to focus on the subject of interest and that shouldn't be diluted. So, the focus area is developing Microservices without the usual containers, like docker etc., but  rather using the existing tools and technologies that belong to ASP.NET. We are targeting the below characteristics of Microservices in this article.

  1. Collection of loosely coupled services
  2. Services with isolated context and so can freely deploy.

Now, let us get an idea of the below terminologies. 

Open Web Interface for .NET
It's a middleware technology, http://owin.org/, and widely using in ASP.NET MVC Web API to enable self-hosting. You need an idea of OWIN before proceeding here.
 
MicroService4Net
It's a wrapper made on top of OWIN to make Microservice development more easy. You are able to create self hosting loosely coupled services without even the need of web servers like IIS. 
 
Two sample projects I uploaded here do simple mathematics of Addition and Multiplication. Here, I have created 2 separate services for each of these operations and you can call them independently. I also made them self hosting without even need of a web server.
 
You may be interested in the below-mentioned code.
 
Run Services using MicroService4Net
  1. var microService = new MicroService(<<port>>);  
  2. microService.Run(args); 
It's self explanatory how to create an instance of MicroService by passing an available port. Then, call microService.Run(args);. Now your service is running. It's that simple! Any MVC controllers defined in your service project can be accessed. Below are my 2 services created for Add and Multiply, they are running on different ports and also in their own context rather than sharing a common context of IIS etc. Here, a console application is basically doing the hosting using OWIN technology. 

As you can see here, hosting has been inside console applications, you can later register it as a Windows Service though. I will  be able to access my both services through browsers. See below.

So now, we have loosely coupled services running without any web server dependency. The technology is OWIN and the tool MicroService4Net made it easy to crack the Microservice part of it. I uploaded 2 projects, and each belongs to these 2 services. You can refer to it and then try to resolve one use case I mentioned below.
 
Use Case to Resolve
A Microservice deployment may contain 100s or 1000s or even more small services and each has its own boundary scope. In a practical scenario, no one wants to run all services 24/7. But what people do, whenever an API of a service has been called, then that service will go up and after execution service will go off again. So services run on demand and as our host is a console application, it seems easy. There are many ways to solve this scenario.

HostForLIFE.eu ASP.NET Core 2.2.1 Hosting
European best, cheap and reliable ASP.NET hosting with instant activation. HostForLIFE.eu is #1 Recommended Windows and ASP.NET hosting in European Continent. With 99.99% Uptime Guaranteed of Relibility, Stability and Performace. HostForLIFE.eu security team is constantly monitoring the entire network for unusual behaviour. We deliver hosting solution including Shared hosting, Cloud hosting, Reseller hosting, Dedicated Servers, and IT as Service for companies of all size.



ASP.NET Core 2.2.1 Hosting - HostForLIFE.eu :: How to Cancel Long Running Task Using Cancellation Token Source In ASP.NET Core?

clock July 18, 2018 09:09 by author Peter

In this article, we are going to learn how to cancel or interrupt the Long Running Task using the Cancellationtokensource method in ASP.NET Core. So, here is the task: My web application will send POST request by making web service API call and get the data via a third-party library. For example, consider, the application is going to send details about the students through the API and will get the response once the POST data is successfully received and saved on another side. Consider that the application is going to send 5000 bits of data through the API. So, by simply using for-each loop by calling third-party API, we can POST 5000 data.

It will take too long to finish this job. Until this job is done the application will not allow other operations to perform as the job is running in the main thread. To make it possible, we will move to “TASK”, to run this job in a separate thread so that the application can do other operations. To understand this, have a look at the code below.
Var task=Task.Factory.StartNew(()=> CallWebServiceandGetResponceData()); 

The code is creating the task which is making API webservice call inside CallWebServiceandGetResponceData function to send a request and get the response data. So by doing this, the application allows this task operation in a separate thread and we can do other operations in the main thread, which will not affect the task operation. Everything is going fine.

If you are using .NET 4.5 Framework you can use Async and wait for keywords to do this. But in .NET 4.0 we can’t use async and await keywords, so I'm going to use “TASK” to achieve this operation. So, now I need to cancel or stop the task whenever I need to. For example, 2673rd data is currently running inside the CallWebServiceandGetResponceData function, when I hit the stop button I need to cancel or stop calling third-party API and exit the loop. To do this I'm going to use CancellationToken.

.NET 4.0 introduces a lightweight struct called the cancellationToken that provide a mechanism to cancel synchronous and asynchronous operations. CancellationToken consist is a CancellationRequested method, it is a Boolean property that can be polled by code to find out if it has been canceled or the cancellation has been requested.

By using instance of CancellationTokenSource class to get a cancellation token, in cancellation model the cancellation token can be passed freely to the listeners, so that canceling token is cleanly separted from the ability to consume a request for cancellation. Here is the sample code to cancel a task,
CancellationTokenSource mCancellationTokenSource = new CancellationTokenSource(); 
CancellationToken token = mCancellationTokenSource.Token; 
Task < string > ObjTask = Task.Factory.StartNew(() => { 
    while (!token.IsCancellationRequested) { 
        try { 
            For(i = 0; i < totalrecordscount; i++) { 
                If(i == 2341) { 
                    Token.Cancecl(); 
                } 
                // Sending data to other end by api call 
            } 
        } catch () { 
            return; 
        } 
    } 
    return; 
}, token); 


The above code will run continuously in while loop until it gets the cancel request. Once the token gets a cancellation request the control will terminate the entire Task and the control will get back to the main thread. Here, the cancel request is made inside the task based on the condition.

Instead we can also stop the task whenever we want, like after the task started execution, when I hit the stop button from front end the token get cancellation request and terminate the execution. Here, is the sample code to cancel a task by clicking stop button:
Bool istasktostop = false; // declare one global bool variable 
CancellationTokenSource mCancellationTokenSource = new CancellationTokenSource(); 
CancellationToken token = mCancellationTokenSource.Token; 
Task < string > ObjTask = Task.Factory.StartNew(() => { 
    while (!token.IsCancellationRequested) { 
        try { 
            For(i = 0; i < totalrecordscount; i++) { 
                //for each loop its checking weather “istasktostop” is 
                If(istasktostop) { 
                    Token.Cancecl(); 
                } 
                // Sending data to other end by api call 
            } 
        } catch () { 
            return; 
        } 
    } 
    return; 
}, token); 
protected void Stop_Sending_Click(object sender, ImageClickEventArgs e) { 
    istasktostop = true; 
}


Normally, if we did the same operation without task and a cancellationtoken, we can't send stop request when the process is in running mode because the current process occupies  the main thread and we can't interrupt the main thread while it's running. When we did the same process inside the task, it is separated and run at a separate thread, so we can do other processes in the main thread.

HostForLIFE.eu ASP.NET Core 2.2.1 Hosting
European best, cheap and reliable ASP.NET hosting with instant activation. HostForLIFE.eu is #1 Recommended Windows and ASP.NET hosting in European Continent. With 99.99% Uptime Guaranteed of Relibility, Stability and Performace. HostForLIFE.eu security team is constantly monitoring the entire network for unusual behaviour. We deliver hosting solution including Shared hosting, Cloud hosting, Reseller hosting, Dedicated Servers, and IT as Service for companies of all size.



ASP.NET Core 2.2.1 Hosting - HostForLIFE.eu :: Appsettings.json In ASP.NET Core 2.0

clock July 16, 2018 11:41 by author Peter

As most of you are aware, in ASP.NET Core, we don't have anything called Web.config which we use to write our connection strings or application specific settings. Rather, here we have a file named appsettings.json to store similar information. There are a few very common use cases where we need to maintain multiple appsettings.json files in a single solution. For example,

Multiple application settings per application
When we need to maintain different-different application-specific settings based on the application environment. Say, one can have one type of application settings for Development, another type of application settings for Production, another one for Staging, and so on. Needless to mention, all the appsettings files will have different names.

To implement inheritance

If there are some common settings between multiple application settings file, the developer can come up with a base application settings file and on top of that specific file can be created. In that case, common information need not to be repeated in all the files.

If you are aware of the ASP.NET Core architecture, then you must agree on the point that such scenarios can also be handled very easily in the ASP.Net Core as plugging in multiple sources is very straightforward.

So, coming to the point. My article is all about how to inherit or read data from an appsettings.json file which is outside of my project. This scenario usually comes into the picture when we are maintaining different projects for holding all the shared resources that have to be consumed across many projects in a given solution file. So, in the real world, one project can have its project specific settings as well as some common (or say global) settings which are placed outside its boundary.

Let me first tell you something about my project structure. Here goes my solution structure:

As you can see in the above figure, 'CommonSettings.json' is the file which is kept outside of the main project named 'AllAboutConfigurations' and here is how my both the JSON files look like.

appsettings.json

"Logging": { 
            "IncludeScopes": false, 
            "LogLevel": { "Default": "Warning" } 
         }, 

"MySettings": {  
              "PersonalizedSettings": " It's just for me"
            }  


CommonSettings.json


"MySettings": {  
                     "CommonSettings": "Hi, I'm common setting. Anyone can use me."
            } 


Now, in order to read 'CommonSettings.json' in 'AllAboutConfigurations' project, I have to update the application configuration while constructing the web host as shown below.
public static IWebHost BuildWebHost(string[] args) => 
        WebHost.CreateDefaultBuilder(args) 
            .ConfigureAppConfiguration((webHostBuilderContext, configurationbuilder) => 
            { 
                var environment = webHostBuilderContext.HostingEnvironment; 
                string pathOfCommonSettingsFile = Path.Combine(environment.ContentRootPath,"..","Common"); 
                configurationbuilder 
                        .AddJsonFile("appSettings.json", optional: true) 
                        .AddJsonFile(Path.Combine(pathOfCommonSettingsFile, "CommonSettings.json"), optional: true); 

                configurationbuilder.AddEnvironmentVariables(); 
            }) 
            .UseStartup() 
            .Build(); 


Now, if we want to see something on our web page, we have to update UI code too. Let's make it simple with just a few lines of code as shown below.
@using Microsoft.Extensions.Configuration; 
@inject IConfiguration configuration; 

@{ 
Layout = null; 


<html> 
<head> 
<title>Settings</title> 
</head> 
<body> 
    Personalized Settings: @configuration.GetSection("MySettings")["PersonalizedSettings"]<br />  
    Common Settings: @configuration.GetSection("MySettings")["CommonSettings"]<br /> 
</body> 
</html> 


If you will run your application now, you will be able to see that both the settings are considered as part of a single project.



ASP.NET Core 2.2.1 Hosting - HostForLIFE.eu :: Indexing In-Memory Collections For Blazing Fast Access

clock July 11, 2018 09:26 by author Peter

The CodexMicroORM open source project on GitHub hosts a few useful .NET goodies that can help you write concise code with great performance. In this article, I’ll cover a collection type that supports multi-property indexing and as such, performs very well with equality lookups. It’s common (and relatively easy) to index large lists using a “side dictionary” to improve performance. Let’s consider this example where we start with a business entity that looks like this:
    class Person { 
        public string Name { 
            get; 
            set; 
        } 
        public int ? Age { 
            get; 
            set; 
        } 
    } 


and use a test harness like this,
    List < Person > people = new List < Person > (); 
    for (int i = 1; i <= 20000; ++i) { 
        people.Add(new Person() { 
            Name = $ "P{i}", Age = (i % 70) + 10 
        }); 
    } 
    Stopwatch sw = new Stopwatch(); 
    sw.Start(); 
    int found = 0; 
    for (int i = 1; i <= 1000; ++i) { 
        found += (from p in people where p.Age == 50 select p).Count(); 
    } 
    sw.Stop(); 
    Console.WriteLine($ "{sw.Elapsed.TotalMilliseconds} microseconds per iteration"); 

On my machine, this takes about 627 microseconds per pass of the final loop which is counting people aged “50” out of a basic list we populated with 20,000 Person instances. The iterating is using a simple LINQ to Objects expression, which is fast – but we can do better! Let’s make a change by adding an additional layer – a pass over the data to create an in-memory index using a Dictionary. That changes our final query considerably: finding those aged 50 becomes as simple as accessing the dictionary by key,
    Stopwatch sw = new Stopwatch(); 
    sw.Start(); 
    Dictionary < int ? , ICollection < Person >> ageMap = new Dictionary < int ? , ICollection < Person >> (); 
    foreach(var p in people) { 
        var age = p.Age; 
        if (ageMap.ContainsKey(age)) { 
            ageMap[age].Add(p); 
        } else { 
            var list = new List < Person > (); 
            list.Add(p); 
            ageMap[age] = list; 
        } 
    } 
    int found = 0; 
    for (int i = 1; i <= 1000; ++i) { 
        found += ageMap[50].Count(); 
    } 
    sw.Stop(); 
    Console.WriteLine($ "{sw.Elapsed.TotalMilliseconds} microseconds per iteration"); 


Even including the time spent building this dictionary, this runs at 9.8 microseconds per iteration. That’s 64 times faster than our LINQ query! (It’s even more dramatic if we used “int” instead of “int?” – then it’s 368 times faster!) This is probably not a great surprise: dictionaries excel with reads. A question could be: why bother with the list at all? For my purposes here, I’m assuming you may not have control over the collection type: for example, you might be consuming it from an existing object model (such as one you might be getting out of Entity Framework), or you might have an object that could be indexed in multiple ways and a more generalized collection makes sense.

The natural temptation might be to use indexes like this for any property where you might query like we did for “Age.” Too bad it increased our code size in the above example; there’s an easy solution for that, though.
ConcurrentIndexedList<T>

To streamline the use of indexes, the ConcurrentIndexedList<T> class comes to the rescue. First, let’s add a NuGet reference to CodexMicroORM.Core,

ConcurrentIndexedList
Now our above example can be simplified greatly,
    ConcurrentIndexedList < Person > people = new ConcurrentIndexedList < Person > (nameof(Person.Age), nameof(Person.Name)); 
    for (int i = 1; i <= 20000; ++i) { 
        people.Add(new Person() { 
            Name = $ "P{i}", Age = (i % 70) + 10 
        }); 
    } 
    Stopwatch sw = new Stopwatch(); 
    sw.Start(); 
    int found = 0; 
    for (int i = 1; i <= 1000; ++i) { 
        found += people.GetAllByNameNoLock(nameof(Person.Age), 50).Count(); 
    } 
    sw.Stop(); 
    Console.WriteLine($ "{sw.Elapsed.TotalMilliseconds} microseconds per iteration"); 


In this case, we’ve indexed both Age and Name, as specified in the constructor. Because of that, we could do something like this,
found += people.GetAllByNameNoLock(nameof(Person.Age), 50).Union(people.GetAllByNameNoLock(nameof(Person.Name), "P15")).Count(); 

Notice here we’re leveraging LINQ to Objects where we’re looking for the union of results from two types of indexed queries: one by age, one by name. The performance on the age lookup isn’t quite as crazy as with a single raw dictionary, but it’s still 40 times better than the plain LINQ query.

We can even index null values, too, which is something that would have complicated our original hand-rolled index for something like Name which is a reference type. For example, this works as expected, if we supported null Names:
    found += people.GetAllByNameNoLock(nameof(Person.Name), null).Count(); 

There’s one big caveat here: the current implementation of ConcurrentIndexedList<T> requires that your “T” in this case implement the ICEFIndexedListItem interface. I do this for a couple of reasons,

To avoid reflection. It does introduce boxing which does kill some performance, but not as much as would happen if we resorted to using System.Reflection.
Offers some value-add such as the ability to “unwrap” values from your objects. In the case of the framework itself, I want to index objects that are contained inside another object, namely a WeakReference object’s “Target” property. If I didn’t do this, I’d be indexing the WeakReference itself which isn’t useful to me.

The code change I used here to let my Person class work includes,
    class Person: ICEFIndexedListItem { 
        public string Name { 
            get; 
            set; 
        } 
        public int ? Age { 
            get; 
            set; 
        } 
        public object GetValue(string propName, bool unwrap) { 
            switch (propName) { 
                case nameof(Person.Name): 
                    return Name; 
                case nameof(Person.Age): 
                    return Age; 
            } 
            throw new ArgumentException("propName is not a valid property."); 
        } 
    }

If you’re unable to modify your business objects, a version of this could be constructed that does use reflection. I plan a second article to discuss my “optimized reflection helpers,” also available for free within the framework.

Added Benefits
Most operations on the collection are guarded by reader/writer locks, meaning multiple concurrent readers are allowed, but only one writer at a time can modify the collection. This resembles the behavior of the types in System.Collections.Concurrent – which I actually used more extensively in the 0.2 version of the framework, but have migrated away for potentially large/unbounded collections based on observed memory and performance issues. In this case, the simplicity of basic dictionaries and slim reader/writer locks seems to do well. Thread-safety may not be useful in every case – but it opens the door to injecting further performance improvements in your apps through the use of the Parallel .NET framework class.

In the 0.5 release of the library, I’ve included the ability to set an initial capacity for the collection. As is true with any dictionary, it’s a good idea to set this if you know up-front the approximate maximum size expected for the collection. Setting it can avoid the resize operations which the collection would otherwise have to do when it hits a size limit (as it grows). If your collections are very large, resize operations can become very slow and memory intensive.

It’s true that LINQ to Objects is often “fast enough” for reasonably small data sets. ConcurrentIndexedList<T> is a general-purpose class to support cases where you either anticipate the possibility for a large data set or can’t anticipate the amount of data at all and want to “play it safe” based on known index requirements. Other libraries exist that try to address the same problem. For example, i4o offers a way to “wrap” collections in a way that lets you use ordinary LINQ to Objects and have that in turn use indexes over the underlying data. This is a great concept, but in real-world benchmarks, I’ve seen it not always deliver in a truly generic way. (Some cases work well, some do not - and can perform worse.) There are different reasons for this, and it’s relevant to work I’m doing in CodexMicroORM, so will be likely covering deeper details in future articles.



ASP.NET Core 2.2.1 Hosting - HostForLIFE.eu :: Push Notification Using Firebase Cloud Messaging Using .NET

clock July 9, 2018 11:52 by author Peter

Firebase Cloud Messaging (FCM) is a cross-platform messaging solution that lets you reliably deliver messages at no cost.
Steps to get server key / SenderID

  1. To get the keys, log in to Firebase account.
  2. Select the project from the list.
  3. Click on "Setting" gear icon and click "Project Settings". 

  4. Click on "Cloud Messaging" tab to select "Server key" and "Sender key" from the below screen.

Add the below code to send push notifications.

//Create the web request with fire base API
WebRequest tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send");  
tRequest.Method = "post";  
//serverKey - Key from Firebase cloud messaging server  
tRequest.Headers.Add(string.Format("Authorization: key={0}", serverKey));  
//Sender Id - From firebase project setting  
tRequest.Headers.Add(string.Format("Sender: id={0}", senderId));  
tRequest.ContentType = "application/json";  
var payload = new {  
    to = deviceId,  
        priority = "high",  
        content_available = true,  
        notification = new {  
            body = txtmsg,  
                title = txttitle.Replace(":", ""),  
                sound = "sound.caf",  
                badge = badgeCounter  
        },  
};  
var serializer = new JavaScriptSerializer();  
Byte[] byteArray = Encoding.UTF8.GetBytes(payload);  
tRequest.ContentLength = byteArray.Length;  
using(Stream dataStream = tRequest.GetRequestStream()) {  
    dataStream.Write(byteArray, 0, byteArray.Length);  
    using(WebResponse tResponse = tRequest.GetResponse()) {  
        using(Stream dataStreamResponse = tResponse.GetResponseStream()) {  
            if (dataStreamResponse != null) using(StreamReader tReader = new StreamReader(ataStreamResponse)) {  
                String sResponseFromServer = tReader.ReadToEnd();  
                result.Response = sResponseFromServer;  
            }  
        }  
    }  
}

HostForLIFE.eu ASP.NET Core 2.2.1 Hosting

European best, cheap and reliable ASP.NET hosting with instant activation. HostForLIFE.eu is #1 Recommended Windows and ASP.NET hosting in European Continent. With 99.99% Uptime Guaranteed of Relibility, Stability and Performace. HostForLIFE.eu security team is constantly monitoring the entire network for unusual behaviour. We deliver hosting solution including Shared hosting, Cloud hosting, Reseller hosting, Dedicated Servers, and IT as Service for companies of all size.



ASP.NET Core 1.1 Hosting - HostForLIFE.eu :: How to Create Login Form In ASP.NET?

clock March 9, 2017 07:58 by author Peter

In this tutorial, I will show you how to create login form in ASP.NET core. You can easily implement this concept anywhere in .NET applications. I have used some controls to build the login form in a .NET application.

  •  Text Box
  •  Label and Button

Please follow these steps to make this application.
Step 1
First, open your Visual Studio -> File -> New -> Website -> ASP.NET Empty website and click OK. Now, open Solution Explorer and go to Add New Item -> Web form -> click Add.

Step 2
Now, make a Login page like the one given below.
.aspx or design page (front-end)
    <html xmlns="http://www.w3.org/1999/xhtml">  
      
    <head runat="server">  
        <title>Login Form</title>  
    </head>  
      
    <body>  
        <form id="form1" runat="server">  
            <div>  
                <table>  
                    <tr>  
                        <td> Username: </td>  
                        <td>  
                            <asp:TextBox ID="txtUserName" runat="server" />  
                            <asp:RequiredFieldValidator ID="rfvUser" ErrorMessage="Please enter Username" ControlToValidate="txtUserName" runat="server" /> </td>  
                    </tr>  
                    <tr>  
                        <td> Password: </td>  
                        <td>  
                            <asp:TextBox ID="txtPWD" runat="server" TextMode="Password" />  
                            <asp:RequiredFieldValidator ID="rfvPWD" runat="server" ControlToValidate="txtPWD" ErrorMessage="Please enter Password" /> </td>  
                    </tr>  
                    <tr>  
                        <td> </td>  
                        <td>  
                            <asp:Button ID="btnSubmit" runat="server" Text="Submit" /> </td>  
                    </tr>  
                </table>  
            </div>  
        </form>  
    </body>  
      
    </html>  


Now, create an event for click button, such as -  onclick="<>".
    onclick="btnSubmit_Click"  

So, our button tag will be like <asp:Button ID="btnSubmit" runat="server" Text="Submit" onclick="btnSubmit_Click" />. And in login.aspx.cs page, we need to write the following code.

C# Code --Code behind(Aspx.cs)
    using System;  
    using System.Data;  
    using System.Data.SqlClient;  
    using System.Configuration;  


After adding the namespaces, write the following code in code behind.
    protected void btnSubmit_Click(object sender, EventArgs e) {  
        SqlConnection con = newSqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString);  
        con.Open();  
        SqlCommand cmd = new SqlCommand("select * from UserInformation where UserName =@username and Password=@password", con);  
        cmd.Parameters.AddWithValue("@username", txtUserName.Text);  
        cmd.Parameters.AddWithValue("@password", txtPWD.Text);  
        SqlDataAdapter da = new SqlDataAdapter(cmd);  
        DataTable dt = new DataTable();  
        da.Fill(dt);  
        if (dt.Rows.Count > 0) {  
            Response.Redirect("Details.aspx");  
        } else {  
            ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Invalid Username and Password')</script>");  
        }  
    }  


Now, for connecting it with the database, write the database connection string like the following.

Web Config
    <connectionStrings>  
        <add name="dbconnection" connectionString="Data Source=BrajeshKr;Integrated Security=true;Initial Catalog=MyDB" /> </connectionStrings>

That's it. Now, our Login page is ready to be executed. You can download the code for this application and run that on your Visual Studio.

HostForLIFE.eu ASP.NET Core 1.1 Hosting

European best, cheap and reliable ASP.NET hosting with instant activation. HostForLIFE.eu is #1 Recommended Windows and ASP.NET hosting in European Continent. With 99.99% Uptime Guaranteed of Relibility, Stability and Performace. HostForLIFE.eu security team is constantly monitoring the entire network for unusual behaviour. We deliver hosting solution including Shared hosting, Cloud hosting, Reseller hosting, Dedicated Servers, and IT as Service for companies of all size.



HostForLIFE.eu Proudly Launches Umbraco 7.5.7 Hosting

clock January 27, 2017 07:16 by author Peter

HostForLIFE.eu, a leading Windows web hosting provider with innovative technology solutions and a dedicated professional services team, today announced the support for Umbraco 7.5.7 hosting plan due to high demand of Umbraco users in Europe. The company has managed to build a strong client base in a very short period of time. It is known for offering ultra-fast, fully-managed and secured services in the competitive market.

 

HostForLIFE.eu hosts its servers in top class data centers that is located in Amsterdam, (NL), London, (UK), Washington, D.C. (US), Paris, (France), Frankfurt, (Germany), Chennai, (India), Milan, (Italy), Toronto, (Canada) and São Paulo, (Brazil) to guarantee 99.9% network uptime. All data centers feature redundancies in network connectivity, power, HVAC, security and fire suppression. All hosting plans from HostForLIFE.eu include 24×7 support and 30 days money back guarantee. HostForLIFE Umbraco hosting plan starts from just as low as €3.49/month only and this plan has supported ASP.NET Core 1.1, ASP.NET MVC 5/6 and SQL Server 2012/2014/2016.

Umbraco is a fully-featured open source content management system with the flexibility to run anything from small campaign or brochure sites right through to complex applications for Fortune 500's and some of the largest media sites in the world. Umbraco is strongly supported by both an active and welcoming community of users around the world, and backed up by a rock-solid commercial organization providing professional support and tools. Umbraco can be used in its free, open-source format with the additional option of professional tools and support if required.

Umbraco release that exemplifies our mission to continue to make Umbraco a bit simpler every day. The other change is that there's now a "ValidatingRequest" event you can hook into. This event allows you to "massage" any of the requests to ImageProcessor to your own liking. So if you'd want to never allow any requests to change BackgroundColor, you can cancel that from the event. Similarly if you have a predefined set of crops that are allowed, you could make sure that no other crop sizes will be processed than those ones you have defined ahead of time.

Further information and the full range of features Umbraco 7.5.7 Hosting can be viewed here: http://hostforlife.eu/European-Umbraco-757-Hosting



ASP.NET Core 1.1 Hosting - HostForLIFE.eu :: How To Drag And Upload Multiple Image Files In ASP.NET?

clock January 25, 2017 08:20 by author Peter

In this tutorial, I will show you How To Drag And Upload Multiple Image Files In ASP.NET. First, I create a blank ASP.NET WebForm Project. And then, right click on the Project ==>select NuGet Package Manager ==> Select DropZone and Download it.

Design a WebForm having a file upload control.
    <head runat="server"> 
        <title></title> 
        <script src="scripts/dropzone/dropzone.min.js"></script> 
        <link href="scripts/dropzone/basic.min.css" rel="stylesheet" /> 
        <link href="scripts/dropzone/dropzone.css" rel="stylesheet" /> 
          
        <script src="scripts/ai.0.15.0-build58334.min.js"></script> 
    </head> 
    <body> 
        <form id="form1" runat="server" class="dropzone"> 
            <div> 
                <div class="fallback"> 
                    <asp:FileUpload ID="file" runat="server" AllowMultiple="true" /> 
                </div> 
            </div> 
        </form> 
    </body> 
    </html> 


Write the following code in the code behind(.cs) file.
    protected void Page_Load(object sender, EventArgs e) 
           { 
               foreach (string s in Request.Files) 
               { 
                   HttpPostedFile file = Request.Files[s]; 
     
                   int fileSizeInBytes = file.ContentLength; 
                   string fileName = file.FileName; 
                   string fileExtension = ""; 
     
                   if (!string.IsNullOrEmpty(fileName)) 
                       fileExtension = Path.GetExtension(fileName); 
     
                   // IMPORTANT! Make sure to validate uploaded file contents, size, etc. to prevent scripts being uploaded into your web app directory 
                   // string savedFileName = Path.Combine(@"..\Files\", Guid.NewGuid().ToString() + fileExtension); 
                  string savepath = Path.Combine(Request.PhysicalApplicationPath, "Files"); 
                   string savefile = Path.Combine(savepath, file.FileName); 
                   file.SaveAs(savefile); 
               } 
           } 


Run the project and select multiple files and drag to the box.

Drag and drop all the images to the box. It will upload the images and save them to your local folder (here in Files Folder).

HostForLIFE.eu ASP.NET Core 1.1 Hosting

European best, cheap and reliable ASP.NET hosting with instant activation. HostForLIFE.eu is #1 Recommended Windows and ASP.NET hosting in European Continent. With 99.99% Uptime Guaranteed of Relibility, Stability and Performace. HostForLIFE.eu security team is constantly monitoring the entire network for unusual behaviour. We deliver hosting solution including Shared hosting, Cloud hosting, Reseller hosting, Dedicated Servers, and IT as Service for companies of all size.

 



ASP.NET Core 1.1 Hosting - HostForLIFE.eu :: Implement .NET Core CSV WriterImplement .NET Core CSV Writer

clock January 18, 2017 07:56 by author Peter

Today, I will explain you about how to implement Generic CSV Writer which may take an input from any list and return a CSV string or write to a certain file, if specified. Although, this is a generic C# implementation and can be used in any .NET Framework supporting generics. Yet, we are going to discuss this with .NET Core. We are going to use .NET Core Console Application which is in continuation to Welcome to .NET Core Console Application.

Add new Class in DotNetCore.ConsoleApplication
We are going to add a new class CsvWriter in DotNetCore.ConsoleApplication.

  • Open existing solution in Visual Studio 2015.
  • Now, add a new class CsvWriter.cs.

        Open Add New Item Screen through DotNetCore.ConsoleApplication Context Menu of Common folder >> Add >> Class >> Installed >> .NET Core >> Class.

        Name it CsvWriter.cs.
        Click OK button.

  • Add CsvWriter implementation.


        Write<T> (IList<T> list, bool includeHeader = true).
        Creates and returns the generated CSV.
        Write<T> (IList<T> list, string fileName, bool includeHeader = true)
        Creates and returns the generated CSV and saves the generated CSV to the specified path.
        CreateCsvHeaderLine
        Creates CSV header line, if includeHeader is set true.
        CreateCsvLine<T>(T item, PropertyInfo[] properties).
        Creates a CSV line for the given type of the object.
        CreateCsvLine(IList<string> list).
        Creates a CSV line for the given list of the string by joining them, demarcated by comma.

        CreateCsvItem
        Adds the provided value item to the processed list, used to create CSV line.

        CreateCsvStringListItem
        Adds the provided string list as a single item to the processed list, which is used to create CSV line.

        CreateCsvStringArrayItem
        Adds the provided string array as a single item to the processed list, which is used to create CSV line.

        CreateCsvStringItem
        Adds the provided string value item to the processed list, used to create CSV line.

        ProcessStringEscapeSequence
        Processes the provided data to handle double quotes and comma value. If we do not apply escape sequences, they can corrupt the data.

        WriteFile
        Writes the generated CSV data to the file.

ConsoleApplication
    public class CsvWriter { 
        privateconst string DELIMITER = ","; 
     
        public string Write < T > (IList < T > list, bool includeHeader = true) { 
            StringBuildersb = new StringBuilder(); 
     
            Type type = typeof(T); 
     
            PropertyInfo[] properties = type.GetProperties(); 
     
            if (includeHeader) { 
                sb.AppendLine(this.CreateCsvHeaderLine(properties)); 
            } 
     
            foreach(var item in list) { 
                sb.AppendLine(this.CreateCsvLine(item, properties)); 
            } 
     
            returnsb.ToString(); 
        } 
     
        public string Write < T > (IList < T > list, string fileName, bool includeHeader = true) { 
            string csv = this.Write(list, includeHeader); 
     
            this.WriteFile(fileName, csv); 
     
            return csv; 
        } 
     
        private string CreateCsvHeaderLine(PropertyInfo[] properties) { 
            List < string > propertyValues = new List < string > (); 
     
            foreach(var prop in properties) { 
                stringstringformatString = string.Empty; 
                string value = prop.Name; 
     
                var attribute = prop.GetCustomAttribute(typeof(DisplayAttribute)); 
                if (attribute != null) { 
                    value = (attribute as DisplayAttribute).Name; 
                } 
     
                this.CreateCsvStringItem(propertyValues, value); 
            } 
     
            returnthis.CreateCsvLine(propertyValues); 
        } 
     
        private string CreateCsvLine < T > (T item, PropertyInfo[] properties) { 
            List < string > propertyValues = new List < string > (); 
     
            foreach(var prop in properties) { 
                stringstringformatString = string.Empty; 
                object value = prop.GetValue(item, null); 
     
                if (prop.PropertyType == typeof(string)) { 
                    this.CreateCsvStringItem(propertyValues, value); 
                } else if (prop.PropertyType == typeof(string[])) { 
                    this.CreateCsvStringArrayItem(propertyValues, value); 
                } else if (prop.PropertyType == typeof(List < string > )) { 
                    this.CreateCsvStringListItem(propertyValues, value); 
                } else { 
                    this.CreateCsvItem(propertyValues, value); 
                } 
            } 
     
            returnthis.CreateCsvLine(propertyValues); 
        } 
     
        private string CreateCsvLine(IList < string > list) { 
            returnstring.Join(CsvWriter.DELIMITER, list); 
        } 
     
        private void CreateCsvItem(List < string > propertyValues, object value) { 
            if (value != null) { 
                propertyValues.Add(value.ToString()); 
            } else { 
                propertyValues.Add(string.Empty); 
            } 
        } 
     
        private void CreateCsvStringListItem(List < string > propertyValues, object value) { 
            string formatString = "\"{0}\""; 
            if (value != null) { 
                value = this.CreateCsvLine((List < string > ) value); 
                propertyValues.Add(string.Format(formatString, this.ProcessStringEscapeSequence(value))); 
            } else { 
                propertyValues.Add(string.Empty); 
            } 
        } 
     
        private void CreateCsvStringArrayItem(List < string > propertyValues, object value) { 
            string formatString = "\"{0}\""; 
            if (value != null) { 
                value = this.CreateCsvLine(((string[]) value).ToList()); 
                propertyValues.Add(string.Format(formatString, this.ProcessStringEscapeSequence(value))); 
            } else { 
                propertyValues.Add(string.Empty); 
            } 
        } 
     
        private void CreateCsvStringItem(List < string > propertyValues, object value) { 
            string formatString = "\"{0}\""; 
            if (value != null) { 
                propertyValues.Add(string.Format(formatString, this.ProcessStringEscapeSequence(value))); 
            } else { 
                propertyValues.Add(string.Empty); 
            } 
        } 
     
        private string ProcessStringEscapeSequence(object value) { 
            returnvalue.ToString().Replace("\"", "\"\""); 
        } 
     
        public bool WriteFile(string fileName, string csv) { 
            boolfileCreated = false; 
     
            if (!string.IsNullOrWhiteSpace(fileName)) { 
                File.WriteAllText(fileName, csv); 
     
                fileCreated = true; 
            } 
     
            returnfileCreated; 
        } 
    } 


Add Test Model Class in DotNetCore.ConsoleApplication
We are going to add a new class TestVM in DotNetCore.ConsoleApplication.

  • Open the existing Solution in Visual Studio 2015
  • Now, add a new class TestVM.cs.

        Open Add New Item Screen through DotNetCore.ConsoleApplication Context Menu of Common folder >> Add >> Class >> Installed >> .NET Core >> Class.
        Name it TestVM.cs.
        Click OK button.

  • Add TestVM implementation.
  • Update Program.cs to initialize List<TestVM> with the dummy data and call CsvWriter.

    public class TestVM { 
        [Display(Name = "Test Id")] 
        publicintTestId { 
            get; 
            set; 
        } 
        [Display(Name = "Name")] 
        public string TestName { 
            get; 
            set; 
        } 
    } 
     
    public class Program { 
        public static void Main(string[] args) { 
            Console.WriteLine("Welcome to .NET Core Console Application"); 
            List < TestVM > tests = new List < TestVM > { 
                newTestVM { 
                    TestId = 1, TestName = "Bill Gates" 
                }, 
                newTestVM { 
                    TestId = 2, TestName = "Warren Buffett" 
                }, 
                newTestVM { 
                    TestId = 3, TestName = "Amancio Ortega" 
                }, 
                newTestVM { 
                    TestId = 4, TestName = "Carlos Slim Helu" 
                } 
            }; 
            stringstringfileName = string.Format("{0}\\test.csv", System.AppContext.BaseDirectory); 
            CsvWritercsvWriter = new CsvWriter(); 
            csvWriter.Write(tests, fileName, true); 
            Console.WriteLine("{0} has been created.", fileName); 
            Console.ReadKey(); 
        } 
    } 

Run Application in Debug Mode

  • Press F5 or Debug Menu >> Start Debugging or Start Console Application button on the toolbar to start the Application in the debugging mode. It will start an Application Console in the debug mode.
  • It will generate test.csv at given path. Therefore at C:\ASP.NET Core\CSV Writer\DotNetCore\ConsoleApplication.NetCore\bin\Debug\netcoreapp1.0.

HostForLIFE.eu ASP.NET Core 1.1 Hosting

European best, cheap and reliable ASP.NET hosting with instant activation. HostForLIFE.eu is #1 Recommended Windows and ASP.NET hosting in European Continent. With 99.99% Uptime Guaranteed of Relibility, Stability and Performace. HostForLIFE.eu security team is constantly monitoring the entire network for unusual behaviour. We deliver hosting solution including Shared hosting, Cloud hosting, Reseller hosting, Dedicated Servers, and IT as Service for companies of all size.



About HostForLIFE

HostForLIFE is European Windows Hosting Provider which focuses on Windows Platform only. We deliver on-demand hosting solutions including Shared hosting, Reseller Hosting, Cloud Hosting, Dedicated Servers, and IT as a Service for companies of all sizes.

We have offered the latest Windows 2019 Hosting, ASP.NET 5 Hosting, ASP.NET MVC 6 Hosting and SQL 2019 Hosting.


Month List

Tag cloud

Sign in