简体   繁体   中英

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

I'm trying to consume a WCF service in the context of a .XLL Excel Add-in. I'm using Excel's C-API as exposed to .NET by the ExcelDNA library. The function code is defined like this:

[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";
}

Which results in the stack trace as shown below. I have tested that within the execution context of the Add-In I do have access to the proxy class as generated by the service reference (ie typeof(HTTPService.ExcelServiceClient) returns correctly during runtime).

   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

How do I include the endpoint configuration within the ExcelDNA compiled code?

=== EDIT

The answer is below

The name of the configuration file in the output directory needs to match the name of the .XLL file that is created by ExcelDNA.

eg

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

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

Looking at the discussion in Google Groups HERE , The Excel-DNA author mentioned I should see the

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

In the \\bin\\Debug\\ directory I saw a file called <slnName>.dll.config that I renamed to <slnName>-AddIn.xll.config and the service is working as expected now. I guess I would also have to have a <slnName>-AddIn64.xll.config files as well. But will wait and see.

Incidentally, I think I saw mention in the Google Group thread that the config file doesn't get copied to the output folder. However, I found that the config file WAS copied to to the output folder and I just had to rename it. I should also note that I'm using Excel-DNA v0.33.9 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM