<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="https://www.w3.org/2005/Atom" xmlns:sy="https://purl.org/rss/1.0/modules/syndication/" xmlns:dc="https://purl.org/dc/elements/1.1/" xmlns:content="https://purl.org/rss/1.0/modules/content/" xmlns:wfw="https://wellformedweb.org/CommentAPI/" xmlns:slash="https://purl.org/rss/1.0/modules/slash/"
  version="2.0">
  <channel>
    <title>Bursts of Random Architecture Thoughts</title>
    <atom:link
      href="https://docs.microsoft.com/archive/blogs/bursts_of_random_architecture_thoughts/feed.xml"
      rel="self"
      type="application/rss+xml" />
    <link>https://docs.microsoft.com/archive/blogs/bursts_of_random_architecture_thoughts/feed.xml</link>
    <description />
    <lastBuildDate>Wed, 17 Apr 2019 03:20:49 GMT</lastBuildDate>
    <language>en-US</language>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
    <item>
      <title>Weekend Fun 03: Retryable Task - js edition</title>
      <link>https://docs.microsoft.com/archive/blogs/bursts_of_random_architecture_thoughts/weekend-fun-03-retryable-task-js-edition</link>
      <pubDate>Sun, 15 Mar 2015 16:13:00 GMT</pubDate>
      <dc:creator><![CDATA[handak_roadrunner@hotmail.com]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/bursts_of_random_architecture_thoughts/2015/03/15/weekend-fun-03-retryable-task-js-edition/</guid>
      <description><![CDATA[for the fun of it let us re-do .NET RetryableTask -...]]></description>
      <content:encoded><![CDATA[<p>for the fun of it let us re-do .NET RetryableTask - <a href="https://blogs.msdn.com/b/bursts_of_random_architecture_thoughts/archive/2015/03/14/weekend-fun-02-retryable-tasks.aspx">https://blogs.msdn.com/b/bursts_of_random_architecture_thoughts/archive/2015/03/14/weekend-fun-02-retryable-tasks.aspx</a>&nbsp;- &nbsp;as JavaScript (for nodejs or modify it for your favorite JS platform). I admit this one is not "for fun" tomorrow I need to modify a nodejs app - will run on raspberry PI - which I have been working on. The app, upon starting,&nbsp;connects to&nbsp;a REST API to get some configuration (example: which event hub I am will connect to etc..). After which I can start listening to telemetry events and send them to Azure Event Hub. If the REST API is experiencing a transient error at the exact moment of my app starting-up the entire app&nbsp;will fail and device becomes a dud until a restart. so I needed&nbsp;to do this one</p>
<p>Here is how it will look like. create a task setting `</p>
<ol>
<li>How many times we want to retry`</li>
<li>How long should we wait between a retry and another.</li>
<li>A&nbsp;function to filter errors. so&nbsp;in my case, server errors are ok to retry against but authentication errors are not. for filtering errors in JS refer to <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch">https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch</a></li>
<li>A function to call if we failed (after trying the given number of times).</li>
<li>A&nbsp;function to call if we succeed (example: in my case this is where I will start listening to events).</li>
<li>The function I want to try executing.</li>
</ol><!-- HTML generated using hilite.me -->
<div style="background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;">
<table>
<tbody>
<tr>
<td>
<pre style="margin: 0; line-height: 125%;"> 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26</pre>
</td>
<td>
<pre style="margin: 0; line-height: 125%;"><span style="color: #008800; font-weight: bold;">var</span> rtmodule <span style="color: #333333;">=</span> require(<span style="background-color: #fff0f0;">'./RetryableTask.js'</span>);

<span style="color: #008800; font-weight: bold;">var</span> totalRetry <span style="color: #333333;">=</span> <span style="color: #0000dd; font-weight: bold;">3</span>;
<span style="color: #008800; font-weight: bold;">var</span> retryIntervalSec <span style="color: #333333;">=</span> <span style="color: #0000dd; font-weight: bold;">1</span>;

<span style="color: #008800; font-weight: bold;">var</span> rt <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">new</span> rtmodule.retrybleTask(totalRetry, retryIntervalSec);
<span style="color: #888888;">// or</span>
<span style="color: #888888;">//var rt = rtmodule.retrybleTask(3, 1);</span>


rt.on(<span style="background-color: #fff0f0;">"failif"</span>, <span style="color: #008800; font-weight: bold;">function</span> (err) {
    console.log(<span style="background-color: #fff0f0;">"failif failed with err: "</span> <span style="color: #333333;">+</span> err);
    <span style="color: #008800; font-weight: bold;">return</span> <span style="color: #008800; font-weight: bold;">false</span>;
}).on(<span style="background-color: #fff0f0;">"finalfail"</span> , <span style="color: #008800; font-weight: bold;">function</span> (err, trycount) {
    console.log(<span style="background-color: #fff0f0;">"final fail: "</span> <span style="color: #333333;">+</span> err <span style="color: #333333;">+</span> <span style="background-color: #fff0f0;">" "</span> <span style="color: #333333;">+</span> trycount);
}).on(<span style="background-color: #fff0f0;">"done"</span>, <span style="color: #008800; font-weight: bold;">function</span> (TriedCound) {
    console.log(<span style="background-color: #fff0f0;">"sucess! try cound is:"</span> <span style="color: #333333;">+</span> TriedCound);
}).on(<span style="background-color: #fff0f0;">"execute"</span>, <span style="color: #008800; font-weight: bold;">function</span> () {
    <span style="color: #008800; font-weight: bold;">var</span> d <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">new</span> <span style="color: #007020;">Date</span>();

    <span style="color: #008800; font-weight: bold;">if</span> (d.getSeconds() <span style="color: #333333;">%</span> <span style="color: #0000dd; font-weight: bold;">2</span> <span style="color: #333333;">==</span> <span style="color: #0000dd; font-weight: bold;">0</span>)
        <span style="color: #008800; font-weight: bold;">throw</span> <span style="background-color: #fff0f0;">"RANDOM ERROR"</span>;
});

console.log(<span style="background-color: #fff0f0;">"about to run"</span>);
rt.run(); <span style="color: #888888;">// let there be light!</span>
</pre>
</td>
</tr>
</tbody>
</table>
</div>
<p>&nbsp;</p>
<p><em><span style="text-decoration: underline;"><strong>the module code is below, enjoy!</strong></span></em></p>
<!-- HTML generated using hilite.me -->
<div style="background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;">
<table>
<tbody>
<tr>
<td>
<pre style="margin: 0; line-height: 125%;">  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104</pre>
</td>
<td>
<pre style="margin: 0; line-height: 125%;">(
<span style="color: #008800; font-weight: bold;">function</span> () {
    <span style="color: #008800; font-weight: bold;">function</span> _retrybleTask(retryCount, delaySeconds) {
        
        <span style="color: #008800; font-weight: bold;">if</span> (<span style="color: #333333;">!</span>(<span style="color: #008800; font-weight: bold;">this</span> <span style="color: #008800; font-weight: bold;">instanceof</span> _retrybleTask)) { 
            <span style="color: #008800; font-weight: bold;">return</span> <span style="color: #008800; font-weight: bold;">new</span> _retrybleTask(retryCount, delaySeconds);
        }

        <span style="color: #008800; font-weight: bold;">this</span>._retryCount <span style="color: #333333;">=</span> retryCount <span style="color: #333333;">|</span> <span style="color: #0000dd; font-weight: bold;">1</span>;
        <span style="color: #008800; font-weight: bold;">this</span>._currentRetry <span style="color: #333333;">=</span> <span style="color: #0000dd; font-weight: bold;">1</span>;
        <span style="color: #008800; font-weight: bold;">this</span>._delaySeconds <span style="color: #333333;">=</span> delaySeconds <span style="color: #333333;">|</span> <span style="color: #0000dd; font-weight: bold;">1</span>;
        
        <span style="color: #888888;">// this is what the task will execute</span>
        <span style="color: #008800; font-weight: bold;">this</span>._execute <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">function</span> () { 
            <span style="color: #008800; font-weight: bold;">throw</span> <span style="background-color: #fff0f0;">"this retryable is empty! with nothing to execute"</span>;
        };
        
        <span style="color: #888888;">// our fail filter</span>
        <span style="color: #008800; font-weight: bold;">this</span>._failIf <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">function</span> (err) {
            <span style="color: #008800; font-weight: bold;">return</span> <span style="color: #008800; font-weight: bold;">true</span> <span style="color: #888888;">// default we fail with every error</span>
        };
        
        <span style="color: #888888;">// if we successed, ultimatly</span>
        <span style="color: #008800; font-weight: bold;">this</span>._done <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">function</span> (tryCount) { 
           <span style="color: #888888;">// this what we will execute if we managed to successfully execute </span>
        };
        
        <span style="color: #888888;">// if we failed, ultimatly</span>
        <span style="color: #008800; font-weight: bold;">this</span>._finalfail <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">function</span> ( err, tryCount) {
            console.log(<span style="background-color: #fff0f0;">"Retryable Task Failed: trycount{"</span> <span style="color: #333333;">+</span> tryCount <span style="color: #333333;">+</span> <span style="background-color: #fff0f0;">"}"</span> <span style="color: #333333;">+</span> <span style="background-color: #fff0f0;">" err was {"</span> <span style="color: #333333;">+</span> err <span style="color: #333333;">+</span> <span style="background-color: #fff0f0;">"}"</span>); 
        };
        <span style="color: #008800; font-weight: bold;">return</span> <span style="color: #008800; font-weight: bold;">this</span>;
    }

    _retrybleTask.prototype.on <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">function</span>(action, callback)
    {
        
        <span style="color: #008800; font-weight: bold;">var</span> events <span style="color: #333333;">=</span> [<span style="background-color: #fff0f0;">"failif"</span>, <span style="background-color: #fff0f0;">"done"</span>, <span style="background-color: #fff0f0;">"execute"</span>, <span style="background-color: #fff0f0;">"finalfail"</span>];
        

        <span style="color: #008800; font-weight: bold;">if</span> (<span style="color: #008800; font-weight: bold;">typeof</span> callback <span style="color: #333333;">!=</span> <span style="background-color: #fff0f0;">"function"</span>)
            <span style="color: #008800; font-weight: bold;">throw</span> <span style="background-color: #fff0f0;">"callback is not a function"</span>;
        
        <span style="color: #008800; font-weight: bold;">if</span> (<span style="color: #333333;">-</span><span style="color: #0000dd; font-weight: bold;">1</span> <span style="color: #333333;">==</span> events.indexOf(action))
            <span style="color: #008800; font-weight: bold;">throw</span> <span style="background-color: #fff0f0;">"unsupported action"</span>;

        <span style="color: #008800; font-weight: bold;">if</span> (<span style="background-color: #fff0f0;">"failif"</span> <span style="color: #333333;">===</span> action) { 
            <span style="color: #008800; font-weight: bold;">this</span>._failIf <span style="color: #333333;">=</span> callback;
            <span style="color: #008800; font-weight: bold;">return</span> <span style="color: #008800; font-weight: bold;">this</span>;
        }

        <span style="color: #008800; font-weight: bold;">if</span> (<span style="background-color: #fff0f0;">"done"</span> <span style="color: #333333;">===</span> action) { 
            <span style="color: #008800; font-weight: bold;">this</span>._done <span style="color: #333333;">=</span> callback;
            <span style="color: #008800; font-weight: bold;">return</span> <span style="color: #008800; font-weight: bold;">this</span>;
        }

        <span style="color: #008800; font-weight: bold;">if</span> (<span style="background-color: #fff0f0;">"execute"</span> <span style="color: #333333;">===</span> action) {
            <span style="color: #008800; font-weight: bold;">this</span>._execute <span style="color: #333333;">=</span> callback;
            <span style="color: #008800; font-weight: bold;">return</span> <span style="color: #008800; font-weight: bold;">this</span>;
        }
        <span style="color: #008800; font-weight: bold;">if</span> (<span style="background-color: #fff0f0;">"finalfail"</span> <span style="color: #333333;">===</span> action) {
            <span style="color: #008800; font-weight: bold;">this</span>._finalfail <span style="color: #333333;">=</span> callback;
            <span style="color: #008800; font-weight: bold;">return</span> <span style="color: #008800; font-weight: bold;">this</span>;
        }
        
    }
    _retrybleTask.prototype.run <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">function</span> () {
        <span style="color: #008800; font-weight: bold;">var</span> self <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">this</span>;
        <span style="color: #008800; font-weight: bold;">var</span> bSucess <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">false</span>;
        <span style="color: #008800; font-weight: bold;">try</span> {
            
            <span style="color: #008800; font-weight: bold;">this</span>._execute();
            bSucess <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">true</span>;
        }
        <span style="color: #008800; font-weight: bold;">catch</span> (err) {
            <span style="color: #008800; font-weight: bold;">if</span> (<span style="color: #008800; font-weight: bold;">this</span>._failIf(err)) {
                <span style="color: #008800; font-weight: bold;">this</span>._finalfail(<span style="color: #008800; font-weight: bold;">this</span>._currentRetry, err);
                <span style="color: #008800; font-weight: bold;">return</span>;
            }
            <span style="color: #008800; font-weight: bold;">if</span> (<span style="color: #008800; font-weight: bold;">this</span>._currentRetry <span style="color: #333333;">===</span> <span style="color: #008800; font-weight: bold;">this</span>._retryCount) { 

                <span style="color: #008800; font-weight: bold;">this</span>._finalfail( err, <span style="color: #008800; font-weight: bold;">this</span>._currentRetry);
                <span style="color: #008800; font-weight: bold;">return</span>;
            }
            <span style="color: #888888;">// set next execute cycle</span>
            <span style="color: #008800; font-weight: bold;">this</span>._currentRetry<span style="color: #333333;">++</span>;
            setTimeout(<span style="color: #008800; font-weight: bold;">function</span> () { self.run() }, <span style="color: #008800; font-weight: bold;">this</span>._delaySeconds <span style="color: #333333;">*</span> <span style="color: #0000dd; font-weight: bold;">1000</span>)    
        }
        <span style="color: #008800; font-weight: bold;">finally</span> {
            <span style="color: #008800; font-weight: bold;">if</span> (bSucess) {
                <span style="color: #008800; font-weight: bold;">this</span>._done(<span style="color: #008800; font-weight: bold;">this</span>._currentRetry );
            }
            
        }

       
    }
    


    module.exports.retrybleTask <span style="color: #333333;">=</span> _retrybleTask; 
    
}
)();
</pre>
</td>
</tr>
</tbody>
</table>
</div>]]></content:encoded>
    </item>
    <item>
      <title>Weekend Fun 02: Retryable Tasks</title>
      <link>https://docs.microsoft.com/archive/blogs/bursts_of_random_architecture_thoughts/weekend-fun-02-retryable-tasks</link>
      <pubDate>Sat, 14 Mar 2015 19:37:09 GMT</pubDate>
      <dc:creator><![CDATA[handak_roadrunner@hotmail.com]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/bursts_of_random_architecture_thoughts/2015/03/14/weekend-fun-02-retryable-tasks/</guid>
      <description><![CDATA[For the fun of it. Let us assume you need a retry logic. Similar to the one that can handle cloud...]]></description>
      <content:encoded><![CDATA[<p>For the fun of it. Let us assume you need a retry logic. Similar to the one that can handle cloud transient failures (more here: <a href="https://www.asp.net/aspnet/overview/developing-apps-with-windows-azure/building-real-world-cloud-apps-with-windows-azure/transient-fault-handling"><span style="color: #0563c1;">https://www.asp.net/aspnet/overview/developing-apps-with-windows-azure/building-real-world-cloud-apps-with-windows-azure/transient-fault-handling</span></a>) but what of more general. Logic can be briefly described</p>
<p>&ldquo;</p>
<p><strong><em>Try N number of times to do X, if at any point you encountered exception A or B or C fail and don&rsquo;t retry</em></strong></p>
<p>&ldquo;</p>
<p>On a side note: I needed the below as a JS function for a something I am doing based on nodejs but turned out to be harder than expected due to the way lexical scope is transferred from one call back to another.</p>
<p>Ultimately speaking you need it to look like that:</p>
<!-- HTML generated using hilite.me -->
<div style="background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;">
<pre style="margin: 0; line-height: 125%;">     RetryableTask&lt;<span style="color: #333399; font-weight: bold;">int</span>&gt; rt = <span style="color: #008800; font-weight: bold;">new</span> RetryableTask&lt;<span style="color: #333399; font-weight: bold;">int</span>&gt;(); <span style="color: #888888;">// this task will return an int when it finally successed</span>
            rt.RertyCount = <span style="color: #6600ee; font-weight: bold;">1000</span>; <span style="color: #888888;">// try for a 1000 time</span>
            rt.FailIF = (exp) =&gt; <span style="color: #888888;">// Stop if you encounter one of these exceptions</span>
            {
                <span style="color: #888888;">// handle and filter error here</span>
                <span style="color: #008800; font-weight: bold;">return</span> <span style="color: #008800; font-weight: bold;">false</span>;

            };
            rt.FuncToExecute = <span style="color: #008800; font-weight: bold;">async</span> (cts) =&gt; <span style="color: #888888;">// this is what we will execute </span>
            {
                Debug.WriteLine(<span style="background-color: #fff0f0;">"FuncToExecute - Thread:"</span> + System.Threading.Thread.CurrentThread.ManagedThreadId);
                <span style="color: #008800; font-weight: bold;">return</span> <span style="color: #6600ee; font-weight: bold;">0</span>;
            };</pre>
<pre style="margin: 0; line-height: 125%;">&nbsp;</pre>
</div>
<p>&nbsp;&nbsp;&nbsp;&nbsp;Then we are ready to execute our task</p>
<!-- HTML generated using hilite.me -->
<div style="background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;">
<pre style="margin: 0; line-height: 125%;"><span style="color: #333399; font-weight: bold;">var</span> nRes = rt.Execute();
		<span style="color: #888888;">// do a lot of things here </span>
		
		await nRes <span style="color: #888888;">// this will return if when we get results </span>
 
<span style="color: #888888;">// if the task failed (i.e. encountered an irrecoverable failure we will get one of the following exceptions </span>
<span style="color: #888888;">// TaskExecutionException as in we tried 1000s times and no results</span>
<span style="color: #888888;">// any other exception as thrown (one that FailIf returned true on).</span>
</pre>
</div>
<p>&nbsp;</p>
<p>the class also supports cancelation by calling rt.Cancel() and support&nbsp;chain-ing the task&nbsp;by setting the cancelation token source field before calling execute.</p>
<p><strong>The complete code is below</strong></p>
<p>&nbsp;</p>
<!-- HTML generated using hilite.me -->
<div style="background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;">
<pre style="margin: 0; line-height: 125%;">    <span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">RetryableTask</span>&lt;T&gt;
    {
        <span style="color: #008800; font-weight: bold;">public</span> Func&lt;CancellationTokenSource, Task&lt;T&gt;&gt; FuncToExecute = <span style="color: #008800; font-weight: bold;">null</span>;
        <span style="color: #008800; font-weight: bold;">public</span> Func&lt;Exception, <span style="color: #333399; font-weight: bold;">bool</span>&gt; FailIF = <span style="color: #008800; font-weight: bold;">new</span> Func&lt;Exception, <span style="color: #333399; font-weight: bold;">bool</span>&gt;((e) =&gt; { <span style="color: #008800; font-weight: bold;">return</span> <span style="color: #008800; font-weight: bold;">false</span>; }); <span style="color: #888888;">// basically always retry</span>
        <span style="color: #008800; font-weight: bold;">public</span> CancellationTokenSource cts = <span style="color: #008800; font-weight: bold;">new</span> CancellationTokenSource();
        <span style="color: #008800; font-weight: bold;">public</span> <span style="color: #333399; font-weight: bold;">int</span> RertyCount = <span style="color: #6600ee; font-weight: bold;">1</span>;
        <span style="color: #008800; font-weight: bold;">public</span> <span style="color: #333399; font-weight: bold;">int</span> DelayMiliSec = <span style="color: #6600ee; font-weight: bold;">0</span>;
     


        <span style="color: #008800; font-weight: bold;">protected</span> <span style="color: #008800; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">Validate</span>()
        {
           <span style="color: #008800; font-weight: bold;">if</span> (<span style="color: #008800; font-weight: bold;">null</span> == <span style="color: #008800; font-weight: bold;">this</span>.FuncToExecute)
                <span style="color: #008800; font-weight: bold;">throw</span> <span style="color: #008800; font-weight: bold;">new</span> <span style="color: #0066bb; font-weight: bold;">ArgumentNullException</span>(<span style="background-color: #fff0f0;">"FuncToExecute"</span>);

            <span style="color: #008800; font-weight: bold;">if</span> (<span style="color: #008800; font-weight: bold;">null</span> == <span style="color: #008800; font-weight: bold;">this</span>.cts)
                <span style="color: #008800; font-weight: bold;">throw</span> <span style="color: #008800; font-weight: bold;">new</span> <span style="color: #0066bb; font-weight: bold;">ArgumentNullException</span>(<span style="background-color: #fff0f0;">"cts"</span>);

            <span style="color: #008800; font-weight: bold;">if</span> (<span style="color: #008800; font-weight: bold;">null</span> == <span style="color: #008800; font-weight: bold;">this</span>.FailIF)
                <span style="color: #008800; font-weight: bold;">throw</span> <span style="color: #008800; font-weight: bold;">new</span> <span style="color: #0066bb; font-weight: bold;">ArgumentNullException</span>(<span style="background-color: #fff0f0;">"FailIF"</span>);
        }

        
        <span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">async</span> Task&lt;T&gt; Execute()
        {

            Validate();
            Debug.WriteLine(<span style="background-color: #fff0f0;">"Do/While - Thread:"</span> + System.Threading.Thread.CurrentThread.ManagedThreadId);


            <span style="color: #333399; font-weight: bold;">int</span> nCurrent = <span style="color: #6600ee; font-weight: bold;">1</span>;
            <span style="color: #008800; font-weight: bold;">do</span>
            {

                <span style="color: #008800; font-weight: bold;">if</span> (cts.IsCancellationRequested)
                {
                    Debug.WriteLine(<span style="background-color: #fff0f0;">"Task canceled.. breaking"</span>);
                    <span style="color: #008800; font-weight: bold;">break</span>;
                }

                <span style="color: #008800; font-weight: bold;">try</span>
                {
                    <span style="color: #008800; font-weight: bold;">return</span> <span style="color: #008800; font-weight: bold;">await</span> <span style="color: #0066bb; font-weight: bold;">FuncToExecute</span>(cts);
                }
                <span style="color: #008800; font-weight: bold;">catch</span> (Exception e)
                {
                    <span style="color: #008800; font-weight: bold;">if</span> (<span style="color: #008800; font-weight: bold;">true</span> == <span style="color: #008800; font-weight: bold;">this</span>.FailIF(e)) <span style="color: #888888;">// fail if delegate should handle and log the error as needed</span>
                        <span style="color: #008800; font-weight: bold;">throw</span>;
                }

                nCurrent++;


                <span style="color: #888888;">// you don't want to wait unncessary</span>
                <span style="color: #008800; font-weight: bold;">if</span> (cts.IsCancellationRequested)
                {
                    Debug.WriteLine(<span style="background-color: #fff0f0;">"Task canceled.. breaking"</span>);
                    <span style="color: #008800; font-weight: bold;">break</span>;
                }

                <span style="color: #008800; font-weight: bold;">if</span> (<span style="color: #008800; font-weight: bold;">this</span>.DelayMiliSec &gt; <span style="color: #6600ee; font-weight: bold;">0</span>)
                    <span style="color: #008800; font-weight: bold;">await</span> Task.Delay(<span style="color: #008800; font-weight: bold;">this</span>.DelayMiliSec, cts.Token);

            } <span style="color: #008800; font-weight: bold;">while</span> (nCurrent &lt;= RertyCount);

           <span style="color: #888888;">// if we are here then task failed to execute given the # of retries</span>
            <span style="color: #008800; font-weight: bold;">throw</span> <span style="color: #008800; font-weight: bold;">new</span> <span style="color: #0066bb; font-weight: bold;">TaskExecutionFailedException</span>(nCurrent);


        }
        <span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">async</span> Task <span style="color: #0066bb; font-weight: bold;">Cancel</span>()
        {
              <span style="color: #008800; font-weight: bold;">if</span> (<span style="color: #008800; font-weight: bold;">null</span> == <span style="color: #008800; font-weight: bold;">this</span>.cts)
                <span style="color: #008800; font-weight: bold;">throw</span> <span style="color: #008800; font-weight: bold;">new</span> <span style="color: #0066bb; font-weight: bold;">ArgumentNullException</span>(<span style="background-color: #fff0f0;">"cts"</span>);

              <span style="color: #008800; font-weight: bold;">this</span>.cts.Cancel();
              <span style="color: #008800; font-weight: bold;">await</span> Task.FromResult(<span style="color: #6600ee; font-weight: bold;">0</span>);
             
        }
    }
</pre>
</div>
<p>&nbsp;</p>
<p><span>The complete code to run the above in a console app is below:</span><span><br /></span></p>
<!-- HTML generated using hilite.me -->
<div style="background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;">
<pre style="margin: 0; line-height: 125%;">RetryableTask&lt;<span style="color: #333399; font-weight: bold;">int</span>&gt; rt = <span style="color: #008800; font-weight: bold;">new</span> RetryableTask&lt;<span style="color: #333399; font-weight: bold;">int</span>&gt;(); <span style="color: #888888;">// this task will return an int when it finally successed</span>
            rt.RertyCount = <span style="color: #6600ee; font-weight: bold;">1000</span>; <span style="color: #888888;">// try for a 1000 time</span>
            rt.FailIF = (exp) =&gt; <span style="color: #888888;">// Stop if you incounter these specic exceptions</span>
            {
                <span style="color: #888888;">// handle and filter error here</span>
                <span style="color: #008800; font-weight: bold;">return</span> <span style="color: #008800; font-weight: bold;">false</span>;

            };
            rt.FuncToExecute = <span style="color: #008800; font-weight: bold;">async</span> (cts) =&gt; <span style="color: #888888;">// this is what we will execute </span>
            {
                Debug.WriteLine(<span style="background-color: #fff0f0;">"FuncToExecute - Thread:"</span> + System.Threading.Thread.CurrentThread.ManagedThreadId);
                <span style="color: #008800; font-weight: bold;">return</span> <span style="color: #6600ee; font-weight: bold;">0</span>;
            };

            <span style="color: #333399; font-weight: bold;">int</span> nResults;
            Task t = Task.Run(<span style="color: #008800; font-weight: bold;">async</span> () =&gt; { nResults = <span style="color: #008800; font-weight: bold;">await</span> rt.Execute(); });

            Task tCancel = Task.Run(<span style="color: #008800; font-weight: bold;">async</span> () =&gt;
            {
                <span style="color: #008800; font-weight: bold;">await</span> Task.Delay(<span style="color: #6600ee; font-weight: bold;">10000</span>);
                Debug.WriteLine(<span style="background-color: #fff0f0;">"About to Cancel"</span>);
                <span style="color: #008800; font-weight: bold;">await</span> rt.Cancel();
            });

            Console.WriteLine(<span style="background-color: #fff0f0;">"running"</span>);
            t.Wait();




            Console.WriteLine(<span style="background-color: #fff0f0;">"Done!"</span>);

            Console.Read();
</pre>
</div>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>Have a good weekend!</p>]]></content:encoded>
    </item>
    <item>
      <title>SaaSBits 02 - Authentication</title>
      <link>https://docs.microsoft.com/archive/blogs/bursts_of_random_architecture_thoughts/saasbits-02-authentication</link>
      <pubDate>Sun, 01 Mar 2015 20:02:49 GMT</pubDate>
      <dc:creator><![CDATA[handak_roadrunner@hotmail.com]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/bursts_of_random_architecture_thoughts/2015/03/01/saasbits-02-authentication/</guid>
      <description><![CDATA[Continuing on Building SaaS solution journey. The past articles are here:


Part 1:...]]></description>
      <content:encoded><![CDATA[<div>
<p><span style="font-family: Calibri; font-size: small;">Continuing on Building SaaS solution journey. The past articles are here:</span></p>
<ol>
<li>
<p>Part 1: <a href="https://blogs.msdn.com/b/bursts_of_random_architecture_thoughts/archive/2015/02/19/saas-bits-01.aspx"><span style="color: #0563c1;">https://blogs.msdn.com/b/bursts_of_random_architecture_thoughts/archive/2015/02/19/saas-bits-01.aspx</span></a></p>
</li>
</ol>
<p><span style="font-family: Calibri; font-size: small;">We have layered the foundation architecture where we decided </span></p>
<ol>
<li>
<p>All requests will be directed to a gateway (reverse proxy, based on IIS ARR). Previous <a href="https://github.com/khenidak/SaaSBits"><span style="color: #0563c1;">code</span></a> (<a href="https://github.com/khenidak/SaaSBits"><span style="color: #0563c1;">https://github.com/khenidak/SaaSBits</span></a> &nbsp;check 2015-2-19 directory) redirect to one of 2 URLs statically defined as didn&rsquo;t build the scale part yet.</p>
</li>
<li>
<p>Based on user information (and dynamic information managed by the capacity and scale component <strong><em>CSC</em></strong>) the request will be routed.</p>
</li>
<li>
<p>The gateway/router routs to a &ldquo;pod&rdquo;. The number and the addresses of pods is managed by the CSC.</p>
</li>
</ol>
<p><span style="font-family: Calibri; font-size: small;">Based on our requirements request is redirected to a certain pod based on:</span></p>
<ol>
<li>
<p>Tenant (request belongs to a premium tenant which is served by a dedicated pod)</p>
</li>
<li>
<p>If not, then the tenant decided by the CSC which can be scaling or compacting (un-provisioning pods) the system.</p>
</li>
</ol>
<p><strong><em><span style="text-decoration: underline;"><span style="font-family: Calibri; font-size: small;">A bit of side info on the need for scale/compacting approach: </span></span></em></strong></p>
<p><span style="font-family: Calibri; font-size: small;">If you keep the cost of building/upgrading your SaaS software aside you will notice that the majority of cost is running/operating cost (i.e the number of active VMs, roles and other resources). Hence the need to keep those as minimum as possible (<em>less - paid for - compute + storage resources serving the same amount of users means more financial yields of the system</em>). </span></p>
<p><span style="font-family: Calibri; font-size: small;">So where should the gateway look for user&rsquo;s tenant (per request? The obvious answer is authentication claim (from the AuthN system). <strong><em>As in the Gateway will have to broker the AuthN for each request before it routes it to a pod</em></strong>. The rest of authentication requirements (aside from the obvious ones) are:</span></p>
<ol>
<li>
<p><strong>Federate</strong>: allow SaaS customers to use their identity providers.</p>
</li>
<li>
<p><strong>Standardize</strong>: no funky AuthN or proprietary mechanisms that doesn&rsquo;t work with your customers.</p>
</li>
<li>
<p><strong>Transparency</strong>: you don&rsquo;t want to change the actual authentication tokens. This to allow SaaS application developers to make use &ndash; <strong><em>AS IS</em></strong> without the need to change - of the fun stuff like ADAL, WIF or OWIN AuthN middleware. With scenarios like impersonation/on-behalf-of enabled.</p>
</li>
</ol>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/50/19/8737.1.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-19/8737.1.png"><img style="width: 706px; height: 392px;" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/50/19/8737.1.png" original-url="https://blogs.msdn.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-19/8737.1.png" alt="" width="551" height="324" border="0" /></a></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p><span style="font-family: Calibri; font-size: small;">In context:</span></p>
<ul>
<li>
<p>A windows Azure A/D WAAD tenant (core tenant) will be created where</p>
<ul>
<li>
<p>SaaS apps (i.e hr.cloudapp.net&gt; or erp.cloudapp.net) will be published as a multi-tenant application (follow these <a href="https://msdn.microsoft.com/en-us/library/azure/dn132599.aspx#BKMK_MultiT"><span style="color: #0563c1;">instructions</span></a> for how, you will need a verified domain on WAAD for multitenant applications). With its redirect URL points to the gateway (more on this later).</p>
</li>
<li>
<p>SaaS gateway/router will be also published to this tenant but not as a multi-tenant. It will carry the URLs (via cname records) the URLs which you will share with your customers (ie hr.&lt;yourcompany&gt;.com erp.&lt;your company&gt;.com).</p>
</li>
</ul>
</li>
<li>
<p>Each of our customers will have a WAAD tenant as the following:</p>
<ul>
<li>
<p><strong>Option1</strong>: customer&rsquo;s self-operated tenant is federated with ADFS (or similar) which authenticates based on an on-prem A/D.</p>
</li>
<li>
<p><strong>Option 2</strong>: customer&rsquo;s self-operated tenant is federated with ADFS which authenticates using a custom user store.</p>
</li>
<li>
<p><strong>Option 3</strong>: customer&rsquo;s self-operated tenanted is synchronized with an on-prem A/D.</p>
</li>
<li>
<p><strong>Option 4</strong>: for customers who don&rsquo;t have a WAAD we can create and operate a tenant for each (this option is also good if we already have an on-prem software and we are converting it to cloud based SaaS). &nbsp;</p>
</li>
</ul>
</li>
</ul>
<p><span style="font-family: Calibri; font-size: small;">WAAD will be providing a single token format for our SaaS applications as the following:</span></p>
<ol>
<li>
<p>oAuth2 JWT for web API apps</p>
</li>
<li>
<p>OpenID-Connect for Web Applications</p>
</li>
</ol>
<p><span style="font-family: Calibri; font-size: small;">&nbsp;</span></p>
<p><span style="font-family: Calibri; font-size: small;">The above pretty much covers most of the authentication and token formats currently available in the industry. </span></p>
<p><span style="font-family: Calibri; font-size: small;">&nbsp;</span></p>
<p><span style="font-family: Calibri; font-size: small;">The authentication for the entire system code will work as the following </span></p>
<p>&nbsp;</p>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/50/19/1440.2.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-19/1440.2.png"><img src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/50/19/1440.2.png" original-url="https://blogs.msdn.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-19/1440.2.png" alt="" border="0" /></a></p>
<p>&nbsp;</p>
<p><span style="font-family: Calibri; font-size: small;">The gateway is an IIS application that has the following:</span></p>
<ol>
<li>
<p>Custom HTTP module:</p>
<ol>
<li>
<p>Identify if the target request is for a web app or web API</p>
<ol>
<li>
<p>For Web App: Checks the OpenID-Connect authentication token (manaually as a multitenant app) extracting the TenantID and UPN into HTTP header.</p>
</li>
<li>
<p>For Web API: checks oAuth bearer token (this code is not implemented yet).</p>
</li>
</ol></li>
<li>
<p>Appends a custom oAuth token to a custom header (it authenticates itself as a daemon check: <a href="https://msdn.microsoft.com/en-us/library/azure/dn646737.aspx#BKMK_Daemon"><span style="color: #0563c1;">https://msdn.microsoft.com/en-us/library/azure/dn646737.aspx#BKMK_Daemon</span></a>).</p>
</li>
</ol></li>
<li>
<p>The IIS ARR components uses the tenant Id and user principal to decide on routing (currently static routing, dynamic routing is not implemented yet).</p>
</li>
<li>
<p>&nbsp;The backend application will be configured as usual in addition to a preceding component (executes before regular AuthN code) to validate the custom gateway oAuth (from the custom header). This ensure that these applications will only be called from the gateway.</p>
</li>
</ol>
<p><span style="font-family: Calibri; font-size: small;">&nbsp;</span></p>
<p><span style="font-family: Calibri; font-size: small;">What is in the Code: </span></p>
<ol>
<li>
<p>The entire code is abstracting IIS functionality. The code is future ready for ASP.NET 6 where System.Web will not be used. Also the abstraction allows you to run the code away from IIS if needed.</p>
</li>
<li>
<p>Note HTTPs not implement yet. So all tokens used in testing is shared on unencrypted channel.</p>
</li>
<li>
<p>An azure web site sample application configured with default OWIN OpenID-Connect and a custom empty OWIN component that will be used as gateway authenticator.</p>
</li>
<li>
<p>A web API application (as azure web site).</p>
</li>
<li>
<p>A custom deployment replicator PowerShell script that deploys the azure web sites multiple times to simulate pods.</p>
</li>
</ol>
<p><span style="font-family: Calibri; font-size: small;">&nbsp;</span></p>
<p><span style="font-family: Calibri; font-size: small;">Setup:</span></p>
<ol>
<li>
<p>Deploy the gateway as Web Role (already configured, just update with your key). The package already performs all the installation needed via startup tasks.</p>
</li>
<li>
<p>Create WAAD tenant (core tenant) with a verified domain ie saasdomain.com</p>
</li>
<li>
<p>Publish gateway application to your tenant as a regular web app/web API.</p>
</li>
<li>
<p>Publish gateway application as a client in your core tenant. (the oAuth bearer token is based on the gateway as an application and a client). &nbsp;</p>
</li>
<li>
<p>Publish a sample SAAS backend app ie hr.saasdomain.com as a multitenant web app/web api.</p>
</li>
<li>
<p>On your DNS (or RDP into the gateway web role and modify hosts file). Create a CNAME of hr.saasdomain.com pointing to the gateway ur lie gateway.cloudapp.net.</p>
</li>
<li>
<p>Create another WAAD tenant (as a customer tanant) add a test user to it.</p>
</li>
<li>
<p>Open your favorite browser browse to hr.saasdomain.com sign in to WAAD you will be redirected to the gateway (where the above process executes).</p>
</li>
<li>
<p>Review the entities statically loaded in GatewayApp class in SaaS.Gateway.Common assembly</p>
</li>
<li>
<p>Publish sample backend applications (not currently needed).</p>
</li>
</ol>
<p><span style="font-family: Calibri; font-size: small;">&nbsp;</span></p>
<p><span style="font-family: Calibri; font-size: small;">&nbsp;</span></p>
<p><span style="font-family: Calibri; font-size: small;">Code has been published here: </span></p>
<p><a href="https://github.com/khenidak/SaaSBits/tree/master/2015-3-1"><span style="color: #0563c1; font-family: Calibri; font-size: small;">https://github.com/khenidak/SaaSBits/tree/master/2015-3-1</span></a></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
</div>]]></content:encoded>
    </item>
    <item>
      <title>Weekend Fun 01: Building Application Request Routing/ Reverse Proxy like components using OWIN</title>
      <link>https://docs.microsoft.com/archive/blogs/bursts_of_random_architecture_thoughts/weekend-fun-01-building-application-request-routing-reverse-proxy-like-components-using-owin</link>
      <pubDate>Sun, 01 Mar 2015 18:55:00 GMT</pubDate>
      <dc:creator><![CDATA[handak_roadrunner@hotmail.com]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/bursts_of_random_architecture_thoughts/2015/03/01/weekend-fun-01-building-application-request-routing-reverse-proxy-like-components-using-owin/</guid>
      <description><![CDATA[For the fun of it. Let us build an IIS ARR without IIS (there might be an actual requirements where...]]></description>
      <content:encoded><![CDATA[<p>For the fun of it. Let us build an IIS ARR without IIS (there might be an actual requirements where you want to run this in Azure Web Sites where you don't have access to do that, or hosting in a worker role).</p>
<p>How can we build that? if you strip out all the fatty details what we need is the following</p>
<p>A pipeline that processes the request with the following stages (in order)</p>
<p>1-HTTP Listener</p>
<p>2- Few components that perform AuthN and AuthZ</p>
<p>3- A component that looks at the incoming request and decides where this request goes say request coming in are for https://domainA.com and it should go https://domainB.com (similar to IIS URL rewrite).</p>
<p>4- A component that takes the request and executes.</p>
<p>&nbsp;</p>
<p>OWIN comes natural to this as it has the pipeline (and an HTTPlistener based on .NET frx HTTP Listener).</p>
<p>The below code is hosted in a console app:</p>
<p>&nbsp;</p>
<p>Program Main</p>
<p>&nbsp;</p>
<!-- HTML generated using hilite.me -->
<div style="background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;">
<table>
<tbody>
<tr>
<td>
<pre style="margin: 0; line-height: 125%;"> 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12</pre>
</td>
<td>
<pre style="margin: 0; line-height: 125%;">    <span style="color: #008800; font-weight: bold;">static</span> <span style="color: #008800; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">Main</span>(<span style="color: #333399; font-weight: bold;">string</span>[] args)
        {
            <span style="color: #333399; font-weight: bold;">string</span> baseUrl = <span style="background-color: #fff0f0;">"https://localhost:5001/"</span>;

            <span style="color: #008800; font-weight: bold;">using</span> (<span style="color: #333399; font-weight: bold;">var</span> server = WebApp.Start&lt;Startup&gt;(<span style="color: #008800; font-weight: bold;">new</span> StartOptions(baseUrl)))
            {
                Console.WriteLine(<span style="background-color: #fff0f0;">"Server Started... Press Enter to quit."</span>);
                Console.ReadKey();
            }


        }
</pre>
</td>
</tr>
</tbody>
</table>
</div>
<p>the above just runs the server</p>
<p>the Startup class sets up 2 components (the URL re-writer and the other is Request Router)</p>
<p>&nbsp;</p>
<!-- HTML generated using hilite.me -->
<div style="background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;">
<table>
<tbody>
<tr>
<td>
<pre style="margin: 0; line-height: 125%;">1
2
3
4
5
6
7
8</pre>
</td>
<td>
<pre style="margin: 0; line-height: 125%;"> <span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">Startup</span>
    {
        <span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">Configuration</span>(IAppBuilder app)
        {
            app.UseURLRewrite();
            app.UseRR();
        }
    }
</pre>
</td>
</tr>
</tbody>
</table>
</div>
<p>&nbsp;</p>
<p>Because this is fun project. I wanted to do more. Let us assume we want to implement 2 very simple pattern <em><span style="text-decoration: underline;"><strong>Broadcast</strong></span></em> (request goes to multiple servers) or <span style="text-decoration: underline;"><em><strong>First Responder</strong></em></span> (request goes to multiple servers returns when any responds).</p>
<p>so my URL rewrite does the following (OWIN invoke method). it randomly selects (based on current time second odd/even) modes (either broadcast or first responder) setting list of URLs and current mode in OWIN environment dictionary</p>
<!-- HTML generated using hilite.me -->
<div style="background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;">
<table>
<tbody>
<tr>
<td>
<pre style="margin: 0; line-height: 125%;"> 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39</pre>
</td>
<td>
<pre style="margin: 0; line-height: 125%;">   <span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">async</span> Task <span style="color: #0066bb; font-weight: bold;">Invoke</span>(IDictionary&lt;<span style="color: #333399; font-weight: bold;">string</span>, <span style="color: #333399; font-weight: bold;">object</span>&gt; env)
        {
            <span style="color: #333399; font-weight: bold;">var</span> response = env[<span style="background-color: #fff0f0;">"owin.ResponseBody"</span>] <span style="color: #008800; font-weight: bold;">as</span> Stream;

   
            List&lt;<span style="color: #333399; font-weight: bold;">string</span>&gt; TargetUrls = <span style="color: #008800; font-weight: bold;">new</span> List&lt;<span style="color: #333399; font-weight: bold;">string</span>&gt;();


            <span style="color: #888888;">//current code does't handle GZIP </span>
            <span style="color: #888888;">// BBC is not using it. other site will give octet/stream response time out of the proxy.</span>

            <span style="color: #008800; font-weight: bold;">if</span>(DateTime.Now.Second %<span style="color: #6600ee; font-weight: bold;">2</span> &gt; <span style="color: #6600ee; font-weight: bold;">0</span> )
            {

                <span style="color: #888888;">// scatter and gather approach</span>
                TargetUrls .Add(<span style="background-color: #fff0f0;">"https://www.bbc.com"</span>);
                TargetUrls.Add(<span style="background-color: #fff0f0;">"https://www.bbc.com"</span>);
                TargetUrls.Add(<span style="background-color: #fff0f0;">"https://www.bbc.com"</span>);
                TargetUrls.Add(<span style="background-color: #fff0f0;">"https://www.bbc.com"</span>);
                TargetUrls.Add(<span style="background-color: #fff0f0;">"https://www.bbc.com"</span>);
                TargetUrls.Add(<span style="background-color: #fff0f0;">"https://www.bbc.com"</span>);
                TargetUrls.Add(<span style="background-color: #fff0f0;">"https://www.bbc.com"</span>);
                TargetUrls.Add(<span style="background-color: #fff0f0;">"https://www.bbc.com"</span>);

                env.Add(<span style="background-color: #fff0f0;">"rr.Mode"</span>, <span style="background-color: #fff0f0;">"ALL"</span>);
                Debug.WriteLine(<span style="background-color: #fff0f0;">"routed first case"</span>);


            }
            <span style="color: #008800; font-weight: bold;">else</span>
            {
                env.Add(<span style="background-color: #fff0f0;">"rr.Mode"</span>, <span style="background-color: #fff0f0;">"ANY"</span>);
                Debug.WriteLine(<span style="background-color: #fff0f0;">"routed second case"</span>);
                TargetUrls.Add(<span style="background-color: #fff0f0;">"https://www.bbc.com"</span>);
            }

            env.Add(<span style="background-color: #fff0f0;">"rr.targetURLs"</span>, TargetUrls);
            <span style="color: #008800; font-weight: bold;">await</span> _next.Invoke(env);
        }
</pre>
</td>
</tr>
</tbody>
</table>
</div>
<p>&nbsp;</p>
<p>My Request router performs the following:</p>
<p>1- copies original request stream</p>
<p>2- prepares an HTTPRequest/HTTPClient instance for each URL.</p>
<p>3- copies original request headers</p>
<p>4- execute the request</p>
<p>5- waits for the results (either All or Any).</p>
<p>6- copies response headers back to the down steam header.</p>
<p>7- copies the response body back into the down stream body.</p>
<!-- HTML generated using hilite.me -->
<div style="background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;">
<table>
<tbody>
<tr>
<td>
<pre style="margin: 0; line-height: 125%;">  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136</pre>
</td>
<td>
<pre style="margin: 0; line-height: 125%;"> <span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">async</span> Task <span style="color: #0066bb; font-weight: bold;">Invoke</span>(IDictionary&lt;<span style="color: #333399; font-weight: bold;">string</span>, <span style="color: #333399; font-weight: bold;">object</span>&gt; env)
    {
        
        <span style="color: #333399; font-weight: bold;">var</span> Tasks = <span style="color: #008800; font-weight: bold;">new</span> List&lt;Task&lt;HttpResponseMessage&gt;&gt;();
        <span style="color: #333399; font-weight: bold;">var</span> outStream = <span style="color: #008800; font-weight: bold;">new</span> MemoryStream();
        Task tCopy = <span style="color: #008800; font-weight: bold;">null</span>;
        <span style="color: #333399; font-weight: bold;">var</span> inStream = env[<span style="background-color: #fff0f0;">"owin.RequestBody"</span>] <span style="color: #008800; font-weight: bold;">as</span> Stream;
        <span style="color: #333399; font-weight: bold;">string</span> rrmode = env[<span style="background-color: #fff0f0;">"rr.Mode"</span>] <span style="color: #008800; font-weight: bold;">as</span> <span style="color: #333399; font-weight: bold;">string</span>;
        <span style="color: #333399; font-weight: bold;">var</span> Clients = <span style="color: #008800; font-weight: bold;">new</span> List&lt;HttpClient&gt;();
        List&lt;<span style="color: #333399; font-weight: bold;">string</span>&gt; sTargetURs = env[<span style="background-color: #fff0f0;">"rr.targetURLs"</span>] <span style="color: #008800; font-weight: bold;">as</span> List&lt;<span style="color: #333399; font-weight: bold;">string</span>&gt;; 




        <span style="color: #008800; font-weight: bold;">if</span>(<span style="color: #008800; font-weight: bold;">null</span> != inStream)
            tCopy =  inStream.CopyToAsync(outStream);
        <span style="color: #008800; font-weight: bold;">else</span>
            tCopy = Task.FromResult(<span style="color: #6600ee; font-weight: bold;">0</span>);






        <span style="color: #008800; font-weight: bold;">foreach</span> (<span style="color: #333399; font-weight: bold;">var</span> s <span style="color: #008800; font-weight: bold;">in</span> sTargetURs)
        {
            <span style="color: #333399; font-weight: bold;">var</span> client = <span style="color: #008800; font-weight: bold;">new</span> System.Net.Http.HttpClient();

            
               <span style="color: #888888;">// copy headers</span>
                <span style="color: #008800; font-weight: bold;">foreach</span> (<span style="color: #333399; font-weight: bold;">var</span> header <span style="color: #008800; font-weight: bold;">in</span> env[<span style="background-color: #fff0f0;">"owin.RequestHeaders"</span>] <span style="color: #008800; font-weight: bold;">as</span> IDictionary&lt;<span style="color: #333399; font-weight: bold;">string</span>, <span style="color: #333399; font-weight: bold;">string</span>[]&gt;)
                  client.DefaultRequestHeaders.Add(header.Key, header.Value);

                <span style="color: #888888;">// copy body</span>
                   HttpRequestMessage request = <span style="color: #008800; font-weight: bold;">new</span> HttpRequestMessage();
                   <span style="color: #008800; font-weight: bold;">await</span> tCopy; <span style="color: #888888;">// finish copy</span>

                   <span style="color: #333399; font-weight: bold;">var</span> sMethod = env[<span style="background-color: #fff0f0;">"owin.RequestMethod"</span>] <span style="color: #008800; font-weight: bold;">as</span> <span style="color: #333399; font-weight: bold;">string</span>;

                    
                 <span style="color: #888888;">// set body - not all types take a body</span>
                <span style="color: #008800; font-weight: bold;">if</span>(<span style="background-color: #fff0f0;">"get"</span> != sMethod.ToLower())
                   request.Content  = <span style="color: #008800; font-weight: bold;">new</span> StreamContent(outStream);
   
                
                 <span style="color: #888888;">//set method</span>
                 request.Method = <span style="color: #008800; font-weight: bold;">new</span> HttpMethod( sMethod);

                <span style="color: #333399; font-weight: bold;">var</span> sPath = (env[<span style="background-color: #fff0f0;">"owin.RequestPath"</span>] <span style="color: #008800; font-weight: bold;">as</span> <span style="color: #333399; font-weight: bold;">string</span>)  ?? <span style="color: #333399; font-weight: bold;">string</span>.Empty;
                <span style="color: #333399; font-weight: bold;">var</span> sQueryString =   (env[<span style="background-color: #fff0f0;">"owin.RequestQueryString"</span>] <span style="color: #008800; font-weight: bold;">as</span> <span style="color: #333399; font-weight: bold;">string</span>)  ?? <span style="color: #333399; font-weight: bold;">string</span>.Empty;
            
                <span style="color: #008800; font-weight: bold;">if</span> (<span style="color: #333399; font-weight: bold;">string</span>.Empty != sQueryString)
                    sQueryString = <span style="background-color: #fff0f0;">"?"</span> + sQueryString ;

                request.RequestUri = <span style="color: #008800; font-weight: bold;">new</span> Uri(s + sPath + sQueryString);
                
                <span style="color: #888888;">// add it to our bag // you should track cancellation tokens as well. </span>
         
                Tasks.Add(client.SendAsync(request));
                Clients.Add(client);
        }

          HttpResponseMessage selectedResponse = <span style="color: #008800; font-weight: bold;">null</span>;<span style="color: #888888;">// :-(</span>
          HttpStatusCode _responseCode = HttpStatusCode.InternalServerError;
          HttpResponseHeaders _responseHeaders = <span style="color: #008800; font-weight: bold;">null</span>;
          HttpContent _responseContent = <span style="color: #008800; font-weight: bold;">null</span>;
          <span style="color: #333399; font-weight: bold;">string</span> _responseStatusMessage = <span style="background-color: #fff0f0;">"Proxy!"</span>; <span style="color: #888888;">// obviously w are not using RFC - review spec: RFC 2616 section 6.1.1</span>


        <span style="color: #888888;">// wait for response</span>
          <span style="color: #008800; font-weight: bold;">try</span>
          {


              <span style="color: #008800; font-weight: bold;">if</span> (rrmode == <span style="background-color: #fff0f0;">"ALL"</span>)
              {
                  Debug.WriteLine(<span style="color: #333399; font-weight: bold;">string</span>.Format(<span style="background-color: #fff0f0;">"waiting for - ALL - {0} requests"</span>, sTargetURs.Count));
                  <span style="color: #333399; font-weight: bold;">var</span> completedTasks = <span style="color: #008800; font-weight: bold;">await</span> Task.WhenAll&lt;HttpResponseMessage&gt;(Tasks);
                  Debug.WriteLine(<span style="color: #333399; font-weight: bold;">string</span>.Format(<span style="background-color: #fff0f0;">"{0} requests completed.."</span>, sTargetURs.Count));
                  <span style="color: #888888;">// you can do fun stuff here like compsing a response, concating it etc..</span>
                  
                  selectedResponse = completedTasks[<span style="color: #6600ee; font-weight: bold;">0</span>];


              }
              <span style="color: #008800; font-weight: bold;">else</span>
              {
                  Debug.WriteLine(<span style="color: #333399; font-weight: bold;">string</span>.Format(<span style="background-color: #fff0f0;">"waiting for - ANY - {0} requests"</span>, sTargetURs.Count));
                  
                  <span style="color: #888888;">// this could be "any" or "Single" mode</span>
                  <span style="color: #333399; font-weight: bold;">var</span> completedTask = <span style="color: #008800; font-weight: bold;">await</span> Task.WhenAny&lt;HttpResponseMessage&gt;(Tasks);
                  
                  <span style="color: #888888;">// you should keep a list of cancelation tokens. and cancel all once one returned. </span>
                  <span style="color: #888888;">// in this example all tasks will keep in running and return to nothing </span>
                  
                  
                  <span style="color: #333399; font-weight: bold;">string</span>.Format(<span style="background-color: #fff0f0;">"1 request completed.."</span>);
                  selectedResponse = <span style="color: #008800; font-weight: bold;">await</span> completedTask;
              }
              selectedResponse.EnsureSuccessStatusCode();

              _responseCode = selectedResponse.StatusCode;
              _responseHeaders = selectedResponse.Headers;
              _responseContent = selectedResponse.Content;

          }
          <span style="color: #008800; font-weight: bold;">catch</span> (AggregateException ae)
          {
                <span style="color: #888888;">// log ae</span>
              _responseStatusMessage = <span style="background-color: #fff0f0;">"error executing the request: "</span> + ae.Flatten().Message;
             
          }
          <span style="color: #008800; font-weight: bold;">catch</span> (Exception e) <span style="color: #888888;">// we are assuming since we are here we actually executed a request and we got a response</span>
          {
              <span style="color: #888888;">//log e</span>
              _responseStatusMessage = <span style="background-color: #fff0f0;">"general internal error"</span> + e.Message;
          
          }
        <span style="color: #008800; font-weight: bold;">finally</span>
          {
              <span style="color: #888888;">// release all clients</span>
              Clients.ForEach(c =&gt; c.Dispose());
          }
          


          <span style="color: #008800; font-weight: bold;">await</span> <span style="color: #0066bb; font-weight: bold;">setResponse</span>(env,
                       _responseCode,
                        _responseHeaders,
                        _responseContent,
                        _responseStatusMessage);

          <span style="color: #008800; font-weight: bold;">await</span> _next.Invoke(env);

         
        }
</pre>
</td>
</tr>
</tbody>
</table>
</div>
<p>&nbsp;</p>
<p>While this is FUN project there is a lot of applications to it. Typically with high-scale write operations are based on eventual consistency using platforms such as Event Hubs/Service Bus/CQRS or databases (name your favorite eventual consistency DB here).&nbsp; Read operations however specially those across multiple shards/systems are harder to implement. My favorite implementation for this a command broker. where the client (imagine SPA page) sends one request that this pipeline intercepts and expand into multiple sub commands each is targeting a WebAPI/System.</p>
<p>The code is attached to this past. hope you had a fun week end</p>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Components.PostAttachments/00/10/59/69/20/Fun.ARR.zip" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-components-postattachments/00-10-59-69-20/Fun.ARR.zip">Fun.ARR.zip</a></p>]]></content:encoded>
    </item>
    <item>
      <title>SaaS Bits 01</title>
      <link>https://docs.microsoft.com/archive/blogs/bursts_of_random_architecture_thoughts/saas-bits-01</link>
      <pubDate>Thu, 19 Feb 2015 10:54:33 GMT</pubDate>
      <dc:creator><![CDATA[handak_roadrunner@hotmail.com]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/bursts_of_random_architecture_thoughts/2015/02/19/saas-bits-01/</guid>
      <description><![CDATA[So you want to build a SaaS/SaaS like solution? Yes!. But what is a SaaS solution?
You can look...]]></description>
      <content:encoded><![CDATA[<div>
<p><span style="font-family: Calibri; font-size: small;">So you want to build a SaaS/SaaS like solution? Yes!. But what is a SaaS solution?</span></p>
<p><span style="font-family: Calibri; font-size: small;">You can look up the definition at </span><a href="https://en.wikipedia.org/wiki/Software_as_a_service"><span style="color: #0563c1; font-family: Calibri; font-size: small;">Wikipedia</span></a><span style="font-family: Calibri; font-size: small;">, spend hours discussing it at </span><a href="https://www.reddit.com/r/saas"><span style="color: #0563c1; font-family: Calibri; font-size: small;">Reddit</span></a><span style="font-family: Calibri;"><span style="font-size: small;"> or discuss the architecture philosophies behind at your favorite tech group(s). For the purpose of this blog entry (and those to follow) we will define it as &ldquo;<strong><em>A hosted software that scale and descale according to demand or configurable parameters&rdquo;.</em></strong></span></span></p>
<p><span style="font-family: Calibri; font-size: small;">While there is a lot of literature around SaaS challenges, components that fits within SaaS architecture or general how-to architecture; there is a limited literature that ties everything together and allow you to actually link all these learned lessons together. <strong><em>This is exactly the focus of this blog series. The focus is to provide a detailed &ndash; as much as we can - descriptive guidance on building SaaS infrastructure. And to provide &ndash; as much as we can &ndash; of the SaaS infrastructure actual building blocks</em></strong>. It will not focus on building the actual SaaS (your CRM, billing, tax etc.). </span></p>
<p><span style="font-family: Calibri; font-size: small;">Few words before we begin, I follow sketch, detail then repeat approach similar to </span><a href="https://www.youtube.com/watch?v=uEdRLlqdgA4"><span style="color: #0563c1; font-family: Calibri; font-size: small;">this</span></a><span style="font-family: Calibri; font-size: small;">. That means broad strokes first and it also means that we will make mistakes (intentionally and unintentionally) and we will fix them as go. </span></p>
<p><span style="font-family: Calibri; font-size: small;">As we go I will be adapting code (there is plenty already written that we can refactor for our purposes), referencing external materials for you to learn more and build a wider understanding of an architecture like this. </span></p>
<p><span style="font-family: Calibri; font-size: small;">Let us begin</span></p>
<p><strong><span style="text-decoration: underline;"><span style="font-family: Calibri; font-size: small;">General Requirements: </span></span></strong></p>
<ol>
<li>
<p>SaaS Clients types: Browsers, desktop apps, mobile or others backend solutions (for B2B scenarios).</p>
</li>
<li>
<p>SaaS solution components: The solution can run anything that is based on HTTP/HTTPs (this means Web, Web API, Web Sockets). It will not include lower level protocols such as AMQP, MQTT etc. (at least for this stage)</p>
</li>
<li>
<p>Offerings Types &ndash; for whatever the SaaS:</p>
<ol>
<li>
<p>Private Jet/Platinum: Those who are premium customers served at the highest performance and availability SLA. &nbsp;Those might include customers with higher level of isolation (government/public sector and so forth).</p>
</li>
<li>
<p>First Class/ Gold: lower performance and so on.</p>
</li>
<li>
<p>Business Class: &hellip;</p>
</li>
<li>
<p>Econ Class:&hellip;.</p>
</li>
</ol></li>
<li>
<p>The system must allow scaling response to the demand or scheduling scaling.</p>
</li>
<li>
<p>The system must be flexible to allow seasonal customer type changes (customer Xyz is an econ customer but wants to use private jets during the last 5 days of the month). &nbsp;</p>
</li>
<li>
<p>The system must allow different versions of the SaaS software running side by side (to allow early adapters, A/B testing and so on).</p>
</li>
<li>
<p>The system must allow private/public cloud deployment.</p>
</li>
<li>
<p>The system must allow deployment of different SaaS software on the same platform (for example crm.contoso.com, taxes.contoso.com, hr.contoso.com).</p>
</li>
<li>
<p>The system must have the lowest footprint/ties possible on/with the underlying platform (azure, private cloud etc.). You will need that to:</p>
<ol>
<li>
<p>Make use of different features from the underlying platform as they get released (without ripping your solution apart).</p>
</li>
<li>
<p>Shift different parts of the solution to different clouds private/public(s) as needed.</p>
</li>
</ol></li>
<li>
<p>Billing/subscription schemes? (Check: <a href="https://msdn.microsoft.com/en-us/library/ff966499.aspx"><span style="color: #0563c1;">https://msdn.microsoft.com/en-us/library/ff966499.aspx</span></a> MSFT P&amp;P <strong><em>Developing Multi-tenant Applications for the Cloud, 3rd Edition</em></strong>). <em>As we add more to our SaaS infrastructure we will be seeing the impact on different billing/subscription types</em>.</p>
</li>
<li>
<p>For different types SLAs and impact on architecture also check &ldquo;<strong><em>Developing Multi-tenant Applications for the Cloud, 3rd Edition&rdquo;</em></strong></p>
</li>
</ol>
<p>&nbsp;</p>
<p><span style="font-family: Calibri; font-size: small;">The above defines broad strokes, let us begin to define our SaaS infrastructure conceptual architecture.</span></p>
<p>&nbsp;</p>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/50/19/1157.SaaSBits%20-%201%20-%201.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-19/1157.SaaSBits-_2D00_-1-_2D00_-1.png"><img src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/50/19/1157.SaaSBits%20-%201%20-%201.png" original-url="https://blogs.msdn.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-19/1157.SaaSBits-_2D00_-1-_2D00_-1.png" alt="" border="0" /></a></p>
<p>&nbsp;</p>
<p><span style="font-family: Calibri; font-size: small;">The easiest way to describe the above conceptual architecture is to walkthrough it. As the following</span></p>
<ol>
<li>
<p>A user or a system (client) will authenticate (as a result of redirection) against the AuthN component.</p>
</li>
<li>
<p>The AuthN component will generate a token for the client.</p>
</li>
<li>
<p>The client will use the token to connect to SaaS infrastructure (via the gateway).</p>
</li>
<li>
<p>The gateway will unpack the token to acquire enough information about the user (example tenant ID, group id etc).</p>
</li>
<li>
<p>Using this info the gateway will connect to the capacity and scale component <strong><span style="text-decoration: underline;">CSC</span></strong> asking &ldquo;where should I send this request to&rdquo;.</p>
</li>
<li>
<p>The CSC will decide which &ldquo;pod&rdquo; should respond to this user request. It can also decide to create more Pod as needed. &nbsp;</p>
</li>
<li>
<p>Gateway routes the request to the pod load balancer based on the CSC answer.</p>
</li>
</ol>
<p><strong><span style="text-decoration: underline;"><span style="font-family: Calibri; font-size: small;">Few Extra Notes:</span></span></strong></p>
<ol>
<li>
<p>The above happens for each request (to allow temporal subscriptions i.e. give me the highest performance last 5 days of the fiscal month).</p>
</li>
<li>
<p>The details of how CSC is making decisions is not relevant now, and will be discussed later.</p>
</li>
<li>
<p>A Pod is a collection of same instances serving the software itself, the instances are behind a load balancer (something we should not write ourselves, details later).</p>
</li>
<li>
<p>A Pod scales within (number of instances can increase up to the highest limits).</p>
</li>
<li>
<p><strong><em>The AuthN engine is expected to normalize tokens, authentication protocols</em></strong> into one gateway specific protocol. &nbsp;</p>
</li>
<li>
<p>The gateway must not retain any shared state to allow multiple instances of the gateway (load balancing) to run without the need to write a shared state component. &nbsp;</p>
</li>
</ol>
<p><strong><span style="text-decoration: underline;"><span style="font-family: Calibri; font-size: small;">Implementation </span></span></strong></p>
<p><span style="font-family: Calibri; font-size: small;">I have selected to start with gateway/router. The gateway is based on the James Baker&rsquo;s Service Gateway published here (</span><a href="https://sg.codeplex.com/"><span style="color: #0563c1; font-family: Calibri; font-size: small;">https://sg.codeplex.com/</span></a><span style="font-family: Calibri; font-size: small;">) for more information visit (</span><a href="https://channel9.msdn.com/Shows/Web+Camps+TV/Decoupling-Large-Applications-with-the-Service-Gateway"><span style="color: #0563c1; font-family: Calibri; font-size: small;">https://channel9.msdn.com/Shows/Web+Camps+TV/Decoupling-Large-Applications-with-the-Service-Gateway</span></a><span style="font-family: Calibri; font-size: small;">):</span></p>
<p><span style="font-family: Calibri; font-size: small;">The gateway is based on </span></p>
<ol>
<li>
<p>IIS ARR Reverse Proxy + Custom URL Rewrite Provider.</p>
</li>
<li>
<p>Hosted as a Azure Web Role</p>
</li>
</ol>
<p><span style="font-family: Calibri; font-size: small;">For more info: </span></p>
<ul>
<li>
<p>IIS URL rewrite module: <a href="https://www.iis.net/downloads/microsoft/url-rewrite"><span style="color: #0563c1;">https://www.iis.net/downloads/microsoft/url-rewrite</span></a></p>
</li>
<li>
<p>IIS URL rewrite custom provider <a href="https://www.iis.net/learn/extensions/url-rewrite-module/developing-a-custom-rewrite-provider-for-url-rewrite-module"><span style="color: #0563c1;">https://www.iis.net/learn/extensions/url-rewrite-module/developing-a-custom-rewrite-provider-for-url-rewrite-module</span></a></p>
</li>
<li>
<p>IIS ARR <a href="https://www.iis.net/downloads/microsoft/application-request-routing"><span style="color: #0563c1;">https://www.iis.net/downloads/microsoft/application-request-routing</span></a></p>
</li>
<li>
<p>IIS Reverse Proxy <a href="https://www.iis.net/learn/extensions/configuring-application-request-routing-%28arr%29/creating-a-forward-proxy-using-application-request-routing"><span style="color: #0563c1;">https://www.iis.net/learn/extensions/configuring-application-request-routing-%28arr%29/creating-a-forward-proxy-using-application-request-routing</span></a></p>
</li>
</ul>
<p><span style="font-family: Calibri; font-size: small;">&nbsp;</span></p>
<p><span style="font-family: Calibri; font-size: small;">My applicationhost.config looks like (under system.webserver section):</span></p>
<p><span style="font-family: Calibri; font-size: small;">&lt;rewrite&gt;</span></p>
<p><span style="font-family: Calibri;"><span style="font-size: small;">&nbsp;&lt;allowedServerVariables&gt;</span></span></p>
<p><span style="font-family: Calibri;"><span style="font-size: small;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;add name="x_custom_header" /&gt;</span></span></p>
<p><span style="font-family: Calibri;"><span style="font-size: small;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/allowedServerVariables&gt;</span></span></p>
<p><span style="font-family: Calibri;"><span style="font-size: small;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;globalRules&gt;</span></span></p>
<p><span style="font-family: Calibri;"><span style="font-size: small;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;rule name="ARR_server_proxy" enabled="true" patternSyntax="ECMAScript" stopProcessing="true"&gt;</span></span></p>
<p><span style="font-family: Calibri;"><span style="font-size: small;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;match url="^(.*)$" /&gt;</span></span></p>
<p><span style="font-family: Calibri;"><span style="font-size: small;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;action type="Rewrite" url="{SaaSUrlExec:{C:0}[***]{R:0}}" appendQueryString="true" /&gt;</span></span></p>
<p><span style="font-family: Calibri;"><span style="font-size: small;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;conditions logicalGrouping="MatchAny" trackAllCaptures="true"&gt;</span></span></p>
<p><span style="font-family: Calibri;"><span style="font-size: small;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;add input="{HTTP_xHEADER}" pattern="(.*)" /&gt;</span></span></p>
<p><span style="font-family: Calibri;"><span style="font-size: small;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/conditions&gt;</span></span></p>
<p><span style="font-family: Calibri;"><span style="font-size: small;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;serverVariables /&gt;</span></span></p>
<p><span style="font-family: Calibri;"><span style="font-size: small;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/rule&gt;</span></span></p>
<p><span style="font-family: Calibri;"><span style="font-size: small;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/globalRules&gt;</span></span></p>
<p><span style="font-family: Calibri;"><span style="font-size: small;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;providers&gt;</span></span></p>
<p><span style="font-family: Calibri;"><span style="font-size: small;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;provider name="SaaSUrlExec" type="Saas.Gateway.UrlRouter.RouterExec, Saas.Gateway.UrlRouter, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f94e44481a93f173"&gt;</span></span></p>
<p><span style="font-family: Calibri;"><span style="font-size: small;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;settings /&gt;</span></span></p>
<p><span style="font-family: Calibri;"><span style="font-size: small;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/provider&gt;</span></span></p>
<p><span style="font-family: Calibri;"><span style="font-size: small;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/providers&gt;</span></span></p>
<p><span style="font-family: Calibri;"><span style="font-size: small;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/rewrite&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></p>
<p><span style="font-family: Calibri;"><span style="font-size: small;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;proxy enabled="true" reverseRewriteHostInResponseHeaders="true"&gt;</span></span></p>
<p><span style="font-family: Calibri;"><span style="font-size: small;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;cache /&gt;</span></span></p>
<p><span style="font-family: Calibri;"><span style="font-size: small;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/proxy&gt;</span></span></p>
<p><span style="font-family: Calibri; font-size: small;">&nbsp;</span></p>
<p><span style="font-family: Calibri; font-size: small;">Basically it is a server wide rule that captures a custom header called xHeader and the Request Path then passes them as to our custom URL rewrite provider as <em>&lt;&lt;xHeader&gt;&gt;[***]&lt;&lt;path&gt;&gt; </em>which currently as a POC routes based on the xHeader content to one of two hard coded URLs (which for me was an empty page that spits &ldquo;server 1 &ldquo; (as deployed on server 1) and &ldquo;server 2&ldquo; (as deployed on server 2).</span></p>
<p><span style="font-family: Calibri; font-size: small;">The rest of the code written at this stage is packaging and deployment code to Azure Web roles (ARR/IIS installation and configuration).</span></p>
<p><span style="font-family: Calibri; font-size: small;">Note: in the code there is a console application that performs tests to confirm that the request has been routed successfully. </span></p>
<p><span style="font-family: Calibri; font-size: small;">&nbsp;</span></p>
<p><span style="font-family: Calibri; font-size: small;">&nbsp;</span></p>
<p><span style="font-family: Calibri; font-size: small;">Code is published on </span><a href="https://github.com/khenidak/SaaSBits"><span style="color: #0563c1; font-family: Calibri; font-size: small;">https://github.com/khenidak/SaaSBits</span></a><span style="font-family: Calibri; font-size: small;"> (with Feb 19th date). Once we have something stable enough we will move it to the root folder.</span></p>
<p><span style="font-family: Calibri; font-size: small;">&nbsp;</span></p>
<p><span style="font-family: Calibri; font-size: small;">Notes:</span></p>
<ol>
<li>
<p>We are currently installing ARR 2.5 (we will need to reconfigure for ARR 3.0 as it supports web sockets).</p>
</li>
</ol>
<p><span style="font-family: Calibri; font-size: small;">&nbsp;</span></p>
<p><span style="font-family: Calibri; font-size: small;">Till next time..</span></p>
<p>&nbsp;</p>
</div>]]></content:encoded>
    </item>
    <item>
      <title>Probably the easiest way to stay up to date.</title>
      <link>https://docs.microsoft.com/archive/blogs/bursts_of_random_architecture_thoughts/probably-the-easiest-way-to-stay-up-to-date</link>
      <pubDate>Tue, 10 Jan 2012 09:12:48 GMT</pubDate>
      <dc:creator><![CDATA[handak_roadrunner@hotmail.com]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/bursts_of_random_architecture_thoughts/2012/01/10/probably-the-easiest-way-to-stay-up-to-date/</guid>
      <description><![CDATA[Microsoft Field Engineers have been putting out a monthly technical roll up. Contains a lot of...]]></description>
      <content:encoded><![CDATA[<p><span style="font-family: 'Bookman Old Style','serif'; font-size: 11pt; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA;">Microsoft Field Engineers have been putting out a monthly technical roll up. Contains a lot of references to recently published downloads, fixes and information by Microsoft. Follow them it is probably the easiest way to stay up to date follow them on </span><a href="https://blogs.technet.com/b/trm/">https://blogs.technet.com/b/trm/</a></p>]]></content:encoded>
    </item>
    <item>
      <title>SharePoint 2010 Service Application Load Balancer</title>
      <link>https://docs.microsoft.com/archive/blogs/bursts_of_random_architecture_thoughts/sharepoint-2010-service-application-load-balancer</link>
      <pubDate>Wed, 04 Jan 2012 06:25:51 GMT</pubDate>
      <dc:creator><![CDATA[handak_roadrunner@hotmail.com]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/bursts_of_random_architecture_thoughts/2012/01/04/sharepoint-2010-service-application-load-balancer/</guid>
      <description><![CDATA[This is the first post,&nbsp;I am keeping it short for testing purposes :-) 
&nbsp;
SharePoint...]]></description>
      <content:encoded><![CDATA[<p><span style="font-family: book antiqua,palatino; font-size: small;">This is the first post,&nbsp;I am keeping it short for testing purposes :-) </span></p>
<p>&nbsp;</p>
<p><span style="font-family: book antiqua,palatino; font-size: small;">SharePoint Application Services provides web service endpoints for WFEs (and other</span><br /><span style="font-family: book antiqua,palatino; font-size: small;">components) for connection. These end point in high available configuration require</span><br /><span style="font-family: book antiqua,palatino; font-size: small;">load balancing which is available OOTB in SharePoint 2010.</span></p>
<p>&nbsp;</p>
<p><span style="font-family: book antiqua,palatino; font-size: small;">This is a great reading for more information on this topic: <a href="https://blogs.msdn.com/b/dtaylor/archive/2011/02/23/sharepoint-2010-service-application-load-balancer.aspx">https://blogs.msdn.com/b/dtaylor/archive/2011/02/23/sharepoint-2010-service-application-load-balancer.aspx</a></span></p>]]></content:encoded>
    </item>
  </channel>
</rss>