<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Uthman Rahimi</title>
    <description>The latest articles on DEV Community by Uthman Rahimi (@uthmanrahimi).</description>
    <link>https://dev.to/uthmanrahimi</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F159152%2Fd4db93c8-7404-4586-a3fe-cf25f5895a1a.png</url>
      <title>DEV Community: Uthman Rahimi</title>
      <link>https://dev.to/uthmanrahimi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/uthmanrahimi"/>
    <language>en</language>
    <item>
      <title>Deploy .NET Core worker Service on Linux</title>
      <dc:creator>Uthman Rahimi</dc:creator>
      <pubDate>Wed, 29 Dec 2021 20:00:09 +0000</pubDate>
      <link>https://dev.to/uthmanrahimi/deploy-net-core-worker-service-on-linux-1mjc</link>
      <guid>https://dev.to/uthmanrahimi/deploy-net-core-worker-service-on-linux-1mjc</guid>
      <description>&lt;p&gt;In the &lt;a href="https://dev.to/uthmanrahimi/deploy-aspnet-core-applications-on-centos-8-43n2"&gt;previous post&lt;/a&gt;, we learned how to deploy an &lt;code&gt;ASP.NET Core&lt;/code&gt; Application on &lt;code&gt;Linux&lt;/code&gt; and configure it, in this post we are going to learn about deploying a &lt;code&gt;Worker Service&lt;/code&gt; on a &lt;code&gt;Linux&lt;/code&gt; machine.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Worker Service?
&lt;/h2&gt;

&lt;p&gt;Before talking about our main topic, for those of you who do not know what is a &lt;code&gt;worker service&lt;/code&gt;, let's learn about what is &lt;code&gt;worker service&lt;/code&gt;.&lt;br&gt;
&lt;code&gt;Worker Service&lt;/code&gt; is a built-in feature in .NET Core for creating background services. One example of using &lt;code&gt;Worker Service&lt;/code&gt; is running periodical schedules like sending &lt;code&gt;newsletter&lt;/code&gt; emails for clients every morning. To learn more about &lt;code&gt;worker service&lt;/code&gt;, refer to this &lt;a href="https://docs.microsoft.com/en-us/dotnet/core/extensions/workers"&gt;link&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;We assume that you have created a &lt;code&gt;Worker Service&lt;/code&gt; and now you want to deploy it on a Linux machine. First of all, as you learned in the previous article, we need to create a new service file, so use the below command to create a service file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo nano /etc/systemd/system/appbackground.service
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and edit its content with the following content to it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[Unit]
Description=Your description 

[Service]
Type=notify
WorkingDirectory=/home/centos/Desktop/services/

ExecStart=/usr/bin/dotnet /home/centos/Desktop/services/myapp.WorkerServic$


Environment=ASPNETCORE_ENVIRONMENT=Production
[Install]
WantedBy=multi-user.target

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then  press &lt;code&gt;ctrl+x&lt;/code&gt; to save its content and run the following commands:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo systemctl daemon-reload
sudo systemctl start appbackground.service
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you get an error after running &lt;code&gt;sudo systemctl start appbackground.service&lt;/code&gt; you will need to add a small change to your &lt;code&gt;worker service&lt;/code&gt; project.&lt;/p&gt;

&lt;p&gt;Install &lt;code&gt;Microsoft.Extensions.Hosting.Systemd&lt;/code&gt; by &lt;code&gt;nuget&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dotnet add package Microsoft.Extensions.Hosting.Systemd
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then change &lt;code&gt;CreateHostBuilder&lt;/code&gt; in &lt;code&gt;program.cs&lt;/code&gt; like below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; public static IHostBuilder CreateHostBuilder(string[] args) =&amp;gt;
            Host.CreateDefaultBuilder(args)
                .UseSystemd() // this method must be added
                .ConfigureServices((hostContext, services) =&amp;gt;
                {
                    services.AddHostedService&amp;lt;appWorker&amp;gt;();
                });
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After applying this change, get a new publish of your project and put it in the path on the &lt;code&gt;Linux&lt;/code&gt; then run start its service.&lt;/p&gt;

</description>
      <category>aspnetcore</category>
      <category>linux</category>
      <category>csharp</category>
    </item>
    <item>
      <title>Deploy ASP.NET CORE Applications on Centos 8</title>
      <dc:creator>Uthman Rahimi</dc:creator>
      <pubDate>Sun, 26 Dec 2021 09:09:30 +0000</pubDate>
      <link>https://dev.to/uthmanrahimi/deploy-aspnet-core-applications-on-centos-8-43n2</link>
      <guid>https://dev.to/uthmanrahimi/deploy-aspnet-core-applications-on-centos-8-43n2</guid>
      <description>&lt;p&gt;A couple of weeks ago I needed to deploy an &lt;code&gt;ASP.NET Core&lt;/code&gt; application on a &lt;code&gt;centos 8&lt;/code&gt; machine and that was the first time I wanted to use Linux because I had no experience working with Linux, it took me a whole day to finish this task.&lt;br&gt;
