从类库中使用 ASP.NET 应用程序中的 Web 服务

发布于 2024-10-18 02:44:29 字数 222 浏览 2 评论 0原文

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

任何想法为什么这在类库中不起作用。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

尤怨 2024-10-25 02:44:29

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

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

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

“凭据似乎不起作用,调用失败”...您能提供错误的小堆栈跟踪吗?

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

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.

内心旳酸楚 2024-10-25 02:44:29

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

  1. 右键单击解决方案资源管理器上的项目
  2. ,单击“添加服务引用”
  3. ,单击“高级”,
  4. 您将在表单末尾找到“添加 Web 引用”

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
一刻暧昧 2024-10-25 02:44:29

如果您在应用程序中添加引用,然后从类库中使用它...您如何调用类库..通过添加引用并调用类库的方法,然后如何从您需要的类库访问代理引用它...在我看来这是一个循环引用。哪个不应该首先编译...您是否正确描述了您的结构???

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

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.

逆夏时光 2024-10-25 02:44:29

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

在解决方案资源管理器上右键单击该项目
单击添加服务引用
单击高级
你会在表单的末尾找到“添加Web引用”

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

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.

小巷里的女流氓 2024-10-25 02:44:29

可能是你叫错了!!
以下是一个示例:

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();

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

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.

内心旳酸楚 2024-10-25 02:44:29

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文