0

I wrote a web-service. I wrote a website. I want the website BLL code to call the web-service.

I have a config table with this service URL. I inject the web-service URL to the calling code. What web client or socket in C# should I use that can receive a dynamic web-service URL?

I thought to use:

WebClient webClient = new WebClient();
UTF8Encoding response = new UTF8Encoding();
string originalStr = response.GetString(webClient.DownloadData(BLLConfig.Current);

But maybe there is more elegant way?

I'm loading the configs at run time from a DB table.

Here is how I tried to use a web-reference in Visual Studio:

using (var client = new GetTemplateParamSoapClient("GetTemplateParamSoap"))
{
    TemplateParamsKeyValue[] responsArray = client.GetTemplatesParamsPerId(CtId, tempalteIds.ToArray());

    foreach (var pair in responsArray)
    {
        string value = FetchTemplateValue(pair.Key, pair.Value);
        TemplateComponentsData.Add(pair.Key, value);
    }
}
DanM7
  • 2,203
  • 3
  • 28
  • 46
Elad Benda
  • 35,076
  • 87
  • 265
  • 471
  • when you say that you load the configs from a db table - do you mean just different URLs for the webservices (then see my answer and follow the links) OR different WSDLs (really different functions in those web services) ? – Yahia Sep 27 '11 at 15:10
  • @Yahia, just the url. same WSDL – Elad Benda Sep 27 '11 at 15:15

2 Answers2

1

You can add the URL of the web service as a Web Reference in Visual Studio and then set the Service.URL property to the value from the config

msmucker0527
  • 5,164
  • 2
  • 22
  • 36
  • Not sure I know how to do it see my code in the original msg. – Elad Benda Sep 27 '11 at 15:10
  • 1
    It looks like it was added as a "Service Reference" instead of a web reference, to add a "Web Reference" you right click on Service References and click Add new Service Reference, then click the Advanced button, then click "Add a Web Reference" – msmucker0527 Sep 27 '11 at 16:54
  • Is it better to use Web-Reference or Service-reference? – Elad Benda Sep 27 '11 at 17:31
  • Service references give you more control through customizable bindings but also add more complexity, when i'm consuming an ASMX web service I generally use a web reference. – msmucker0527 Sep 27 '11 at 17:45
  • I couldn't find the: click "Add a Web Reference". I work with visual-studio 2010 – Elad Benda Sep 28 '11 at 07:04
  • Right click on your project and click "Add Service Reference" from the menu. At the bottom left of the new window that pops up, click the "Advanced..." Button, at the bottom of the window there should be a button titled "Add Web Reference..." in the Compatibility section – msmucker0527 Sep 28 '11 at 13:56
0

.NET has lots of built-in support for consuming web services... after adding the service reference to your project it generates the necessary code... whcih you can use as is - if you need to configure the URL the generated client class has a URL property which you can set accordingly... for an excellent walkthrough see http://johnwsaunders3.wordpress.com/2009/05/17/how-to-consume-a-web-service/ and see SOAP xml client - using Visual Studio 2010 c# - how?

Community
  • 1
  • 1
Yahia
  • 69,653
  • 9
  • 115
  • 144