tag:blogger.com,1999:blog-6357484745585996232024-03-13T03:02:19.231+03:00How-to: C#, ASP.NET MVCI will try to collect here all intersting and useful things about c#, asp.net mvc and many more. Welcome!alexzaitzevhttp://www.blogger.com/profile/07485959744945069091noreply@blogger.comBlogger11125tag:blogger.com,1999:blog-635748474558599623.post-53248259650382158632017-04-16T13:39:00.000+03:002017-04-16T13:39:40.015+03:00Building microservices with pipes & filters<div style="text-align: justify;">
Couple month ago I made a short report on .NET meetup in Minsk about how we build microservices in our company. Report is on Russian, but if will be requests, I can make an English subtitles.</div>
<iframe allowfullscreen="" frameborder="0" height="315" src="https://www.youtube.com/embed/SKlfYHVBJYs" width="560"></iframe>
alexzaitzevhttp://www.blogger.com/profile/07485959744945069091noreply@blogger.com1tag:blogger.com,1999:blog-635748474558599623.post-41179826745194365852017-01-17T01:34:00.001+03:002017-01-17T01:48:06.643+03:00Writing Web API controller tests<div class="MsoNormal">
<a href="https://www.blogger.com/blogger.g?blogID=635748474558599623" imageanchor="1" style="clear: right; float: right; margin-bottom: 1em; margin-left: 1em;"></a><span lang="EN-US">I am fan of
TDD principle, and always trying to write tests first. Today I want to show,
how I usually test Web API controller. There are two approaches:<o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US"><br /></span></div>
<div class="MsoNormal">
<span lang="EN-US" style="font-size: 12.0pt; mso-ansi-language: EN-US; mso-bidi-font-size: 10.0pt;"><b>1. Unit test</b><o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US">Consider
<i>UserController</i> class, which has POST method that registers users.</span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span lang="EN-US" style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> </span><span lang="EN-US" style="color: blue; font-family: "consolas"; font-size: 9.5pt;">class</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> </span><span lang="EN-US" style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">UserController</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> : </span><span lang="EN-US" style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">ApiController</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> </span><span lang="EN-US" style="color: blue; font-family: "consolas"; font-size: 9.5pt;">private</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> </span><span lang="EN-US" style="color: blue; font-family: "consolas"; font-size: 9.5pt;">readonly</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> </span><span lang="EN-US" style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">IRegistrationService</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> registrationService;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> </span><span lang="EN-US" style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;">
UserController(</span><span lang="EN-US" style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">IRegistrationService</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> registrationService)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> </span><span lang="EN-US" style="color: blue; font-family: "consolas"; font-size: 9.5pt;">this</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;">.registrationService = registrationService;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> </span><span lang="EN-US" style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> </span><span lang="EN-US" style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">HttpResponseMessage</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> Post([</span><span lang="EN-US" style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">FromBody</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;">]</span><span lang="EN-US" style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> json)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> </span><span lang="EN-US" style="color: blue; font-family: "consolas"; font-size: 9.5pt;">var</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> model = </span><span lang="EN-US" style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> </span><span lang="EN-US" style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">JavaScriptSerializer</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;">().Deserialize<</span><span lang="EN-US" style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">RegistrationModel</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;">>(json);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> </span><span lang="EN-US" style="color: blue; font-family: "consolas"; font-size: 9.5pt;">bool</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> success = </span><span lang="EN-US" style="color: blue; font-family: "consolas"; font-size: 9.5pt;">this</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;">.registrationService.Register(model);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> </span><span lang="EN-US" style="color: blue; font-family: "consolas"; font-size: 9.5pt;">return</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> </span><span lang="EN-US" style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> </span><span lang="EN-US" style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">HttpResponseMessage</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;">(success ? </span><span lang="EN-US" style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">HttpStatusCode</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;">.OK : </span><span lang="EN-US" style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">HttpStatusCode</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;">.InternalServerError);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;">}</span><br />
<a name='more'></a></div>
<div class="MsoNormal">
<span lang="EN-US">And test
(which, of course, was written first)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;">[</span><span lang="EN-US" style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Test</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;">]<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span lang="EN-US" style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> </span><span lang="EN-US" style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> TestUserCanBeRegistered()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> </span><span lang="EN-US" style="color: green; font-family: "consolas"; font-size: 9.5pt;">// Arrange</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> </span><span lang="EN-US" style="color: blue; font-family: "consolas"; font-size: 9.5pt;">var</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;">
registrationService = </span><span lang="EN-US" style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> </span><span lang="EN-US" style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Mock</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"><</span><span lang="EN-US" style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">IRegistrationService</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;">>();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> registrationService<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> .Setup(x => x.Register(</span><span lang="EN-US" style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">It</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;">.IsAny<</span><span lang="EN-US" style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">RegistrationModel</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;">>()))<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> .Returns(</span><span lang="EN-US" style="color: blue; font-family: "consolas"; font-size: 9.5pt;">true</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> </span><span lang="EN-US" style="color: blue; font-family: "consolas"; font-size: 9.5pt;">var</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;">
userController = </span><span lang="EN-US" style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> </span><span lang="EN-US" style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">UserController</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;">(registrationService.Object);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> </span><span lang="EN-US" style="color: green; font-family: "consolas"; font-size: 9.5pt;">// Act</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> </span><span lang="EN-US" style="color: blue; font-family: "consolas"; font-size: 9.5pt;">var</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> response = userController.Post(</span><span lang="EN-US" style="color: maroon; font-family: "consolas"; font-size: 9.5pt;">@"{</span><span lang="EN-US" style="color: #ff007f; font-family: "consolas"; font-size: 9.5pt;">""</span><span lang="EN-US" style="color: maroon; font-family: "consolas"; font-size: 9.5pt;">login</span><span lang="EN-US" style="color: #ff007f; font-family: "consolas"; font-size: 9.5pt;">""</span><span lang="EN-US" style="color: maroon; font-family: "consolas"; font-size: 9.5pt;">:</span><span lang="EN-US" style="color: #ff007f; font-family: "consolas"; font-size: 9.5pt;">""</span><span lang="EN-US" style="color: maroon; font-family: "consolas"; font-size: 9.5pt;">Alex</span><span lang="EN-US" style="color: #ff007f; font-family: "consolas"; font-size: 9.5pt;">""</span><span lang="EN-US" style="color: maroon; font-family: "consolas"; font-size: 9.5pt;">, </span><span lang="EN-US" style="color: #ff007f; font-family: "consolas"; font-size: 9.5pt;">""</span><span lang="EN-US" style="color: maroon; font-family: "consolas"; font-size: 9.5pt;">password</span><span lang="EN-US" style="color: #ff007f; font-family: "consolas"; font-size: 9.5pt;">""</span><span lang="EN-US" style="color: maroon; font-family: "consolas"; font-size: 9.5pt;">: </span><span lang="EN-US" style="color: #ff007f; font-family: "consolas"; font-size: 9.5pt;">""</span><span lang="EN-US" style="color: maroon; font-family: "consolas"; font-size: 9.5pt;">password</span><span lang="EN-US" style="color: #ff007f; font-family: "consolas"; font-size: 9.5pt;">""</span><span lang="EN-US" style="color: maroon; font-family: "consolas"; font-size: 9.5pt;">}"</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> </span><span lang="EN-US" style="color: green; font-family: "consolas"; font-size: 9.5pt;">// Assert</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> </span><span lang="EN-US" style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Assert</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;">.AreEqual(</span><span lang="EN-US" style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">HttpStatusCode</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;">.OK, response.StatusCode);<o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"><br /></span></div>
<div class="separator" style="clear: both; text-align: left;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiZtRCBcmElT58rCJwESHakZljf3BvbyKEl4G641_kIyxmA6BRaOTDaNa_Kl2b1BkaiujzgLAYtmHUIZlfMzlwx_TnXi8bXWh2YSUSWblQ160-8q_EdXitao1h4DLnPzF18E4w_qemWe422/s1600/testok.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiZtRCBcmElT58rCJwESHakZljf3BvbyKEl4G641_kIyxmA6BRaOTDaNa_Kl2b1BkaiujzgLAYtmHUIZlfMzlwx_TnXi8bXWh2YSUSWblQ160-8q_EdXitao1h4DLnPzF18E4w_qemWe422/s1600/testok.png" /></a></div>
<div class="" style="clear: both; text-align: left;">
Test runs
OK</div>
<div class="MsoNormal">
<span lang="EN-US">BUT if you
try to send real request through the fiddler, for example, you will get an error.<o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span lang="EN-US" style="font-family: "lucida console"; font-size: 8.5pt;">POST
</span><a href="http://localhost:51954/user/"><span lang="EN-US" style="color: blue; font-family: "lucida console"; font-size: 8.5pt;">http://localhost:51954/user/</span></a><span lang="EN-US" style="font-family: "lucida console"; font-size: 8.5pt;"> HTTP/1.1<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span lang="EN-US" style="font-family: "lucida console"; font-size: 8.5pt;">Content-Type:
application/json; charset=utf-8<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span lang="EN-US" style="font-family: "lucida console"; font-size: 8.5pt;">Content-Length:
40</span></div>
<div class="MsoNormal">
<span lang="EN-US" style="font-family: "lucida console"; font-size: 8.5pt;">{"login":"Alex",
"password": "password"}</span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm;">
<span lang="EN-US" style="font-family: "lucida console"; font-size: 8.5pt;">HTTP/1.1 500 Internal Server Error<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm;">
<span lang="EN-US" style="font-family: "lucida console"; font-size: 8.5pt;">Cache-Control: no-cache<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm;">
<span lang="EN-US" style="font-family: "lucida console"; font-size: 8.5pt;">Pragma: no-cache<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm;">
<span lang="EN-US" style="font-family: "lucida console"; font-size: 8.5pt;">Content-Type: application/json;
charset=utf-8<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm;">
<span lang="EN-US" style="font-family: "lucida console"; font-size: 8.5pt;">Expires: -1<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm;">
<span lang="EN-US" style="font-family: "lucida console"; font-size: 8.5pt;">Server: Microsoft-IIS/10.0<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm;">
<span lang="EN-US" style="font-family: "lucida console"; font-size: 8.5pt;">X-AspNet-Version: 4.0.30319<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm;">
<span lang="EN-US" style="font-family: "lucida console"; font-size: 8.5pt;">X-SourceFiles:
=?UTF-8?B?RTpcVGVtcFxXZWJBcHBcV2ViQXBwXHVzZXJc?=<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm;">
<span lang="EN-US" style="font-family: "lucida console"; font-size: 8.5pt;">X-Powered-By: ASP.NET<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm;">
<span lang="EN-US" style="font-family: "lucida console"; font-size: 8.5pt;">Date: Thu, 12 Jan 2017 12:57:07 GMT<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm;">
<span lang="EN-US" style="font-family: "lucida console"; font-size: 8.5pt;">Content-Length: 2339<o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US" style="font-family: "lucida console"; font-size: 8.5pt;">{"Message":"An
error has occurred.",<span style="background: yellow; mso-highlight: yellow;">"ExceptionMessage":"Value
cannot be null.\r\nParameter name: input"</span>,"ExceptionType":"System.ArgumentNullException","StackTrace":" at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer
serializer, String input, Type type, Int32 depthLimit)<o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US"><br /></span></div>
<div class="MsoNormal">
<span lang="EN-US">How we can
get it, if test passed OK? This happens, because we fill method parameters by
ourselves, not by real request. </span>Error in
tests costs much more than in the code, that's why I prefer another method of
controller testing</div>
<div class="MsoNormal">
<span lang="EN-US"><br /></span></div>
<div class="MsoNormal">
<span lang="EN-US" style="font-size: 12.0pt; mso-ansi-language: EN-US; mso-bidi-font-size: 10.0pt;"><b>2. Integration test</b>*<o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US">Let’s test it in the other manner.<br />
First, we need to add some references to our project with tests.<o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm;">
<span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;">Install-Package
Microsoft.Owin.Host.SystemWeb<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm;">
<span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;">Install-Package
Microsoft.AspNet.WebApi.Owin<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm;">
<span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;">Install-Package
Microsoft.AspNet.WebApi.OwinSelfHost<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm;">
<span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;">Install-Package
Microsoft.Owin.Testing<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm;">
<span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;">Install-Package Unity<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; text-indent: -36.0pt;">
<span lang="EN-US">For our convenience we create base class for
integration tests<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span lang="EN-US" style="color: blue; font-family: "consolas"; font-size: 9.5pt;">internal</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> </span><span lang="EN-US" style="color: blue; font-family: "consolas"; font-size: 9.5pt;">class</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> </span><span lang="EN-US" style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">IntegrationTestBase</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> </span><span lang="EN-US" style="color: blue; font-family: "consolas"; font-size: 9.5pt;">protected</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> </span><span lang="EN-US" style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">TestServer</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> Server { </span><span lang="EN-US" style="color: blue; font-family: "consolas"; font-size: 9.5pt;">get</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;">; </span><span lang="EN-US" style="color: blue; font-family: "consolas"; font-size: 9.5pt;">private</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> </span><span lang="EN-US" style="color: blue; font-family: "consolas"; font-size: 9.5pt;">set</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;">; }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> [</span><span lang="EN-US" style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">TestFixtureSetUp</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;">]<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> </span><span lang="EN-US" style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> </span><span lang="EN-US" style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> BindServices()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> </span><span lang="EN-US" style="color: blue; font-family: "consolas"; font-size: 9.5pt;">this</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;">.Server = </span><span lang="EN-US" style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">TestServer</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;">.Create<</span><span lang="EN-US" style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">TestStartup</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;">>();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> [</span><span lang="EN-US" style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">TestFixtureTearDown</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;">]<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> </span><span lang="EN-US" style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> </span><span lang="EN-US" style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> DisposeServer()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> </span><span lang="EN-US" style="color: blue; font-family: "consolas"; font-size: 9.5pt;">this</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;">.Server.Dispose();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm;">
<span style="font-family: "consolas"; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm;">
<span lang="EN-US"><i>TestStartup.cs</i>. If you are hosting
your application with OWIN you can use <i>Startup.cs</i> here.</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span lang="EN-US" style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> </span><span lang="EN-US" style="color: blue; font-family: "consolas"; font-size: 9.5pt;">class</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> </span><span lang="EN-US" style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">TestStartup</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> </span><span lang="EN-US" style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> </span><span lang="EN-US" style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> Configuration(</span><span lang="EN-US" style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">IAppBuilder</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> builder)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<a href="https://www.blogger.com/blogger.g?blogID=635748474558599623" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"></a><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> </span><span lang="EN-US" style="color: blue; font-family: "consolas"; font-size: 9.5pt;">var</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> config = </span><span lang="EN-US" style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> </span><span lang="EN-US" style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">HttpConfiguration</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;">();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> config.Routes.MapHttpRoute(</span><span lang="EN-US" style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"users"</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;">, </span><span lang="EN-US" style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"users"</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;">, </span><span lang="EN-US" style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> { controller = </span><span lang="EN-US" style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"User"</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> });<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> </span><span lang="EN-US" style="color: blue; font-family: "consolas"; font-size: 9.5pt;">var</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> container = </span><span lang="EN-US" style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> </span><span lang="EN-US" style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">UnityContainer</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;">();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> container.RegisterType<</span><span lang="EN-US" style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">IRegistrationService</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;">, </span><span lang="EN-US" style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">RegistrationService</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;">>();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> config.DependencyResolver = </span><span lang="EN-US" style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> </span><span lang="EN-US" style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">UnityResolver</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;">(container);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> builder.UseWebApi(config);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm;">
<span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm;">
<span lang="EN-US">And finally write new test using
OWIN TestServer that will be hosted InProc.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;">[</span><span lang="EN-US" style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Test</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;">]<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span lang="EN-US" style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> </span><span lang="EN-US" style="color: blue; font-family: "consolas"; font-size: 9.5pt;">async</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> </span><span lang="EN-US" style="color: blue; font-family: "consolas"; font-size: 9.5pt;">void</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> TestRequestWasMappedCorrectlyAndUserRegistered()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> </span><span lang="EN-US" style="color: green; font-family: "consolas"; font-size: 9.5pt;">// Arrange</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> </span><span lang="EN-US" style="color: blue; font-family: "consolas"; font-size: 9.5pt;">using</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> (</span><span lang="EN-US" style="color: blue; font-family: "consolas"; font-size: 9.5pt;">var</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> client = </span><span lang="EN-US" style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> </span><span lang="EN-US" style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">HttpClient</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;">(</span><span lang="EN-US" style="color: blue; font-family: "consolas"; font-size: 9.5pt;">this</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;">.Server.Handler))<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> </span><span lang="EN-US" style="color: blue; font-family: "consolas"; font-size: 9.5pt;">var</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> body = </span><span lang="EN-US" style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> </span><span lang="EN-US" style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">StringContent</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;">(</span><span lang="EN-US" style="color: maroon; font-family: "consolas"; font-size: 9.5pt;">@"{</span><span lang="EN-US" style="color: #ff007f; font-family: "consolas"; font-size: 9.5pt;">""</span><span lang="EN-US" style="color: maroon; font-family: "consolas"; font-size: 9.5pt;">login</span><span lang="EN-US" style="color: #ff007f; font-family: "consolas"; font-size: 9.5pt;">""</span><span lang="EN-US" style="color: maroon; font-family: "consolas"; font-size: 9.5pt;">:</span><span lang="EN-US" style="color: #ff007f; font-family: "consolas"; font-size: 9.5pt;">""</span><span lang="EN-US" style="color: maroon; font-family: "consolas"; font-size: 9.5pt;">Alex</span><span lang="EN-US" style="color: #ff007f; font-family: "consolas"; font-size: 9.5pt;">""</span><span lang="EN-US" style="color: maroon; font-family: "consolas"; font-size: 9.5pt;">, </span><span lang="EN-US" style="color: #ff007f; font-family: "consolas"; font-size: 9.5pt;">""</span><span lang="EN-US" style="color: maroon; font-family: "consolas"; font-size: 9.5pt;">password</span><span lang="EN-US" style="color: #ff007f; font-family: "consolas"; font-size: 9.5pt;">""</span><span lang="EN-US" style="color: maroon; font-family: "consolas"; font-size: 9.5pt;">: </span><span lang="EN-US" style="color: #ff007f; font-family: "consolas"; font-size: 9.5pt;">""</span><span lang="EN-US" style="color: maroon; font-family: "consolas"; font-size: 9.5pt;">password</span><span lang="EN-US" style="color: #ff007f; font-family: "consolas"; font-size: 9.5pt;">""</span><span lang="EN-US" style="color: maroon; font-family: "consolas"; font-size: 9.5pt;">}"</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;">, </span><span lang="EN-US" style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Encoding</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;">.UTF8, </span><span lang="EN-US" style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"application/json"</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> </span><span lang="EN-US" style="color: green; font-family: "consolas"; font-size: 9.5pt;">// Act</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> </span><span lang="EN-US" style="color: blue; font-family: "consolas"; font-size: 9.5pt;">var</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> response = </span><span lang="EN-US" style="color: blue; font-family: "consolas"; font-size: 9.5pt;">await</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> client.PostAsync(</span><span lang="EN-US" style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"http://api.webapp.com/users/"</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;">, body);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
</div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> </span><span lang="EN-US" style="color: green; font-family: "consolas"; font-size: 9.5pt;">// Assert</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> </span><span lang="EN-US" style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Assert</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;">.AreEqual(</span><span lang="EN-US" style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">HttpStatusCode</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;">.OK, response.StatusCode);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm;">
<span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;">}</span><span lang="EN-US"><o:p></o:p></span><br />
<span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"><br /></span>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjeJYx5FEsNn3GXJls2D5oPg57wH2Q4r6jTan9KVpyklr0S9H0tssMJ9-gxDf0Zr26dKcy4EVyWDPwRo-rFB0BR5LyQa82x8t0ZYEvi_jN3Uessfl6uy-Sn2cmNJolwrREhpKH0csS-3Su-/s1600/testfail.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjeJYx5FEsNn3GXJls2D5oPg57wH2Q4r6jTan9KVpyklr0S9H0tssMJ9-gxDf0Zr26dKcy4EVyWDPwRo-rFB0BR5LyQa82x8t0ZYEvi_jN3Uessfl6uy-Sn2cmNJolwrREhpKH0csS-3Su-/s1600/testfail.png" /></a></div>
<span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"><br /></span></div>
<div class="MsoNormal">
<span lang="EN-US"><br /></span>
<span lang="EN-US"><br /></span>
<span lang="EN-US"><br /></span>
<span lang="EN-US"><br /></span>
<span lang="EN-US"><br /></span>
<span lang="EN-US"><br /></span>
<span lang="EN-US">This
approach simulates real request to the API resource from the client. </span>Test didn’t
pass. Now this correlates with the real behavior. Time to “green” this test.
There are many ways to do it:<br />
<br /></div>
<div class="MsoNormal">
<span lang="EN-US">1. Change type
of the input parameter to HttpRequestMessage</span></div>
<div class="MsoNormal">
<span lang="EN-US" style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> </span><span lang="EN-US" style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">HttpResponseMessage</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> Post(</span><span lang="EN-US" style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">HttpRequestMessage</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> request) { … }</span></div>
<div class="MsoNormal">
<span lang="EN-US">and then
gets the content<o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US" style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> body =
message.Content.ReadAsStringAsync().Result;</span><span lang="EN-US"><o:p></o:p></span><br />
<span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"><br /></span></div>
<div class="MsoNormal">
<span lang="EN-US">2. Change
type of the input parameter to <i>IDictionary<string, string></i> (using
<i>IDictionary<string, object></i> is not recommended)<o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US" style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> </span><span lang="EN-US" style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">HttpResponseMessage</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> Post(</span><span lang="EN-US" style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">IDictionary</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"><</span><span lang="EN-US" style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;">, </span><span lang="EN-US" style="color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;">> dict) { … }<o:p></o:p></span><br />
<span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"><br /></span></div>
<div class="MsoNormal">
<span lang="EN-US">3. Or
specify concrete type <i>RegistrationModel</i><o:p></o:p></span></div>
<div class="MsoNormal">
<a href="https://www.blogger.com/null" name="OLE_LINK2"></a><a href="https://www.blogger.com/null" name="OLE_LINK1"><span lang="EN-US" style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span></a><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> </span><span lang="EN-US" style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">HttpResponseMessage</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> Post(</span><span lang="EN-US" style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">RegistrationModel</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> model) { … }</span><span lang="EN-US"><o:p></o:p></span><br />
<span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"><br /></span></div>
<div class="MsoNormal">
<span lang="EN-US">4. Or vice versa
make it more generic with dynamic<o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US" style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> </span><span lang="EN-US" style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">HttpResponseMessage</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> Post([</span><span lang="EN-US" style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">FromBody</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;">]</span><span lang="EN-US" style="color: blue; font-family: "consolas"; font-size: 9.5pt;">dynamic</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> obj) { … }</span><span lang="EN-US"><o:p></o:p></span><br />
<span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"><br /></span></div>
<div class="MsoNormal">
<span lang="EN-US">I chose
third variant, so our controller method will look like<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span lang="EN-US" style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> </span><span lang="EN-US" style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">HttpResponseMessage</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> Post(</span><span lang="EN-US" style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">RegistrationModel</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> model)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> </span><span lang="EN-US" style="color: blue; font-family: "consolas"; font-size: 9.5pt;">bool</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> success = </span><span lang="EN-US" style="color: blue; font-family: "consolas"; font-size: 9.5pt;">this</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;">.registrationService.Register(model);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> </span><span lang="EN-US" style="color: blue; font-family: "consolas"; font-size: 9.5pt;">return</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> </span><span lang="EN-US" style="color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;"> </span><span lang="EN-US" style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">HttpResponseMessage</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;">(success ? </span><span lang="EN-US" style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">HttpStatusCode</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;">.OK : </span><span lang="EN-US" style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">HttpStatusCode</span><span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;">.InternalServerError);<o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US" style="font-family: "consolas"; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US">Now all
tests pass OK (of course we should fix test ‘TestUserCanBeRegistered’, because
we changed the contract).<o:p></o:p></span></div>
<div class="MsoNormal">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgJBNPugL0Lz_jFea3CAhmdf1n30ImSjqxZtw8zcAbTXkS6bEsNnzdj7JRIXoJqYJXRZSGZkmL7iTYLQv1X7y_mUDQnDnPkHRiZgea611mIgYqJNUiInEfJ-h9vlFswa0ghdOxO6NE0JMXI/s1600/testsok.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgJBNPugL0Lz_jFea3CAhmdf1n30ImSjqxZtw8zcAbTXkS6bEsNnzdj7JRIXoJqYJXRZSGZkmL7iTYLQv1X7y_mUDQnDnPkHRiZgea611mIgYqJNUiInEfJ-h9vlFswa0ghdOxO6NE0JMXI/s1600/testsok.png" /></a></div>
<br /></div>
<div class="MsoNormal">
<br /></div>
<span lang="EN-US" style="font-family: "calibri" , sans-serif; font-size: 11.0pt;"><br /></span>
<span lang="EN-US" style="font-family: "calibri" , sans-serif; font-size: 11.0pt;"><br /></span>
<span lang="EN-US" style="font-family: "calibri" , sans-serif; font-size: 11.0pt;"><br /></span><br />
<br />
Example of the code available on <a href="https://github.com/alexzaitzev/WebApiControllerTests" target="_blank">github</a><br />
<br />
<span lang="EN-US" style="font-family: "calibri" , sans-serif; font-size: 11.0pt;">* - I
name it integration, because it checks not only controller class but also MediaTypeFormatter</span>alexzaitzevhttp://www.blogger.com/profile/07485959744945069091noreply@blogger.com2tag:blogger.com,1999:blog-635748474558599623.post-79979029297128317192016-01-31T02:10:00.001+03:002016-02-03T21:05:36.388+03:00Unreachable code affects the result<div style="text-align: justify;">
"Can unreachable code affects the result?" - it's very tricky question. I sure your first answer will be "No". Because it is too natural. Unreachable code means, that this code never be executed. But let's have a look on such example:<br />
<a name='more'></a></div>
<pre class="prettyprint lang-cs">using System;
using static System.Console;
namespace UnreachableCodeExample
{
internal class Program
{
private static void Main()
{
F(() =>
{
return 10;
return 10.123;
});
}
private static void F(Func<int> f)
{
WriteLine(f.Invoke() + " Integer");
}
private static void F(Func<double> f)
{
WriteLine(f.Invoke() + " Double");
}
}
}
</pre>
<br />
IDE gives us a hint, that second <i>return </i>is redundant.<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgRsBashd0fEAJ8mntEqwLC9pROrGtrAg_jFYcXmxT4U0NpNhR4tKYvjm_sDj9-YlLdDYaoJj5GIhmtvzt8LzSvglF8d7rJXnEA_5uen5KjFV4LPcfUBAt_1ze4OyxMizee-REDwjbfPTD5/s1600/unreach.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgRsBashd0fEAJ8mntEqwLC9pROrGtrAg_jFYcXmxT4U0NpNhR4tKYvjm_sDj9-YlLdDYaoJj5GIhmtvzt8LzSvglF8d7rJXnEA_5uen5KjFV4LPcfUBAt_1ze4OyxMizee-REDwjbfPTD5/s1600/unreach.png" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
</div>
Well, compile the code and see the result<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgEQivXqsXTlGfhCMAfrqDaxUg2VUSDetQNhIz0cWzzNPkXngqKOu9HKFf5phH_truGYb_a3Z2CcjaFu4vQ8GkQvMBX4a2lkTji2oXR38f3j7SXpVg4WAchrN3gz33ruv61fKF3bevFhJ8F/s1600/result.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgEQivXqsXTlGfhCMAfrqDaxUg2VUSDetQNhIz0cWzzNPkXngqKOu9HKFf5phH_truGYb_a3Z2CcjaFu4vQ8GkQvMBX4a2lkTji2oXR38f3j7SXpVg4WAchrN3gz33ruv61fKF3bevFhJ8F/s1600/result.png" /></a></div>
<div class="separator" style="clear: both;">
Very interesting. We've got right result with incorrect type. Why it happened? Looks like unreachable code affects our result. To test this hypothesis let's try another example.</div>
<div>
<pre class="prettyprint lang-cs">using System;
using static System.Console;
namespace UnreachableCodeExample
{
internal class Program
{
private static void Main()
{
F(() =>
{
return (byte)10;
return (int)10;
});
}
private static void F(Func<byte> f)
{
WriteLine(f.Invoke() + " Byte");
}
private static void F(Func<int> f)
{
WriteLine(f.Invoke() + " Integer");
}
}
}
</pre>
</div>
<div class="separator" style="clear: both; text-align: left;">
Now we expect to see "10 Integer".</div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgoVb28Vypqob1c6c1XoyG6wAIYeDaJR13DyxlfzaNSgX2PDK1nHgyaeVicZcbSDsA6LlXYh6kzD9KVbEE0gQSZuQ2oTBp4xeKnW_mD9zII-YyAVImSa0gfamsEy6kiXUqzzZWGVMtcqFt7/s1600/result1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgoVb28Vypqob1c6c1XoyG6wAIYeDaJR13DyxlfzaNSgX2PDK1nHgyaeVicZcbSDsA6LlXYh6kzD9KVbEE0gQSZuQ2oTBp4xeKnW_mD9zII-YyAVImSa0gfamsEy6kiXUqzzZWGVMtcqFt7/s1600/result1.png" /></a></div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both;">
Yes, that's it. So we can definitely say, that compiler analyzes all return types, regardless their location and choose the biggest one as a returning type for lambda function.</div>
<div class="separator" style="clear: both;">
<br /></div>
<div class="separator" style="clear: both;">
In our example it would lead to unintended consequences, so be careful with such constructions. And happy coding!</div>
<br />alexzaitzevhttp://www.blogger.com/profile/07485959744945069091noreply@blogger.com7tag:blogger.com,1999:blog-635748474558599623.post-29407161123042380262015-08-30T03:00:00.000+03:002015-08-30T03:26:33.496+03:00C# 6: what's new<div style="text-align: justify;">
C# 6 was officially released about month ago, but it was in "preview" mode since the last year. So I am sure that everyone who wanted, can touch it and see all the new features. If not - here's the list of updates in the latest version of C#.</div>
<br />
<div style="text-align: justify;">
Along with C# Microsoft released VB 14. They are both part of the Roslyn compiler, who is available on <a href="https://github.com/dotnet/roslyn" target="_blank">github</a>.</div>
<br />
<div style="text-align: justify;">
<b><span style="font-size: large;">String Interpolation</span></b></div>
<pre class="prettyprint lang-cs">C#6
var str = $"Hello, {name}. You clicked {clickCount} time{{s}}";
</pre>
It's good alternative to String.Format function. The same piece of code on C# 5 will looks like this<br />
<pre class="prettyprint lang-cs">C#5
var str = String.Format("Hello, {0}. You clicked {1} time{s}", name, clickCount);</pre>
<br />
<a name='more'></a><br />
<b><span style="font-size: large;">Null-Conditional operator (?.)</span></b><br />
<pre class="prettyprint lang-cs">C#6
var result = Foo()?.Length
var length = str?.Length ?? 0;</pre>
It's also called null-propagating operator or "Elvis" operator. As previous feature it's only the syntactic sugar for accessing a member or invoking a method on a value if it's not <b>null</b>.<br />
<pre class="prettyprint lang-cs">C#5
int result;
if(Foo() != null)
{
result = Foo().Length;
}
int length;
if(str != null)
{
length = str.Length;
}
else
{
length = 0;
}
//or
var length = str != null ? str.Length : 0;</pre>
<br />
<b><span style="font-size: large;">nameof operator</span></b><br />
<pre class="prettyprint lang-cs">C#6
Console.WriteLine(nameof(flags));</pre>
<div style="text-align: justify;">
For me it's the killer feature of this release. There are many cases when you can use this operator. Moreover it's evaluated at <b>compile-time </b>to produce a string.</div>
<div style="text-align: justify;">
There is no equivalent in C#5, but we can write<b> run-time</b> synonym</div>
<pre class="prettyprint lang-cs">C#5
Console.WriteLine(typeof(flags).Name);</pre>
<br />
<b><span style="font-size: large;">Default values to auto properties</span></b><br />
<pre class="prettyprint lang-cs">C#6
private class Man
{
public string Name { get; set; } = "Alex";
public uint Age { get; set; } = 25;
}</pre>
Now you can assign property values right in the place where they are declared, without creating constructor or special init method.<br />
<pre class="prettyprint lang-cs">C#5
internal class Man
{
public string Name { get; set; }
public uint Age { get; set; }
public Man(string name, uint age)
{
Name = name;
Age = age;
}
}
</pre>
<br />
<b><span style="font-size: large;">Static imports</span></b><br />
<pre class="prettyprint lang-cs">C#6
using static System.Console;
using static System.Math;
class Program
{
static void Main()
{
WriteLine(Pow(2.0, 3.0));
}
}</pre>
Now <b>using </b>directive has one more use case:<br />
<ul>
<li style="text-align: justify;">allows you to access static members of a type or namespace without having to qualify the access with the type name or namespace.</li>
</ul>
<pre class="prettyprint lang-cs">C#5
using System;
class Program
{
static void Main()
{
Console.WriteLine(Math.Pow(2.0, 3.0));
}
}</pre>
<div>
Just a recap, here's another usages of <b>using </b>directive:</div>
<div>
<ul>
<li style="text-align: justify;">allows the use of types in a namespace so that you do not have to qualify the use of a type in that namespace;</li>
<li>creates an alias for a namespace or a type. This is called a <i>using alias directive</i>.</li>
</ul>
<div>
<a href="https://msdn.microsoft.com/en-us/library/sf0df423(v=vs.140).aspx" target="_blank">'using' on msdn</a></div>
</div>
<br />
<div>
<b><span style="font-size: large;">Expression-bodied members</span></b></div>
<pre class="prettyprint lang-cs">С#6
public string Name => "Alex";
[HttpGet]
public bool Get(string id) => CheckUserExist(id);
</pre>
<div>
<div style="text-align: justify;">
This is quite ambiguous improvement. It allows you to write body of the properties and methods as a lambda expression. But in my opinion old variant is more clear for understanding, especially for methods.</div>
<pre class="prettyprint lang-cs">С#5
public string Name
{
get
{
return "Alex";
}
}
[HttpGet]
public bool Get(string id)
{
return CheckUserExist(id);
}
</pre>
<div style="text-align: justify;">
<br />
<b><span style="font-size: large;">Await in catch/finally</span></b><br />
<pre class="prettyprint lang-cs">C#6
try
{
throw new Exception();
}
catch (Exception e)
{
await HandleException(e);
}
finally
{
await Finalize();
}
</pre>
In C#5 you can use <b>await</b> only in try section. Now you able to do it in the catch and finally blocks. That makes code more asynchronous, without writing complicated code.<br />
<br />
<span style="font-size: large;"><b>Exception filters</b></span><br />
<pre class="prettyprint lang-cs">C#6
try
{
throw new IndexOutOfRangeException("There is no such buyer in the array");
}
catch (Exception e) when (e.Message.Contains("buyer")) //in preview versions was <i>if</i> instead of <i>when</i>
{
WriteLine("buyer error");
}
catch (Exception e) when (e is IndexOutOfRangeException) //example for educational purpose only
{
WriteLine("IndexOutOfRangeException");
}
</pre>
Now you can filter exception not only by exception type but by everything you want. When exception occurs, it looks the nearest catch block. If catch expression evaluates to true, code in the catch block will run, if not - exception will search another catch block satisfying its exception type. But be careful, if no filter satisfies the conditions, unhandled exception will be thrown, so it's better to add catch block without any conditions at the end.<br />
<br />
<b><span style="font-size: large;">Constrcutor assignment to getter-only autoprops</span></b><br />
<pre class="prettyprint lang-cs">C#6
internal class Man
{
public string Name { get; }
public Man()
{
Name = "Alex";
}
//or just
public Name { get; } = "Alex";
}
</pre>
Probably, it's the least mentioned feature. It allows you to set a value to getter-only auto-implemented properties from constructor, or you can use set value as it's described above, using another new possibility of C#6.</div>
</div>
alexzaitzevhttp://www.blogger.com/profile/07485959744945069091noreply@blogger.com1tag:blogger.com,1999:blog-635748474558599623.post-41877760157891945762015-05-15T17:09:00.001+03:002015-05-16T02:24:01.583+03:00Far 3 Manager CheatSheet<div style="text-align: justify;">
From all file managers I love <a href="http://www.farmanager.com/" target="_blank">Far </a>the most. And idea of creating a cheat sheet for it was long time in my head. But it was a problem for me to make a nice view for this table of shortcuts, until I accidentally found a <a href="http://www.cheatography.com/">http://www.cheatography.com/</a> site.</div>
<div style="text-align: justify;">
Вespite the many flaws and bugs this tool really cool! It helps me to realize a long-standing idea of creating cheat sheet for Far3. And here it is.<br />
<a name='more'></a></div>
<iframe height="435" src="https://drive.google.com/file/d/0BwFhgCvuK9OcUHE3T2U3ZFR6dGM/preview" width="580"></iframe>
<br />
<div>
<div style="text-align: justify;">
You can download it by this link:<br />
<a href="http://www.cheatography.com/alexzaitzev/cheat-sheets/far-3-0-4040/">http://www.cheatography.com/alexzaitzev/cheat-sheets/far-3-0-4040/</a></div>
</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
I know, that many hot keys are missing (for example for working with console), but I tried to fit all information in one page.<br />
If you find this cheat sheet useful, I will make full version of Far3 shortcuts. </div>
<div style="text-align: justify;">
<br /></div>
alexzaitzevhttp://www.blogger.com/profile/07485959744945069091noreply@blogger.com2tag:blogger.com,1999:blog-635748474558599623.post-75481541517882472862014-11-26T12:34:00.001+03:002015-02-16T01:25:20.668+03:00Befriending ASP.NET Web.API2, OWIN and Ninject<div style="text-align: justify;">
There are many articles where people writing about how to use WebAPI + Owin, much less talking about Owin + Ninject and very little gathering all together. Also in these rare articles I couldn't find working solution, so I decided to put all eggs in one basket and write how to use WebAPI + Owin + Ninject. It's very strange that most of the posts talking about using Unity, not Ninject, which I love the most (think not only I).</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
First, let's prepare our environment, I'm using Visual Studio 2013, but if you are still VS2012 user, you can download <a href="http://www.microsoft.com/en-us/download/details.aspx?id=41532">ASP.NET and Web Tools 2013.1 for Visual Studio 2012</a> from Microsoft site. Now we are ready to rock!</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
1. Create new solution. Choose Empty ASP.NET Web application<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEinaVdl5r7aelqsIWRLht3ovQ2G8OGCGhQf7m9a8XB4X7vXLPf_z8eyI1vLHEfvrv0NIfBZNfjJGCZ87oF4F7hPB4PgQT8qg6a9Kj-SXOgS7AlzoDbjYZ9Gt1Xvnyyxu13dEPK4mIW-Fjxi/s1600/Screenshot_1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEinaVdl5r7aelqsIWRLht3ovQ2G8OGCGhQf7m9a8XB4X7vXLPf_z8eyI1vLHEfvrv0NIfBZNfjJGCZ87oF4F7hPB4PgQT8qg6a9Kj-SXOgS7AlzoDbjYZ9Gt1Xvnyyxu13dEPK4mIW-Fjxi/s1600/Screenshot_1.png" height="224" width="320" /></a></div>
<br />
<a name='more'></a>2. Download required packages from NuGet. I prefer to use <i>Package Manager Console </i>rather than UI form (hello from 90th). Also we won't download all packages at once, to understand what libraries will be needed at every step.</div>
<pre class="prettyprint lang-cs">Install-Package Microsoft.AspNet.WebApi.Owin
Install-Package Microsoft.Owin.Host.SystemWeb</pre>
<div style="text-align: justify;">
3. Create in you root directory file <i>Startup.cs</i>, which will be fired once when application starts.<br />
<pre class="prettyprint lang-cs">public class Startup
{
public void Configuration(IAppBuilder app)
{
var config = new HttpConfiguration();
WebApiConfig.Register(config);
app.UseWebApi(config);
}
}</pre>
</div>
<i>HttpConfiguration </i>responsible for API routes. Routes itself described in <i>WebApiConfig </i>class, which will be automatically created with selected template.<br />
Extension method <i>UseWebApi</i> allow us to wire up ASP.NET Web API to our Owin server pipeline.<br />
<br />
4. Since we use <i>Startup.cs </i>for start-up configuring, feel free to delete <i>Global.asax</i> file<br />
<br />
5. Add an interface for the service, the actual service and controller where our service will be injected<br />
<i>HomeController.cs</i>
<br />
<pre class="prettyprint lang-cs">[RoutePrefix("api/home")]
public class HomeController : ApiController
{
private readonly IFakeService _fakeService;
public HomeController(IFakeService fakeService)
{
_fakeService = fakeService;
}
[Route("")]
public IHttpActionResult Index(string question)
{
if (_fakeService.GetAnswer(question))
return Ok();
return BadRequest();
}
}
</pre>
<i>IFakeService.cs</i>
<br />
<pre class="prettyprint lang-cs">public interface IFakeService
{
bool GetAnswer(string question);
}</pre>
<i>FakeService.cs</i>
<br />
<pre class="prettyprint lang-cs">public class FakeService : IFakeService
{
public bool GetAnswer(string question)
{
return question.Contains("Is");
}
}</pre>
<br />
6. Now it's time to install Ninject and related extensions<br />
<pre class="prettyprint lang-cs">Install-Package Ninject.Web.Common.OwinHost
Install-Package Ninject.Web.WebApi.OwinHost</pre>
<br />
7. And the final steps to make all stuff works.<br />
Create <i>NinjectConfig.cs</i> file<br />
<pre class="prettyprint lang-cs">public static class NinjectConfig
{
public static Lazy<IKernel><ikernel> CreateKernel = new Lazy<IKernel><ikernel>(() =>
{
var kernel = new StandardKernel();
kernel.Load(Assembly.GetExecutingAssembly());
RegisterServices(kernel);
return kernel;
});
private static void RegisterServices(KernelBase kernel)
{
kernel.Bind<IFakeService><ifakeservice>()
.To<FakeService><fakeservice>();
}
}</fakeservice></ifakeservice></ikernel></ikernel></pre>
Add dependency assembly to section runtime of <i>Web.config</i><br />
<pre class="prettyprint lang-xml"><dependentAssembly>
<assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.2.2.0" />
</dependentAssembly></pre>
Change <i>Startup.cs file </i>this way<br />
<pre class="prettyprint lang-cs">public class Startup
{
public void Configuration(IAppBuilder app)
{
var config = new HttpConfiguration();
WebApiConfig.Register(config);
<strike>app.UseWebApi(config);</strike>
app.UseNinjectMiddleware(() => NinjectConfig.CreateKernel.Value);
app.UseNinjectWebApi(config);
}
}</pre>
<div style="text-align: justify;">
That's all. Now you can send POST request to your controller like this<br />
<blockquote class="tr_bq">
<i>http://localhost:PORT/api/home?question=QUESTION_STRING </i></blockquote>
and see there is no error with absence parameterless public constructor. Happy coding!<br />
<br />
Full code available on <a href="https://github.com/alexzaitzev/WepApiOwinNinject">github</a></div>
alexzaitzevhttp://www.blogger.com/profile/07485959744945069091noreply@blogger.com8tag:blogger.com,1999:blog-635748474558599623.post-16672138401815281102013-11-15T15:55:00.000+04:002013-11-15T15:55:24.841+04:00Deserialize JSON to Dictionary<div style="text-align: justify;">
Passing JSON is a very popular way to get data from the UI. For deserializing I am using <a href="http://james.newtonking.com/json">Json.Net</a> You can download this package from the site above or use NuGet.</div>
<div style="text-align: justify;">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhmQfMWrpBYrfEmgI-byPA4UziOrq9_lVl2048MESym5iAt0OpixCEimArrSxIblcxzgJuYt8NKVj7XTgUPMbUBWy24MYdkclJGn3Q_ZLaAwQgFPqHmDiw3fDBCSPEDdolKcaQuLFhAjlfJ/s1600/screen.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="266" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhmQfMWrpBYrfEmgI-byPA4UziOrq9_lVl2048MESym5iAt0OpixCEimArrSxIblcxzgJuYt8NKVj7XTgUPMbUBWy24MYdkclJGn3Q_ZLaAwQgFPqHmDiw3fDBCSPEDdolKcaQuLFhAjlfJ/s400/screen.jpg" width="400" /></a></div>
<br /></div>
<div style="text-align: justify;">
It works fine for converting JSON string to simple Dictionary<string, string> or Dictionary<string, object></div>
<div style="text-align: justify;">
<br /></div>
<pre class="prettyprint lang-cs">string json = @"{""key1"":""value1"",""key2"":""value2""}";
Dictionary<string, string> dictionary = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
//and
Dictionary<string, object> dictionary = JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
</pre>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
But you can have a little problem with deserialization JSON string to Dictionary<string, List<string>> type. </div>
Explicit conversion like in an example failes. To solve this problem we should make conversion in two steps:<br />
1. convert original string to Dictionary<string, object> as we have done before;<br />
2. deserialize recieved object (which is also the JSON string) to the type List<string>.<br />
<pre class="prettyprint lang-cs">string json = "{\"key1\":[\"value1\"],\"key2\":[\"value2\",\"value3\"]}";
Dictionary<string, List<string>> result = new Dictionary<string, List<string>>();
Dictionary<string, object> desirializedJsonObject = JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
foreach (var obj in desirializedJsonObject)
{
var value = JsonConvert.DeserializeObject<List<string>>(obj.Value.ToString());
result.Add(obj.Key, value);
}</pre>
<br />
Looks pretty easy, but can take some time for a first. Happy coding!alexzaitzevhttp://www.blogger.com/profile/07485959744945069091noreply@blogger.com5tag:blogger.com,1999:blog-635748474558599623.post-89238183278419635172013-10-13T01:53:00.001+04:002013-10-13T01:54:20.923+04:00Sticky notes always on top in Windows 7<div style="text-align: justify;">
Today I want to write about not programming thing but very useful feature, that I was needed. Windows 'Sticky notes' are very helpful for developers and not only for them. I often write on it some things that don't want to keep in memory. But there is a problem - they are not always on top of all other windows.</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
And that is very strange, because concept of a 'sticker' implies that it stays on top of things, before our eyes. There is no any option to do it, so you can write a program or use a tool like <b>NIRCMD</b>. I chose the last variant.</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
So, first you need to download it from the site <a href="http://www.nirsoft.net/utils/nircmd.html">http://www.nirsoft.net/utils/nircmd.html</a>. And then just type the command:</div>
<div style="text-align: justify;">
</div>
<div style="text-align: justify;">
<pre class="prettyprint lang-cs">C:\> nircmd.exe win settopmost class Sticky_Notes_Top_Window 1</pre>
</div>
<div style="text-align: justify;">
To undo this action, type this:</div>
<div style="text-align: justify;">
<pre class="prettyprint lang-cs">C:\> nircmd.exe win settopmost class Sticky_Notes_Top_Window 0</pre>
</div>
<div style="text-align: justify;">
This command changes the TOPMOST flag on all windows with the ‘Sticky_Notes_Top_Window’ class name. That's it. Hope it helps.</div>
alexzaitzevhttp://www.blogger.com/profile/07485959744945069091noreply@blogger.com1tag:blogger.com,1999:blog-635748474558599623.post-57288765216058225472013-09-08T02:01:00.002+04:002013-09-08T13:57:17.960+04:00Dependency Injection using Ninject in MVC 4<div style="text-align: justify;">
Today let's talk how to implement DI using the most popular IoC container Ninject in MVC 4, because it differs from the previous versions of framework.</div>
<br />
<div style="text-align: justify;">
<b>Small recall: </b>the dependency injection is a software design pattern, which allows us easily change hard-coded dependencies between interfaces and their implementations.</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
<b>Problem statement</b>: for example we have <i>HomeController </i>and <i>GuestController</i> classes, which tightly coupled to the <i>HomeService</i> class as on the image below.<br />
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
<img border="0" height="156" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjPO7VfH9JkUD7sx1g6ZT5HKikN0LARNjxetBVL9pTv2bSG4wXnpMvAQPkNn7FWXggwkQhlAhPC-rl45wJ2V18Da6GuK04hh6970EjI6TrM8bEgJcbSweX8LzekwfqsAalYAC_bB5rmyKlm/s320/1.jpg" width="320" /></div>
<a name='more'></a><br />
<div style="text-align: justify;">
<span style="text-align: left;">Then if we want to replace the </span><i style="text-align: left;">HomeService</i><span style="text-align: left;"> class, we have to </span>make changes in the classes that are tightly-coupled to it. This is pretty easy in <span style="text-align: left;">a small project, but it becomes a laborious and error-prone process in a real project, when we have much more dependencies between classes.</span></div>
<div class="separator" style="clear: both; text-align: justify;">
We can solve part of this problem using C# interfaces to abstract definition from its implementation. Let's add interface <i>IHomeService </i>on our scheme and see how it will change.</div>
<div style="text-align: center;">
<img border="0" height="265" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj0W7l_vZ0ETjXvf1yam2tlTqy45tTMWMhvBDuLAqMpe3V79iPQBdipRKUKxVHwvzOw5sBiHI1W1DcWrtRvTpawDEDFop8I22dl_tpNeXe6jrDq6maEHR_Kynzke-VStkQOoH2z-F5E5o6F/s320/2.png" width="320" />
</div>
<div class="separator" style="clear: both; text-align: justify;">
As you can notice directions of the arrows have been changed TO the interface from the <i>HomeService</i> class. It calls Inversion of Control (IoC), when modules make no assumptions about what other systems do but rely on their interfaces.</div>
<div class="separator" style="clear: both; text-align: justify;">
Ok, that's much better, but C# requires us to specify the implementation class for an interface during instantiation, because it needs to know which implementation class we want to use. Our goal with Ninject is to reach the point where we specify that we want to instantiate an implementation of the <i>IHomeService </i>interface, but the details of which implementation is required are not part of the code in the controllers.</div>
<span style="font-size: large;">Installing Ninject</span><br />
The easiest way to install ninject is to use the integrated Visual Studio support for NuGet, which makes it easy to install a wide range of packages and keep them up to date. Right click on the project, then choose <i>Manage NuGet Package for Solution</i> to open NuGet package window. After it type <i>Ninject </i>in the search field as in example and click install.<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjgzbU5G1uz51KOcjr6NKG_FWBZVHjnqXQgZRFHee_Ag7291DGHUJyc4pktSk_026nCvo7XN7LkTQM04jVxKov-3vHJbWMFxn3LAetVdSYITp4adD9WNGlTuY4jpeddeuQyk3Ke266YuQyv/s1600/3.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="266" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjgzbU5G1uz51KOcjr6NKG_FWBZVHjnqXQgZRFHee_Ag7291DGHUJyc4pktSk_026nCvo7XN7LkTQM04jVxKov-3vHJbWMFxn3LAetVdSYITp4adD9WNGlTuY4jpeddeuQyk3Ke266YuQyv/s400/3.png" width="400" /></a></div>
That's it. NuGet will do all necessary stuff: download package, add reference and check updates if it is needed.<br />
<br />
<span style="font-size: large;">Create the Dependency Resolver</span><br />
Before using Ninject we need to create custom dependency resolver first. By creating it, we ensure that Ninject is used whenever an object is going to be created. The MVC Framework has it own IoC container <i>Unity </i>and default resolver for it, but we chose <i>Ninject </i>for our purposes, so let's create <span style="text-align: start;"><i>NinjectDependencyResolver</i> class for it.</span><br />
<pre class="prettyprint lang-cs">using System;
using System.Collections.Generic;
using Ninject;
using System.Web.Mvc;
namespace Test.Infrastructure
{
public class NinjectDependencyResolver : IDependencyResolver
{
private readonly IKernel _kernel;
public NinjectDependencyResolver()
{
_kernel = new StandardKernel();
AddBindings();
}
public object GetService(Type serviceType)
{
return _kernel.TryGet(serviceType);
}
public IEnumerable<object> GetServices(Type serviceType)
{
return _kernel.GetAll(serviceType);
}
private void AddBindings()
{
_kernel.Bind<IHomeService>().To<HomeService>();
}
}
}</pre>
In constructor we created an instance of a Ninject <i>kernel</i>, which is the object we will use to communicate with Ninject and request implementations of interfaces.<br />
The MVC Framework will call the <i>GetService </i>or <i>GetServices </i>methods when it needs an instance of a class to service an incoming request. The job of a dependency resolver is to create that instance - a task that we fulfill by calling the Ninject <i>TryGet </i>and <i>GetAll </i>methods. The <i>TryGet</i> method returns null when there is no suitable binding, rather than throwing an exception. The <i>GetAll </i>method supports multiple bindings for a single type, which is used when there are several different service providers available. Our dependency resolver class is also where we have set up our Ninject bindings. In the <i>AddBindings </i>method, we use the <i>Bind </i>and <i>To </i>methods to set up the relationship between the interface and the implementation class.<br />
<br />
<span style="font-size: large;">Register the Dependency Resolver</span><br />
We have to tell the MVC Framework that we want to use our own dependency resolver, which we do by modifying the <i>Global.asax.cs</i> file.<br />
<pre class="prettyprint lang-cs">using System.Web.Http;
using System.Web.Mvc;
using System.Web.Routing;
using Test.Infrastructure;
namespace Test
{
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
<b> DependencyResolver.SetResolver(new NinjectDependencyResolver());</b>
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}</pre>
</div>
<br />
<span style="font-size: large;">Adding Injection into Controller</span><br />
The last thing we should done is to add some lines of the code to the controllers<br />
<pre class="prettyprint lang-cs">using System.Web.Mvc;
namespace Test.Controllers
{
public class HomeController : Controller
{
private readonly IHomeService _homeService;
public HomeController(IHomeService homeService)
{
_homeService = homeService;
}
public ActionResult Index()
{
return View();
}
}
}</pre>
And do the same with <i>GuestController</i>. Hope you can do it yourself.<br />
<div style="text-align: justify;">
We just add an implementation of the <i>IHomeService</i> interface in a class constructor. We have not specified which implementation we want to work with (we have made it in our custom dependency resolver), and we have added an instance variable called <i>_homeService </i>that we can use to refer to the IHomeService we receive in the constructor throughout the controller class.</div>
alexzaitzevhttp://www.blogger.com/profile/07485959744945069091noreply@blogger.comtag:blogger.com,1999:blog-635748474558599623.post-4735884912267065812013-02-26T01:24:00.002+04:002013-02-26T01:32:22.088+04:00Singleton pattern<div style="text-align: justify;">
Singleton is using when you need a class that has only one instance, and you need to provide a global point of access to the instance. As you can notice, singleton is similar to the global variable. And as the global variable it can violates testability and scalability of your system. So you should use it carefully or not to use at all if you can. But if you need it, you can use this 'lazy' implementation. ('Lazy' means that the instantiation is not performed until an object asks for an instance, whereas global variables always consume resources when the application starts):</div>
<pre class="prettyprint lang-cs">using System;
public class Singleton
{
private static Singleton instance;
private Singleton() {}
public static Singleton Instance
{
get
{
if (instance == null)
{
instance = new Singleton();
}
return instance;
}
}
}
</pre>
<a name='more'></a><span style="text-align: justify;">Looks pretty easy. But this way singleton can have a successor, that may introduce unwelcome dependencies sometimes. To prevent derivation let's mark the class <b>sealed</b>:</span><br />
<pre class="prettyprint lang-cs">public sealed class Singleton
{
private static readonly Singleton instance = new Singleton();
private Singleton() {}
public static Singleton Instance
{
get
{
return instance;
}
}
}
</pre>
<div style="text-align: justify;">
Here we still have 'lazy' implementation. The <i>Singleton </i>instance is referenced by a private static member variable, thus the instantiation does not occur until the class is first referenced by a call to the <i>Instance </i>property. In addition, the variable is marked <b>readonly</b>, which means that it can be assigned only during static initialization or in a class constructor.<br />
<br />
The main disadvantage of these implementations, is that they are not safe for multithreaded environments. If separate threads of execution enter the <i>Instance </i>property method at the same time, more that one instance of the <i>Singleton </i>object may be created. Each thread could execute the following statement and decide that a new instance has to be created.<br />
The following implementation allows only a single thread to enter the critical area, which the <b>lock </b>block identifies, when no instance of <i>Singleton </i>has yet been created:</div>
<pre class="prettyprint lang-cs">using System;
public sealed class Singleton
{
private static volatile Singleton instance;
private static object syncRoot = new Object();
private Singleton() {}
public static Singleton Instance
{
get
{
if (instance == null)
{
lock (syncRoot)
{
if (instance == null)
instance = new Singleton();
}
}
return instance;
}
}
}</pre>
<div style="text-align: justify;">
<b>The bottom line:</b> generally singletons are not preferred to use. However it's better than the global vars, because they don't pollute the global namespace with unnecessary variables and also permit lazy allocation and initialization.</div>alexzaitzevhttp://www.blogger.com/profile/07485959744945069091noreply@blogger.comtag:blogger.com,1999:blog-635748474558599623.post-22142289669836252042013-02-24T14:04:00.003+04:002013-02-24T14:04:28.760+04:00Hello World!<div style="text-align: justify;">
Hello! My name is Alex. I've been working as a C# ASP.NET Developer for the last few years and to structure my knowledge I decided to write a blog. Also I will be glad if the information I will write here be useful for someone. As you can see at the top of the site I will be writing here mainly about C# and all that related to it. This is not my first experience of the writting a blog. But about such technical things on English I write at the first time. So if you see any mistake in my posts or English or just want to say something that can make this blog better, feel free to email me at<a href="mailto: info@alexzaitzev.pro"> info@alexzaitzev.pro</a> or hit me on skype: <a href="skype:alexzaitzev">alexzaitzev.</a></div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
I want to start my blog from the post about<b> Design Patterns</b>. A bit later I will write about algorithms and some interesting tasks and hints in C#, but I think that Design Patters are one of the most important and useful thing, so let's start from them.</div>
<div style="text-align: justify;">
There are many articles about them, but when I need to find implementation some of them I had to visit many sites: no one has full and clear information about most of them. Not to mention their implementation. Thus next post I will devote to <b>Singleton pattern</b>. The easiest for understanding and using pattern.</div>
alexzaitzevhttp://www.blogger.com/profile/07485959744945069091noreply@blogger.com1