<?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: Abobade Kolawole</title>
    <description>The latest articles on DEV Community by Abobade Kolawole (@omoabobade).</description>
    <link>https://dev.to/omoabobade</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%2F126020%2F27bdd12f-c898-4c30-bebe-09f5c8c13729.jpeg</url>
      <title>DEV Community: Abobade Kolawole</title>
      <link>https://dev.to/omoabobade</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/omoabobade"/>
    <language>en</language>
    <item>
      <title>Introducing nugetpush a nifty tool to help reduce your nuget packaging headache.</title>
      <dc:creator>Abobade Kolawole</dc:creator>
      <pubDate>Sat, 28 Nov 2020 06:31:27 +0000</pubDate>
      <link>https://dev.to/omoabobade/introducing-nugetpush-a-nifty-tool-to-help-reduce-your-nuget-packaging-headache-3lbe</link>
      <guid>https://dev.to/omoabobade/introducing-nugetpush-a-nifty-tool-to-help-reduce-your-nuget-packaging-headache-3lbe</guid>
      <description>&lt;p&gt;&lt;a href="https://droplet.uno/introducing-nugetpush-a-tool-to-help-publish-your-package-without-hassle/"&gt;https://droplet.uno/introducing-nugetpush-a-tool-to-help-publish-your-package-without-hassle/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>dotnet</category>
      <category>csharp</category>
    </item>
    <item>
      <title>Creating Custom Server in .Net Core</title>
      <dc:creator>Abobade Kolawole</dc:creator>
      <pubDate>Thu, 19 Nov 2020 22:16:16 +0000</pubDate>
      <link>https://dev.to/omoabobade/creating-custom-server-in-net-core-3fp2</link>
      <guid>https://dev.to/omoabobade/creating-custom-server-in-net-core-3fp2</guid>
      <description>&lt;p&gt;We will be taking a look at the internal working of a Server in .Net Core by implementing our own custom server.&lt;/p&gt;

&lt;p&gt;To create a custom server, the custom server class must implement IServer interface and its corresponding methods. The server we will be building will bind to localhost on a port (127.0.0.1:8091), listen for HTTP request then forward the HTTP request to the rest of the request pipeline of the application. After the request is processed a corresponding response is issued.&lt;/p&gt;

&lt;p&gt;We will start by creating a new project, a console app project called CustomServerExample. &lt;/p&gt;

&lt;p&gt;The &lt;em&gt;Program.cs &lt;/em&gt;is the main entry point of the application and should look like this. &lt;/p&gt;

&lt;pre&gt;&lt;code&gt;  class Program
    {
        static void Main(string[] args)
        {
            BuildWebHost(args).Run();
        }

        public static IWebHost BuildWebHost(string[] args) =&amp;gt;
            WebHost.CreateDefaultBuilder(args)
                .UseCustomServer(o =&amp;gt; o.ListeningEndpoint = @"127.0.0.1:8091")
                .UseStartup&amp;lt;Startup&amp;gt;()
                .Build();
    }&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The &lt;em&gt;UseCustomServer &lt;/em&gt;method is the point of entry for our Custom Server implementation. We are also passing the endpoint at which our server will be listening for HTTP traffic.&lt;/p&gt;

&lt;p&gt;We will create an extension class that will define our &lt;em&gt;UseCustomerServer&lt;/em&gt; Method.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;        public static class ServerExtensions
        {
            public static IWebHostBuilder UseCustomServer(this IWebHostBuilder hostBuilder, Action&amp;lt;CustomServerOptions&amp;gt; options)
            {
                return hostBuilder.ConfigureServices(services =&amp;gt;
                {
                    services.Configure(options);
                    services.AddSingleton&amp;lt;IServer, CustomServer&amp;gt;();
                });
            }
        }&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In the extension above we registered our &lt;em&gt;CustomServer &lt;/em&gt;Class for Injection into the application pool by registering a singleton of type IServer.&lt;/p&gt;

&lt;p&gt;With all this done we can now move into implementation of our CustomServer proper. &lt;/p&gt;

&lt;p&gt;The &lt;em&gt;IServer &lt;/em&gt;Interface has the following declarations: &lt;/p&gt;

&lt;pre&gt;&lt;code&gt;    //
    // Summary:
    //     Represents a server.
    public interface IServer : IDisposable
    {
        //
        // Summary:
        //     A collection of HTTP features of the server.
        IFeatureCollection Features { get; }

        //
        // Summary:
        //     Start the server with an application.
        //
        // Parameters:
        //   application:
        //     An instance of Microsoft.AspNetCore.Hosting.Server.IHttpApplication`1.
        //
        //   cancellationToken:
        //     Indicates if the server startup should be aborted.
        //
        // Type parameters:
        //   TContext:
        //     The context associated with the application.
        Task StartAsync&amp;lt;TContext&amp;gt;(IHttpApplication&amp;lt;TContext&amp;gt; application, CancellationToken cancellationToken);
        //
        // Summary:
        //     Stop processing requests and shut down the server, gracefully if possible.
        //
        // Parameters:
        //   cancellationToken:
        //     Indicates if the graceful shutdown should be aborted.
        Task StopAsync(CancellationToken cancellationToken);
    }&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The &lt;em&gt;Features &lt;/em&gt;property contains necessary HTTP features of this Server, it also contains &lt;em&gt;StartAsync &lt;/em&gt;and &lt;em&gt;StopAsync &lt;/em&gt;methods. The former is invoked when the server starts with the necessary application context and the latter is invoked to gracefully shut down a server.&lt;/p&gt;

