从 IIS 7.5 托管的 WCF 执行 HttpWebRequest
我有以下奇怪的情况。
我尝试在 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 下运行的代码尝试了以下操作:
- 将代理显式设置为从 IE 复制设置的请求对象(在那里使用代理)并且它有效。
- 显式地将代理设置为 null 并且它也有效。
现在,我有几个问题:
- 为什么在 IIS 7.5 下初始代码不起作用,但在控制台应用程序中一切都按预期工作?
- 原始代码和第一个解决方案有什么区别?据我了解,如果我不指定代理,默认情况下,将应用 IE 代理设置。然而,事实证明这些解决方案并不等效。
- 是否还有其他解决方案可以使来自 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:
- Explicitly set the proxy to the request object copying settings from IE (proxy is used there) and it worked.
- Explicitly set the proxy to null and it also worked.
Now, I have several questions:
- Why under IIS 7.5 the initial code is not working, however in console app everything works as expected?
- 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.
- Is there any other solution to make the call originated from WCF successfully?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
IIS 代码在 IIS 用户帐户下运行,该帐户可能没有配置您的代理。
The IIS code is running under an IIS user account, which probably doesn't have your proxy configured.