从 IIS 7.5 托管的 WCF 执行 HttpWebRequest

发布于 2024-12-09 00:18:56 字数 865 浏览 1 评论 0原文

我有以下奇怪的情况。

我尝试在 IIS 7.5 Express 中托管的 WCF 服务中执行以下代码序列:

 var request = WebRequest.Create(some_uri) as HttpWebRequest;

 request.Method = "GET";
 request.Credentials = CredentialCache.DefaultNetworkCredentials;

 request.GetResponse();

从 WCF 服务执行时,出现以下异常:

[WebException:无法连接到远程服务器]

及其内部异常:

[SocketException:无法建立连接,因为目标 机器主动拒绝127.0.0.1:400]

来自控制台应用程序相同代码序列已成功执行。

我对在 IIS 7.5 Express 下运行的代码尝试了以下操作:

  1. 将代理显式设置为从 IE 复制设置的请求对象(在那里使用代理)并且它有效。
  2. 显式地将代理设置为 null 并且它也有效。

现在,我有几个问题:

  1. 为什么在 IIS 7.5 下初始代码不起作用,但在控制台应用程序中一切都按预期工作?
  2. 原始代码和第一个解决方案有什么区别?据我了解,如果我不指定代理,默认情况下,将应用 IE 代理设置。然而,事实证明这些解决方案并不等效。
  3. 是否还有其他解决方案可以使来自 WCF 的呼叫成功?

I have the following weird situation.

I am trying to execute the following sequence of code within a WCF service, hosted in IIS 7.5 Express:

 var request = WebRequest.Create(some_uri) as HttpWebRequest;

 request.Method = "GET";
 request.Credentials = CredentialCache.DefaultNetworkCredentials;

 request.GetResponse();

When executed from a WCF service, I get the following exception:

[WebException: Unable to connect to the remote server]

and its inner exception:

[SocketException: No connection could be made because the target
machine actively refused it 127.0.0.1:400]

The same sequence of code from the console application is executed successfully.

I tried the following things for the code running under IIS 7.5 Express:

  1. Explicitly set the proxy to the request object copying settings from IE (proxy is used there) and it worked.
  2. Explicitly set the proxy to null and it also worked.

Now, I have several questions:

  1. Why under IIS 7.5 the initial code is not working, however in console app everything works as expected?
  2. What is the difference between the original code and the first solution? As I understand, if I do not specify the proxy, by default, the IE proxy settings will be applied. However, it turns out that the solutions are not equivalent.
  3. Is there any other solution to make the call originated from WCF successfully?

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

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

发布评论

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

评论(1

触ぅ动初心 2024-12-16 00:18:56

IIS 代码在 IIS 用户帐户下运行,该帐户可能没有配置您的代理。

The IIS code is running under an IIS user account, which probably doesn't have your proxy configured.

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