In this article, I am going to share whatever you need to know about deploying the asp.net core application on a Linux machine.&lt;/p&gt;

&lt;p&gt;First of all, if you are using Windows or Mac OS and you want to have a Linux OS you can use &lt;strong&gt;VirtualBox&lt;/strong&gt; which is free and open-source to run a Linux OS as a gust on your Windows.&lt;/p&gt;

&lt;p&gt;In order to install a package, you need to use its command which is like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo dnf install PackageName

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To deploy the &lt;code&gt;ASP.NET Core&lt;/code&gt; application we need to install some packages. These packages are required and we have to install them.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Prerequisites&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;- SDK&lt;/li&gt;
&lt;li&gt;- AspNetCoreRuntime&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Open a terminal and run the following command to install them :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo dnf install dotnet-sdk-5.0
sudo dnf install aspnetcore-runtime-5.0

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;PackageManager for &lt;code&gt;CentOs 8&lt;/code&gt; is &lt;code&gt;dnf&lt;/code&gt; and &lt;code&gt;yum&lt;/code&gt; is for &lt;code&gt;CentOs 7&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Install Nginx
&lt;/h2&gt;

&lt;p&gt;I am using &lt;code&gt;Nginx&lt;/code&gt; as a web server but you can choose &lt;code&gt;Apache&lt;/code&gt; if you do not want to use &lt;code&gt;Nginx&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So use below command to install &lt;code&gt;nginx&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo dnf install nginx
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After installation is finished, you need to enable and start &lt;code&gt;Nginx&lt;/code&gt;. To enable and  run  &lt;code&gt;Nginx&lt;/code&gt; (or any other services) you can use the below command :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo systemctl enable nginx

sudo systemctm start nginx
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will enable and start &lt;code&gt;Nginx&lt;/code&gt;. But if you want to make sure that it is running, you can check its status by using the below command to see its status.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo systemctl status nginx
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;bear in mind that whenever you want to check the status of a service you can use the above command and I think it's one of the most used commands at the beginning.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you get a result like below, it means &lt;code&gt;Nginx&lt;/code&gt; is working and it's ready to use.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9worl6fncrta96pnevvt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9worl6fncrta96pnevvt.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Good job, we have installed all the things that are required to deploy our application, next we have to create a new service to run our application.&lt;/p&gt;

&lt;h2&gt;
  
  
  Configure Nginx
&lt;/h2&gt;

