-
Notifications
You must be signed in to change notification settings - Fork 0
/
atom.xml
980 lines (813 loc) · 65.1 KB
/
atom.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title><![CDATA[The QCoach Dev Notes]]></title>
<link href="http://qualtrak.github.com/atom.xml" rel="self"/>
<link href="http://qualtrak.github.com/"/>
<updated>2012-10-17T12:43:06+02:00</updated>
<id>http://qualtrak.github.com/</id>
<author>
<name><![CDATA[Kornelije Sajler]]></name>
</author>
<generator uri="http://octopress.org/">Octopress</generator>
<entry>
<title type="html"><![CDATA[Deploying Web Api application to the Azure Cloud Service]]></title>
<link href="http://qualtrak.github.com/2012/10/16/deploying-web-api-application-to-the-azure-cloud-service/"/>
<updated>2012-10-16T12:25:00+02:00</updated>
<id>http://qualtrak.github.com/2012/10/16/deploying-web-api-application-to-the-azure-cloud-service</id>
<content type="html"><![CDATA[<p>After the simple application is created and the database is connected to <em>SQL Azure</em> now
comes a part to deploy it. There are two ways to deploy it to a <em>Azure Cloud</em>:</p>
<ul>
<li>Web Site Application</li>
<li>Cloud Service - Web Role Application</li>
</ul>
<p>We shall deploy it as the Cloud Service - Web Role application.</p>
<h2>Add web app to Windows Azure Cloud Service Project</h2>
<ul>
<li>Open the <code>RestApiAzure</code> solution.</li>
<li>Right-click on <code>RestApiWeb</code> project in <em>Solution Explorer</em> and from context menu
click <code>Add Windows Azure Cloud Service Project</code>.</li>
<li>The <code>RestApiWeb.Azure</code> project will be created and <code>RestApiWeb</code> will get some Azure
libraries in Reference.</li>
</ul>
<h2>Testing the application by Azure Emulator</h2>
<p>By Running the application (Ctrl+F5) you’ll probably get message box saying that Azure
need elevated permissions to be started.</p>
<p>This was easy to do in Windows 7, right-click the shortcut click <code>Properties</code> and through
<em>Compatibility</em> tab, but in Windows 8 is hidden away.</p>
<h3>Adding Elevated Permissions to Visual Studio on Windows 8</h3>
<ul>
<li>Go to <em>C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE</em> and right-click on
<code>devenv.exe</code>.</li>
<li>In context menu click <code>Troubleshoot Compatibility</code>.</li>
<li>In <code>Program Compatibility Troubleshooter</code> click <code>Troubleshoot Program</code>.</li>
<li>Check the <code>The program requires additional permissions</code>.</li>
<li>Click two times <code>Next</code> and then click <code>Yes, save these settings for program</code>.</li>
<li>Click <code>Close the troubleshooter</code>.</li>
<li>Close and reopen <code>RestApiAzure</code> Visual Studio project.</li>
<li>Make sure that <code>RestApiWeb.Azure</code> is startup project.</li>
<li>Run the application with (Ctrl+F5).</li>
<li>If the Windows Firewall popup click <code>Allow access</code>.</li>
<li>The application should run and in in system tray should be blue <code>Azure Emulator</code> icon.</li>
</ul>
<h2>Deploy application to Azure</h2>
<ul>
<li>Right-click <code>RestApiWeb.Azure</code> project in <code>Solution Explorer</code> click <code>Publish</code>.</li>
</ul>
<h3>Getting publishing settings</h3>
<ul>
<li>Opens <code>Publish Windows Azure Application</code> click <code>Sign in to download credentials</code>,</li>
<li>Opens Internet Explorer (IE), click <code>Sign in</code> link.</li>
<li>Enter credentials and click <code>Sign in</code> button.</li>
<li>IE will prompt to save the publish settings, then save it to some local folder.</li>
<li>Close IE message to open the file and close IE.</li>
</ul>
<h3>Applying publish settings and Creating Cloud Service</h3>
<ul>
<li>Return to Visual Studio and in <code>Publish Windows Azure Application</code> click <code>Import</code> button.</li>
<li>Go to folder where the publishing settings is saved, select it and click <code>Open</code> then <code>Next</code> button.</li>
<li>In Settings For <code>Cloud Service</code> in dropbox choose <code><Create New...></code>. Enter name and choose region. Click <code>OK</code> button.</li>
<li>Click <code>Next</code> button.</li>
<li>In Summary click <code>Publish</code> button.</li>
</ul>
<h3>Publishing</h3>
<ul>
<li>In Visual Studio <code>Windows Azure Activity Log</code> opens.</li>
<li>Double click <em>Deploying RestApiWeb.Azure to qcoach - Prodution</em> to see more details:</li>
</ul>
<p><img src="http://qualtrak.github.com/images/02-01.png"></p>
<ul>
<li>Try the REST API naw with URL</li>
</ul>
<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>http://qcoach.cloudapp.net/api/accounts</span></code></pre></td></tr></table></div></figure>
<p>It should work on Azure as it was locally!</p>
<blockquote> Github qualtrak/RestApiAzure commit:
<br/>
<a href="https://github.com/qualtrak/RestApiAzure/commit/30e8cbdc9603f859b7a6d888a8d7bc6002a8fe3b" target="_blank">
Added Azure Publish project and publishing is setup.</a>
</blockquote>
<div class='octopress-authorbox'>
<div class="author-pic">
<img src="http://www.gravatar.com/avatar/4e0d581521137646abac17331917e95e" alt="Kornelije Sajler" />
</div>
<div class="author-about">
<h3>Kornelije Sajler</h3>
<p>The Lead Software Developer at Qualtrak</p>
<ul class="author-links">
<li>
<a href="http://twitter.com/xajler">@xajler</a>
</li>
<li>
<a href="http://blog.metaintellect.com/">Blog</a>
</li>
</ul>
</div>
</div>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[Connecting the ASP.NET Web Api with SQL Azure]]></title>
<link href="http://qualtrak.github.com/2012/10/12/connecting-the-asp-dot-net-web-api-with-sql-azure/"/>
<updated>2012-10-12T17:56:00+02:00</updated>
<id>http://qualtrak.github.com/2012/10/12/connecting-the-asp-dot-net-web-api-with-sql-azure</id>
<content type="html"><![CDATA[<p>The step-by-step creation of ASP.NET MVC Web Api <em>RestApiAzure</em> application.</p>
<p>The full code is hosted on github as <a href="https://github.com/qualtrak/RestApiAzure">qualtrak/RestApiAzure</a>.
Each commited step will be as note and link to particular changeset.</p>
<p>This is mostly learning by doing process so comments, suggestions, different opinions, and
code reviews for Github commits are very welcome!</p>
<h2>Creating SQL Azure</h2>
<ol>
<li>Login to a <a href="https://manage.windowsazure.com">Windows Azure Portal</a>.</li>
<li>In left pane cluck <em>SQL Database</em>.</li>
<li>In bottom command menu click <em>Add</em>.</li>
<li>In <em>Specify database settings</em> popup:
<ul>
<li>Add wanted <code>name</code> of database.</li>
<li>Specify <code>edition</code>: <em>Web</em> or <em>Business</em>.</li>
<li>Specify <code>limit database size</code> depending on edition (affects billing).</li>
<li>Specify <code>collation</code> or leave default.</li>
<li><p>Specify <code>server</code>: existing or create new one.</p>
<p><img src="http://qualtrak.github.com/images/01-01.png"></p></li>
</ul>
</li>
</ol>
<h2>Configure Allowed IP addresses of SQL Azure</h2>
<p>To properly connect to the SQL Azure from the client (your PC) there is need to add the
IP address to allowed IP addresses.</p>
<p>In <a href="https://manage.windowsazure.com">Windows Azure Portal</a> <em>SQL databases</em> click the
<em>Servers</em> link, right below title ”<em>sql databases</em>”.</p>
<p>From list of <em>Servers</em> click on created or existing Server that created database is part.
In this case it was <code>qh9pkltozj</code> server.</p>
<p><img src="http://qualtrak.github.com/images/01-02.png"></p>
<p>Now in <code>qh9pkltozj</code> server click the <em>Configure</em> link, below <em>qh9pkltozj</em> server title.</p>
<p>In <em>allowed ip addresses</em>, by default, if the client IP address is not allowed will be displayed
with the action <em>add to allowed IP addresses</em>.</p>
<p><img src="http://qualtrak.github.com/images/01-03.png"></p>
<p>Click action <em>add to allowed IP addresses</em> to gain access to the SQL Azure database(s) on
<code>qh9pkltozj</code> server.</p>
<p>In bottom command menu click <em>Save</em> to save firewall rule.</p>
<p>The SQL Azure is now ready to be connected from client (your PC) with SQL Server Management Studio (SSMS)
or via connection string from local application.</p>
<h2>Create ASP.NET MVC Web Api project</h2>
<ol>
<li>Open Visual Studio 2012 (or 2010).</li>
<li><em>File</em> | <em>Create New Project</em> (Ctrl+Shift+n)</li>
<li>Choose from <em>Other Project Types</em> | <em>Visual Studio Solutions</em> the <em>Blank Solutions</em>:
<ul>
<li>For solution <em>Name</em> enter <code>RestApiAzure</code>.</li>
<li>For <em>Location</em> enter <code>c:\source</code>.</li>
<li>Click <em>OK</em> to confirm.</li>
</ul>
<p> <img src="http://qualtrak.github.com/images/01-04.png"></p></li>
<li>Right-click <code>RestApiAzure</code> in the <em>Solution Explorer</em>, <em>Add</em> | <em>New Project</em>.</li>
<li>Choose from <em>Visual C#</em> | <em>Web</em> the <em>ASP.NET MVC 4 Web Application</em>:
<ul>
<li>For project <em>Name</em> enter <code>RestApiWeb</code>.</li>
<li>For <em>Location</em> enter leave default (<code>c:\source\RestApiAzure</code>).</li>
<li>Click <em>OK</em> to confirm.</li>
</ul>
<p> <img src="http://qualtrak.github.com/images/01-05.png"></p>
<ul>
<li>In <em>New ASP.NET MVC Project</em> choose <em>Web API</em> template.</li>
<li>Click <em>OK</em> to confirm.</li>
</ul>
</li>
</ol>
<blockquote> Github qualtrak/RestApiAzure commit:
<br/>
<a href="https://github.com/qualtrak/RestApiAzure/commit/ae5d9994a4c5694675f981341b1b6e55cfb3bba5" target="_blank">
Initial commit. Default empty Web Api solution with README.</a>
</blockquote>
<h2>Create POCO models: Account and Tenant</h2>
<p>The simple two models Account and Tenant (Tenant belongs to Account).</p>
<ul>
<li>In <code>RestApiWeb.Models</code> create <code>Account.cs</code> class:</li>
</ul>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
</pre></td><td class='code'><pre><code class='csharp'><span class='line'><span class="k">namespace</span> <span class="nn">RestApiWeb.Models</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'> <span class="k">using</span> <span class="nn">System</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'> <span class="k">public</span> <span class="k">class</span> <span class="nc">Account</span>
</span><span class='line'> <span class="p">{</span>
</span><span class='line'> <span class="k">public</span> <span class="n">Guid</span> <span class="n">Id</span> <span class="p">{</span> <span class="k">get</span><span class="p">;</span> <span class="k">set</span><span class="p">;</span> <span class="p">}</span>
</span><span class='line'>
</span><span class='line'> <span class="k">public</span> <span class="kt">string</span> <span class="n">Name</span> <span class="p">{</span> <span class="k">get</span><span class="p">;</span> <span class="k">set</span><span class="p">;</span> <span class="p">}</span>
</span><span class='line'>
</span><span class='line'> <span class="k">public</span> <span class="kt">string</span> <span class="n">Email</span> <span class="p">{</span> <span class="k">get</span><span class="p">;</span> <span class="k">set</span><span class="p">;</span> <span class="p">}</span>
</span><span class='line'>
</span><span class='line'> <span class="k">public</span> <span class="kt">bool</span> <span class="n">IsActive</span> <span class="p">{</span> <span class="k">get</span><span class="p">;</span> <span class="k">set</span><span class="p">;</span> <span class="p">}</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>
<ul>
<li>In <code>RestApiWeb.Models</code> create <code>Tenant.cs</code> class:</li>
</ul>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
</pre></td><td class='code'><pre><code class='csharp'><span class='line'><span class="k">namespace</span> <span class="nn">RestApiWeb.Models</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'> <span class="k">using</span> <span class="nn">System</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'> <span class="k">public</span> <span class="k">class</span> <span class="nc">Tenant</span>
</span><span class='line'> <span class="p">{</span>
</span><span class='line'> <span class="k">public</span> <span class="n">Guid</span> <span class="n">Id</span> <span class="p">{</span> <span class="k">get</span><span class="p">;</span> <span class="k">set</span><span class="p">;</span> <span class="p">}</span>
</span><span class='line'>
</span><span class='line'> <span class="k">public</span> <span class="kt">int</span> <span class="n">Code</span> <span class="p">{</span> <span class="k">get</span><span class="p">;</span> <span class="k">set</span><span class="p">;</span> <span class="p">}</span>
</span><span class='line'>
</span><span class='line'> <span class="k">public</span> <span class="kt">string</span> <span class="n">Name</span> <span class="p">{</span> <span class="k">get</span><span class="p">;</span> <span class="k">set</span><span class="p">;</span> <span class="p">}</span>
</span><span class='line'>
</span><span class='line'> <span class="k">public</span> <span class="kt">string</span> <span class="n">Description</span> <span class="p">{</span> <span class="k">get</span><span class="p">;</span> <span class="k">set</span><span class="p">;</span> <span class="p">}</span>
</span><span class='line'>
</span><span class='line'> <span class="k">public</span> <span class="kt">bool</span> <span class="n">IsActive</span> <span class="p">{</span> <span class="k">get</span><span class="p">;</span> <span class="k">set</span><span class="p">;</span> <span class="p">}</span>
</span><span class='line'>
</span><span class='line'> <span class="k">public</span> <span class="n">Guid</span> <span class="n">AccountId</span> <span class="p">{</span> <span class="k">get</span><span class="p">;</span> <span class="k">set</span><span class="p">;</span> <span class="p">}</span>
</span><span class='line'>
</span><span class='line'> <span class="k">public</span> <span class="n">Account</span> <span class="n">Account</span> <span class="p">{</span> <span class="k">get</span><span class="p">;</span> <span class="k">set</span><span class="p">;</span> <span class="p">}</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>
<blockquote> Github qualtrak/RestApiAzure commit:
<br/>
<a href="https://github.com/qualtrak/RestApiAzure/commit/cdddf56734624342c646c899c369d536891cf4fc" target="_blank">
Created Models: Account and Tenant.</a>
</blockquote>
<h2>Create the “RestApiAzure” Db Context</h2>
<p>Create <code>RestApiWeb.Context</code> folder and <code>RestApiAzureContext.cs</code> class as a <em>Entity Framework</em>
<code>DbContext</code>:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
</pre></td><td class='code'><pre><code class='csharp'><span class='line'><span class="k">namespace</span> <span class="nn">RestApiWeb.Context</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'> <span class="k">using</span> <span class="nn">RestApiWeb.Models</span><span class="p">;</span>
</span><span class='line'> <span class="k">using</span> <span class="nn">System.Data.Entity</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'> <span class="k">public</span> <span class="k">class</span> <span class="nc">RestApiAzureContext</span> <span class="p">:</span> <span class="n">DbContext</span>
</span><span class='line'> <span class="p">{</span>
</span><span class='line'> <span class="k">public</span> <span class="nf">RestApiAzureContext</span><span class="p">()</span>
</span><span class='line'> <span class="p">:</span> <span class="k">base</span><span class="p">(</span><span class="s">"RestApiAzure"</span><span class="p">)</span>
</span><span class='line'> <span class="p">{</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'>
</span><span class='line'> <span class="k">public</span> <span class="n">DbSet</span><span class="p"><</span><span class="n">Account</span><span class="p">></span> <span class="n">Accounts</span> <span class="p">{</span> <span class="k">get</span><span class="p">;</span> <span class="k">set</span><span class="p">;</span> <span class="p">}</span>
</span><span class='line'>
</span><span class='line'> <span class="k">public</span> <span class="n">DbSet</span><span class="p"><</span><span class="n">Tenant</span><span class="p">></span> <span class="n">Tenants</span> <span class="p">{</span> <span class="k">get</span><span class="p">;</span> <span class="k">set</span><span class="p">;</span> <span class="p">}</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>
<p>The connection string name “RestApiAzure” is declared in <code>base</code>. This connection string
will be created in <code>web.config</code> in next chapter.</p>
<blockquote> Github qualtrak/RestApiAzure commit:
<br/>
<a href="https://github.com/qualtrak/RestApiAzure/commit/9994d211bfae7b46c7c929aa879f675abc1a764b" target="_blank">
Created RestApiAzure Entity Framework Context.</a>
</blockquote>
<h2>Adding SQL Azure connecting string to web.config</h2>
<ul>
<li>Login to a <a href="https://manage.windowsazure.com">Windows Azure Portal</a>.</li>
<li>In left pane cluck <em>SQL Database</em>.</li>
<li>Click the database that you want connection string, in this case <code>qcoach</code> t get details of database.</li>
<li>From right <em>quick glance</em> sidebar click on <em>Show connection strings</em>.</li>
</ul>
<p><img src="http://qualtrak.github.com/images/01-06.png"></p>
<ul>
<li>In Visual Studio project <code>RestApiWeb</code> open <code>web.config</code> and add a <code><connectionStrings></code> named <em>RestApiAzure</em>:</li>
</ul>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='xml'><span class='line'><span class="nt"><add</span> <span class="na">name=</span><span class="s">"RestApiAzure"</span>
</span><span class='line'> <span class="na">providerName=</span><span class="s">"System.Data.SqlClient"</span>
</span><span class='line'> <span class="na">connectionString=</span><span class="s">"Server=qh9pkltozj.database.windows.net,1433;Database=qcoach;User ID=xajler@qh9pkltozj;Password=abc123$%;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;"</span><span class="nt">/></span>
</span></code></pre></td></tr></table></div></figure>
<blockquote> Github qualtrak/RestApiAzure commit:
<br/>
<a href="https://github.com/qualtrak/RestApiAzure/commit/a3c3216d837057613d2d0cee75c7c8bb0beaaaa2" target="_blank">
Added SQL Azure connection string to web.config.</a>
</blockquote>
<h2>Create Migration for Account and Tenant, added seeded data and create schema</h2>
<ul>
<li>Open <code>Package Manager Console</code> (<em>View</em> | <em>Other Windows</em>).</li>
<li>Enable migrations with:</li>
</ul>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='xml'><span class='line'>PM> Enable-Migrations
</span><span class='line'>Checking if the context targets an existing database...
</span><span class='line'>Code First Migrations enabled for project RestApiWeb.
</span></code></pre></td></tr></table></div></figure>
<ul>
<li>There should be new folder <code>RestApiWeb.Migrations</code> with <code>Configuration.cs</code> class and <code>Seed</code> method.</li>
<li>Replace the default commented code in <code>Seed</code> method with:</li>
</ul>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
</pre></td><td class='code'><pre><code class='csharp'><span class='line'><span class="k">protected</span> <span class="k">override</span> <span class="k">void</span> <span class="nf">Seed</span><span class="p">(</span><span class="n">RestApiWeb</span><span class="p">.</span><span class="n">Context</span><span class="p">.</span><span class="n">RestApiAzureContext</span> <span class="n">context</span><span class="p">)</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'> <span class="n">Account</span> <span class="n">account</span> <span class="p">=</span> <span class="k">new</span> <span class="n">Account</span> <span class="p">{</span> <span class="n">Id</span> <span class="p">=</span> <span class="n">Guid</span><span class="p">.</span><span class="n">NewGuid</span><span class="p">(),</span> <span class="n">Name</span> <span class="p">=</span> <span class="s">"Account One"</span><span class="p">,</span> <span class="n">Email</span> <span class="p">=</span> <span class="s">"[email protected]"</span><span class="p">,</span> <span class="n">IsActive</span> <span class="p">=</span> <span class="k">true</span> <span class="p">};</span>
</span><span class='line'>
</span><span class='line'> <span class="n">context</span><span class="p">.</span><span class="n">Accounts</span><span class="p">.</span><span class="n">AddOrUpdate</span><span class="p">(</span><span class="n">account</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'> <span class="n">context</span><span class="p">.</span><span class="n">Tenants</span><span class="p">.</span><span class="n">AddOrUpdate</span><span class="p">(</span>
</span><span class='line'> <span class="k">new</span> <span class="n">Tenant</span> <span class="p">{</span> <span class="n">Id</span> <span class="p">=</span> <span class="n">Guid</span><span class="p">.</span><span class="n">NewGuid</span><span class="p">(),</span> <span class="n">Code</span> <span class="p">=</span> <span class="m">1000</span><span class="p">,</span> <span class="n">Name</span> <span class="p">=</span> <span class="s">"Tenant 1000"</span><span class="p">,</span> <span class="n">Description</span> <span class="p">=</span> <span class="s">"The Tenant Thousand"</span><span class="p">,</span> <span class="n">Account</span> <span class="p">=</span> <span class="n">account</span> <span class="p">}</span>
</span><span class='line'> <span class="p">);</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>
<ul>
<li>To <code>Seed</code> you’ll need the <code>using RestApiWeb.Models;</code> for Account and Tenant medel.</li>
<li>If there is no <em>Initial Migration</em> created. Create migrations for Account and Tenant.</li>
<li>In <code>Package Manager Console</code> start command to create Account and Tenant migration:</li>
</ul>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='csharp'><span class='line'><span class="n">PM</span><span class="p">></span> <span class="n">Add</span><span class="p">-</span><span class="n">Migration</span> <span class="n">InitialCreate</span>
</span><span class='line'><span class="n">Scaffolding</span> <span class="n">migration</span> <span class="err">'</span><span class="n">InitialCreate</span><span class="err">'</span><span class="p">.</span>
</span><span class='line'><span class="n">The</span> <span class="n">Designer</span> <span class="n">Code</span> <span class="k">for</span> <span class="k">this</span> <span class="n">migration</span> <span class="n">file</span> <span class="n">includes</span> <span class="n">a</span> <span class="n">snapshot</span> <span class="n">of</span> <span class="n">your</span> <span class="n">current</span> <span class="n">Code</span> <span class="n">First</span> <span class="n">model</span><span class="p">.</span> <span class="n">This</span> <span class="n">snapshot</span> <span class="k">is</span> <span class="n">used</span> <span class="n">to</span> <span class="n">calculate</span> <span class="n">the</span> <span class="n">changes</span> <span class="n">to</span> <span class="n">your</span> <span class="n">model</span> <span class="n">when</span> <span class="n">you</span> <span class="n">scaffold</span> <span class="n">the</span> <span class="n">next</span> <span class="n">migration</span><span class="p">.</span> <span class="n">If</span> <span class="n">you</span> <span class="n">make</span> <span class="n">additional</span> <span class="n">changes</span> <span class="n">to</span> <span class="n">your</span> <span class="n">model</span> <span class="n">that</span> <span class="n">you</span> <span class="n">want</span> <span class="n">to</span> <span class="n">include</span> <span class="k">in</span> <span class="k">this</span> <span class="n">migration</span><span class="p">,</span> <span class="n">then</span> <span class="n">you</span> <span class="n">can</span> <span class="n">re</span><span class="p">-</span><span class="n">scaffold</span> <span class="n">it</span> <span class="n">by</span> <span class="n">running</span> <span class="err">'</span><span class="n">Add</span><span class="p">-</span><span class="n">Migration</span> <span class="m">201210151549095</span><span class="n">_InitialCreate</span><span class="err">'</span> <span class="n">again</span><span class="p">.</span>
</span></code></pre></td></tr></table></div></figure>
<ul>
<li>In Migration folder now it should be a <code>201210151549095_InitialCreate.cs</code> migration file.</li>
<li>In <code>Package Manager Console</code> run:</li>
</ul>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='csharp'><span class='line'><span class="n">PM</span><span class="p">></span> <span class="n">Update</span><span class="p">-</span><span class="n">Database</span>
</span><span class='line'><span class="n">Specify</span> <span class="n">the</span> <span class="err">'</span><span class="p">-</span><span class="n">Verbose</span><span class="err">'</span> <span class="n">flag</span> <span class="n">to</span> <span class="n">view</span> <span class="n">the</span> <span class="n">SQL</span> <span class="n">statements</span> <span class="n">being</span> <span class="n">applied</span> <span class="n">to</span> <span class="n">the</span> <span class="n">target</span> <span class="n">database</span><span class="p">.</span>
</span><span class='line'><span class="n">Applying</span> <span class="n">code</span><span class="p">-</span><span class="n">based</span> <span class="n">migrations</span><span class="p">:</span> <span class="p">[</span><span class="m">201210151549095</span><span class="n">_InitialCreate</span><span class="p">].</span>
</span><span class='line'><span class="n">Applying</span> <span class="n">code</span><span class="p">-</span><span class="n">based</span> <span class="n">migration</span><span class="p">:</span> <span class="m">201210151549095</span><span class="n">_InitialCreate</span><span class="p">.</span>
</span><span class='line'><span class="n">Running</span> <span class="n">Seed</span> <span class="n">method</span><span class="p">.</span>
</span></code></pre></td></tr></table></div></figure>
<ul>
<li>Connect with SQL Server Management Studio to SQL Azure and you should get Accounts and Tenants tables.</li>
</ul>
<p><img src="http://qualtrak.github.com/images/01-07.png"></p>
<blockquote> Github qualtrak/RestApiAzure commit:
<br/>
<a href="https://github.com/qualtrak/RestApiAzure/commit/7a345c8be4602d0bcbb49444259189adcfb8b886" target="_blank">
Enabled Migrations and created initial Migration with Seeded data.</a>
</blockquote>
<h2>Creating Wep Api Controllers</h2>
<p>We will create full CRUD (Create-Read-Update-Delete) api for Accounts and Tenants resources.</p>
<p><strong>Please note that you should never ever want have this kind of code in real application, this
code is created only to test the Azure Cloud Services, and have as less as possible business
logic and layer abstractions!!!</strong></p>
<ul>
<li><p>First delete the created by web api <code>Controllers.ValuesController.cs</code>.</p></li>
<li><p>Create in <code>Controllers</code> folder the <code>AccountsController</code> and add the code:</p></li>
</ul>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
<span class='line-number'>37</span>
<span class='line-number'>38</span>
<span class='line-number'>39</span>
<span class='line-number'>40</span>
<span class='line-number'>41</span>
<span class='line-number'>42</span>
<span class='line-number'>43</span>
<span class='line-number'>44</span>
<span class='line-number'>45</span>
<span class='line-number'>46</span>
<span class='line-number'>47</span>
<span class='line-number'>48</span>
<span class='line-number'>49</span>
<span class='line-number'>50</span>
<span class='line-number'>51</span>
<span class='line-number'>52</span>
<span class='line-number'>53</span>
<span class='line-number'>54</span>
<span class='line-number'>55</span>
<span class='line-number'>56</span>
<span class='line-number'>57</span>
<span class='line-number'>58</span>
<span class='line-number'>59</span>
<span class='line-number'>60</span>
<span class='line-number'>61</span>
<span class='line-number'>62</span>
<span class='line-number'>63</span>
<span class='line-number'>64</span>
<span class='line-number'>65</span>
<span class='line-number'>66</span>
<span class='line-number'>67</span>
<span class='line-number'>68</span>
<span class='line-number'>69</span>
<span class='line-number'>70</span>
<span class='line-number'>71</span>
<span class='line-number'>72</span>
<span class='line-number'>73</span>
<span class='line-number'>74</span>
<span class='line-number'>75</span>
<span class='line-number'>76</span>
<span class='line-number'>77</span>
<span class='line-number'>78</span>
<span class='line-number'>79</span>
<span class='line-number'>80</span>
<span class='line-number'>81</span>
<span class='line-number'>82</span>
<span class='line-number'>83</span>
<span class='line-number'>84</span>
</pre></td><td class='code'><pre><code class='csharp'><span class='line'><span class="k">namespace</span> <span class="nn">RestApiWeb.Controllers</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'> <span class="k">using</span> <span class="nn">RestApiWeb.Context</span><span class="p">;</span>
</span><span class='line'> <span class="k">using</span> <span class="nn">RestApiWeb.Models</span><span class="p">;</span>
</span><span class='line'> <span class="k">using</span> <span class="nn">System</span><span class="p">;</span>
</span><span class='line'> <span class="k">using</span> <span class="nn">System.Collections.Generic</span><span class="p">;</span>
</span><span class='line'> <span class="k">using</span> <span class="nn">System.Diagnostics</span><span class="p">;</span>
</span><span class='line'> <span class="k">using</span> <span class="nn">System.Linq</span><span class="p">;</span>
</span><span class='line'> <span class="k">using</span> <span class="nn">System.Web.Http</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'> <span class="k">public</span> <span class="k">class</span> <span class="nc">AccountsController</span> <span class="p">:</span> <span class="n">ApiController</span>
</span><span class='line'> <span class="p">{</span>
</span><span class='line'> <span class="k">private</span> <span class="k">readonly</span> <span class="n">RestApiAzureContext</span> <span class="n">_context</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'> <span class="k">public</span> <span class="nf">AccountsController</span><span class="p">()</span>
</span><span class='line'> <span class="p">{</span>
</span><span class='line'> <span class="k">this</span><span class="p">.</span><span class="n">_context</span> <span class="p">=</span> <span class="k">new</span> <span class="n">RestApiAzureContext</span><span class="p">();</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'>
</span><span class='line'> <span class="k">public</span> <span class="n">IEnumerable</span><span class="p"><</span><span class="n">Account</span><span class="p">></span> <span class="n">Get</span><span class="p">()</span>
</span><span class='line'> <span class="p">{</span>
</span><span class='line'> <span class="n">ICollection</span><span class="p"><</span><span class="n">Account</span><span class="p">></span> <span class="n">result</span> <span class="p">=</span> <span class="k">this</span><span class="p">.</span><span class="n">_context</span><span class="p">.</span><span class="n">Accounts</span><span class="p">.</span><span class="n">ToList</span><span class="p">();</span>
</span><span class='line'> <span class="k">return</span> <span class="n">result</span><span class="p">;</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'>
</span><span class='line'> <span class="k">public</span> <span class="n">Account</span> <span class="nf">Get</span><span class="p">(</span><span class="n">Guid</span> <span class="n">id</span><span class="p">)</span>
</span><span class='line'> <span class="p">{</span>
</span><span class='line'> <span class="n">Account</span> <span class="n">result</span> <span class="p">=</span> <span class="k">this</span><span class="p">.</span><span class="n">_context</span><span class="p">.</span><span class="n">Accounts</span><span class="p">.</span><span class="n">SingleOrDefault</span><span class="p">(</span><span class="n">x</span> <span class="p">=></span> <span class="n">x</span><span class="p">.</span><span class="n">Id</span> <span class="p">==</span> <span class="n">id</span><span class="p">);</span>
</span><span class='line'> <span class="k">return</span> <span class="n">result</span><span class="p">;</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'>
</span><span class='line'> <span class="k">public</span> <span class="n">Account</span> <span class="nf">Post</span><span class="p">(</span><span class="n">Account</span> <span class="n">account</span><span class="p">)</span>
</span><span class='line'> <span class="p">{</span>
</span><span class='line'> <span class="k">try</span>
</span><span class='line'> <span class="p">{</span>
</span><span class='line'> <span class="n">account</span><span class="p">.</span><span class="n">Id</span> <span class="p">=</span> <span class="n">Guid</span><span class="p">.</span><span class="n">NewGuid</span><span class="p">();</span>
</span><span class='line'> <span class="k">this</span><span class="p">.</span><span class="n">_context</span><span class="p">.</span><span class="n">Accounts</span><span class="p">.</span><span class="n">Add</span><span class="p">(</span><span class="n">account</span><span class="p">);</span>
</span><span class='line'> <span class="k">this</span><span class="p">.</span><span class="n">_context</span><span class="p">.</span><span class="n">SaveChanges</span><span class="p">();</span>
</span><span class='line'> <span class="k">return</span> <span class="n">account</span><span class="p">;</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'> <span class="k">catch</span><span class="p">(</span><span class="n">Exception</span> <span class="n">exception</span><span class="p">)</span>
</span><span class='line'> <span class="p">{</span>
</span><span class='line'> <span class="n">Debug</span><span class="p">.</span><span class="n">WriteLine</span><span class="p">(</span><span class="n">exception</span><span class="p">.</span><span class="n">Message</span> <span class="p">+</span> <span class="s">"\n\n"</span> <span class="p">+</span> <span class="n">exception</span><span class="p">.</span><span class="n">StackTrace</span><span class="p">);</span>
</span><span class='line'> <span class="k">return</span> <span class="n">account</span><span class="p">;</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'>
</span><span class='line'> <span class="k">public</span> <span class="n">Account</span> <span class="nf">Put</span><span class="p">(</span><span class="n">Guid</span> <span class="n">id</span><span class="p">,</span> <span class="p">[</span><span class="n">FromBody</span><span class="p">]</span><span class="n">Account</span> <span class="n">account</span><span class="p">)</span>
</span><span class='line'> <span class="p">{</span>
</span><span class='line'> <span class="k">try</span>
</span><span class='line'> <span class="p">{</span>
</span><span class='line'> <span class="n">Account</span> <span class="n">result</span> <span class="p">=</span> <span class="k">this</span><span class="p">.</span><span class="n">_context</span><span class="p">.</span><span class="n">Accounts</span><span class="p">.</span><span class="n">SingleOrDefault</span><span class="p">(</span><span class="n">x</span> <span class="p">=></span> <span class="n">x</span><span class="p">.</span><span class="n">Id</span> <span class="p">==</span> <span class="n">id</span><span class="p">);</span>
</span><span class='line'> <span class="n">result</span><span class="p">.</span><span class="n">Name</span> <span class="p">=</span> <span class="n">account</span><span class="p">.</span><span class="n">Name</span><span class="p">;</span>
</span><span class='line'> <span class="n">result</span><span class="p">.</span><span class="n">Email</span> <span class="p">=</span> <span class="n">account</span><span class="p">.</span><span class="n">Email</span><span class="p">;</span>
</span><span class='line'> <span class="n">result</span><span class="p">.</span><span class="n">IsActive</span> <span class="p">=</span> <span class="n">account</span><span class="p">.</span><span class="n">IsActive</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'> <span class="k">this</span><span class="p">.</span><span class="n">_context</span><span class="p">.</span><span class="n">SaveChanges</span><span class="p">();</span>
</span><span class='line'>
</span><span class='line'> <span class="k">return</span> <span class="n">result</span><span class="p">;</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'> <span class="k">catch</span> <span class="p">(</span><span class="n">Exception</span> <span class="n">exception</span><span class="p">)</span>
</span><span class='line'> <span class="p">{</span>
</span><span class='line'> <span class="n">Debug</span><span class="p">.</span><span class="n">WriteLine</span><span class="p">(</span><span class="n">exception</span><span class="p">.</span><span class="n">Message</span> <span class="p">+</span> <span class="s">"\n\n"</span> <span class="p">+</span> <span class="n">exception</span><span class="p">.</span><span class="n">StackTrace</span><span class="p">);</span>
</span><span class='line'> <span class="k">return</span> <span class="n">account</span><span class="p">;</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'>
</span><span class='line'> <span class="k">public</span> <span class="kt">bool</span> <span class="nf">Delete</span><span class="p">(</span><span class="n">Guid</span> <span class="n">id</span><span class="p">)</span>
</span><span class='line'> <span class="p">{</span>
</span><span class='line'> <span class="k">try</span>
</span><span class='line'> <span class="p">{</span>
</span><span class='line'> <span class="n">Account</span> <span class="n">account</span> <span class="p">=</span> <span class="k">this</span><span class="p">.</span><span class="n">_context</span><span class="p">.</span><span class="n">Accounts</span><span class="p">.</span><span class="n">SingleOrDefault</span><span class="p">(</span><span class="n">x</span> <span class="p">=></span> <span class="n">x</span><span class="p">.</span><span class="n">Id</span> <span class="p">==</span> <span class="n">id</span><span class="p">);</span>
</span><span class='line'> <span class="k">this</span><span class="p">.</span><span class="n">_context</span><span class="p">.</span><span class="n">Accounts</span><span class="p">.</span><span class="n">Remove</span><span class="p">(</span><span class="n">account</span><span class="p">);</span>
</span><span class='line'> <span class="k">this</span><span class="p">.</span><span class="n">_context</span><span class="p">.</span><span class="n">SaveChanges</span><span class="p">();</span>
</span><span class='line'> <span class="k">return</span> <span class="k">true</span><span class="p">;</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'> <span class="k">catch</span> <span class="p">(</span><span class="n">Exception</span> <span class="n">exception</span><span class="p">)</span>
</span><span class='line'> <span class="p">{</span>
</span><span class='line'> <span class="n">Debug</span><span class="p">.</span><span class="n">WriteLine</span><span class="p">(</span><span class="n">exception</span><span class="p">.</span><span class="n">Message</span> <span class="p">+</span> <span class="s">"\n\n"</span> <span class="p">+</span> <span class="n">exception</span><span class="p">.</span><span class="n">StackTrace</span><span class="p">);</span>
</span><span class='line'> <span class="k">return</span> <span class="k">false</span><span class="p">;</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>
<ul>
<li>Create in <code>Controllers</code> folder the <code>TenantsController</code> and add the code:</li>
</ul>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
<span class='line-number'>37</span>
<span class='line-number'>38</span>
<span class='line-number'>39</span>
<span class='line-number'>40</span>
<span class='line-number'>41</span>
<span class='line-number'>42</span>
<span class='line-number'>43</span>
<span class='line-number'>44</span>
<span class='line-number'>45</span>
<span class='line-number'>46</span>
<span class='line-number'>47</span>
<span class='line-number'>48</span>
<span class='line-number'>49</span>
<span class='line-number'>50</span>
<span class='line-number'>51</span>
<span class='line-number'>52</span>
<span class='line-number'>53</span>
<span class='line-number'>54</span>
<span class='line-number'>55</span>
<span class='line-number'>56</span>
<span class='line-number'>57</span>
<span class='line-number'>58</span>
<span class='line-number'>59</span>
<span class='line-number'>60</span>
<span class='line-number'>61</span>
<span class='line-number'>62</span>
<span class='line-number'>63</span>
<span class='line-number'>64</span>
<span class='line-number'>65</span>
<span class='line-number'>66</span>
<span class='line-number'>67</span>
<span class='line-number'>68</span>
<span class='line-number'>69</span>
<span class='line-number'>70</span>
<span class='line-number'>71</span>
<span class='line-number'>72</span>
<span class='line-number'>73</span>
<span class='line-number'>74</span>
<span class='line-number'>75</span>
<span class='line-number'>76</span>
<span class='line-number'>77</span>
<span class='line-number'>78</span>
<span class='line-number'>79</span>
<span class='line-number'>80</span>
<span class='line-number'>81</span>
<span class='line-number'>82</span>
<span class='line-number'>83</span>
<span class='line-number'>84</span>
<span class='line-number'>85</span>
<span class='line-number'>86</span>
</pre></td><td class='code'><pre><code class='csharp'><span class='line'><span class="k">namespace</span> <span class="nn">RestApiWeb.Controllers</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'> <span class="k">using</span> <span class="nn">RestApiWeb.Context</span><span class="p">;</span>
</span><span class='line'> <span class="k">using</span> <span class="nn">RestApiWeb.Models</span><span class="p">;</span>
</span><span class='line'> <span class="k">using</span> <span class="nn">System</span><span class="p">;</span>
</span><span class='line'> <span class="k">using</span> <span class="nn">System.Collections.Generic</span><span class="p">;</span>
</span><span class='line'> <span class="k">using</span> <span class="nn">System.Diagnostics</span><span class="p">;</span>
</span><span class='line'> <span class="k">using</span> <span class="nn">System.Linq</span><span class="p">;</span>
</span><span class='line'> <span class="k">using</span> <span class="nn">System.Web.Http</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'> <span class="k">public</span> <span class="k">class</span> <span class="nc">TenantsController</span> <span class="p">:</span> <span class="n">ApiController</span>
</span><span class='line'> <span class="p">{</span>
</span><span class='line'> <span class="k">private</span> <span class="k">readonly</span> <span class="n">RestApiAzureContext</span> <span class="n">_context</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'> <span class="k">public</span> <span class="nf">TenantsController</span><span class="p">()</span>
</span><span class='line'> <span class="p">{</span>
</span><span class='line'> <span class="k">this</span><span class="p">.</span><span class="n">_context</span> <span class="p">=</span> <span class="k">new</span> <span class="n">RestApiAzureContext</span><span class="p">();</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'>
</span><span class='line'> <span class="k">public</span> <span class="n">IEnumerable</span><span class="p"><</span><span class="n">Tenant</span><span class="p">></span> <span class="n">Get</span><span class="p">()</span>
</span><span class='line'> <span class="p">{</span>
</span><span class='line'> <span class="n">ICollection</span><span class="p"><</span><span class="n">Tenant</span><span class="p">></span> <span class="n">result</span> <span class="p">=</span> <span class="k">this</span><span class="p">.</span><span class="n">_context</span><span class="p">.</span><span class="n">Tenants</span><span class="p">.</span><span class="n">ToList</span><span class="p">();</span>
</span><span class='line'> <span class="k">return</span> <span class="n">result</span><span class="p">;</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'>
</span><span class='line'> <span class="k">public</span> <span class="n">Tenant</span> <span class="nf">Get</span><span class="p">(</span><span class="n">Guid</span> <span class="n">id</span><span class="p">)</span>
</span><span class='line'> <span class="p">{</span>
</span><span class='line'> <span class="n">Tenant</span> <span class="n">result</span> <span class="p">=</span> <span class="k">this</span><span class="p">.</span><span class="n">_context</span><span class="p">.</span><span class="n">Tenants</span><span class="p">.</span><span class="n">SingleOrDefault</span><span class="p">(</span><span class="n">x</span> <span class="p">=></span> <span class="n">x</span><span class="p">.</span><span class="n">Id</span> <span class="p">==</span> <span class="n">id</span><span class="p">);</span>
</span><span class='line'> <span class="k">return</span> <span class="n">result</span><span class="p">;</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'>
</span><span class='line'> <span class="k">public</span> <span class="n">Tenant</span> <span class="nf">Post</span><span class="p">(</span><span class="n">Tenant</span> <span class="n">tenant</span><span class="p">)</span>
</span><span class='line'> <span class="p">{</span>
</span><span class='line'> <span class="k">try</span>
</span><span class='line'> <span class="p">{</span>
</span><span class='line'> <span class="n">tenant</span><span class="p">.</span><span class="n">Id</span> <span class="p">=</span> <span class="n">Guid</span><span class="p">.</span><span class="n">NewGuid</span><span class="p">();</span>
</span><span class='line'> <span class="k">this</span><span class="p">.</span><span class="n">_context</span><span class="p">.</span><span class="n">Tenants</span><span class="p">.</span><span class="n">Add</span><span class="p">(</span><span class="n">tenant</span><span class="p">);</span>
</span><span class='line'> <span class="k">this</span><span class="p">.</span><span class="n">_context</span><span class="p">.</span><span class="n">SaveChanges</span><span class="p">();</span>
</span><span class='line'> <span class="k">return</span> <span class="n">tenant</span><span class="p">;</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'> <span class="k">catch</span><span class="p">(</span><span class="n">Exception</span> <span class="n">exception</span><span class="p">)</span>
</span><span class='line'> <span class="p">{</span>
</span><span class='line'> <span class="n">Debug</span><span class="p">.</span><span class="n">WriteLine</span><span class="p">(</span><span class="n">exception</span><span class="p">.</span><span class="n">Message</span> <span class="p">+</span> <span class="s">"\n\n"</span> <span class="p">+</span> <span class="n">exception</span><span class="p">.</span><span class="n">StackTrace</span><span class="p">);</span>
</span><span class='line'> <span class="k">return</span> <span class="n">tenant</span><span class="p">;</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'>
</span><span class='line'> <span class="k">public</span> <span class="n">Tenant</span> <span class="nf">Put</span><span class="p">(</span><span class="n">Guid</span> <span class="n">id</span><span class="p">,</span> <span class="p">[</span><span class="n">FromBody</span><span class="p">]</span><span class="n">Tenant</span> <span class="n">tenant</span><span class="p">)</span>
</span><span class='line'> <span class="p">{</span>
</span><span class='line'> <span class="k">try</span>
</span><span class='line'> <span class="p">{</span>
</span><span class='line'> <span class="n">Tenant</span> <span class="n">result</span> <span class="p">=</span> <span class="k">this</span><span class="p">.</span><span class="n">_context</span><span class="p">.</span><span class="n">Tenants</span><span class="p">.</span><span class="n">SingleOrDefault</span><span class="p">(</span><span class="n">x</span> <span class="p">=></span> <span class="n">x</span><span class="p">.</span><span class="n">Id</span> <span class="p">==</span> <span class="n">id</span><span class="p">);</span>
</span><span class='line'> <span class="n">result</span><span class="p">.</span><span class="n">Code</span> <span class="p">=</span> <span class="n">tenant</span><span class="p">.</span><span class="n">Code</span><span class="p">;</span>
</span><span class='line'> <span class="n">result</span><span class="p">.</span><span class="n">Name</span> <span class="p">=</span> <span class="n">tenant</span><span class="p">.</span><span class="n">Name</span><span class="p">;</span>
</span><span class='line'> <span class="n">result</span><span class="p">.</span><span class="n">Description</span> <span class="p">=</span> <span class="n">tenant</span><span class="p">.</span><span class="n">Description</span><span class="p">;</span>
</span><span class='line'> <span class="n">result</span><span class="p">.</span><span class="n">AccountId</span> <span class="p">=</span> <span class="n">tenant</span><span class="p">.</span><span class="n">AccountId</span><span class="p">;</span>
</span><span class='line'> <span class="n">result</span><span class="p">.</span><span class="n">IsActive</span> <span class="p">=</span> <span class="n">tenant</span><span class="p">.</span><span class="n">IsActive</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'> <span class="k">this</span><span class="p">.</span><span class="n">_context</span><span class="p">.</span><span class="n">SaveChanges</span><span class="p">();</span>
</span><span class='line'>
</span><span class='line'> <span class="k">return</span> <span class="n">result</span><span class="p">;</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'> <span class="k">catch</span> <span class="p">(</span><span class="n">Exception</span> <span class="n">exception</span><span class="p">)</span>
</span><span class='line'> <span class="p">{</span>
</span><span class='line'> <span class="n">Debug</span><span class="p">.</span><span class="n">WriteLine</span><span class="p">(</span><span class="n">exception</span><span class="p">.</span><span class="n">Message</span> <span class="p">+</span> <span class="s">"\n\n"</span> <span class="p">+</span> <span class="n">exception</span><span class="p">.</span><span class="n">StackTrace</span><span class="p">);</span>
</span><span class='line'> <span class="k">return</span> <span class="n">tenant</span><span class="p">;</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'>
</span><span class='line'> <span class="k">public</span> <span class="kt">bool</span> <span class="nf">Delete</span><span class="p">(</span><span class="n">Guid</span> <span class="n">id</span><span class="p">)</span>
</span><span class='line'> <span class="p">{</span>
</span><span class='line'> <span class="k">try</span>
</span><span class='line'> <span class="p">{</span>
</span><span class='line'> <span class="n">Tenant</span> <span class="n">tenant</span> <span class="p">=</span> <span class="k">this</span><span class="p">.</span><span class="n">_context</span><span class="p">.</span><span class="n">Tenants</span><span class="p">.</span><span class="n">SingleOrDefault</span><span class="p">(</span><span class="n">x</span> <span class="p">=></span> <span class="n">x</span><span class="p">.</span><span class="n">Id</span> <span class="p">==</span> <span class="n">id</span><span class="p">);</span>
</span><span class='line'> <span class="k">this</span><span class="p">.</span><span class="n">_context</span><span class="p">.</span><span class="n">Tenants</span><span class="p">.</span><span class="n">Remove</span><span class="p">(</span><span class="n">tenant</span><span class="p">);</span>
</span><span class='line'> <span class="k">this</span><span class="p">.</span><span class="n">_context</span><span class="p">.</span><span class="n">SaveChanges</span><span class="p">();</span>
</span><span class='line'> <span class="k">return</span> <span class="k">true</span><span class="p">;</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'> <span class="k">catch</span> <span class="p">(</span><span class="n">Exception</span> <span class="n">exception</span><span class="p">)</span>
</span><span class='line'> <span class="p">{</span>
</span><span class='line'> <span class="n">Debug</span><span class="p">.</span><span class="n">WriteLine</span><span class="p">(</span><span class="n">exception</span><span class="p">.</span><span class="n">Message</span> <span class="p">+</span> <span class="s">"\n\n"</span> <span class="p">+</span> <span class="n">exception</span><span class="p">.</span><span class="n">StackTrace</span><span class="p">);</span>
</span><span class='line'> <span class="k">return</span> <span class="k">false</span><span class="p">;</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>
<blockquote> Github qualtrak/RestApiAzure commit:
<br/>
<a href="https://github.com/qualtrak/RestApiAzure/commit/6834b452e034b01c2e2abfab7489a3bcb0a48eaf" target="_blank">
Created Account and Tenant full CRUD REST API Controllers.</a>
</blockquote>
<h2>Running and testing REST API</h2>
<ul>
<li>Debug and start the application (F5).</li>
<li>Use browser, fiddler or in my case Chrome extension Rest Console to test the API:</li>
<li>Set <em>Request URI</em>:</li>
</ul>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='csharp'><span class='line'><span class="n">http</span><span class="p">:</span><span class="c1">//localhost:16826/api/accounts</span>
</span></code></pre></td></tr></table></div></figure>
<ul>
<li>Set <em>Content-Type</em>:</li>
</ul>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='csharp'><span class='line'><span class="n">application</span><span class="p">/</span><span class="n">json</span>
</span></code></pre></td></tr></table></div></figure>
<ul>
<li>Run by clicking <em>GET</em> or <em>Send</em>. The output should be something like this:</li>
</ul>
<p><img src="http://qualtrak.github.com/images/01-08.png"></p>
<ul>
<li>Incidentally, it is encouraged to try and test all the Account and Tenant methods.</li>
</ul>
<h2>Conclusion</h2>
<p>This long tutorial provides a knowledge to create a simple REST API and use SQL Azure as
database to connect to.</p>
<p>In second part, the deployment of applicatin will be provided as <em>Web Role</em> in Azure <em>Cloud Services</em>.</p>
<div class='octopress-authorbox'>
<div class="author-pic">
<img src="http://www.gravatar.com/avatar/4e0d581521137646abac17331917e95e" alt="Kornelije Sajler" />
</div>
<div class="author-about">
<h3>Kornelije Sajler</h3>
<p>The Lead Software Developer at Qualtrak</p>
<ul class="author-links">
<li>
<a href="http://twitter.com/xajler">@xajler</a>
</li>
<li>
<a href="http://blog.metaintellect.com/">Blog</a>
</li>
</ul>
</div>
</div>
]]></content>
</entry>
</feed>