C# – How to move service references to their own assembly


A little background:

I'm creating a set of adapters to allow communication with mobile devices over different cellular networks. This will be achieved using a class factory pattern. At least one of the networks requires a service reference to communicate with their devices through a web service.

So far I've got 3 assemblies so far which represent:

  • An assembly which contains the main adapter library: this contains
    • The interface definition for each of the adapters
    • Base classes
    • The class factory to instantiate the specified adapter at runtime.
  • An assembly for each network adapter implementation.
  • An assembly that contains my main application.

Given that I don't want to be adding service references and their configuration to the main application assembly [as that's not relevant to the main application], how do I force each assembly's service reference to get its configuration from its own app.config?

If I have the service reference configuration in the main app.config, everything works just fine, but if I move the configuration to the adapter's app.config everything stops working throwing the following exception at the point where I new up the Soap1Client.

"Could not find default endpoint element that references contract 'MobileService.Service1Soap' 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 contract could be found in the client element."

Best Solution

In the end, I just removed the service reference and added a web reference [i.e. did it the 2.0 way]. For some reason the web reference will access its own app.config instead of the main application's app.config.

Far easier than the alternative...

Related Question