• WCF hosted in IIS reading configuration section in *.config other than web.config


    Sample: we have Test folder under the application root, we want to read the connection string in test.config in the Test folder.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.ServiceModel;
    using System.Text;
    using System.Web;
    using System.Web.Configuration;
    using System.Configuration;
    using System.ServiceModel.Activation;
    
    namespace WebApplication1
    {
        public interface IService1
        {
            [OperationContract]
            string DoWork();
        }
        // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
         [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
        public class Service1 : IService1
        {
            public void DoWork()
            {
                String physDir = HttpContext.Current.Request.PhysicalApplicationPath;
                WebConfigurationFileMap fileMap = new WebConfigurationFileMap();                    
                
                VirtualDirectoryMapping vDirMap =new VirtualDirectoryMapping(physDir + "Test", true);
                
                fileMap.VirtualDirectories.Add("/Test", vDirMap);
                VirtualDirectoryMapping vDirMapBase = new VirtualDirectoryMapping(physDir, true, "test.config");            
                fileMap.VirtualDirectories.Add("/", vDirMapBase);
    
                System.Configuration.Configuration config = WebConfigurationManager.OpenMappedWebConfiguration(fileMap,"/Test");
    
                ConnectionStringsSection connectionStringsSection =
                     config.GetSection("connectionStrings")
                     as ConnectionStringsSection;        
            }
        }
    }
  • 相关阅读:
    前端开发神器
    React表单明文密文切换,携带禁止浏览器自动回填,简单验证提示功能
    webapp 虚拟键盘隐藏留下空白解决办法
    jQuery常用表单事件执行顺序
    localStorage+cookie实现存取表单历史记录
    js.cookie.js使用方法
    H5超细边框
    JS删除数组中某个元素
    JS获取地址栏参数(支持中文)
    React书写规范
  • 原文地址:https://www.cnblogs.com/LeoTang/p/2799272.html
Copyright © 2020-2023  润新知