We have requirement where we need to consume a service (asmx or WCF) in a class then we need to consume that class in client application to get the response from service. But the problem arises as configuration of the class (which is consuming the service) in app.config will be loaded into an app.config and this configuration file will not be read if the dll is referred in the application.
This will throw the following error:
“Could not find default endpoint element that references contract ‘MyService.IService1’ 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”
1- WCF service http://localhost:54655/Service1.svc
2- DLLClass: A class library project which is consuming the WSDL, I call this project as DLLClass. Here is the implementation.
I. Create a Class Library project DLLClass.
II. Add Service Reference of the service http://localhost:54655/Service1.svc , I named it as Myservice
III. Write a method in DLLClass as below:
WCFResponse method gets the response from WCF service, http://localhost:54655/Service1.svc.
Build this class and we will be using this class in console application by referencing it as dll.
3- Create a new Console application ProxyDLLConsole and add the reference of DLLClass dll. Now we will use the WCFResponse method of Class1 in DLLClass dll to connect with the WCF service. Following implementation is the sample:
Now, press F5 to run the ProxyDLLConsole. And we get the following error.
So as per the error message, either the configuration details of the service is not available or there is no endpoint available where the service runs. But my service is up and running. So let’s concentrate on the configuration part of the error message. Let’s have a look into the app.config of ProxyDLLConsole. Below is the app.config:
Here is the problem, no details of service end point so the above error makes sense as the calling process is not able to identify the configuration details of the service. This implementation will always fail.
Now what can be done to overcome this?
Let’s have look into the app.config of DLLClass class library project.
This configuration detail is contains the service endpoint information. But it is not getting loaded when the ProxyDLLConsole loads this dll. So to get it working, copy the configuration from the app.config of DLLClass to that of ProxyDLLConsole. So your app.config of ProxyDLLConsole will look like as below:
Now let’s test it and it worked and I am getting the response.
This is happening because app.config of the executing process will always be looked up for the configuration. If the configuration is not available there the configuration should go with dll itself as build.
Note: Configuration details can be embedded into the class library itself if the service metadata are loaded at run time. This way we need not to merge the app.configs explained above.