&lt;p&gt;Since our &lt;em&gt;CustomServer &lt;/em&gt;class was registered as type &lt;em&gt;IServer &lt;/em&gt;in the &lt;em&gt;ServerExtension &lt;/em&gt;earlier declared, we will be implementing all the signatures of this interface in our class. So our CustomServer will look like below&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;    public class CustomServer : IServer
    {
        public CustomServer(IOptions&amp;lt;CustomServerOptions&amp;gt; options)
        {
            Features.Set&amp;lt;IHttpRequestFeature&amp;gt;(new HttpRequestFeature());
            Features.Set&amp;lt;IHttpResponseFeature&amp;gt;(new HttpResponseFeature());

            var serverAddressesFeature = new ServerAddressesFeature();
            serverAddressesFeature.Addresses.Add(options.Value.ListeningEndpoint);
            Features.Set&amp;lt;IServerAddressesFeature&amp;gt;(serverAddressesFeature);
        }

        public IFeatureCollection Features { get; } = new FeatureCollection();

        public void Dispose() { }

        public Task StartAsync&amp;lt;TContext&amp;gt;(IHttpApplication&amp;lt;TContext&amp;gt; application, CancellationToken cancellationToken)
        {
            return Task.Run(() =&amp;gt;
            {
                var listener = new CustomHTTPListener&amp;lt;TContext&amp;gt;(application, Features);
                listener.Listen();
            });
        }
...
    }&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In the constructor, we have the Features for both Request and Response setup also we initiate a new instance of &lt;em&gt;ServerAddressesFeature&lt;/em&gt; while adding the listening endpoint from the &lt;em&gt;IOptions &lt;/em&gt;injected then adding the feature to the IFeatureCollection. &lt;/p&gt;

&lt;p&gt;The &lt;em&gt;CustomServer &lt;/em&gt;has a server listening address now and can process HTTP request and response. Next, we move to StartAsync. &lt;/p&gt;

&lt;p&gt;When the Server starts, a new instance of &lt;em&gt;CustomHTTPListener&lt;/em&gt; is created and it is responsible for listening to HTTP request. For the content in &lt;em&gt;CustomHTTPListener&lt;/em&gt;;&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;    public class CustomHTTPListener&amp;lt;TContext&amp;gt;
    {
        private IHttpApplication&amp;lt;TContext&amp;gt; application;
        private IFeatureCollection features;
        private string uri;
        private HttpListener listener;

        public CustomHTTPListener(IHttpApplication&amp;lt;TContext&amp;gt; application, IFeatureCollection features)
        {
            this.application = application;
            this.features = features;
            uri = features.Get&amp;lt;IServerAddressesFeature&amp;gt;().Addresses.FirstOrDefault();
            listener = new HttpListener();
            listener.Prefixes.Add(uri);

        }

        public void Listen()
        {
            while (true)
            {
              if (listener.IsListening) return;
                ThreadPool.QueueUserWorkItem(async (_) =&amp;gt;
                {
                    listener.Start();
                    HttpListenerContext ctx = listener.GetContext();
                    var context = (HostingApplication.Context)(object)application.CreateContext(features);
                    context.HttpContext = new CustomHttpContext(features, ctx);
                    await application.ProcessRequestAsync((TContext)(object)context);
                    context.HttpContext.Response.OnCompleted(null, null);
                    //listener.Stop();
                });
            }
        }
    }&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The listener constructor accepts an IHttpApplication&amp;lt;TContext&amp;gt;, which is an instance of the application itself, it is the conduit to which important information is passed around the application. We also have &lt;em&gt;IFeatureCollection &lt;/em&gt;as a parameter. An instance of &lt;em&gt;HttpListerner &lt;/em&gt;is initialized and the uri gotten from the Application Features is added to the &lt;em&gt;listener&lt;/em&gt;.&lt;em&gt;prefixes.add &lt;/em&gt;method. &lt;/p&gt;

&lt;p&gt;In the &lt;em&gt;Listen &lt;/em&gt;method,  within the while loop,  if the Listener is listening, the iteration is exited. We add a delegate to a thread pool to help manage the listening. We then start the listener, get the listener context and pass this and the application features to &lt;em&gt;CustomHttpContext&lt;/em&gt; which is intended to infuse the derivative of the &lt;em&gt;HttpRequest &lt;/em&gt;and &lt;em&gt;HttpResponse &lt;/em&gt;into the current application Context.&lt;/p&gt;

