[ISSUE #6817] sample code add C# (#6818)

This commit is contained in:
Catcher Wong 2021-09-09 09:30:32 +08:00 committed by GitHub
parent 6df0f22244
commit bfa94e318d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 260 additions and 0 deletions

View File

@ -42,6 +42,7 @@ class ShowCodeing extends React.Component {
this.cppCode = 'TODO';
this.shellCode = 'TODO';
this.pythonCode = 'TODO';
this.csharpCode = 'TODO';
this.record = {};
this.sprigboot_code = `// Refer to document: https://github.com/nacos-group/nacos-examples/tree/master/nacos-spring-boot-example/nacos-spring-boot-config-example
package com.alibaba.nacos.example.spring.boot.controller;
@ -121,6 +122,7 @@ public class ConfigController {
this.cppCode = this.getCppCode(obj);
this.shellCode = this.getShellCode(obj);
this.pythonCode = this.getPythonCode(obj);
this.csharpCode = this.getCSharpCode(obj);
this.forceUpdate();
}
@ -208,6 +210,136 @@ public class ConfigExample {
return 'TODO';
}
getCSharpCode(data) {
return `/*
Demo for Basic Nacos Opreation
App.csproj
<ItemGroup>
<PackageReference Include="nacos-sdk-csharp" Version="\${latest.version}" />
</ItemGroup>
*/
using Microsoft.Extensions.DependencyInjection;
using Nacos.V2;
using Nacos.V2.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
class Program
{
static async Task Main(string[] args)
{
string serverAddr = "http://localhost:8848";
string dataId = "${data.dataId}";
string group = "${data.group}";
IServiceCollection services = new ServiceCollection();
services.AddNacosV2Config(x =>
{
x.ServerAddresses = new List<string> { serverAddr };
x.Namespace = "cs-test";
// swich to use http or rpc
x.ConfigUseRpc = true;
});
IServiceProvider serviceProvider = services.BuildServiceProvider();
var configSvc = serviceProvider.GetService<INacosConfigService>();
var content = await configSvc.GetConfig(dataId, group, 3000);
Console.WriteLine(content);
var listener = new ConfigListener();
await configSvc.AddListener(dataId, group, listener);
var isPublishOk = await configSvc.PublishConfig(dataId, group, "content");
Console.WriteLine(isPublishOk);
await Task.Delay(3000);
content = await configSvc.GetConfig(dataId, group, 5000);
Console.WriteLine(content);
var isRemoveOk = await configSvc.RemoveConfig(dataId, group);
Console.WriteLine(isRemoveOk);
await Task.Delay(3000);
content = await configSvc.GetConfig(dataId, group, 5000);
Console.WriteLine(content);
await Task.Delay(300000);
}
internal class ConfigListener : IListener
{
public void ReceiveConfigInfo(string configInfo)
{
Console.WriteLine("recieve:" + configInfo);
}
}
}
/*
Refer to document: https://github.com/nacos-group/nacos-sdk-csharp/tree/dev/samples/MsConfigApp
Demo for ASP.NET Core Integration
MsConfigApp.csproj
<ItemGroup>
<PackageReference Include="nacos-sdk-csharp.Extensions.Configuration" Version="\${latest.version}" />
</ItemGroup>
*/
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Serilog;
using Serilog.Events;
public class Program
{
public static void Main(string[] args)
{
Log.Logger = new LoggerConfiguration()
.Enrich.FromLogContext()
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
.MinimumLevel.Override("System", LogEventLevel.Warning)
.MinimumLevel.Debug()
.WriteTo.Console()
.CreateLogger();
try
{
Log.ForContext<Program>().Information("Application starting...");
CreateHostBuilder(args, Log.Logger).Build().Run();
}
catch (System.Exception ex)
{
Log.ForContext<Program>().Fatal(ex, "Application start-up failed!!");
}
finally
{
Log.CloseAndFlush();
}
}
public static IHostBuilder CreateHostBuilder(string[] args, Serilog.ILogger logger) =>
Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((context, builder) =>
{
var c = builder.Build();
builder.AddNacosV2Configuration(c.GetSection("NacosConfig"), logAction: x => x.AddSerilog(logger));
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>().UseUrls("http://*:8787");
})
.UseSerilog();
}
`;
}
openDialog(record) {
this.setState({
dialogvisible: true,
@ -317,6 +449,12 @@ public class ConfigExample {
key={6}
onClick={this.changeTab.bind(this, 'commoneditor6', this.pythonCode)}
/>
<TabPane
title={'C#'}
key={7}
onClick={this.changeTab.bind(this, 'commoneditor7', this.csharpCode)}
/>
{}
</Tab>
<div ref={'codepreview'} />

View File

@ -52,6 +52,7 @@ class ShowServiceCodeing extends React.Component {
this.springCode = 'TODO';
this.sprigbootCode = 'TODO';
this.sprigcloudCode = 'TODO';
this.csharpCode = 'TODO';
}
componentDidMount() {}
@ -85,6 +86,7 @@ class ShowServiceCodeing extends React.Component {
this.cppCode = this.getCppCode(obj);
this.shellCode = this.getShellCode(obj);
this.pythonCode = this.getPythonCode(obj);
this.csharpCode = this.getCSharpCode(obj);
this.forceUpdate();
}
@ -363,6 +365,120 @@ public class NacosConsumerApplication {
return 'TODO';
}
getCSharpCode(data) {
return `/* Refer to document: https://github.com/nacos-group/nacos-sdk-csharp/
Demo for Basic Nacos Opreation
App.csproj
<ItemGroup>
<PackageReference Include="nacos-sdk-csharp" Version="\${latest.version}" />
</ItemGroup>
*/
using Microsoft.Extensions.DependencyInjection;
using Nacos.V2;
using Nacos.V2.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
class Program
{
static async Task Main(string[] args)
{
IServiceCollection services = new ServiceCollection();
services.AddNacosV2Naming(x =>
{
x.ServerAddresses = new List<string> { "http://localhost:8848/" };
x.Namespace = "cs-test";
// swich to use http or rpc
x.NamingUseRpc = true;
});
IServiceProvider serviceProvider = services.BuildServiceProvider();
var namingSvc = serviceProvider.GetService<INacosNamingService>();
await namingSvc.RegisterInstance("${this.record.name}", "11.11.11.11", 8888, "TEST1");
await namingSvc.RegisterInstance("${this.record.name}", "2.2.2.2", 9999, "DEFAULT");
Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(await namingSvc.GetAllInstances("${this.record.name}")));
await namingSvc.DeregisterInstance("${this.record.name}", "2.2.2.2", 9999, "DEFAULT");
var listener = new EventListener();
await namingSvc.Subscribe("${this.record.name}", listener);
}
internal class EventListener : IEventListener
{
public Task OnEvent(IEvent @event)
{
Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(@event));
return Task.CompletedTask;
}
}
}
/* Refer to document: https://github.com/nacos-group/nacos-sdk-csharp/
Demo for ASP.NET Core Integration
App.csproj
<ItemGroup>
<PackageReference Include="nacos-sdk-csharp.AspNetCore" Version="\${latest.version}" />
</ItemGroup>
*/
/* Refer to document: https://github.com/nacos-group/nacos-sdk-csharp/blob/dev/samples/App1/appsettings.json
* appsettings.json
{
"nacos": {
"ServerAddresses": [ "http://localhost:8848" ],
"DefaultTimeOut": 15000,
"Namespace": "cs",
"ServiceName": "App1",
"GroupName": "DEFAULT_GROUP",
"ClusterName": "DEFAULT",
"Port": 0,
"Weight": 100,
"RegisterEnabled": true,
"InstanceEnabled": true,
"Ephemeral": true,
"NamingUseRpc": true,
"NamingLoadCacheAtStart": ""
}
}
*/
// Refer to document: https://github.com/nacos-group/nacos-sdk-csharp/blob/dev/samples/App1/Startup.cs
using Nacos.AspNetCore.V2;
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
// ....
services.AddNacosAspNet(Configuration);
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
// ....
}
}
`;
}
openDialog(record) {
this.setState({
dialogvisible: true,
@ -479,6 +595,12 @@ public class NacosConsumerApplication {
key={6}
onClick={this.changeTab.bind(this, 'commoneditor6', this.pythonCode)}
/>
<TabPane
title={'C#'}
key={7}
onClick={this.changeTab.bind(this, 'commoneditor7', this.csharpCode)}
/>
{}
</Tab>
<div ref={'codepreview'} />