ExcelDNA:如何使 WCF 配置文件可供我的 Excel 加载项使用?

ExcelDNA: How to make a WCF configuration file available to my Excel AddIn?

我正在尝试在 .XLL Excel 加载项的上下文中使用 WCF 服务。我正在使用 Excel 的 C-API,因为 ExcelDNA 库向 .NET 公开。函数代码定义如下:

[ExcelFunction(Description = "Getting the WCF stack trace")]
public static string GetWCFStackTrace(string name)
{
    try
    {
        HTTPService.ExcelServiceClient client = new HTTPService.ExcelServiceClient("BasicHttpBinding_IExcelService");
        client.LogExcelRows("Hello World");
    }
    catch (Exception e)
    {
        Debug.WriteLine(e);
    }
    return "function complete";
}

这导致堆栈跟踪,如下所示。我已经测试过,在加载项的执行上下文中,我确实可以访问由服务引用生成的代理 class(即 typeof(HTTPService.ExcelServiceClient) returns 在运行时正确)。

   Exception thrown: 'System.InvalidOperationException' in System.ServiceModel.dll
    System.InvalidOperationException: Could not find endpoint element with name 'BasicHttpBinding_IExcelService' and contract 'HTTPService.IExcelService' 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.
       at System.ServiceModel.Description.ConfigLoader.LoadChannelBehaviors(ServiceEndpoint serviceEndpoint, String configurationName)
       at System.ServiceModel.ChannelFactory.ApplyConfiguration(String configurationName, Configuration configuration)
       at System.ServiceModel.ChannelFactory.ApplyConfiguration(String configurationName)
       at System.ServiceModel.ChannelFactory.InitializeEndpoint(String configurationName, EndpointAddress address)
       at System.ServiceModel.ChannelFactory`1..ctor(String endpointConfigurationName, EndpointAddress remoteAddress)
       at System.ServiceModel.ConfigurationEndpointTrait`1.CreateSimplexFactory()
       at System.ServiceModel.ConfigurationEndpointTrait`1.CreateChannelFactory()
       at System.ServiceModel.ClientBase`1.CreateChannelFactoryRef(EndpointTrait`1 endpointTrait)
       at System.ServiceModel.ClientBase`1.InitializeChannelFactoryRef()
       at System.ServiceModel.ClientBase`1..ctor(String endpointConfigurationName)
       at ExcelDNATest.HTTPService.ExcelServiceClient..ctor(String endpointConfigurationName) in C:\Users\zach\Documents\Visual Studio 2015\Projects\ExcelDNATest\ExcelDNATest\Service References\HTTPService\Reference.cs:line 37
       at ExcelDNATest.TestFunctions.HelloDna(String name) in C:\Users\zach\Documents\Visual Studio 2015\Projects\ExcelDNATest\ExcelDNATest\TestFunction.cs:line 163

如何在 ExcelDNA 编译代码中包含端点配置?

=== 编辑

答案如下

查看 Google 组中的讨论 HERE,Excel-DNA 作者提到我应该看到

... .config file in the output directory next to the .xll file ...

\bin\Debug\ 目录中,我看到一个名为 <slnName>.dll.config 的文件,我将其重命名为 <slnName>-AddIn.xll.config,并且该服务现在按预期工作。我想我也必须有一个 <slnName>-AddIn64.xll.config 文件。但会拭目以待。

顺便说一下,我想我在 Google 组线程中看到了配置文件没有被复制到输出文件夹中的提及。但是,我发现配置文件被复制到输出文件夹,我只需要重命名它。我还应该注意,我正在使用 Excel-DNA v0.33.9.

输出目录中配置文件的名称需要与 ExcelDNA 创建的 .XLL 文件的名称相匹配。

例如

MyApp-AddIn.xll -> MyApp-AddIn.xll.config

MyApp-AddIn64.xll -> MyApp-AddIn64.xll.config