Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically set URL of web service

Scenario: I'm running code on the client that connects to a server and uses a web service to retrieve data about a SharePoint list. I'm using the Visual Studio 2010 "service reference" to get a web service for my SP Site and get my data from the list. It works. Now how do I go about coding it such that when I want to move from Test to Production, my web service calls will still work? Note that the web service is a SharePoint web service, I did not write it. I am only using it. Is what I am suggesting possible? I do have the ability to ensure that the Site is the exact same (except for URLS) on both environments (e.g. backup the SP site and put it on production). Thanks for any suggestions.

Summary:

Basically I'm looking for the best way to go from test to production without re-compiling my code which consumes the SP web service. Also, as a side note, if anyone knows how similar the test/production sharepoint sites have to be, [in order for the web service to work on both without anything but the URL being changed].. that would be helpful info.

Solution

The project configuration file can be used to specify the web service location. The .svcdatamap and other files in the VS project are for design time use only, and the URL which is actually used to connect to the SharePoint web service is passed as an argument to the System.Data.Services.Client.DataServiceContext object. This is only tangentially related, but to create your own WCF web service see this link. BTW, the web service will work without recompilation anywhere the SharePoint List has the same List name and the column you're querying has the same name.

like image 414
KyleM Avatar asked Jul 22 '11 20:07

KyleM


2 Answers

As far as I know, Visual Studio Tools for Office projects allow you to have an app.config file in your project. I'd expect that Visual Studio created the app.config file and added the necessary configuration settings related to the web service reference. In any case, you need to store the correct web service url somewhere - app.config, registry or even a database.

If you are not able to store the web service endpoint address information in the app.config file, there's a way to configure the proxy manually.

  1. If it's a .asmx reference added as a legacy "Web Reference" in Visual Studio then all you need to do is set the Url property value of the proxy object before you call any web service methods. For example:

    MyASMXWebService proxy = new MyASMXWebService();
    proxy.Url = "web service url";
    proxy.HelloWorld();
    
  2. If it's a .svc WCF service reference then things get a bit more complicated. You'll need to create your web service endpoint programmatically. For example:

    BasicHttpBinding binding = new BasicHttpBinding();
    EndpointAddress endpoint = new EndpointAddress("web service url");
    
    ChannelFactory<IMyWCFWebService> factory = new ChannelFactory<IMyWCFWebService>(binding, endpoint);
    IMyWCFWebService proxy = factory.CreateChannel();
    
    proxy.DoWork();
    
like image 72
Vassili Altynikov Avatar answered Nov 11 '22 02:11

Vassili Altynikov


Since SharePoint services (SVC) do not provide a proxy, option 2 will not work. References to both locations must be included in your application and then use a parm to differentiate production from test. I believe 2013 will likely correct this bug.

like image 37
Clem Avatar answered Nov 11 '22 02:11

Clem