<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Webapi &#8211; blog.boro2g .co.uk</title>
	<atom:link href="https://blog.boro2g.co.uk/category/webapi/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.boro2g.co.uk</link>
	<description>Some ideas about coding, dev and all things online.</description>
	<lastBuildDate>Fri, 18 Sep 2020 15:09:42 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.5.8</generator>
	<item>
		<title>Monolith, microservice or something in-between</title>
		<link>https://blog.boro2g.co.uk/monolith-microservice-or-something-in-between/</link>
					<comments>https://blog.boro2g.co.uk/monolith-microservice-or-something-in-between/#respond</comments>
		
		<dc:creator><![CDATA[boro]]></dc:creator>
		<pubDate>Fri, 18 Sep 2020 15:05:49 +0000</pubDate>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Webapi]]></category>
		<guid isPermaLink="false">https://blog.boro2g.co.uk/?p=1129</guid>

					<description><![CDATA[<p>In a recent project we had an interesting challenge as to how we structured and architected our dotnetcore web api&#8217;s. We wanted development and deployment agility, but to maintain the flexibility that comes from micro(macro*)services. * Arguably the term you use here depends on how you choose to cut your system and services What to [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://blog.boro2g.co.uk/monolith-microservice-or-something-in-between/">Monolith, microservice or something in-between</a> appeared first on <a rel="nofollow" href="https://blog.boro2g.co.uk">blog.boro2g .co.uk</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>In a recent project we had an interesting challenge as to how we structured and architected our dotnetcore web api&#8217;s. We wanted development and deployment agility, but to maintain the flexibility that comes from micro(macro*)services. </p>



<p><em>* Arguably the term you use here depends on how you choose to cut your system and services</em></p>



<p><strong>What to expect</strong></p>



<p>Well, maybe lets start with the opposite &#8211; what not to expect?</p>



<ul><li>This isn&#8217;t aimed to be a starter kit, it&#8217;s goal is to provide examples</li><li>How you choose to cut up your apis, well that&#8217;s one for you too &#8211; sorry</li><li>And finally, how you choose to name things (models vs data, services vs domain, core vs base, shared vs common) &#8211; yep, that&#8217;s also up to you</li></ul>



<p>Now the good bits, what to expect?</p>



<p>The goal of the example project (<a rel="noreferrer noopener" href="https://github.com/boro2g/MonolithToMicroserviceApi" target="_blank">https://github.com/boro2g/MonolithToMicroserviceApi</a>) is to show working examples of the following scenarios:</p>



<ul><li>Each WebApi project can run in isolation, without knowledge of others<ul><li>The same mindset applies to deployments &#8211; each WebApi can be deployed in isolation</li></ul></li><li>All the WebApi&#8217;s can be run and deployed as a single application</li></ul>



<p><strong>Why add this extra complexity?</strong></p>



<p>Good question. The key thing for me is flexibility. If it&#8217;s done right you give yourself options &#8211; you want to deploy as a monolith, no problem. You want to deploy each bit in isolation, well that&#8217;s fine too.</p>



<p><strong>How does it look?</strong></p>



<figure class="wp-block-image size-large"><img fetchpriority="high" decoding="async" width="387" height="231" src="https://blog.boro2g.co.uk/wp-content/uploads/2020/09/image.png" alt="" class="wp-image-1131" srcset="https://blog.boro2g.co.uk/wp-content/uploads/2020/09/image.png 387w, https://blog.boro2g.co.uk/wp-content/uploads/2020/09/image-300x179.png 300w" sizes="(max-width: 387px) 100vw, 387px" /></figure>



<p>Some key highlights of the image above:</p>



<ol><li><em>MonolithToMicroserviceApi.WebApi</em><ol><li>This is the shared singular WebApi project that brings everything together</li><li>You can run this via IISExpress, or IIS etc and all the Api&#8217;s from the other projects will work within it</li></ol></li><li><em>MonolithToMicroserviceApi.Search.WebApi</em><ol><li>This is the search micro(macro) service</li><li>You can run this in isolation, much like you can the common one</li></ol></li><li><em>MonolithToMicroserviceApi.Weather.WebApi</em><ol><li>The same concept as Search, but with other example controllers and code</li></ol></li><li><em>MonolithToMicroserviceApi.Shared.*</em><ol><li>These libraries contain common functionality that&#8217;s shared between each WebApi</li></ol></li></ol>



<p><strong>Adding a new WebApi</strong></p>



<p>The search project has a good example of this. If you look in <em>MonolithToMicroserviceApi.Search.WebApi.Startup</em> </p>



<figure class="wp-block-image size-large"><img decoding="async" width="623" height="395" src="https://blog.boro2g.co.uk/wp-content/uploads/2020/09/image-1.png" alt="" class="wp-image-1134" srcset="https://blog.boro2g.co.uk/wp-content/uploads/2020/09/image-1.png 623w, https://blog.boro2g.co.uk/wp-content/uploads/2020/09/image-1-300x190.png 300w" sizes="(max-width: 623px) 100vw, 623px" /></figure>



<p>You need to add the ApiConfiguration class itself (see the project for examples), the ApiConfigurations code above and then register them all.</p>



<p>Similarly in the common project startup (<em>MonolithToMicroserviceApi.WebApi.Startup</em>). Simply add each ApiConfiguration and register them.</p>



<figure class="wp-block-image size-large"><img decoding="async" width="323" height="108" src="https://blog.boro2g.co.uk/wp-content/uploads/2020/09/image-2.png" alt="" class="wp-image-1135" srcset="https://blog.boro2g.co.uk/wp-content/uploads/2020/09/image-2.png 323w, https://blog.boro2g.co.uk/wp-content/uploads/2020/09/image-2-300x100.png 300w" sizes="(max-width: 323px) 100vw, 323px" /></figure>



<p><strong>The Api glue</strong></p>



<p>So how does it all glue together? The key underlying code that allows you to pool controllers from one project into another is:</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="485" height="22" src="https://blog.boro2g.co.uk/wp-content/uploads/2020/09/image-3.png" alt="" class="wp-image-1136" srcset="https://blog.boro2g.co.uk/wp-content/uploads/2020/09/image-3.png 485w, https://blog.boro2g.co.uk/wp-content/uploads/2020/09/image-3-300x14.png 300w" sizes="(max-width: 485px) 100vw, 485px" /></figure>



<p><strong>What issues you might run into?</strong></p>



<ul><li>Routing<ul><li>There is a commented out example of this &#8211; in the core project and weather project we have a &#8216;WeatherForecastController&#8217; &#8211; if both of these have the same [Route] attribute you will get an exception. </li><li>A simple fix is to ensure each controller has isolated routes. I&#8217;m sure a more clever approach could be used if you have LOTS of WebApi projects, but I&#8217;ll leave that for you to work out</li></ul></li></ul>



<figure class="wp-block-image"><img loading="lazy" decoding="async" width="465" height="121" src="https://blog.boro2g.co.uk/wp-content/uploads/2020/09/image-4.png" alt="" class="wp-image-1137" srcset="https://blog.boro2g.co.uk/wp-content/uploads/2020/09/image-4.png 465w, https://blog.boro2g.co.uk/wp-content/uploads/2020/09/image-4-300x78.png 300w" sizes="(max-width: 465px) 100vw, 465px" /></figure>



<ul><li>Dependency bleeding<ul><li>I don&#8217;t feel like this approach introduces any more risk of either cyclic dependencies or &#8216;<a href="https://en.wikipedia.org/wiki/Big_ball_of_mud" target="_blank" rel="noreferrer noopener">balls of mud</a>&#8216; &#8211; IMO that comes down the discipline of the team building your solutions.</li></ul></li></ul>



<p><strong>Summary</strong></p>



<p>What I like about this approach is flexibility. On day 1 you can deploy your common project to a single box and all your api&#8217;s are working in one place. Over time, as complexity grows, or your dev teams evolve, different parts can be cut apart but without any fundamental changes needed.</p>



<p>You need to scale your search api, well no problem &#8211; deploy it as a single api and scale as you need. </p>



<p>You need to push the weather api to multiple data centres for geo reasons, cut it out and deploy as you want.</p>



<p>Another team needs to own search, again thats fine &#8211; you could even pull out to another solution, remove the ApiConfiguration and everyone is happy!? <img src="https://s.w.org/images/core/emoji/15.0.3/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<p>I hope it provides some good inspiration. It really doesn&#8217;t take much code, or configuration to build what I&#8217;d consider to be a very flexible approach to structuring your dotnetcore WebApi projects.</p>
<p>The post <a rel="nofollow" href="https://blog.boro2g.co.uk/monolith-microservice-or-something-in-between/">Monolith, microservice or something in-between</a> appeared first on <a rel="nofollow" href="https://blog.boro2g.co.uk">blog.boro2g .co.uk</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.boro2g.co.uk/monolith-microservice-or-something-in-between/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Documenting webapi with Swagger</title>
		<link>https://blog.boro2g.co.uk/documenting-webapi-with-swagger/</link>
					<comments>https://blog.boro2g.co.uk/documenting-webapi-with-swagger/#comments</comments>
		
		<dc:creator><![CDATA[boro]]></dc:creator>
		<pubDate>Mon, 26 Oct 2015 15:28:38 +0000</pubDate>
				<category><![CDATA[asp.net]]></category>
		<category><![CDATA[Webapi]]></category>
		<guid isPermaLink="false">http://blog.boro2g.co.uk/?p=647</guid>

					<description><![CDATA[<p>If you&#8217;ve ever worked with webservices, chances are you&#8217;ve run into WSDL (http://www.w3.org/TR/wsdl). In the webapi world you don&#8217;t get so much out the box &#8211; this is where swagger can help expose test methods, documentation and a lot more. For asp.net projects you can make use of a library: https://github.com/domaindrivendev/Swashbuckle. Install via nuget and you get [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://blog.boro2g.co.uk/documenting-webapi-with-swagger/">Documenting webapi with Swagger</a> appeared first on <a rel="nofollow" href="https://blog.boro2g.co.uk">blog.boro2g .co.uk</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>If you&#8217;ve ever worked with webservices, chances are you&#8217;ve run into WSDL (<a href="http://www.w3.org/TR/wsdl" target="_blank">http://www.w3.org/TR/wsdl</a>). In the webapi world you don&#8217;t get so much out the box &#8211; this is where <a href="http://swagger.io/" target="_blank">swagger</a> can help expose test methods, documentation and a lot more.</p>
<p>For asp.net projects you can make use of a library: <a href="https://github.com/domaindrivendev/Swashbuckle" target="_blank">https://github.com/domaindrivendev/Swashbuckle</a>. Install via nuget and you get a UI allowing a configurable interaction with all the webapi methods in your solution:</p>
<p>The swagger UI:<br />
<a href="http://blog.boro2g.co.uk/wp-content/uploads/2015/10/swagger-ui.png"><img loading="lazy" decoding="async" class="alignnone size-full wp-image-648" src="http://blog.boro2g.co.uk/wp-content/uploads/2015/10/swagger-ui.png" alt="swagger ui" width="312" height="393" srcset="https://blog.boro2g.co.uk/wp-content/uploads/2015/10/swagger-ui.png 312w, https://blog.boro2g.co.uk/wp-content/uploads/2015/10/swagger-ui-238x300.png 238w" sizes="(max-width: 312px) 100vw, 312px" /></a></p>
<p>The test controller and methods:<br />
<a href="http://blog.boro2g.co.uk/wp-content/uploads/2015/10/webapi-methods.png"><img loading="lazy" decoding="async" class="alignnone size-full wp-image-649" src="http://blog.boro2g.co.uk/wp-content/uploads/2015/10/webapi-methods.png" alt="webapi methods" width="326" height="377" srcset="https://blog.boro2g.co.uk/wp-content/uploads/2015/10/webapi-methods.png 326w, https://blog.boro2g.co.uk/wp-content/uploads/2015/10/webapi-methods-259x300.png 259w" sizes="(max-width: 326px) 100vw, 326px" /></a></p>
<p><strong>All pretty simple stuff &#8211; how about if you want to secure things?</strong><br />
An example scenario might be you only want swagger accessible if you are visiting via <em>http://localhost</em> (or a loopback url).</p>
<p>It&#8217;s straight forwards if you implement a custom webapi DelegatingHandler.</p><pre class="crayon-plain-tag">using System.Net.Http;
using System.Security;
using System.Threading;
using System.Threading.Tasks;

namespace SwaggerDemo.Controllers.Api
{
    public class SwaggerAccessHandler : DelegatingHandler
    {
        protected override async Task&lt;HttpResponseMessage&gt; SendAsync(
            HttpRequestMessage request, CancellationToken cancellationToken)
        {
            if (request.RequestUri.ToString().ToLower().Contains(&quot;/swagger/&quot;))
            {
                if (IsTrustedRequest(request))
                {
                    return await base.SendAsync(request, cancellationToken);
                }
                else
                {
                    throw new SecurityException();
                }
            }

            return await base.SendAsync(request, cancellationToken);
        }

        private bool IsTrustedRequest(HttpRequestMessage request)
        {
            //concoct the logic you require here to determine if the request is trusted or not
            // an example could be: if request.IsLocal()
            return true;
        }
    }
}</pre><p></p>
<p>This then needs wiring into the webapi request pipelines. In your WebApiConfig file (OTB in your solution in the folder: App_Start) add:</p><pre class="crayon-plain-tag">config.MessageHandlers.Add(new SwaggerAccessHandler());</pre><p></p>
<p>In the TestController example above we had several httpPost methods available &#8211; to enable this functionality you need to allow the routes to include the {action} url chunk.</p><pre class="crayon-plain-tag">config.Routes.MapHttpRoute(
    name: &quot;DefaultApiWithAction&quot;,
    routeTemplate: &quot;api/{controller}/{action}/{id}&quot;,
    defaults: new { id = RouteParameter.Optional }
);</pre><p></p>
<p>Azure webapi&#8217;s are now compatible with swagger &#8211; see <a href="https://azure.microsoft.com/en-gb/documentation/articles/app-service-dotnet-create-api-app/" target="_blank">https://azure.microsoft.com/en-gb/documentation/articles/app-service-dotnet-create-api-app/</a> for more info.</p>
<p>The post <a rel="nofollow" href="https://blog.boro2g.co.uk/documenting-webapi-with-swagger/">Documenting webapi with Swagger</a> appeared first on <a rel="nofollow" href="https://blog.boro2g.co.uk">blog.boro2g .co.uk</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.boro2g.co.uk/documenting-webapi-with-swagger/feed/</wfw:commentRss>
			<slash:comments>3</slash:comments>
		
		
			</item>
	</channel>
</rss>
