在 web.config 中使用 web.config 变量

Ant*_*ott 5 asp.net web-config

我想在 web.config 中定义一个变量,我可以在 web.config 文件(和其他配置文件)中的多个位置使用该变量。通过例子来解释可能更容易......

网络配置

<?xml version="1.0"?>
<configuration>
    <appSettings>
        <add key="AuthServiceEndPoint" value="any_old_name_i_like"/>
    </appSettings>
    <system.web>

    ...

    <system.serviceModel>
        <client>
            <endpoint
                address="net.tcp://localhost/AuthService"
                binding="netTcpBinding"
                contract="MyServices.Contracts.IAuthService"
                name="#{AppSettings.AuthServiceEndPoint}"
                bindingConfiguration="netTcpBindingConfig"
            />

        </client>
    </system.serviceModel>
</configuration>
Run Code Online (Sandbox Code Playgroud)

温莎配置文件

<?xml version="1.0" encoding="utf-8" ?>
<castle>
    <components>

        ...

        <component
            id="AuthProvider"
            service="MyServices.Client.IAuthProvider, MyServices.Client"
            type="MyServices.Client.AuthProvider, MyServices.Client"
            lifestyle="transient">
            <parameters>
                <endpoint>#{AppSettings.AuthServiceEndPoint}</endpoint>
            </parameters>
        </component>

    </components>
</castle>
Run Code Online (Sandbox Code Playgroud)

这可能吗?


编辑(更多信息)

我已经能够从 Windsor.config 文件访问 AppSettings(该文件实际上由温莎城堡和自定义 XmlInterpreter 处理)。

真正的问题是我可以在 web.config 中执行此操作吗?

<?xml version="1.0"?>
<configuration>
    <appSettings>
        <add key="AuthServiceEndPoint" value="any_old_name_i_like"/>
    </appSettings>
    <system.web>

    ...

    <system.serviceModel>
        <client>
            <endpoint
                address="net.tcp://localhost/AuthService"
                binding="netTcpBinding"
                contract="MyServices.Contracts.IAuthService"
                name="#{AppSettings.AuthServiceEndPoint}"
                bindingConfiguration="netTcpBindingConfig"
            />

        </client>
    </system.serviceModel>
</configuration>
Run Code Online (Sandbox Code Playgroud)

<appSettings>ie -从 web.config 文件的其他部分访问 my 中的变量。

Ant*_*ott 0

我在这里再次回答我自己的问题:-S

我通过编写 NetTcpServiceLocator 解决了这个问题...

public interface INetTcpServiceLocator
{
    EndpointAddress GetAddress(Type serviceType);
}
Run Code Online (Sandbox Code Playgroud)

...以及自定义配置部分处理程序,它也实现上述接口并读取以下配置部分...

<services>
    <service contract="My.Services.TestService.Contracts.ITestService" address="net.tcp://localhost/TestService" />
</services>
Run Code Online (Sandbox Code Playgroud)

然后我为每个服务创建了一个代理......

public class TestServiceProxy : ITestService
{
    public SomeInformation GetSomeInformation(SomeParams @params)
    {
        using (var factory = new NetTcpServiceFactory<ITestService>())
        {
            var service = factory.Service;
            return service.GetSomeInformation(@params);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我的控制器依赖于服务,而服务又依赖于 ITestService。所有这些都通过 Castle Windsor 并使用属性依赖注入粘合在一起。

因此,我的控制器调用它的 Service,后者又调用 ITestService(在本例中是一个代理,它从自定义部分处理程序获取它的端点)。

自定义部分处理程序(也是 INetTcpServiceLocator)具有“perWebRequest”的 Windsor 生活方式,因此它由框架调用,并且 web.config 被读入内存中的数组中。当服务代理被调用时,它只是根据契约类型拉取相关端点。

这一切都是由合约类型驱动的,因此 web.config 中不再需要任何变量。

我选择了基于代码的解决方案,因为我不在本地使用构建过程,只有当我将代码提交到 subversion 时,构建过程才会在我们的构建服务器上启动。