&lt;p&gt;The next step is configuring &lt;code&gt;Nginx&lt;/code&gt;  to forward HTTP Requests to our &lt;code&gt;ASP.NET Core&lt;/code&gt; application, to do this we should modify its default configuration which is located in &lt;code&gt;/etc/nginx/nginx.conf&lt;/code&gt;&lt;br&gt;
run following command to open it and modify it like below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo nano /etc/nginx/nginx.conf

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;this will open the file and now replace its content with the following :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;location / {
    proxy_pass http://0.0.0.0:5000;
    proxy_redirect off;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To verify if the change we applied is fine and there is no mistake in our &lt;code&gt;syntax&lt;/code&gt;, run &lt;code&gt;sudo nginx -t&lt;/code&gt;, if the test is successful then we need to reload &lt;code&gt;nginx&lt;/code&gt;: &lt;code&gt;sudo nginx -s reload&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If you open a browser and enter &lt;code&gt;http:localhost&lt;/code&gt; you should see the default page of &lt;code&gt;Nginx&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Create a new Service
&lt;/h2&gt;

&lt;p&gt;Up until now we installed the required packages and configured &lt;code&gt;Nginx&lt;/code&gt;, now we should create a new service to run our application.&lt;/p&gt;

&lt;p&gt;To create a new Service File, use the following command :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo nano /etc/systemd/system/myapp.service

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And add following example to it, then save it by &lt;code&gt;ctrl+x&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[Unit]
Description=Example .NET Web API App running on CentOs 8

[Service]
WorkingDirectory=/var/www/myapp
ExecStart=/usr/bin/dotnet /var/www/myapp/myapp.dll
Restart=always
# Restart service after 10 seconds if the dotnet service crashes:
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=dotnet-example
User=www-data
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false

[Install]
WantedBy=multi-user.target
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And copy published Project to &lt;code&gt;var/www/myapp&lt;/code&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note that &lt;code&gt;www-data&lt;/code&gt; user must exist, otherwise your service could not be run. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If you have done all the things above, now we should enable and start our  service to run the application:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo systemctl enable myapp.service
sudo systemctl start myapp.service

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After running it, make sure it's running by checking its status &lt;code&gt;sudo systemctl satatus myapp.service&lt;/code&gt; , if you get a green &lt;code&gt;running&lt;/code&gt; result, it is working and you can access it by entering &lt;code&gt;http:localhost:5000&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Redirect Traffic to 5000
&lt;/h2&gt;

&lt;p&gt;If you want to access your project on &lt;code&gt;port 80&lt;/code&gt; and also access to it from other computers, you will need to add below line in your &lt;code&gt;appSetting.json&lt;/code&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; "Urls": "http://0.0.0.0:5000",
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;in this way, we just need to enter Server IP Address in browser, &lt;code&gt;Nginx&lt;/code&gt; will redirect to port 5000.&lt;/p&gt;

</description>
      <category>aspnetcore</category>
      <category>linux</category>
      <category>centos</category>
      <category>netcore</category>
    </item>
    <item>
      <title>Render html tags based on condition in asp.net core </title>
      <dc:creator>Uthman Rahimi</dc:creator>
      <pubDate>Fri, 17 Dec 2021 09:57:18 +0000</pubDate>
      <link>https://dev.to/uthmanrahimi/render-html-tags-based-on-condition-in-aspnet-core-4chm</link>
      <guid>https://dev.to/uthmanrahimi/render-html-tags-based-on-condition-in-aspnet-core-4chm</guid>
      <description>&lt;p&gt;If you are using  Razor in your applications based on &lt;code&gt;ASP.NET CORE&lt;/code&gt;, sometimes you might want to check something and according to that generate &lt;code&gt;HTML&lt;/code&gt; tags and show them to clients. For example one of the common things is when we check to know if a user is logged-in or not, if User is not logged-In we would show the Login button otherwise show the &lt;code&gt;Sign-out&lt;/code&gt; button.&lt;br&gt;
To accomplish this, we end up with a code like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (User.Identity.IsAuthenticated)
{
    &amp;lt;a href="/Account/Signout"&amp;gt;SignOut&amp;lt;/a&amp;gt;
}
else {
&amp;lt;a href="/Account/SignIn"&amp;gt;SignIn&amp;lt;/a&amp;gt;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is fine and does what we want, but there is one more way that I think is much more cleaner and that is using &lt;code&gt;Tag Helper&lt;/code&gt;.&lt;br&gt;
After implementing a custom Tag Helper, we can refactor our code and render HTML tags like below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;a href="/Account/Signout" condition=="@User.Identity.IsAuthenticated"&amp;gt;SignOut&amp;lt;/a&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see above, there is no &lt;code&gt;if&lt;/code&gt; statement and just used &lt;code&gt;condition&lt;/code&gt; in &lt;code&gt;HTML&lt;/code&gt; tag and passed it a &lt;code&gt;boolean&lt;/code&gt; value.&lt;/p&gt;

&lt;p&gt;Implementation of &lt;code&gt;Condition&lt;/code&gt; tag helper is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 using Microsoft.AspNetCore.Razor.TagHelpers;

namespace CustomTagHelpers.TagHelpers
{
    [HtmlTargetElement(Attributes = nameof(Condition))]
     public class ConditionTagHelper : TagHelper
    {
        public bool Condition { get; set; }

        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            if (!Condition)
            {
                output.SuppressOutput();
            }
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To make this &lt;code&gt;tag helpe&lt;/code&gt; available throughout all &lt;code&gt;views&lt;/code&gt;, add its &lt;code&gt;namespace&lt;/code&gt; to &lt;code&gt;_ViewImports.cshtml&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Condition tag helper renders output when passed a true value.&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>csharp</category>
      <category>netcore</category>
      <category>aspnetcore</category>
    </item>
    <item>
      <title>What do you do when you have nothing to do at work.</title>
      <dc:creator>Uthman Rahimi</dc:creator>
      <pubDate>Sat, 05 Oct 2019 07:52:53 +0000</pubDate>
      <link>https://dev.to/uthmanrahimi/what-do-you-do-when-you-have-nothing-to-do-at-work-104k</link>
      <guid>https://dev.to/uthmanrahimi/what-do-you-do-when-you-have-nothing-to-do-at-work-104k</guid>
      <description></description>
      <category>work</category>
    </item>
  </channel>
</rss>
