Consume WCF service in a library
Windows Communication Foundation (WCF) is an excellent technology for constructing and consuming web services. Though web services are complicated under the hood, MS Visual Studio (VS) makes both the construction and consumption of them very easy. All the tedious, non-business logic related work is done by VS after a few mouse clicks. For WCF consumption, the key component created by VS is the proxy class that encapsulates all the plumbing work. The information of the consumed web service is stored in code (e.g. C#) and a configuration file. Unfortunately, this structure creates an annoying problem which is described as following:
Suppose a WCF service is so useful and widely applicable that one wants to place it in a function of a library so that it can be called by different applications. One can add a reference to this WCF service in the library project under VS, a proxy class for the service will be generated automatically by VS, and the configuration information is put in the library's app.config file. When a project calls the library's function, an error will occur - "Could not find default endpoint element that references contract ...". This is because the application cannot find the binding and endpoint information from its configuration file. To avoid this error, the WCF configuration information in the library's configuration file needs to be copied to the project's configuration file. This needs to be done for every application that calls the function consuming the WCF service. However, The best way is to bypass configuration files completely for the service, and configure end poins and binding in the code of the library.