从类库中使用asp.net应用程序中的Web服务

时间:2021-12-09 01:40:15

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.

我的团队和我有一个asp.net Web表单应用程序,并使用几个类库。在其中一个库中,我们尝试使用Web服务。 Web引用已添加到Web应用程序项目中,并添加了相应的引用。该应用程序编译。尝试在类库中使用所述Web服务时,凭据似乎不起作用,并且调用失败。但是,如果我们从类库中取出Web服务调用,并在Web应用程序中使用它,它就可以工作。

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

任何想法为什么这不适用于类库。

6 个解决方案

#1


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.

确保您输入了正确的凭据用户名和密码,并确保您已将Web服务发布到您访问它的位置。

#2


2  

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

仔细检查配置文件是否包含Web服务的正确信息。

Try changing the URL behavior to dynamic as well.

尝试将URL行为更改为动态。

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.

此外,正如约翰所说,我假设您正在将服务添加到类库中,因为您打算在库中使用它,而不是Web应用程序的其他区域。

"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.

只是为了澄清一下,在我当前的项目中,我们使用带有绑定和凭据的类库中的WCF端点。在您尝试时,可以对SOAP ASMX Web引用执行相同的操作。

#3


1  

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

您可以通过执行以下步骤添加Web服务引用:

  1. right click on the project on the Solution Explorer
  2. 右键单击Solution Explorer上的项目

  3. click Add Service Reference
  4. 单击添加服务引用

  5. click Advanced
  6. you will find "Add Web Reference" at the end of the form
  7. 您将在表单的末尾找到“添加Web引用”

#4


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.

最好只添加一个带有Web引用的简单项目,然后在需要它的所有项目上添加该项目的引用。

#5


1  

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.

您是否在Web应用程序的配置文件中定义了任何凭据信息?如果是这样,类库可能无法正确获取它们。只是一个猜测。约翰桑德斯是对的。似乎有点向后阅读您对应用程序结构的描述。

#6


1  

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

您可以通过执行以下步骤添加Web服务引用:

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

右键单击Solution Explorer上的项目单击Add Service Reference,单击Advanced,您将在表单的末尾找到“Add Web Reference”

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

通过@AMgdy的解决方案,它将自动生成一个Reference.cs类。它定义了webservices的所有方法。

#1


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.

确保您输入了正确的凭据用户名和密码,并确保您已将Web服务发布到您访问它的位置。

#2


2  

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

仔细检查配置文件是否包含Web服务的正确信息。

Try changing the URL behavior to dynamic as well.

尝试将URL行为更改为动态。

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.

此外,正如约翰所说,我假设您正在将服务添加到类库中,因为您打算在库中使用它,而不是Web应用程序的其他区域。

"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.

只是为了澄清一下,在我当前的项目中,我们使用带有绑定和凭据的类库中的WCF端点。在您尝试时,可以对SOAP ASMX Web引用执行相同的操作。

#3


1  

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

您可以通过执行以下步骤添加Web服务引用:

  1. right click on the project on the Solution Explorer
  2. 右键单击Solution Explorer上的项目

  3. click Add Service Reference
  4. 单击添加服务引用

  5. click Advanced
  6. you will find "Add Web Reference" at the end of the form
  7. 您将在表单的末尾找到“添加Web引用”

#4


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.

最好只添加一个带有Web引用的简单项目,然后在需要它的所有项目上添加该项目的引用。

#5


1  

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.

您是否在Web应用程序的配置文件中定义了任何凭据信息?如果是这样,类库可能无法正确获取它们。只是一个猜测。约翰桑德斯是对的。似乎有点向后阅读您对应用程序结构的描述。

#6


1  

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

您可以通过执行以下步骤添加Web服务引用:

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

右键单击Solution Explorer上的项目单击Add Service Reference,单击Advanced,您将在表单的末尾找到“Add Web Reference”

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

通过@AMgdy的解决方案,它将自动生成一个Reference.cs类。它定义了webservices的所有方法。