21

I wrote an asmx webSerivce on srv1. I wrote a bll project of an asp.net (original text: an asp.net) project on srv2. Both are hosted under the same web domain

I want to call the asmx from the bll project of the asp.net (original text: asp.net(c#) code behind).

1) I added a web reference, but couldn't find any tutorial how to really call the referenced service.

I have tried:

private void GetTemplateComponentsData()
{
    var service = new ServiceReference.GetTemplateParamSoapClient();
    TemplateParamsKeyValue[] responsArray = service.GetTemplatesParamsPerId(id);

    foreach (var pair in responsArray)
    {
        TemplateComponentsData.Add(pair.Key, pair.Value);
    }
}

but get the following error when executing the first line: Could not find default endpoint element that references contract 'ServiceReference.GetTemplateParamSoap' 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.

What am I missing?

2) I plane to migrate the asp.net proj and asmx together from one domain to another. Is there any way to reference this webservice relatively ?

Elad Benda
  • 35,076
  • 87
  • 265
  • 471

1 Answers1

42

OK, let me try to rephrase your scenario to make sure I got it right:

  1. You have an ASMX web service hosted on some domain.
  2. You have an ASP.NET application hosted on the same or a different domain (it doesn't really matter) from which you want to to consume this ASMX web service using a WCF client (svcutil).

The first step is to add a Service Reference to the ASP.NET application by pointing to the WSDL of the ASMX service:

enter image description here

This will do 2 things:

  • It will add a ServiceReference to your web application

enter image description here

  • It will modify your web.config and include the client endpoints:

    <client>
      <endpoint address="http://ws.cdyne.com/NotifyWS/phonenotify.asmx"
        binding="basicHttpBinding" bindingConfiguration="PhoneNotifySoap"
        contract="ServiceReference1.PhoneNotifySoap" name="PhoneNotifySoap" />
      <endpoint address="http://ws.cdyne.com/NotifyWS/phonenotify.asmx"
        binding="customBinding" bindingConfiguration="PhoneNotifySoap12"
        contract="ServiceReference1.PhoneNotifySoap" name="PhoneNotifySoap12" />
    </client>
    

Now when you want to invoke this service from your application you will have to choose the endpoint you want to use:

using (var client = new ServiceReference1.PhoneNotifySoapClient("PhoneNotifySoap"))
{
    var result = client.GetVersion();
}

Now simply replace my code snippets with your actual service names.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • My bad, I need to call this asmx from the bll project (library project), so I need to add manually the elements to the web.config? – Elad Benda Sep 22 '11 at 08:05
  • 2
    @Elad Benda, yes you do need a client endpoint in the web.config of the ASP.NET application that uses this library. You could also configure the endpoint programmatically in the BLL but it is better practice to have a config section as it will allow you to more easily change settings such as urls, bindings, ... – Darin Dimitrov Sep 22 '11 at 08:06
  • I amazed by your detailed answer. thanks a bunch ! Can I ask on what site did you host you images? – Elad Benda Sep 22 '11 at 09:27
  • @Elad Benda, I use the add image button in the answer area. Images are automatically hosted on `imgur.com`. – Darin Dimitrov Sep 22 '11 at 09:39
  • what con/pros for using Advanced --> Add Web Reference ( for .net 2.0 version asmx service ) – Zakos Aug 31 '15 at 07:18