manishdhoj-blog
manishdhoj-blog
#.NET
4 posts
Don't wanna be here? Send us removal request.
manishdhoj-blog · 8 years ago
Text
Thread and ConcurrentQueue
ConcurrentQueue is a thread safe queue in .NET. We use queue to decouple tasks and also to process things faster. Recently I had to work on a task where you fetch records from a xml file,parse it and then map it into database tables. First I tried to approach this with regular procedural process. This took way longer than I had expected. In fact I was not able to run the complete process even once. I had to kill the process after 12 hours of baby sitting.
After researching, I landed on this object called ConcurrentQueue. also I decided to use thread to make the processing faster. Concurrentqueue is a thread safe queue. to summarize the steps.
1. Create a structure that contain all the essential objects for processing the task.
2. Create a concurrent queue.
3. Create a Task with the item to be queued.
3. Create an array of  Task. This asynchronously processes the method.
Also one main change I had to do is to move away from the individual read/write and move towards block read/write. I had used the EF save method in the beginning but later changed to call stored procedures passing the DataTable.
Below is the code snippet:
 private void ProcessItemWithQueue(ConcurrentQueueItem myConcurrentQueueItem)
{ _sharedQueue= new ConcurrentQueue<ConcurrentQueueItem>();  var taskToQueue = Task.Run(() =>  {  _sharedQueue.Enqueue(myConcurrentQueueItem);  }); Task[] tasks = new Task[2]; for (int i = 0; i < tasks.Length; i++)            {tasks[i] = new Task(() =>  {      ProcessQueueItems(_sharedQueue, taskToQueue); }); tasks[i].Start(); } taskToQueue.Wait(); Task.WaitAll(tasks); }
 private void ProcessQueueItems(ConcurrentQueue<ConcurrentQueueItem> queue,Task taskToQueue)        {            do            {                while (!queue.IsEmpty)                {                     ConcurrentQueueItem myUnitOfWorkDequed;                    bool gotElement = queue.TryDequeue(out myUnitOfWorkDequed);                    if (gotElement)                    {                        Process(myUnitOfWorkDequed);                    }
             }
           } while (!taskToQueue.IsCompleted);
       }
After making these changes to my code I was able to complete the process in about half an hour. Mission accomplished.
0 notes
manishdhoj-blog · 8 years ago
Text
Cloning a new git repository in Visual Studio
Recently my team moved the source control from TFS to git. I was told that it is easier to work in git when multiple programmers are working on the same code.
A repository is the location where all the files of a project is located and managed over time.
Before this you have to make sure that you have git installed into your visual studio 2017. You can install git similar to installing any other nuget.
Here are the steps I took to clone a new repository.
1. Open visual studio and open team explorer window. View>Team Explorer. 2. At the top bar of team explorer click Manage Connections. It has the icon of power plug and green in color.
Tumblr media
3.  Click Clone. Enter the URL of git repo to clone.  Enter the local path where you want all the files and folders to be copied locally after the clone completes. You may have to create new folder. The folder should be empty. 
Someone in internet suggested to create new repo with New link but I ran into issues when I did this. When you create new repo and clone into it (basically two steps). The first step New repo creates new git folders which prevents cloning into it. I ran into git error saying the folder is not empty.So I directly created the repo with Clone link.
4. After you plugin the URL of remote git repo and give it the local path, click Clone. This will copy all the files and folders locally. It may take some time depending on the size of the remote repository.
Congratulations you just cloned a git repo locally.
0 notes
manishdhoj-blog · 8 years ago
Text
Client certificate Authentication
Lately I had to implement a feature in one of my application where I had to authenticate a client before making an API call. Below are the steps I followed to implement it in .NET.
1. Getting the SSL certificate
First step is to obtain the client SSL certificate. This can be obtained from the CA or from the party who authenticates the client if he/she has it. When a CA creates a certificate it creates two certificate one for the server and the other the client. The client certificate includes 
CA name Validity period Client Name Public key/Private Key Additional Info Digital Signature of CA The client certificate is in X.509 format.
2. Once we have the client certificate we install the certificate. You may be able to install the server by clicking and install.
3. After the certificate is installed, it needs to be retrieved and added to the HttpRequest when making API Call.
If this application is to be deployed, the certificate also needs to be installed into the server where it is to be deployed.
Below is the code snippet how the client authentication is done by passing the client certificate.
var request = WebRequest.Create(apiURL) as HttpWebRequest;            X509Store store = new X509Store("My", StoreLocation.LocalMachine);            store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);            X509Certificate2Collection cers = store.Certificates.Find(X509FindType.FindBySubjectName, “myCertificateName”, false);            if(cers.Count != 1)                throw new ObjectNotFoundException("SSL certificate "+ certificateName + " not installed in the machine. Please install certificate.");             
           request.Method = "POST";            request.ContentType = "application/json";            request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;            request.Headers["username"] = "username”;            request.Headers["password"] = "password”;            request.ClientCertificates.Add(cers[0]);            Byte[] bt = Encoding.UTF8.GetBytes(jsonRequestParameters);            Stream st = request.GetRequestStream();            st.Write(bt, 0, bt.Length);            st.Close();
           using (var response = (HttpWebResponse)request.GetResponse())            {                if (response.StatusCode != HttpStatusCode.OK)                    throw new Exception(                        $"Server error (HTTP {response.StatusCode}: {response.StatusDescription}).");                using (var webResponse = new StreamReader(response.GetResponseStream()))                {                    var myResponse= webResponse.ReadToEnd();                    return myResponse ;                }            }
