forked from marfarma/ServiceStack.Hello
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefault.htm
625 lines (619 loc) · 43.7 KB
/
default.htm
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
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Tutorial - Creating a Hello World ServiceStack Web Service</title>
<link rel="stylesheet" type="text/css" href="Content/Css/default.css" />
</head>
<body>
<a href="http://www.servicestack.net" style="display: block; position: absolute;
top: 5px; left: 10px;">
<img src="http://servicestack.net/icon-home.jpg" alt="ServiceStack Home" /></a>
<div id="header-links">
<a href="../ServiceStack.Hello/">Hello World</a> <a href="../Backbone.Todos/">Todos</a>
<a href="../RedisStackOverflow/">Redis StackOverflow</a> <a href="../RestFiles/">REST
Files</a> <a href="../ServiceStack.MovieRest/">REST Movies</a> <a href="../ServiceStack.Northwind/">
Northwind Database</a> <a href="../ServiceStack.Examples.Clients/">Ajax Client</a>
<a href="../ServiceStack.Examples.Clients/Soap12.aspx">Soap 1.2</a>
</div>
<a class="lnk-src" href="https://github.com/ServiceStack/ServiceStack">
<img src="Content/Images/btn-github.png" width="186" height="84" /></a>
<h1 class="title">
Creating a Web service from scratch with Service Stack</h1>
<p>
This tutorial will go through and develop and call a simple Hello world Web Service
from scratch using <a href="http://www.servicestack.net">Service Stack</a> and VS.NET.
</p>
<a name="nuget"></a>
<h2>
Installing via NuGet</h2>
<p>
If you have NuGet you can skip steps 1 - 3 below by installing one of the NuGet
Host packages below:
</p>
<h3>
Install ServiceStack Only at root path <b style="font-size: 20px; line-height: 20px">
/</b>
</h3>
<p>
Create an empty ASP.NET Web Application and
</p>
<div class="nuget-badge">
<p>
<pre>PM> Install-Package ServiceStack.Host.AspNet</pre>
</p>
</div>
<h3>
Install ServiceStack Side-by-Side with ASP.NET MVC at custom path <b style="font-size: 20px;
line-height: 20px">/api</b>
</h3>
<p>
Create an empty ASP.NET MVC Web Application and
</p>
<div class="nuget-badge">
<p>
<pre>PM> Install-Package ServiceStack.Host.Mvc</pre>
</p>
</div>
<h3>
Completing the NuGet Installation</h3>
<p>
The NuGet package provides an AppHost template in your project at <b>App_Start\AppHost.cs</b>
and some <b>App_Start\WebServiceExamples.cs</b> to get you started.
</p>
<p>
After looking at the <b>README.txt</b> - <b>Skip the Manual Installation instructions
below</b> and start exploring ServiceStack features at:
</p>
<p>
<h3>
<a href="#view-webservice">Viewing your Web Service >></a></h3>
</p>
<a name="manual"></a>
<h2>
Install Manually - 1. Creating your application</h2>
<p>
Create a new ASP.NET Web Application by going into Visual Studio and selecting <strong>
File -> New -> Project</strong> on the File menu.
</p>
<p>
<img src="Content/Images/NewProject.png" alt="New ASP.NET Project" width="776" height="552" /></p>
<h2>
2. Configuring Service Stack to run in your application</h2>
<p>
Add a reference to the ServiceStack dlls with:
</p>
<div class="nuget-badge">
<p>
<pre>PM> Install-Package ServiceStack</pre>
</p>
</div>
<p>
There are 2 supported options available to register ServiceStack in your ASP.NET
application <strong>Web.config</strong>:
</p>
<h3 class="hr">
<a name="rootpath"></a>a) You do not require an existing Web Framework - Host from
the root path: <b>/</b></h3>
<p>
You can host at the root path <b>/</b> when you don't need to use an existing Web
Framework with your ServiceStack web services (i.e. project only has static files)
</p>
<h4>
The configuration below allows you to host your webservices from the root path:
<b>/</b></h4>
<code class="web-config">
<span style="color:blue;"> <</span><span style="color:#a31515;">system.web</span><span style="color:blue;">></span>
<span style="color:blue;"> <</span><span style="color:#a31515;">httpHandlers</span><span style="color:blue;">></span>
<span style="color:blue;"> <</span><span style="color:#a31515;">add</span><span style="color:blue;"> </span><span style="color:red;">path</span><span style="color:blue;">=</span>"<span style="color:blue;">*</span>"<span style="color:blue;"> </span><span style="color:red;">type</span><span style="color:blue;">=</span>"<span style="color:blue;">ServiceStack.WebHost.Endpoints.ServiceStackHttpHandlerFactory, ServiceStack</span>"<span style="color:blue;"> </span><span style="color:red;">verb</span><span style="color:blue;">=</span>"<span style="color:blue;">*</span>"<span style="color:blue;">/></span>
<span style="color:blue;"> </</span><span style="color:#a31515;">httpHandlers</span><span style="color:blue;">></span>
<span style="color:blue;"> </</span><span style="color:#a31515;">system.web</span><span style="color:blue;">></span>
<span style="color:blue;"> <!--</span><span style="color:green;"> Required for IIS7 </span><span style="color:blue;">--></span>
<span style="color:blue;"> <</span><span style="color:#a31515;">system.webServer</span><span style="color:blue;">></span>
<span style="color:blue;"> <</span><span style="color:#a31515;">modules</span><span style="color:blue;"> </span><span style="color:red;">runAllManagedModulesForAllRequests</span><span style="color:blue;">=</span>"<span style="color:blue;">true</span>"<span style="color:blue;">/></span>
<span style="color:blue;"> <</span><span style="color:#a31515;">validation</span><span style="color:blue;"> </span><span style="color:red;">validateIntegratedModeConfiguration</span><span style="color:blue;">=</span>"<span style="color:blue;">false</span>"<span style="color:blue;"> /></span>
<span style="color:blue;"> <</span><span style="color:#a31515;">handlers</span><span style="color:blue;">></span>
<span style="color:blue;"> <</span><span style="color:#a31515;">add</span><span style="color:blue;"> </span><span style="color:red;">path</span><span style="color:blue;">=</span>"<span style="color:blue;">*</span>"<span style="color:blue;"> </span><span style="color:red;">name</span><span style="color:blue;">=</span>"<span style="color:blue;">ServiceStack.Factory</span>"<span style="color:blue;"> </span><span style="color:red;">type</span><span style="color:blue;">=</span>"<span style="color:blue;">ServiceStack.WebHost.Endpoints.ServiceStackHttpHandlerFactory, ServiceStack</span>"<span style="color:blue;"> </span><span style="color:red;">verb</span><span style="color:blue;">=</span>"<span style="color:blue;">*</span>"<span style="color:blue;"> </span><span style="color:red;">preCondition</span><span style="color:blue;">=</span>"<span style="color:blue;">integratedMode</span>"<span style="color:blue;"> </span><span style="color:red;">resourceType</span><span style="color:blue;">=</span>"<span style="color:blue;">Unspecified</span>"<span style="color:blue;"> </span><span style="color:red;">allowPathInfo</span><span style="color:blue;">=</span>"<span style="color:blue;">true</span>"<span style="color:blue;"> /></span>
<span style="color:blue;"> </</span><span style="color:#a31515;">handlers</span><span style="color:blue;">></span>
<span style="color:blue;"> </</span><span style="color:#a31515;">system.webServer</span><span style="color:blue;">></span>
</code>
<h3 class="hr">
<a name="custompath"></a>b) You want to use ServiceStack with an existing Web Framework
- Host web services at: <b>/custompath</b></h3>
<p>
In order to avoid conflicts with your existing ASP.NET web framework it is recommended
to host your ServiceStack web services at a custom path.<br />
This will allow you to use ServiceStack together with an existing web framework
e.g. <a href="http://www.asp.net/mvc">ASP.NET MVC 3</a> or <a href="http://code.google.com/p/fubumvc/">
FUBU MVC</a>, etc.
</p>
<h4>
The location configuration below hosts your webservices at custom path: <b>/api</b></h4>
<code class="web-config">
<span style="color:blue;"> <!--</span><span style="color:green;"> ServiceStack: Required to host at: /servicestack </span><span style="color:blue;">--></span>
<span style="color:blue;"> <</span><span style="color:#a31515;">location</span><span style="color:blue;"> </span><span style="color:red;">path</span><span style="color:blue;">=</span>"<span style="color:blue;">api</span>"<span style="color:blue;">></span>
<span style="color:blue;"> <</span><span style="color:#a31515;">system.web</span><span style="color:blue;">></span>
<span style="color:blue;"> <</span><span style="color:#a31515;">httpHandlers</span><span style="color:blue;">></span>
<span style="color:blue;"> <</span><span style="color:#a31515;">add</span><span style="color:blue;"> </span><span style="color:red;">path</span><span style="color:blue;">=</span>"<span style="color:blue;">*</span>"<span style="color:blue;"> </span><span style="color:red;">type</span><span style="color:blue;">=</span>"<span style="color:blue;">ServiceStack.WebHost.Endpoints.ServiceStackHttpHandlerFactory, ServiceStack</span>"<span style="color:blue;"> </span><span style="color:red;">verb</span><span style="color:blue;">=</span>"<span style="color:blue;">*</span>"<span style="color:blue;">/></span>
<span style="color:blue;"> </</span><span style="color:#a31515;">httpHandlers</span><span style="color:blue;">></span>
<span style="color:blue;"> </</span><span style="color:#a31515;">system.web</span><span style="color:blue;">></span>
<span style="color:blue;"> <!--</span><span style="color:green;"> Required for IIS7 </span><span style="color:blue;">--></span>
<span style="color:blue;"> <</span><span style="color:#a31515;">system.webServer</span><span style="color:blue;">></span>
<span style="color:blue;"> <</span><span style="color:#a31515;">modules</span><span style="color:blue;"> </span><span style="color:red;">runAllManagedModulesForAllRequests</span><span style="color:blue;">=</span>"<span style="color:blue;">true</span>"<span style="color:blue;">/></span>
<span style="color:blue;"> <</span><span style="color:#a31515;">validation</span><span style="color:blue;"> </span><span style="color:red;">validateIntegratedModeConfiguration</span><span style="color:blue;">=</span>"<span style="color:blue;">false</span>"<span style="color:blue;"> /></span>
<span style="color:blue;"> <</span><span style="color:#a31515;">handlers</span><span style="color:blue;">></span>
<span style="color:blue;"> <</span><span style="color:#a31515;">add</span><span style="color:blue;"> </span><span style="color:red;">path</span><span style="color:blue;">=</span>"<span style="color:blue;">*</span>"<span style="color:blue;"> </span><span style="color:red;">name</span><span style="color:blue;">=</span>"<span style="color:blue;">ServiceStack.Factory</span>"<span style="color:blue;"> </span><span style="color:red;">type</span><span style="color:blue;">=</span>"<span style="color:blue;">ServiceStack.WebHost.Endpoints.ServiceStackHttpHandlerFactory, ServiceStack</span>"<span style="color:blue;"> </span><span style="color:red;">verb</span><span style="color:blue;">=</span>"<span style="color:blue;">*</span>"<span style="color:blue;"> </span><span style="color:red;">preCondition</span><span style="color:blue;">=</span>"<span style="color:blue;">integratedMode</span>"<span style="color:blue;"> </span><span style="color:red;">resourceType</span><span style="color:blue;">=</span>"<span style="color:blue;">Unspecified</span>"<span style="color:blue;"> </span><span style="color:red;">allowPathInfo</span><span style="color:blue;">=</span>"<span style="color:blue;">true</span>"<span style="color:blue;"> /></span>
<span style="color:blue;"> </</span><span style="color:#a31515;">handlers</span><span style="color:blue;">></span>
<span style="color:blue;"> </</span><span style="color:#a31515;">system.webServer</span><span style="color:blue;">></span>
<span style="color:blue;"> </</span><span style="color:#a31515;">location</span><span style="color:blue;">></span>
<span style="color:blue;"> <!--</span><span style="color:green;"> Required for MONO </span><span style="color:blue;">--></span>
<span style="color:blue;"> <</span><span style="color:#a31515;">system.web</span><span style="color:blue;">></span>
<span style="color:blue;"> <</span><span style="color:#a31515;">httpHandlers</span><span style="color:blue;">></span>
<span style="color:blue;"> <</span><span style="color:#a31515;">add</span><span style="color:blue;"> </span><span style="color:red;">path</span><span style="color:blue;">=</span>"<span style="color:blue;">api*</span>"<span style="color:blue;"> </span><span style="color:red;">type</span><span style="color:blue;">=</span>"<span style="color:blue;">ServiceStack.WebHost.Endpoints.ServiceStackHttpHandlerFactory, ServiceStack</span>"<span style="color:blue;"> </span><span style="color:red;">verb</span><span style="color:blue;">=</span>"<span style="color:blue;">*</span>"<span style="color:blue;">/></span>
<span style="color:blue;"> </</span><span style="color:#a31515;">httpHandlers</span><span style="color:blue;">></span>
<span style="color:blue;"> </</span><span style="color:#a31515;">system.web</span><span style="color:blue;">></span>
<span style="color:blue;"> <!--</span><span style="color:green;"> Required for IIS7 </span><span style="color:blue;">--></span>
<span style="color:blue;"> <</span><span style="color:#a31515;">system.webServer</span><span style="color:blue;">></span>
<span style="color:blue;"> <!--</span><span style="color:green;"> ServiceStack: Required </span><span style="color:blue;">--></span>
<span style="color:blue;"> <</span><span style="color:#a31515;">validation</span><span style="color:blue;"> </span><span style="color:red;">validateIntegratedModeConfiguration</span><span style="color:blue;">=</span>"<span style="color:blue;">false</span>"<span style="color:blue;">/></span>
<span style="color:blue;"> </</span><span style="color:#a31515;">system.webServer</span><span style="color:blue;">></span>
</code>
<p>
Note: Due to limitations in IIS 6 - the <b>/custompath</b> must end with <b>.ashx</b>,
e.g: <b>path="api.ashx"</b>
</p>
<p>
To avoid conflicts with ASP.NET MVC add ignore rule in Global.asax RegisterRoutes
method e.g: <b>routes.IgnoreRoute ("api/{*pathInfo}");</b>
</p>
<a name="create"></a>
<h2>
3. Creating your first Web Service</h2>
<p>
For simplicity we will add the Web Service and the start up script all in the same
Global.asax.cs. To do this Right-click on your project and go
<br />
<strong>Add -> New Item</strong> then select the <strong>Global Application class</strong>.
</p>
<p>
Within the same file add the following code:
</p>
<h4>
1. Create the name of your Web Service (i.e. the Request DTO)</h4>
<code class="csharp"> <span style="color:gray;">///</span><span style="color:green;"> </span><span style="color:gray;"><summary></span>
<span style="color:gray;">///</span><span style="color:green;"> Define your ServiceStack web service request (i.e. the Request DTO).</span>
<span style="color:gray;">///</span><span style="color:green;"> </span><span style="color:gray;"></summary></span><span style="color:green;"> </span>
<span style="color:blue;">public</span> <span style="color:blue;">class</span> <span style="color:#2b91af;">Hello</span>
{
<span style="color:blue;">public</span> <span style="color:blue;">string</span> Name { <span style="color:blue;">get</span>; <span style="color:blue;">set</span>; }
}</code>
<h4>
2. Define what your Web Service will return (i.e. Response DTO)</h4>
<code class="csharp"> <span style="color:gray;">///</span><span style="color:green;"> </span><span style="color:gray;"><summary></span>
<span style="color:gray;">///</span><span style="color:green;"> Define your ServiceStack web service response (i.e. Response DTO).</span>
<span style="color:gray;">///</span><span style="color:green;"> </span><span style="color:gray;"></summary></span>
<span style="color:blue;">public</span> <span style="color:blue;">class</span> <span style="color:#2b91af;">HelloResponse</span>
{
<span style="color:blue;">public</span> <span style="color:blue;">string</span> Result { <span style="color:blue;">get</span>; <span style="color:blue;">set</span>; }
}
</code>
<h4>
3. Create your Web Service implementation</h4>
<code class="csharp"> <span style="color:gray;">///</span><span style="color:green;"> </span><span style="color:gray;"><summary></span>
<span style="color:gray;">///</span><span style="color:green;"> Create your ServiceStack web service implementation.</span>
<span style="color:gray;">///</span><span style="color:green;"> </span><span style="color:gray;"></summary></span>
<span style="color:blue;">public</span> <span style="color:blue;">class</span> <span style="color:#2b91af;">HelloService</span> : <span style="color:#2b91af;">IService</span>
{
<span style="color:blue;">public</span> <span style="color:blue;">object</span> Any(<span style="color:#2b91af;">Hello</span> request)
{
<span style="color:green;">//Looks strange when the name is null so we replace with a generic name.</span>
<span style="color:blue;">var</span> name = request.Name ?? <span style="color:#a31515;">"John Doe"</span>;
<span style="color:blue;">return</span> <span style="color:blue;">new</span> <span style="color:#2b91af;">HelloResponse</span> { Result = <span style="color:#a31515;">"Hello, "</span> + name };
}
}
</code>
<h3>
Registering your web services and starting your application</h3>
The final step is to Configure setup to tell Service Stack where to find your web
services: <a name="AppHost"></a>
<h4>
Web Service AppHost Singleton</h4>
<code class="csharp"> <span style="color:blue;">public</span> <span style="color:blue;">class</span> <span style="color:#2b91af;">Global</span> : System.Web.<span style="color:#2b91af;">HttpApplication</span>
{
<span style="color:gray;">///</span><span style="color:green;"> </span><span style="color:gray;"><summary></span>
<span style="color:gray;">///</span><span style="color:green;"> Create your ServiceStack web service application with a singleton AppHost.</span>
<span style="color:gray;">///</span><span style="color:green;"> </span><span style="color:gray;"></summary></span><span style="color:green;"> </span>
<span style="color:blue;">public</span> <span style="color:blue;">class</span> <span style="color:#2b91af;">HelloAppHost</span> : <span style="color:#2b91af;">AppHostBase</span>
{
<span style="color:gray;">///</span><span style="color:green;"> </span><span style="color:gray;"><summary></span>
<span style="color:gray;">///</span><span style="color:green;"> Initializes a new instance of your ServiceStack application, with the specified name and assembly containing the services.</span>
<span style="color:gray;">///</span><span style="color:green;"> </span><span style="color:gray;"></summary></span>
<span style="color:blue;">public</span> HelloAppHost() : <span style="color:blue;">base</span>(<span style="color:#a31515;">"Hello Web Services"</span>, <span style="color:blue;">typeof</span>(<span style="color:#2b91af;">HelloService</span>).Assembly) { }
<span style="color:gray;">///</span><span style="color:green;"> </span><span style="color:gray;"><summary></span>
<span style="color:gray;">///</span><span style="color:green;"> Configure the container with the necessary routes for your ServiceStack application.</span>
<span style="color:gray;">///</span><span style="color:green;"> </span><span style="color:gray;"></summary></span>
<span style="color:gray;">///</span><span style="color:green;"> </span><span style="color:gray;"><param name=</span><span style="color:gray;">"container"</span><span style="color:gray;">></span><span style="color:green;">The built-in IoC used with ServiceStack.</span><span style="color:gray;"></param></span>
<span style="color:blue;">public</span> <span style="color:blue;">override</span> <span style="color:blue;">void</span> Configure(<span style="color:#2b91af;">Container</span> container)
{
<span style="color:green;">//Register user-defined REST-ful urls. You can access the service at the url similar to the following.</span>
<span style="color:green;">//http://localhost/ServiceStack.Hello/servicestack/hello or http://localhost/ServiceStack.Hello/servicestack/hello/John%20Doe</span>
<span style="color:green;">//You can change /servicestack/ to a custom path in the web.config.</span>
Routes
.Add<<span style="color:#2b91af;">Hello</span>>(<span style="color:#a31515;">"/hello"</span>)
.Add<<span style="color:#2b91af;">Hello</span>>(<span style="color:#a31515;">"/hello/{Name}"</span>);
}
}
<span style="color:blue;">protected</span> <span style="color:blue;">void</span> Application_Start(<span style="color:blue;">object</span> sender, <span style="color:#2b91af;">EventArgs</span> e)
{
<span style="color:green;">//Initialize your application</span>
(<span style="color:blue;">new</span> <span style="color:#2b91af;">HelloAppHost</span>()).Init();
}
}
</code>
<p>
Done! You now have a working application :)
</p>
<a name="view-webservice"></a>
<h1>
Viewing your Web Services</h1>
<p>
Now that you have a working Web Service lets see what ServiceStack does for you
out of the box:
</p>
<h4>
The Web Service Index Metadata page</h4>
<p>
If everything is configured correctly you can go to the <strong><a href="servicestack/metadata">
/api/metadata</a></strong> to see a list of your web services and the
various end points its available on.
</p>
<p>
<img src="Content/Images/MetadataIndex.png" alt="Service Stack Metadata page" width="747"
height="647" /></p>
<p>
The <b>Metadata</b> page contains:
<ul>
<li>A list of all your webservices and the endpoints they are available on. </li>
<li>A list of coding examples showing you how to call each endpoint a number of different
ways. </li>
<li>Links to all the XSD types used by your web services </li>
<li>Links to the web services SOAP 1.1 / 1.2 WSDLs </li>
</ul>
</p>
<h2>
The different ways of calling your Web Service</h2>
<p>
Without any configuration required, your web services are callable via the following
endpoints, formats and calling conventions.
</p>
<div class="endpoints">
<h3>
XML (<acronym title="Plain old XML">POX</acronym>), JSON, JSV, CSV urls</h3>
<h4>
/api/<b>[xml<i>|</i>json<i>|</i>html<i>|</i>jsv<i>|</i>csv]</b>/<b>[reply<i>|</i>oneway]</b>/<b>[servicename]</b>
</h4>
<h3>
SOAP Web Service urls</h3>
<h4>
/api/<b>[soap11<i>|</i>soap12]</b>
</h4>
<h3>
User defined REST-ful urls</h3>
<p>
Accessible via any REST-ful urls defined using the [RestService] Attribute. e.g.
in this case:
</p>
<h4>
/api/<b>hello/<i>{Name}</i></b>
</h4>
</div>
<a name="predefinedroutes"></a>
<h2>
XML, JSON, <a href="https://github.com/ServiceStack/ServiceStack/wiki/HTML5ReportFormat">
HTML5</a>, <a href="https://github.com/ServiceStack/ServiceStack.Text/wiki/JSV-Format">
JSV</a>, <a href="https://github.com/ServiceStack/ServiceStack/wiki/ServiceStack-CSV-Format">
CSV</a> Web Services via HTTP GET or POST</h2>
<table>
<thead>
<tr>
<th>
CONTENT TYPE
</th>
<th>
HTTP GET
</th>
<th>
HTTP POST CONTENT TYPE
</th>
<th>
HTTP POST FORM DATA
</th>
</tr>
</thead>
<tbody>
<tr>
<th>
XML
</th>
<td>
<a href="servicestack/xml/syncreply/Hello?Name=World">/api/xml/reply/Hello?Name=World</a>
</td>
<td>
<a href="servicestack/xml/metadata?op=Hello">/api/xml/reply/Hello</a>
</td>
<td>
<form action="servicestack/xml/syncreply/Hello" method="post">
<label>
Name:</label>
<input type="text" name="Name" value="World!" />
<input type="submit" />
</form>
</td>
</tr>
<tr>
<th>
JSON
</th>
<td>
<a href="servicestack/json/syncreply/Hello?Name=World">/api/json/reply/Hello?Name=World</a>
</td>
<td>
<a href="servicestack/json/metadata?op=Hello">/api/json/reply/Hello</a>
</td>
<td>
<form action="servicestack/json/syncreply/Hello" method="post">
<label>
Name:</label>
<input type="text" name="Name" value="World!" />
<input type="submit" />
</form>
</td>
</tr>
<tr>
<th>
HTML
</th>
<td>
<a href="servicestack/html/syncreply/Hello?Name=World">/api/html/reply/Hello?Name=World</a>
</td>
<td>
<a href="servicestack/html/metadata?op=Hello">/api/html/reply/Hello</a>
</td>
<td>
<form action="servicestack/html/syncreply/Hello" method="post">
<label>
Name:</label>
<input type="text" name="Name" value="World!" />
<input type="submit" />
</form>
</td>
</tr>
<tr>
<th>
JSV
</th>
<td>
<a href="servicestack/jsv/syncreply/Hello?Name=World">/api/jsv/reply/Hello?Name=World</a>
</td>
<td>
<a href="servicestack/jsv/metadata?op=Hello">/api/jsv/reply/Hello</a>
</td>
<td>
<form action="servicestack/jsv/syncreply/Hello" method="post">
<label>
Name:</label>
<input type="text" name="Name" value="World!" />
<input type="submit" />
</form>
</td>
</tr>
<tr>
<th>
CSV
</th>
<td>
<a href="servicestack/csv/syncreply/Hello?Name=World">/api/csv/reply/Hello?Name=World</a>
</td>
<td>
<a href="servicestack/csv/metadata?op=Hello">/api/csv/reply/Hello</a>
</td>
<td>
<form action="servicestack/csv/syncreply/Hello" method="post">
<label>
Name:</label>
<input type="text" name="Name" value="World!" />
<input type="submit" />
</form>
</td>
</tr>
</tbody>
</table>
<h4>
CONTENT TYPE</h4>
<p>
The endpoint type and resulting serialization format of your web service.
</p>
<h4>
HTTP GET</h4>
<p>
The url to call your web service using only the url and the query string to populate
the request.<br />
</p>
<p>
<em>NOTE: Service Stack also lets you submit any arbitary complex type (using <a
href="https://github.com/ServiceStack/ServiceStack.Text/wiki/JSV-Format">JSV format</a>)
via the query string or form data: <a href="http://www.servicestack.net/ServiceStack.Examples.Host.Web/ServiceStack/Json/SyncReply/StoreLogs?Loggers=[{Id:786,Devices:[{Id:5955,Type:Panel,TimeStamp:1199303309,Channels:[{Name:Temperature,Value:58},{Name:Status,Value:On}]},{Id:5956,Type:Tank,TimeStamp:1199303309,Channels:[{Name:Volume,Value:10035},{Name:Status,Value:Full}]}]}]">
see this example</a>. </em>
</p>
<h4>
HTTP POST CONTENT TYPE</h4>
<p>
You can HTTP POST the 'Content Type' representation of the Request DTO to the same
url. Check the links to the metadata page for examples of this.
</p>
<h4>
HTTP POST FORM DATA</h4>
<p>
As an alternative to posting the Content Type, each service also accepts <strong>x-www-form-urlencoded</strong>
Content-Types allowing you to call each web service using a HTML FORM. Here is the
HTML for the live examples:
</p>
<code class="html"><span style="color:blue;"><</span><span style="color:maroon;">form</span> <span style="color:red;">action</span><span style="color:blue;">=</span><span style="color:blue;">"api/xml/syncreply/Hello"</span> <span style="color:red;">method</span><span style="color:blue;">=</span><span style="color:blue;">"post"</span><span style="color:blue;">></span>
<span style="color:blue;"><</span><span style="color:maroon;">label</span><span style="color:blue;">></span>Name:<span style="color:blue;"></</span><span style="color:maroon;">label</span><span style="color:blue;">></span>
<span style="color:blue;"><</span><span style="color:maroon;">input</span> <span style="color:red;">type</span><span style="color:blue;">=</span><span style="color:blue;">"text"</span> <span style="color:red;">name</span><span style="color:blue;">=</span><span style="color:blue;">"Name"</span> <span style="color:red;">value</span><span style="color:blue;">=</span><span style="color:blue;">"World!"</span> <span style="color:blue;">/></span>
<span style="color:blue;"><</span><span style="color:maroon;">input</span> <span style="color:red;">type</span><span style="color:blue;">=</span><span style="color:blue;">"submit"</span> <span style="color:blue;">/></span>
<span style="color:blue;"></</span><span style="color:maroon;">form</span><span style="color:blue;">></span></code>
<p>
Example:<br />
<form action="servicestack/xml/syncreply/Hello" method="post">
<label>
Name:</label>
<input type="text" name="Name" value="World!" />
<input type="submit" />
</form>
</p>
<h2>
SOAP Web Services</h2>
<table>
<thead>
<tr>
<th>
SOAP VERSION
</th>
<th>
WSDL + END POINTS
</th>
<th>
SOAP EXAMPLE
</th>
</tr>
</thead>
<tbody>
<tr>
<th>
SOAP 1.1
</th>
<td>
<a href="servicestack/soap11">/api/soap11</a>
</td>
<td>
<a href="servicestack/soap11/metadata?op=Hello">soap11/metadata?op=Hello</a>
</td>
</tr>
<tr>
<th>
SOAP 1.2
</th>
<td>
<a href="servicestack/soap12">/api/soap12</a>
</td>
<td>
<a href="servicestack/soap12/metadata?op=Hello">soap12/metadata?op=Hello</a>
</td>
</tr>
</tbody>
</table>
<h4>
WSDL + END POINTS</h4>
<p>
A <b>HTTP GET</b> on the url returns the <b>WSDL</b>, while <b>HTTP POST</b>-ing
a SOAP Request calls the web service.
</p>
<h4>
SOAP EXAMPLE</h4>
<p>
Shows example of what a SOAP Request looks like.
</p>
<a name="customroutes"></a>
<h2>
REST Web Services</h2>
<p>
A new addition to Service Stack is the ability to define your own custom urls letting
you expose your web services via REST-ful urls.
</p>
<p>
Defining user-defined REST-ful urls can be done in one of two ways:
<ul>
<li>Using the MVC-like <b>Routes</b> config property in your <a href="#AppHost"><b>AppHost</b></a></li>
<li>Using the <b>[RestService]</b> Attribute on your Request DTOs</li>
</ul>
</p>
<p>
e.g: the following mapping:</p>
<h4>
<b>[Route("/hello/{Name}")]</b></h4>
<p>
Allows this web service to be called with: <a href="servicestack/hello/World!?format=json">
/api/hello/World!</a>
</p>
<p>
Just like the other endpoints, REST-ful urls can also be called with a HTML FORM
POST. However in this case we need to define another mapping so it matches the url
that we want, i.e.
</p>
<h4>
<b>[Route("/hello")]</b></h4>
<p>
Which lets us now call this web service using the following HTML:
</p>
<code class="html"><span style="color:blue;"><</span><span style="color:maroon;">form</span> <span style="color:red;">action</span><span style="color:blue;">=</span><span style="color:blue;">"api/hello?format=json"</span> <span style="color:red;">method</span><span style="color:blue;">=</span><span style="color:blue;">"post"</span><span style="color:blue;">></span>
<span style="color:blue;"><</span><span style="color:maroon;">label</span><span style="color:blue;">></span>Name:<span style="color:blue;"></</span><span style="color:maroon;">label</span><span style="color:blue;">></span>
<span style="color:blue;"><</span><span style="color:maroon;">input</span> <span style="color:red;">type</span><span style="color:blue;">=</span><span style="color:blue;">"text"</span> <span style="color:red;">name</span><span style="color:blue;">=</span><span style="color:blue;">"Name"</span> <span style="color:red;">value</span><span style="color:blue;">=</span><span style="color:blue;">"World!"</span> <span style="color:blue;">/></span>
<span style="color:blue;"><</span><span style="color:maroon;">input</span> <span style="color:red;">type</span><span style="color:blue;">=</span><span style="color:blue;">"submit"</span> <span style="color:blue;">/></span>
<span style="color:blue;"></</span><span style="color:maroon;">form</span><span style="color:blue;">></span></code>
<p>
Example:<br />
<form action="servicestack/hello?format=json" method="post">
<label>
Name:</label>
<input type="text" name="Name" value="World!" />
<input type="submit" />
</form>
</p>
<p>
This makes it very easy to Ajax-ify your existing HTML forms, cleanly, without messy
configuration and generated code mandated by other options.
</p>
<a name="clients"></a>
<h2>
Calling Web Services from Code</h2>
<p>
Using DTOs to define your web service interface makes it possible to provide strong-typed
generic service clients without any code-gen or extra build-steps, leading to a
productive end-to-end type-safe communication gateway from client to server.
</p>
<p>
All REST and ServiceClients share the same interfaces so they can easily be replaced
(for increased perf/debuggability/etc) with a single line of code as seen in the
<a href="https://github.com/ServiceStack/ServiceStack/blob/master/tests/ServiceStack.WebHost.IntegrationTests/Tests/HelloWorldServiceClientTests.cs">
Hello Service Integration tests</a>.
</p>
<p>
C#/.NET Clients can call the above Hello Service using any of the <a href="https://github.com/ServiceStack/ServiceStack/wiki/C%23-client">
JSON, JSV, XML or SOAP Service Clients</a> as well as a
<a href="https://github.com/ServiceStack/ServiceStack/wiki/SilverlightServiceClient">Silverlight</a>,
<a href="https://github.com/ServiceStack/ServiceStack/wiki/Javascript-client">JavaScript</a>,
<a href="https://github.com/ServiceStack/ServiceStack/wiki/Dart-Client">Dart</a> or even
<a href="https://github.com/ServiceStack/ServiceStack/wiki/Messaging-and-redis">MQ Client</a>.
<a name="links"></a>
<h2>
Where to now?</h2>
<h3>
Northwind Database Examples</h3>
<p>
Now that you've got the hang of how easy it is to create a simple web service, check
out the <a href="http://www.servicestack.net/ServiceStack.Northwind/">Northwind Database
examples</a> to see how useful servicestack becomes with just a little db code.
</p>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-7722718-7']);
_gaq.push(['_trackPageview']);
(function () {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
</body>
</html>