我想向我的应用程序添加自定义设置,如下所示:
<Parameter Name="AuthService1" url="http://localhost:11200/" method="post" value3="123" />
<Parameter Name="AuthService2" url="https://auth.com/" method="get" value3="133" />
或者分组键值参数,如下所示:
<subSettings name="AuthService1">
<parametr name="url" value ="http://localhost:11200/" />
<parametr name="method" value ="post" />
<subSettings/>
<subSettings name="AuthService2">
<parametr name="url" value ="https://auth.com/" />
<parametr name="method" value ="get" />
<subSettings/>
我可以在哪里存储它?
我能想到的一些选择:
-
使用 json(或 xml(作为值,如下所示:
<Parameter Name="AuthService1" Value="{"Url":"http://localhost:11200/","Method":"post","value3":123}">
-
使用多个参数,如下所示:
<Parameter Name="AuthService1.Url" Value="http://localhost:11200/" /> <Parameter Name="AuthService1.Method" Value="Post" /> <Parameter Name="AuthService1.Value3" Value="123" />
如果要解决这个问题,我宁愿采用分组方法。可以在 Service Fabric"设置.xml"中包含如下部分:
<Section Name="AuthService1">
<Parameter Name="Url" Value="http://localhost:11200/" />
<Parameter Name="Method" Value="POST" />
</Section>
现在,如果要在代码中访问这些设置,则需要执行以下操作:
Configuration.GetSection("AuthService1")["Url"];
或
Configuration["AuthService1:Url"];
在上面给出的示例中,我假设配置是Microsoft.Extensions.IConfiguration对象。