views:

2973

answers:

4

I come across this problem when i am writing an event handler in SharePoint. My event handler has a web reference. When i create this web reference, the URL of the web service will be added in the .config file of the assembly. If i have to change the web reference URL i just have to change the link in the config file.

Problem comes when I try to GAC the dll. When i GAC the DLL, the config file cannot be GACed along with the dll, and hence, there is no way for me to update the web reference.

One workaround i have found is to modify the constructor method Reference.cs class which is autogenerated by visual studio when i add a reference, so that the constructor reads the web service url from some other location, say a registry or an XML file in some predetermined location. But this poses a problem sometimes, as when i update the web referenc using visual studio, this Reference.cs file gets regenerated, and all my modifications would be lost.

Is there a better way to solve this problem?

+1  A: 

If you have Visual Studio 2008, use a Service Reference instead of a Web Reference, which will generate partial classes that you can use to override functionality without your code overwritten by the generator.

For Visual Studio 2005, you could just add the partial keyword to the class in Reference.cs and keep a separate file with your own partial class:

public partial class WebServiceReference
 { public WebServiceReference(ExampleConfigurationClass config) 
    { /* ... */
    }
 }

WebServiceReference svc = new WebServiceReference(myConfig);
Mark Cidade
oh ok, Is there any 3rd party partial class generator which i can use to generate this partial class so that i can use in in VS2005? if so, then may be i can compile the web reference class into a DLand refer that DLL in my project.L
ashwnacharya
+1  A: 

Any application hosted by SharePoint is using the web.config located at the root of your SharePoint web site in IIS. What you need to do is add the configuration generated by the Web/Service Reference wizard to your web.config.

This is roughly how it works:

  • SharePoint application pool loads your DLL
  • Your DLL looks for the service information in the current application configuration file
  • Your DLL finds web.config and looks for configuration information there

Basically, the app.config that is being generated in your DLL is not used. As the application in this case is the Application Pool (w3wp.exe) that is hosting the SharePoint application. For SharePoint the app.config is actually named web.config and exists at the root of the SharePoint website.

spoon16
Why did this get down voted... is it incorrect? There is no reason the config can't go into the web.config
spoon16
if it's only one downvote don't take it personally, usually means one of the other posters thought their answer was better; 2 downvotes is a problem, 3 means you're probably wrong, 4 means you're almost certainly wrong :)
jcollum
I know, I was just wondering if there was a real problem with this answer.
spoon16
A: 

You could try this: Rather than using the dynamic web reference make it a static reference so that the code in Reference.cs won't go looking for a value in the .config file for the url. Then sub-class the generated web service client code and in that derived class, add your own logic to set the .Url property. Then VS.NET can re-gen Reference.cs all it likes, and your url setting code will remain. Of course, you have to update any downstream code to use your derived class, but that should be a simple global replace.

ZeroBugBounce
A: 

I resolved this by making the web reference dynamic for my class library and then copying the applicationSettings config section containing the web reference from the app.config file into my Sharepoint site web.config.

Note you will also need to copy the entry for applicationSettings into your web.config as this is not in there normally.