Skip to content

Instantly share code, notes, and snippets.

@gistlyn
Last active April 29, 2020 09:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gistlyn/95af30f3bce3f5044d91fa8706e5b958 to your computer and use it in GitHub Desktop.
Save gistlyn/95af30f3bce3f5044d91fa8706e5b958 to your computer and use it in GitHub Desktop.
Empty .NET Core 3.1 LTS ServiceStack App
{
"Logging": {
"IncludeScopes": false,
"Debug": {
"LogLevel": {
"Default": "Warning"
}
},
"Console": {
"LogLevel": {
"Default": "Warning"
}
}
}
}
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TypeScriptToolsVersion>latest</TypeScriptToolsVersion>
<TypeScriptCompileBlocked>true</TypeScriptCompileBlocked>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="ServiceStack" Version="5.*" />
</ItemGroup>
</Project>
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using ServiceStack;
namespace MyApp
{
public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseModularStartup<Startup>()
.Build();
}
}
using System;
using ServiceStack;
using MyApp.ServiceModel;
namespace MyApp.ServiceInterface
{
public class MyServices : Service
{
public object Any(Hello request)
{
return new HelloResponse { Result = $"Hello, {request.Name}!" };
}
}
}
using ServiceStack;
namespace MyApp.ServiceModel
{
[Route("/hello")]
[Route("/hello/{Name}")]
public class Hello : IReturn<HelloResponse>
{
public string Name { get; set; }
}
public class HelloResponse
{
public string Result { get; set; }
}
}
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using Funq;
using ServiceStack;
using ServiceStack.Configuration;
using MyApp.ServiceInterface;
using ServiceStack.Script;
using ServiceStack.Web;
using System;
using ServiceStack.Text;
using ServiceStack.Logging;
namespace MyApp
{
public class Startup : 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
public new void ConfigureServices(IServiceCollection services)
{
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseServiceStack(new AppHost
{
AppSettings = new NetCoreAppSettings(Configuration)
});
}
}
public class AppHost : AppHostBase
{
public AppHost() : base("My App", typeof(MyServices).Assembly) { }
// Configure your AppHost with the necessary configuration and dependencies your App needs
public override void Configure(Container container)
{
SetConfig(new HostConfig
{
UseSameSiteCookies = true,
DebugMode = HostingEnvironment.IsDevelopment()
});
}
}
}
<html>
<head>
<title>My App</title>
<style>
body { padding: 3em; }
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="/json/metadata?op=Hello">Hello</a> API</h2>
<input type="text" id="txtName" onkeyup="callHello(this.value)">
<div id="result"></div>
<h4>Example Code</h4>
<div style="font-size:20px;line-height:26px">
<pre>&lt;script src="/js/servicestack-client.js"&gt;&lt;/script&gt;</pre>
<h3>JavaScript</h3>
<pre>Object.assign(window, window['@servicestack/client']) //import
var client = new JsonServiceClient()
client.get(new Hello({ name: name }))
.then(function(r) {
console.log(r.result)
})
</pre>
<h3>TypeScript</h3>
<pre>$ npm install @servicestack/client</pre>
<pre>import { JsonServiceClient } from '@servicestack/client'
let client = new JsonServiceClient()
let response = await client.get(new Hello({ name }))
</pre>
<p>
Generate Typed DTOs using
<a href="https://docs.servicestack.net/typescript-add-servicestack-reference">Add ServiceStack Reference</a>
</p>
</div>
<script src="/js/servicestack-client.js"></script>
<script>
Object.assign(window, window['@servicestack/client']); //import into global namespace
// generate typed dtos with https://docs.servicestack.net/typescript-add-servicestack-reference
var Hello = /** @class */ (function () {
function Hello(init) { Object.assign(this, init); }
Hello.prototype.createResponse = function () { return new HelloResponse(); };
Hello.prototype.getTypeName = function () { return 'Hello'; };
return Hello;
}());
var HelloResponse = /** @class */ (function () {
function HelloResponse(init) { Object.assign(this, init); }
return HelloResponse;
}());
var client = new JsonServiceClient();
function callHello(val) {
client.get(new Hello({ name: val }))
.then(function(r) {
document.getElementById('result').innerHTML = r.result;
});
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment