Skip to content

Commit

Permalink
Merge pull request #111 from nacos-group/dev
Browse files Browse the repository at this point in the history
Update sample and readme
  • Loading branch information
catcherwong authored Jun 13, 2021
2 parents 3db7938 + eae5fe8 commit 9577a69
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 13 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# nacos-sdk-csharp             [中文](./README.zh-cn.md)
# nacos-sdk-csharp             [中文](./README.zh-cn.md)

csharp(dotnet core) implementation of [nacos](https://nacos.io/) OpenAPI.

Expand Down Expand Up @@ -71,7 +71,7 @@ public static IHostBuilder CreateHostBuilder(string[] args) =>
"Group": "DEFAULT_GROUP"
}
],
"Tenant": "csharp-demo",
"Namespace": "csharp-demo",
"ServerAddresses": [ "http://localhost:8848/" ],
"UserName": "test2",
"Password": "123456",
Expand Down
4 changes: 2 additions & 2 deletions README.zh-cn.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# nacos-sdk-csharp             [English](./README.md)
# nacos-sdk-csharp             [English](./README.md)

基于C#(dotnet core)实现 [nacos](https://nacos.io/) OpenAPI 的官方版本

Expand Down Expand Up @@ -72,7 +72,7 @@ public static IHostBuilder CreateHostBuilder(string[] args) =>
"Group": "DEFAULT_GROUP"
}
],
"Tenant": "csharp-demo",
"Namespace": "csharp-demo",
"ServerAddresses": [ "http://localhost:8848/" ],
"UserName": "test2",
"Password": "123456",
Expand Down
8 changes: 4 additions & 4 deletions samples/MsConfigApp/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace MsConfigApp
namespace MsConfigApp
{
using System.Collections.Generic;
using Microsoft.AspNetCore.Hosting;
Expand Down Expand Up @@ -53,7 +53,7 @@ public static IHostBuilder CreateHostBuilder(string[] args, Serilog.ILogger logg

var dataId = c.GetValue<string>("NacosConfig:DataId");
var group = c.GetValue<string>("NacosConfig:Group");
var tenant = c.GetValue<string>("NacosConfig:Tenant");
var @namespace = c.GetValue<string>("NacosConfig:Namespace");
var optional = c.GetValue<bool>("NacosConfig:Optional");
var serverAddresses = c.GetSection("NacosConfig:ServerAddresses").Get<List<string>>();

Expand All @@ -71,7 +71,7 @@ public static IHostBuilder CreateHostBuilder(string[] args, Serilog.ILogger logg
{
x.DataId = dataId;
x.Group = group;
x.Tenant = tenant;
x.Namespace = @namespace;
x.Optional = optional;
x.ServerAddresses = serverAddresses;
});*/
Expand All @@ -82,4 +82,4 @@ public static IHostBuilder CreateHostBuilder(string[] args, Serilog.ILogger logg
})
.UseSerilog();
}
}
}
2 changes: 1 addition & 1 deletion samples/MsConfigApp/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"Optional": false,
"DataId": "demo",
"Group": "DEFAULT_GROUP",
"Tenant": "cs",
"Namespace": "cs",
"ServerAddresses": [ "http://localhost:8848/" ],
"UserName": "",
"Password": "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public NacosV2ConfigurationProvider(NacosV2ConfigurationSource configurationSour
var options = Options.Create(new NacosSdkOptions()
{
ServerAddresses = configurationSource.ServerAddresses,
Namespace = configurationSource.Tenant,
Namespace = configurationSource.GetNamespace(),
AccessKey = configurationSource.AccessKey,
ContextPath = configurationSource.ContextPath,
EndPoint = configurationSource.EndPoint,
Expand Down Expand Up @@ -111,7 +111,7 @@ public override void Load()
var config = _client.GetConfig(listener.DataId, listener.Group, 3000)
.ConfigureAwait(false).GetAwaiter().GetResult();

_configDict.AddOrUpdate($"{_configurationSource.Tenant}#{listener.Group}#{listener.DataId}", config, (x, y) => config);
_configDict.AddOrUpdate($"{_configurationSource.GetNamespace()}#{listener.Group}#{listener.DataId}", config, (x, y) => config);

var data = _parser.Parse(config);

Expand Down Expand Up @@ -140,7 +140,7 @@ public override void Load()
var config = _client.GetConfig(_configurationSource.DataId, _configurationSource.Group, 3000)
.ConfigureAwait(false).GetAwaiter().GetResult();

_configDict.AddOrUpdate($"{_configurationSource.Tenant}#{_configurationSource.Group}#{_configurationSource.DataId}", config, (x, y) => config);
_configDict.AddOrUpdate($"{_configurationSource.GetNamespace()}#{_configurationSource.Group}#{_configurationSource.DataId}", config, (x, y) => config);

var data = _parser.Parse(config);

Expand Down Expand Up @@ -179,7 +179,7 @@ internal MsConfigListener(string dataId, string group, bool optional, NacosV2Con
this._optional = optional;
this._provider = provider;
this._logger = logger;
_key = $"{provider._configurationSource.Tenant}#{_group}#{_dataId}";
_key = $"{provider._configurationSource.GetNamespace()}#{_group}#{_dataId}";
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using global::Microsoft.Extensions.Configuration;
using global::Microsoft.Extensions.Logging;
using Nacos.Config;
using Nacos.V2.Utils;
using System;
using System.Collections.Generic;

Expand Down Expand Up @@ -34,6 +35,7 @@ public class NacosV2ConfigurationSource : Nacos.V2.NacosSdkOptions, IConfigurati
/// <summary>
/// Tenant information. It corresponds to the Namespace field in Nacos.
/// </summary>
[Obsolete("please use Namespace to configure")]
public string Tenant { get; set; }

/// <summary>
Expand All @@ -55,5 +57,23 @@ public IConfigurationProvider Build(IConfigurationBuilder builder)
{
return new NacosV2ConfigurationProvider(this);
}

public string GetNamespace()
{
if (Namespace.IsNotNullOrWhiteSpace())
{
return Namespace;
}
#pragma warning disable CS0618
else if (Tenant.IsNotNullOrWhiteSpace())
{
return Tenant;
}
#pragma warning restore CS0618
else
{
return string.Empty;
}
}
}
}

0 comments on commit 9577a69

Please sign in to comment.