0

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.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • You should add the service reference to the class library if you're using it from the class library. – John Saunders Feb 16 '11 at 15:21
  • Are you referring to adding the actual web reference, or the System.Web.Services reference. –  Feb 16 '11 at 15:27
  • 1
    I'm proposing you use "Add Service Reference" instead of "Add Web Reference", and add it to the class library. – John Saunders Feb 16 '11 at 20:20
  • sounds like Brian helped lead you to the solution, consider marking his answer as the correct one. – NotMe Feb 16 '11 at 20:29
  • http://stackoverflow.com/questions/13188860/get-custom-class-from-webservices I think you can see a solution for your question in this link. I have a problem same your question.I want to get object from webservices without calling AddWebReference. Thanks – Brian Nov 03 '12 at 02:41

6 Answers6

2

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.

  • The request failed with HTTP status 401: Unauthorized. An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. –  Feb 16 '11 at 15:44
  • [WebException: The request failed with HTTP status 401: Unauthorized.] System.Web.Services.Protocols.SoapHttpClientProtocol.ReadResponse(SoapClientMessage message, WebResponse response, Stream responseStream, Boolean asyncCall) +1484609 System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke(String methodName, Object[] parameters) +221 –  Feb 16 '11 at 15:45
  • TRM.WebApp.webservices.clearview.com.ClearviewServicesV2.GetDTOContactV2ByAccountAndSSN(String accountNumber, String ssn) in C:\WINDOWS\Profiles\mmcdowell\My Documents\VS2010\TRM\Development-IdentityManagement\WebApp\Web References\webservices.clearview.com\Reference.cs:336 –  Feb 16 '11 at 15:45
  • TRM.Identity.Management.ASPSecuritySetupService.GetClearviewContactForAccountAndSSN(String accountNumber, String ssn) in C:\WINDOWS\Profiles\mmcdowell\My Documents\VS2010\TRM\Development-IdentityManagement\WebApp\OAMIdentityManagement\ASPSecuritySetupService.cs:558 Accounts_Account_Login.LoginButton_OnClick(Object sender, EventArgs e) in C:\WINDOWS\Profiles\mmcdowell\My Documents\VS2010\TRM\Development-IdentityManagement\WebApp\Account-Registration\Account-Login.aspx.cs:237 –  Feb 16 '11 at 15:46
  • System.Web.UI.WebControls.Button.OnClick(EventArgs e) +118 System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +112 System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563 –  Feb 16 '11 at 15:46
  • Also, the service works when allowing anonymous users but not with authenticated users. –  Feb 16 '11 at 15:47
  • 1
    Based on the stack trace, I'm with daft on this one...you are probably missing a config entry for credentials. – Brian Swiger Feb 16 '11 at 16:11
1

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.

Brian
  • 163
  • 2
  • 18
1

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
Ahmed Magdy
  • 5,956
  • 8
  • 43
  • 75
1

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.

S M Kamran
  • 4,423
  • 7
  • 25
  • 35
0

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.

Henric
  • 1,380
  • 2
  • 16
  • 30
  • Ok, got everything to work. Added web reference in class library, using class library in web app. All compiles and works. As it turns out, had fat fingered the password in the appSettings for the particular user. Thanks for all the help. :) –  Feb 16 '11 at 16:19
0

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.

Ahmed Magdy
  • 5,956
  • 8
  • 43
  • 75