<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Muhammad Tayyab on Medium]]></title>
        <description><![CDATA[Stories by Muhammad Tayyab on Medium]]></description>
        <link>https://medium.com/@muhammadtayyab7?source=rss-b8df84d32d3e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*cBnVMiybRWj73SUpYGWzMA.png</url>
            <title>Stories by Muhammad Tayyab on Medium</title>
            <link>https://medium.com/@muhammadtayyab7?source=rss-b8df84d32d3e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 18:49:08 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@muhammadtayyab7/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How to Add an API Gateway in Microservices Architecture with Full Code Snippet in .NET 6]]></title>
            <link>https://medium.com/@muhammadtayyab7/how-to-add-an-api-gateway-in-microservices-architecture-with-full-code-snippet-in-net-6-65afadad07bf?source=rss-b8df84d32d3e------2</link>
            <guid isPermaLink="false">https://medium.com/p/65afadad07bf</guid>
            <dc:creator><![CDATA[Muhammad Tayyab]]></dc:creator>
            <pubDate>Thu, 25 May 2023 08:31:13 GMT</pubDate>
            <atom:updated>2023-05-25T08:31:13.964Z</atom:updated>
            <content:encoded><![CDATA[<h3>How to Add an API Gateway in Microservices Architecture with Full Code Snippet in .NET 6</h3><p>Microservices architecture is a popular way to build scalable and resilient applications. In microservices architecture, applications are broken down into small, independent services that communicate with each other over a network. This makes it easier to develop, deploy, and scale applications.</p><p>One of the challenges of microservices architecture is managing the communication between services. This is where an API gateway comes in. An API gateway is a central point of entry for all requests to the microservices architecture. It can be used to route requests to the appropriate service, authenticate users, and manage security.</p><p>In this article, we will show you how to add an API gateway in microservices architecture with full code snippet in .NET 6.</p><h3>Prerequisites</h3><p>Before you start, you will need the following:</p><ul><li>.NET 6 installed</li><li>Visual Studio 2022 installed</li><li>A code editor</li></ul><h3>Creating the API Gateway Project</h3><p>First, we need to create a new API gateway project in Visual Studio. To do this, open Visual Studio and select “Create a new project”. In the “New Project” dialog, select the “Web” category and then select the “API Gateway” template. Click “Next”.</p><p>In the next screen, enter a name for your project and select a location to save it. Click “Create”.</p><h3>Adding the API Gateway NuGet Package</h3><p>Next, we need to add the API Gateway NuGet package to our project. To do this, open the Package Manager Console and run the following command:</p><pre>PM&gt; Install-Package Microsoft.AspNetCore.ApiGateway</pre><h3>Configuring the API Gateway</h3><p>Now that we have added the API Gateway NuGet package, we need to configure it. To do this, open the Startup.cs file and add the following code to the ConfigureServices method:</p><p>Code snippet</p><pre>services.AddControllers();<br>services.AddApiGateway(options =&gt;<br>{<br>    options.DefaultHost = &quot;localhost&quot;;<br>    options.DefaultPort = 8080;<br>});</pre><p>This code configures the API Gateway to listen on port 8080 on the localhost.</p><h3>Adding a Service to the API Gateway</h3><p>Now that we have configured the API Gateway, we can add a service to it. To do this, we will create a new controller in our project. In the Solution Explorer, right-click on the Controllers folder and select &quot;Add &gt; Controller&quot;.</p><p>In the “Add Controller” dialog, select the “API Controller” template and click “Add”.</p><p>In the new controller, add the following code:</p><pre>public class HomeController : Controller<br>{<br>    public IActionResult Index()<br>    {<br>        return View();<br>    }<br>}</pre><p>This controller will simply return a view with the text “Hello World”.</p><h3>Exposing the Service to the API Gateway</h3><p>Now that we have created a service, we need to expose it to the API Gateway. To do this, we need to add a route to the API Gateway. In the Startup.cs file, add the following code to the Configure method:</p><p>Code snippet</p><pre>routes.MapHttpRoute(<br>    name: &quot;DefaultApi&quot;,<br>    routeTemplate: &quot;api/{controller}/{action}/{id}&quot;,<br>    defaults: new { controller = &quot;Home&quot;, action = &quot;Index&quot;, id = UrlParameter.Optional }<br>);</pre><p>This code adds a route to the API Gateway that will map requests to the HomeController.</p><h3>Testing the API Gateway</h3><p>Now that we have configured and exposed our service to the API Gateway, we can test it. To do this, open a web browser and navigate to http://localhost:8080/api/home. You should see the text &quot;Hello World&quot;.</p><h3>Conclusion</h3><p>In this article, we have shown you how to add an API gateway in microservices architecture with full code snippet in .NET 6. By adding an API gateway, you can make it easier to manage the communication between services in your microservices architecture.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=65afadad07bf" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Building Microservices with ASP.NET Core]]></title>
            <link>https://medium.com/@muhammadtayyab7/building-microservices-with-asp-net-core-faee1a46c1c7?source=rss-b8df84d32d3e------2</link>
            <guid isPermaLink="false">https://medium.com/p/faee1a46c1c7</guid>
            <category><![CDATA[microservices]]></category>
            <category><![CDATA[net-core]]></category>
            <dc:creator><![CDATA[Muhammad Tayyab]]></dc:creator>
            <pubDate>Mon, 22 May 2023 13:33:02 GMT</pubDate>
            <atom:updated>2023-05-22T13:33:02.128Z</atom:updated>
            <content:encoded><![CDATA[<p>In the world of software development, building large-scale applications can be a challenging endeavor. As applications grow in complexity, maintaining agility and scalability becomes crucial. This is where microservices architecture comes into play. In this article, we will dive into the world of microservices in .NET, exploring its benefits and demonstrating how to build scalable and independent applications using .NET technologies.</p><p>ASP.NET Core, Microsoft’s cross-platform web framework, provides excellent support for developing microservices. Let’s take a look at how we can leverage ASP.NET Core to build microservices in .NET.</p><pre>public class Startup<br>{<br>    public void ConfigureServices(IServiceCollection services)<br>    {<br>        services.AddControllers();<br><br>        // Add other services as needed...<br><br>        // Configure authentication and authorization<br>        services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)<br>            .AddJwtBearer(options =&gt;<br>            {<br>                options.Authority = &quot;https://identityprovider.com&quot;;<br>                options.Audience = &quot;microserviceapi&quot;;<br>            });<br><br>        services.AddAuthorization();<br>    }<br><br>    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)<br>    {<br>        // Configure middleware and pipelines<br>        app.UseRouting();<br>        app.UseAuthentication();<br>        app.UseAuthorization();<br>        app.UseEndpoints(endpoints =&gt;<br>        {<br>            endpoints.MapControllers();<br>        });<br>    }<br>}</pre><p>ASP.NET Core provides a flexible middleware pipeline, allowing us to add authentication and authorization to our microservices effortlessly. We can integrate with various identity providers and implement token-based authentication for secure communication between services.</p><p><strong>Service-to-Service Communication in Microservices</strong></p><p>One of the fundamental aspects of microservices architecture is the interaction between services. Let’s explore how services can communicate with each other using REST APIs and asynchronous messaging.</p><pre>// REST API communication example<br>[HttpGet]<br>public async Task&lt;ActionResult&gt; Get()<br>{<br>    using (var httpClient = new HttpClient())<br>    {<br>        httpClient.BaseAddress = new Uri(&quot;https://otherservice.com&quot;);<br>        var response = await httpClient.GetAsync(&quot;api/data&quot;);<br><br>        if (response.IsSuccessStatusCode)<br>        {<br>            var data = await response.Content.ReadAsStringAsync();<br>            // Process the data...<br>            return Ok(data);<br>        }<br>        else<br>        {<br>            return StatusCode((int)response.StatusCode);<br>        }<br>    }<br>}</pre><pre>// Asynchronous messaging with RabbitMQ<br>public void ConfigureEventConsumers(IApplicationBuilder app)<br>{<br>    var bus = app.ApplicationServices.GetService&lt;IBus&gt;();<br><br>    bus.PubSub.SubscribeAsync&lt;MyEvent&gt;(async (context, @event) =&gt;<br>    {<br>        // Handle the event asynchronously...<br>        await Task.CompletedTask;<br>    });<br>}</pre><p>By leveraging REST APIs and asynchronous messaging systems like RabbitMQ, services can communicate efficiently and asynchronously. This decoupled communication enables each service to evolve independently, promoting scalability and fault tolerance.</p><p><strong>Scaling Microservices with Containerization and Orchestration</strong></p><pre># Dockerfile example<br>FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build<br>WORKDIR /app<br>COPY . .<br>RUN dotnet publish -c Release -o out<br><br>FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS runtime<br>WORKDIR /app<br>COPY --from=build /app/out .<br>ENTRYPOINT [&quot;dotnet&quot;, &quot;YourMicroservice.dll&quot;]</pre><p>Containerization allows us to package microservices along with their dependencies, making them portable and isolated. We can then utilize container orchestration platforms like Kubernetes to manage the deployment, scaling, and resilience of our microservices.</p><p><strong>Monitoring and Observability in Microservices</strong></p><p>Monitoring and observability are critical in a microservices architecture to ensure the health and performance of individual services and the overall system. Let’s explore how we can implement monitoring using tools like Application Insights and Prometheus.</p><pre>public void ConfigureServices(IServiceCollection services)<br>{<br>    // Configure Application Insights<br>    services.AddApplicationInsightsTelemetry();<br><br>    // Other service configurations...<br>}</pre><pre># Prometheus configuration for scraping metrics<br>scrape_configs:<br>  - job_name: &#39;yourmicroservice&#39;<br>    static_configs:<br>      - targets: [&#39;yourmicroservice:5000&#39;]</pre><p>By integrating tools like Application Insights, we can collect metrics, logs, and traces from our microservices. We can also configure Prometheus, a popular monitoring and alerting toolkit, to scrape and store metrics for further analysis and visualization.</p><p><strong>Conclusion:</strong></p><p>Microservices architecture provides a scalable and flexible approach to building complex applications. In this article, we explored the world of microservices in .NET, leveraging ASP.NET Core for building independent services, enabling secure communication between services, scaling with containerization and orchestration, and monitoring with tools like Application Insights and Prometheus. By adopting microservices architecture in .NET, you can empower your applications to be more agile, scalable, and resilient in the face of evolving business needs.</p><p>Remember, adopting microservices is a journey, and it requires careful planning and design. It’s important to consider factors like service boundaries, data consistency, and deployment strategies to ensure the success of your microservices architecture. Happy microservices building in .NET!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=faee1a46c1c7" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>