0 notes
manishdhoj-blog · 8 years ago
Text
Mocking .net objects in controller for unit tests
Recently I was writing some unit tests for the controller in the application I worked on. I ran into some issues creating fake objects and setting fake values for the .net classes and linked to the controller. Due to these objects creating unit tests for methods in controller was a little tricky. Here are some of my findings that I came across that may be helpful for others creating unit tests for mvc controller in .NET.
1. The controller class is always tied with the HttpContext. Faking a HttpContext maybe a first step to write a unit test. Below I have created an HttpContext class with no values. You can write the values according to your need
Create a Fake HttpContext public class MockHelpers     {         public static HttpContext FakeHttpContext()         {             var httpRequest = new HttpRequest("", "http://localhost/", "");             var stringWriter = new StringWriter();             var httpResponse = new HttpResponse(stringWriter);             var httpContext = new HttpContext(httpRequest, httpResponse);             return httpContext;         }     }
You can then consume the object like this HttpContext.Current = MockHelpers.FakeHttpContext();
2. There are multiple instances where you check for error in ModelState before moving forward with rest of the code. One issue I faced during writing test was to find a way to set the ModelState error to false. Setting this Boolean to false will allow you to test the case when an error occurs in the Model.
To set ModelState=false   controller.ModelState.AddModelError("test","testing controller...");   
3. There was an instance where I had to fake Url.Content(""). My test failed at this point because the virtualPath value was null. To resolve this I had to find a way to set virtualPath value. To do this   To set virtualpath
_context = new Mock<HttpContextBase>(); _context.Setup(c => c.Request.ApplicationPath).Returns("/tmp/testpath"); _context.Setup(c => c.Response.ApplyAppPathModifier(It.IsAny<string>())).Returns("/mynewVirtualPath/");
Upon doing this the code below will not fail. var urlBuilder = new UriBuilder(Request.Url.AbsoluteUri);
4. At one place I was faced with the problem of setting UrlHelper object. Here is how you fake urlHelper object.
To set UrlHelper.Url    var requestContext = new RequestContext(_context.Object, new RouteData());   var urlHelper = new UrlHelper(requestContext);    _controller.Url = urlHelper;
this was the code that was failing which resolved after UrlHelper was assigned to controller.
var path= Url.Content("~/images/test.png");
0 notes