简体   繁体   中英

Consume web service in asp.net app from a class library

My team and I have a asp.net web forms application and are using several class libraries. In one of those libraries, we are trying to consume a web service. The web reference was added in the web app project and the appropriate references have been added. The app compiles. When attempting to consume said web service in the class library, the credentials don't seem to work, and the call fails. However, if we take the web service call out of the class library, and consume it within the web app, it works.

Any ideas why this is not working in the class library.

Double check your configuration file includes the correct information for the Web service.

Try changing the URL behavior to dynamic as well.

Also, as John stated, I'm assuming you're adding the service to the class library because you intend to use it from the library, as opposed to other areas of the Web application.

"the credentials don't seem to work, and the call fails"...can you give a small stack trace of the error?

Just to clarify, in my current project, we use WCF endpoints within a class library with bindings and credentials. The same can be done for a SOAP ASMX Web reference as you're attempting.

You can add a web service reference by doing the following steps:

right click on the project on the Solution Explorer click Add Service Reference click Advanced you will find "Add Web Reference" at the end of the form

By @AMgdy 's solution,It'll auto generate a Reference.cs class.It defined all of method of webservices.

You can add a web service reference by doing the following steps:

  1. right click on the project on the Solution Explorer
  2. click Add Service Reference
  3. click Advanced
  4. you will find "Add Web Reference" at the end of the form

If you are adding the reference in application and then consuming it from class library... How you call the class library.. by adding reference and invoking the method of class library and then how you are accessing proxy from the class library you need to reference it... It seems to me a circular reference. Which shouldn't be compiled at first place... Are you describing your structure correctly???

It's always better to add a simple project with just web reference and then add the reference of this project on all the projects which requires it.

Have you defined any credential information in a config file in the web app? If so, the class library probably can't fetch them correctly. Just a guess though. And John Saunders is right. Seems a bit backwards reading your description of your apps structure.

May be you called it wrong!! Here is an example:

var serviceName = new ServiceName
    {
        Credentials = new NetworkCredential("Username", "Password", "Domain"),
        Url = "Here you put the correct url of the web service if you published somewhere else"
    };
serviceName.CallWebMethod();

make sure that you entered the correct Credential username and password and make sure the you published the webservice to a place you access it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM