1

I have a .DLL that makes calls to a WCF service. The DLL is loaded by a separate program that makes method calls to the DLL and the DLL decides whether or not to use WCF by a flag that is passed in the method signatures. This works normally when I have the WCF binding information in the application, but I do not want to have to put the WCF binding information in the application. I have tried the following in my .DLL to get the WCF binding information from the DLL's app.config, but each time I try it, I get the following error:

Could not find endpoint element with name 'Service' and contract 'Service.IService' 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.

Here is the code I am using to try and get the WCF binding/settings from the DLL's app.config:

        private static readonly Configuration Config = LoadWCFConfiguration();

    public void CreateOpsConnection (AccountingOpConnectionDTO opsConnectionDTOFromCaller, bool UseWCFValue)
    {
        opsConnectionDTO = opsConnectionDTOFromCaller;
        UseWCF = UseWCFValue;

        if (UseWCF == true)
        {

            ConfigurationChannelFactory<AccountingOpsServiceReference.IAccountingOperationsService> accountingOpsChannelFactory = new ConfigurationChannelFactory<AccountingOpsServiceReference.IAccountingOperationsService>
            ("AccountingOperationsService", Config, null);
            WCFClient = accountingOpsChannelFactory.CreateChannel();
            //WCFClient = new AccountingOpsServiceReference.AccountingOperationsServiceClient();
            WCFClient.CreateConnection(opsConnectionDTO);
Stevie White
  • 569
  • 1
  • 7
  • 24
  • 1
    You can't do this - the .NET config system is built on the assumption that only the **main program** (the console app, Winforms program, the web site) has a config file that is being read out. Don't fight against it - accept it and go with the flow ! – marc_s Oct 24 '16 at 19:17

2 Answers2

1

While I feel that this is somewhat of a hack, I was able to get the endpoint/connection string from the .DLL's config file using the following code. Thanks to answers located here:

Programatically adding an endpoint

And Here:

How to programmatically change an endpoint's identity configuration?

        private static string LoadWCFConfiguration()
    {
        XmlDocument doc = null;
        Assembly currentAssembly = Assembly.GetCallingAssembly();
        string configIsMissing = string.Empty;
        string WCFEndPointAddress = string.Empty;
        string NodePath = "//system.serviceModel//client//endpoint";

        try
        {
            doc = new XmlDocument();
            doc.Load(Assembly.GetEntryAssembly().Location + ".config");
        }
        catch (Exception)
        {

            configIsMissing = "Configuration file is missing for Manager or cannot be loaded. Please create or add one and try again.";

            return configIsMissing;
        }

        XmlNode node = doc.SelectSingleNode(NodePath);

        if (node == null)
            throw new InvalidOperationException("Error. Could not find the endpoint node in config file");

        try
        {
            WCFEndPointAddress = node.Attributes["address"].Value;
        }
        catch (Exception)
        {

            throw;
        }

        return WCFEndPointAddress;
    }

Afterwords the only thing left to do is is instantiate the client object with the endpoint address returned from the above method:

System.ServiceModel.BasicHttpBinding binding = new System.ServiceModel.BasicHttpBinding();

System.ServiceModel.EndpointAddress endpoint = new System.ServiceModel.EndpointAddress(new Uri(wcfEndpoint));

            WCFClient = new ServiceReference.ServiceClient(binding, endpoint);
Community
  • 1
  • 1
Stevie White
  • 569
  • 1
  • 7
  • 24
0

Copy endpoint configuration from DLL config to application configuration file. The application cannot read the DLL configuration file.

selami
  • 2,478
  • 1
  • 21
  • 25