Class library with service references

bloparod picture bloparod · Jul 7, 2009 · Viewed 11.4k times · Source

I have a class library (.NET) with a reference to a web service (in some server, not a project in the same solution). The class library has a class that is exposed to COM. This class invokes the web service.

When I add the service reference, this adds code to the class library's app.config.

I also have a desktop application in the same solution, just for test purposes. When I run this application, it throws this exception:

Could not find default endpoint element that references contract 'ServiceProxy.EventsServices' 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.

This exception can be solved by copying and pasting the generated code for the service reference in the class library's app.config into the desktop application' config file.

When I deploy, I have to deploy only the DLL (built from the class library) and not the desktop app. I need to include the service reference in a config file that can be read by the DLL.

Any suggestions?

Thanks!

Answer

David Lay picture David Lay · Jul 7, 2009

When you add a service reference, Visual Studio generates a proxy for you, which reads the app.config file for the url of the service.

You have the option to provide static URL wich does not uses the config file.

If you want to get complicated, and provide dynamic URL without the app.config settings, you could copy this generated code and modify it to use another kind of source for the configuration data (like parameters, for example) and that way you can deploy just the dll.

The generated code is hidden in the code behind of the service reference. In order to see the code, you have to activate the "show all files" option for the solution explorer, and look for the file Refecence.cs (or .vb) under Reference.map file.

This code you should not modify directly, instead, copy the code and then create a new class within your structure. (and delete the original reference)

Take notice, that if you modify the service (or the Wsdl) then you must modify the class manually.