Skip to content

Instantly share code, notes, and snippets.

@gistlyn
Last active Jun 12, 2022
Embed
What would you like to do?
VB.NET Empty .NET 6 ServiceStack App
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TypeScriptToolsVersion>latest</TypeScriptToolsVersion>
<TypeScriptCompileBlocked>true</TypeScriptCompileBlocked>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="ServiceStack" Version="6.*" />
</ItemGroup>
</Project>
Imports System
Imports System.Collections.Generic
Imports System.IO
Imports System.Linq
Imports System.Threading.Tasks
Imports Microsoft.AspNetCore
Imports Microsoft.AspNetCore.Hosting
Imports Microsoft.Extensions.Configuration
Imports Microsoft.Extensions.Logging
Imports ServiceStack
Module Program
Sub Main(args As String())
BuildWebHost(args).Run()
End Sub
Function BuildWebHost(args As String()) As IWebHost
Return WebHost.CreateDefaultBuilder(args).UseModularStartup(Of Startup)().Build()
End Function
End Module
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "https://localhost:5001/",
"sslPort": 0
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"MyApp": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "https://localhost:5001/"
}
}
}
Imports System
Imports ServiceStack
Imports MyApp.ServiceModel
Namespace Global.MyApp.ServiceInterface
Public Class MyServices
Inherits Service
public Function Any(request AS Hello) AS Object
Return new HelloResponse With { .Result = $"Hello, {request.Name}!" }
End Function
End Class
End Namespace
Imports ServiceStack
Namespace Global.MyApp.ServiceModel
<Route("/hello")>
<Route("/hello/{Name}")>
Public Partial Class Hello
Implements IReturn(Of HelloResponse)
Public Overridable Property Name As String
End Class
Public Partial Class HelloResponse
Public Overridable Property Result As String
End Class
End Namespace
Imports System.Collections.Generic
Imports System.Linq
Imports Microsoft.AspNetCore.Builder
Imports Microsoft.AspNetCore.Hosting
Imports Microsoft.Extensions.Hosting
Imports Microsoft.Extensions.DependencyInjection
Imports Microsoft.Extensions.Configuration
Imports Funq
Imports ServiceStack
Imports ServiceStack.Configuration
Imports MyApp.ServiceInterface
Imports ServiceStack.Script
Imports ServiceStack.Web
Imports System
Imports ServiceStack.Text
Imports ServiceStack.Logging
Namespace Global.MyApp
Class Startup
Inherits ModularStartup
' This method gets called by the runtime. Use this method to add services to the container.
' For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
Overloads Sub ConfigureServices(Services as IServiceCollection)
End Sub
Overloads Sub Configure(app AS IApplicationBuilder, env AS IWebHostEnvironment)
If env.IsDevelopment()
app.UseDeveloperExceptionPage()
End If
app.UseServiceStack(new AppHost With {
.AppSettings = new NetCoreAppSettings(Configuration)
})
End Sub
End Class
Class AppHost
Inherits AppHostBase
Public Sub New()
MyBase.New("My App", GetType(MyServices).Assembly)
End Sub
' Configure your AppHost with the necessary configuration and dependencies your App needs
Public Overrides Sub Configure(container AS Container)
SetConfig(new HostConfig With {
.UseSameSiteCookies = true,
.DebugMode = HostingEnvironment.IsDevelopment()
})
End Sub
End Class
End Namespace
<html>
<head>
<title>My App</title>
<style>
body { padding: 1em 1em 0 1em; }
body, input[type=text] { font: 32px/36px -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif }
input { padding:.25em .5em; margin-right:.5em; }
a { color: #007bff }
#result { display:inline-block;color:#28a745; }
pre { background: #f1f1f1; padding: 1em; }
</style>
</head>
<body>
<h2><a href="/ui/Hello">Hello</a> API</h2>
<input type="text" id="txtName" onkeyup="callHello(this.value)">
<div id="result"></div>
<script src="/js/require.js"></script>
<script src="/js/servicestack-client.js"></script>
<script src="/types/js"></script>
<script>
var { JsonServiceClient, Hello } = exports
var client = new JsonServiceClient();
function callHello(name) {
client.get(new Hello({ name }))
.then(function(r) {
document.getElementById('result').innerHTML = r.result;
});
}
callHello(document.querySelector('#txtName').value = 'World')
</script>
<div style="font-size:20px;line-height:26px">
<h3>View in API Explorer</h3>
<ul>
<li>
<a href="/ui/Hello">Call API</a>
</li>
<li>
<a href="/ui/Hello?tab=details">View API Details</a>
</li>
<li>
<a href="/ui/Hello?tab=code">Browse API Source Code</a>
</li>
</ul>
<h3>Using JsonServiceClient in Web Pages</h3>
<p>
The easiest way to call your APIs in a webpage is to include your JavaScript DTOs <b>/types/js</b> and built-in
UMD <a href="https://docs.servicestack.net/servicestack-client-umd">@servicestack/client</a> library:
</p>
<pre>&lt;script src="/js/require.js"&gt;&lt;/script&gt;
&lt;script src="/js/servicestack-client.js"&gt;&lt;/script&gt;
&lt;script src="/types/js"&gt;&lt;/script&gt;</pre>
<p>
We can then import and use the library and DTO types:
</p>
<pre>var { JsonServiceClient, Hello } = exports
var client = new JsonServiceClient()
client.api(new Hello({ name }))
.then(function(api) {
if (api.succeeded)
console.log(api.response)
})
</pre>
<h3>Using @servicestack/client in npm projects</h3>
<p>
Update your App's
<a href="https://docs.servicestack.net/typescript-add-servicestack-reference">TypeScript DTOs</a> and
compile to JS (requires <a href="https://www.typescriptlang.org/download">TypeScript</a>):
</p>
<pre>$ x scripts dtos</pre>
<h3>Including @servicestack/client &amp; Typed DTOs</h3>
<p>
Where you'll be able to use your APIs typed DTOs with ServiceStack's generic **JsonServiceClient**
</p>
<pre>$ npm install @servicestack/client</pre>
<pre>import { JsonServiceClient } from '@servicestack/client'
import { Hello } from './dtos'
let client = new JsonServiceClient()
let api = await client.api(new Hello({ name }))
if (api.succeeded)
console.log(api.response.result)
</pre>
<p>
Typed DTOs generated using
<a href="https://docs.servicestack.net/typescript-add-servicestack-reference">TypeScript Add ServiceStack Reference</a>
</p>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment