如何在ConfigurationBuilder中使用JSON对象本身而不是文件路径?

发布于 2025-02-05 05:34:58 字数 106 浏览 2 评论 0原文

我是在k8s群集中存储为configmap的JSON文件。因此,我没有C#中ConfigurationBuilder类所需的路径。有没有一种方法可以从JSON对象本身而不是FilePath构建配置?

I am gettin the json file stored as ConfigMap in K8s cluster. So I do not have the path as required in ConfigurationBuilder class in C#. Is there a way to build configuration from JSON object itself rather than the filepath ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

戒ㄋ 2025-02-12 05:34:58

如果您能够从源获取JSON字符串,则可以调用 configurationBuild.add 方法通过将其传递给

以下是示例代码。

ConfigurationBuilder builder = new ConfigurationBuilder();

// GetConfigJson method should get the JSON string from the source.
// I am leaving the implementation of that method up to you.
string jsonData = GetConfigJson();
    
// Load the JSON into MemoryStream
var stream = new MemoryStream(Encoding.UTF8.GetBytes(jsonData));
    
// Create an object of JsonStreamConfigurationSource class.
var configSource = new JsonStreamConfigurationSource();

// Assign stream to it.
configSource.Stream = stream;

// Call Add method of builder by passing the configSource object.
builder.Add(configSource);
    

您也可以致电 addjsonstream 构建器上的方法通过将流传递给它。.

ConfigurationBuilder builder = new ConfigurationBuilder();

// GetConfigJson method should get the JSON string from the source.
// I am leaving the implementation of that method up to you.
string jsonData = GetConfigJson();
    
// Load the JSON into MemoryStream
var stream = new MemoryStream(Encoding.UTF8.GetBytes(jsonData));

// Call AddJsonStream method of builder by passing the stream object.
builder.AddJsonStream(stream);
    

我希望这将有助于解决您的问题。

If you are able to get the JSON string from the source, you can call ConfigurationBuild.Add method by passing it an object of JsonStreamConfigurationSource class.

Following is the sample code.

ConfigurationBuilder builder = new ConfigurationBuilder();

// GetConfigJson method should get the JSON string from the source.
// I am leaving the implementation of that method up to you.
string jsonData = GetConfigJson();
    
// Load the JSON into MemoryStream
var stream = new MemoryStream(Encoding.UTF8.GetBytes(jsonData));
    
// Create an object of JsonStreamConfigurationSource class.
var configSource = new JsonStreamConfigurationSource();

// Assign stream to it.
configSource.Stream = stream;

// Call Add method of builder by passing the configSource object.
builder.Add(configSource);
    

You can also call AddJsonStream method on builder by passing the stream to it..

ConfigurationBuilder builder = new ConfigurationBuilder();

// GetConfigJson method should get the JSON string from the source.
// I am leaving the implementation of that method up to you.
string jsonData = GetConfigJson();
    
// Load the JSON into MemoryStream
var stream = new MemoryStream(Encoding.UTF8.GetBytes(jsonData));

// Call AddJsonStream method of builder by passing the stream object.
builder.AddJsonStream(stream);
    

I hope this will help resolve you issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文