代码之家  ›  专栏  ›  技术社区  ›  Antony Scott

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

  •  4
  • Antony Scott  · 技术社区  · 14 年前

    我希望在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>
    

    配置文件

    <?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>
    

    这有可能吗?


    编辑 (更多信息)

    我已经能够从windsor.config文件访问appsettings(该文件实际上由castle windsor和自定义xml解释器处理)。

    真正的问题是我能在web.config中做到这一点吗?

    <?XML版本=“1.0”?gt;
    <配置>
    <应用程序设置>
    <add key=“AuthServiceEndpoint”value=“任何\u旧名称\u我喜欢”/>gt;
    </appsettings>
    <system.web>
    
    …
    
    <system.serviceModel>
    客户& GT;
    端点
    address=“net.tcp://localhost/authservice”
    binding=“nettcpbinding”
    contract=“myServices.contracts.iauthService”
    name=“AppSettings.AuthServiceEndpoint”
    bindingConfiguration=“nettcpbindingConfig”
    /gt;
    
    和/客户& GT;
    </System.ServiceModel>
    </configuration>
    

    IE-我的访问变量 <appSettings> 来自web.config文件的其他部分。

    4 回复  |  直到 14 年前
        1
  •  0
  •   Damian Powell    14 年前

    从我的头顶上,我想知道你是否可以用T4来做这个?我想也许你可以定义一个模板来解析web-template.config并输出web.config?当然,这只适用于单个文件。

        2
  •  0
  •   Tchami    14 年前

    你可以使用 NAnt 或用于此的msbuild。这两种配置都需要单独的配置文件,但是当您构建项目时,它们可以在web.config和其他配置文件上自动进行转换。

        3
  •  0
  •   Neil Barnwell    14 年前

    不是我能想到的。您可以在global.asax.cs的c中进行配置,而不是在xml文件中进行配置。

    或者,让构建过程编辑web.config以替换所有这些值。finalbuilder有一个neato“编辑XML文件”操作,它非常好地使用xpath来完成此操作,finalbuilder 有变量。问题解决了。这就是我在工作中做构建的方式。

        4
  •  0
  •   Antony Scott    14 年前

    我又开始回答我自己的问题了。

    我通过编写nettcpserviceLocator解决了这个问题…

    public interface INetTcpServiceLocator
    {
        EndpointAddress GetAddress(Type serviceType);
    }
    

    …还有一个自定义的配置节处理程序,它也实现了上述接口并在下面的配置节中读取…

    <services>
        <service contract="My.Services.TestService.Contracts.ITestService" address="net.tcp://localhost/TestService" />
    </services>
    

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

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

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

    所以,我的控制器调用它的服务,然后调用ITestService(在本例中是一个代理,它从定制的部分处理程序获取其端点)。

    自定义节处理程序(也是inettcpservicelocator)的windsor生活方式为“perwebrequest”,因此框架调用它,并将web.config读取到内存中的数组中。当调用服务代理时,它只是根据契约类型提取相关的端点。

    所有这些都是由契约类型驱动的,所以在web.config中不再需要任何变量。

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