&lt;p&gt;Below is what out &lt;em&gt;CustomHttpContext &lt;/em&gt;looks like;&lt;/p&gt;

&lt;pre&gt;&lt;code&gt; public class CustomHttpContext : HttpContext
    {
        private IFeatureCollection features;
        private HttpListenerContext ctx;
        private readonly HttpRequest request;
        private readonly HttpResponse response;

        public CustomHttpContext(IFeatureCollection features, HttpListenerContext ctx)
        {
            this.Features = features;
            Request = new CustomHttpRequest(this, ctx);
            Response = new CustomHttpResponse(this, ctx);
        }

        public override IFeatureCollection Features { get; }

        public override HttpRequest Request { get;  }

        public override HttpResponse Response { get; }

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

&lt;p&gt;&lt;em&gt;CustomHttpContext&lt;/em&gt; inherits the Abstract class &lt;em&gt;HttpContext&lt;/em&gt; and we are implementing it default methods. &lt;/p&gt;

&lt;p&gt;In the constructor, Request and Response property are assigned with &lt;em&gt;CustomHttpRequest&lt;/em&gt; and &lt;em&gt;CustomHttpResponse&lt;/em&gt; both inherit from and override the &lt;em&gt;HttpRequest &lt;/em&gt;and &lt;em&gt;HttpResponse&lt;/em&gt;, respectively.&lt;/p&gt;

&lt;p&gt;Looking at &lt;em&gt;CustomHttpRequest&lt;/em&gt;, we set the Method and Path properties which both needed for proper request routing within the application pipeline.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt; public class CustomHttpRequest : HttpRequest
    {
        private CustomHttpContext customHttpContext;
        public CustomHttpRequest(CustomHttpContext customHttpContext, HttpListenerContext ctx)
        {
            this.customHttpContext = customHttpContext;
            Method = ctx.Request.HttpMethod;
            Path = ctx.Request.RawUrl;
        }

        public override string Method { get; set; }
        public override string Scheme { get; set; }
        public override bool IsHttps { get; set; }
        public override HostString Host { get; set; }

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

&lt;p&gt;In the &lt;em&gt;CustomHttpResponse&lt;/em&gt; below, the OnCompleted method is triggered and the response body is read and added back to the &lt;em&gt;HttpListener &lt;/em&gt;stream of response.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;...
        public override Stream Body { get; set; } = new MemoryStream();

        public override void OnCompleted(Func&amp;lt;object, Task&amp;gt; callback, object state)
        {
            if (!Body.CanRead) return;
            using (var reader = new StreamReader(Body))
            {
                HttpListenerResponse response = ctx.Response;
                Body.Position = 0;
                var text = reader.ReadToEnd();
                byte[] buffer = System.Text.Encoding.UTF8.GetBytes(text);
                response.ContentLength64 = Body.Length;
                response.ContentLength64 = buffer.Length;
                System.IO.Stream output = response.OutputStream;
                output.Write(buffer, 0, buffer.Length);
                output.Close();
                Body.Flush();
                Body.Dispose();
            }
        }
...&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;With all these done we are still missing the critical route handling part. So we will create &lt;em&gt;Startup.cs&lt;/em&gt; and in there we add callback into the application pipeline that will help us handle the routing.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;        public void Configure(IApplicationBuilder app)
        {

            app.MapWhen(c =&amp;gt; c.Request.Path == "/foo/bar", config =&amp;gt;
            {
                config.Run(async (context) =&amp;gt;
                {
                    context.Response.StatusCode = 200;
                    await context.Response.WriteAsync("This is Foo Bar!");
                });
            })
            .Run(async (context) =&amp;gt;
            {
                context.Response.StatusCode = 404;
                await context.Response.WriteAsync("Not Found");
            });
        }&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;With this, our server is done and we can give it a spin. Running our code we should see the terminal pop up with something like this.&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%2Fdroplet.uno%2Fwp-content%2Fuploads%2F2020%2F11%2FListener-1024x491.jpg" 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%2Fdroplet.uno%2Fwp-content%2Fuploads%2F2020%2F11%2FListener-1024x491.jpg" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;While you necessarily wouldn't have to implement a server of your own as the default one that comes with .netcore is powerful enough and the webapi already abstract and bootstrapped things like routing and HTTP request and response handling. This post is meant to help show you what goes on under the hood. &lt;/p&gt;

&lt;p&gt;The full repository of code can be found here &lt;a href="https://github.com/omoabobade/CustomServer" rel="noopener noreferrer"&gt;https://github.com/omoabobade/CustomServer&lt;/a&gt;&lt;/p&gt;

</description>
      <category>dotnet</category>
    </item>
  </channel>
</rss>
