Excel 加载项未加载带有服务参考配置信息的 app.config [英] Excel Add-In not loading app.config with service reference config information

查看:24
本文介绍了Excel 加载项未加载带有服务参考配置信息的 app.config的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个带有服务引用的应用程序,用于对特定 URL 进行网络服务调用,并且效果很好.我想将此代码移动到 Excel 插件中,但我遇到了这个问题:

I've written an app with a service reference to make web services calls to a specific URL and it works great. I want to move this code into an Excel Add-In, but I run into this problem:

未处理的异常消息:在 ServiceModel 客户端配置部分中找不到名称为ConnectivityHttpsSoap12Endpoint"且合同为Connectivity.ConnectivityPortType"的端点元素.这可能是因为找不到您的应用程序的配置文件,或者因为在客户端元素中找不到与此名称匹配的端点元素.

Unhandled Exception Message: Could not find endpoint element with name 'ConnectivityHttpsSoap12Endpoint' and contract 'Connectivity.ConnectivityPortType' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this name could be found in the client element.

问题是我的 app.config(带有服务引用信息)没有被加载,因为 Excel 正在调用我的类库,并且调用应用程序的 app.config 需要将服务引用添加到它.但这可以用 Excel 完成吗?更好的是,有没有办法从代码中加载我的 app.config?

The problem is that my app.config (w/the service reference info) isn't being loaded because Excel is calling my class library, and the app.config of the calling application would need to have the service reference added to it. But can that be done with Excel? Better yet, is there a way to just load my app.config from code?

推荐答案

您需要使用 ConfigurationManager 打开它.您可以在调用程序集路径(通常)中找到您的 app.config 文件,因此您可以编写这样的方法:

You would need to open it using the ConfigurationManager. You can find your app.config file in the calling assembly path (usually), so you could write a method like this:

public static Configuration LoadLocalConfigurationFile(string fileName)
{
    // fileName is the configuration file you want to open
    var configMap = new ExeConfigurationFileMap 
    { 
        ExeConfigFilename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName)
    };

    return ConfigurationManager.OpenMappedExeConfiguration(
        configMap, ConfigurationUserLevel.None);
}

这篇关于Excel 加载项未加载带有服务参考配置信息的 app.config的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