views:

115

answers:

2

I have the same ASP.NET WebService deployed 3 times under 3 different URI's configured to look at 3 different databases environments.

I have a Class Library that uses the WebService using a Web Reference.

Every time I want to compile the Class Library to a different environment I have to change the Web Reference by hand to the appropriate URL.

Although not complicated this is a pain for creating Live versions of the Class Library and slows down our deployment process (A quick note there are multiple .asmx files but this example describes only 1 so that it is easier to understand).

What I am trying to do is have a DataServices class that returns an Interface for the WebService. In the Class Library all 3 Web References exist and I instantiate the correct one via simple configuration in the Class Library itself then return the right Web Service as an Interface.

This all seems straightforward to me but it isn't working as the Interface doesn't appear to work though Web Services. Can someone please confirm this for me or tell me what I'm doing wrong?

Unable to cast object of type 'Company.Project.Classes.MyWebServiceLive.MyWebService' to type 'Company.Project.Interfaces.IMyWebService'.

Thanks

EDIT The WebService is of course implementing the Interface, which is all I thought I'd need to do...

+2  A: 

You do not need to change the web reference and recompile. Simply set the Url property of the web service proxy to point at the correct service:

using (var svc = new MyWebServiceReference.MyService()) {
    svc.Url = theUrlToUse;
    return svc.SomeMethod();
}
John Saunders
Man I hope your right! Given my answer... gonna go test it now...
David A Gibson
I'm right. See http://johnwsaundersiii.spaces.live.com/blog/cns!600A2BE4A82EA0A6!435.entry. You're not the first with this problem.
John Saunders
That's awesome cheers John, I've been looking for this for such a long time but never thought to look to change the proxy class. Thanks!
David A Gibson
A: 

OK, after further research I understand that it's because the Web Reference creates a proxy in the Class Library. This proxy DOES not implement the Interface and so the conversion fails.

I can manually modify the proxies generated code files to implement the Interface which works. But then updating the Web Reference overwrites all the changes.

To add a new function would require:

  • Add function Interface
  • Add function to Web Service
  • Update Web Reference
  • Manually implement the Interface on the new proxy class for each Web Service

Unless someone can suggest something else (like how to use a WebService by generating your own proxy classes I think I'll go back to changing the Web Reference URI for each environment build... painful but not as daunting as the above!

David A Gibson