I'm writing a small C# reporting application for TFS 2012.

TFS server is remote and can be accessed only via HTTP proxy server in the network, as firewalls block direct access.

The proxy is configured in Internet Explorer, so I can open TFS URL in IE, and also it's automatically used by Visual Studio when working with TFS.

The problem is that my application ignores IE proxy settings and tries to connect to TFS server directly (I see this in Wireshark), so it fails after a timeout due to firewalls.

Here is the code I use:

Uri TfsCollectionURL = new Uri("...");
NetworkCredential credential = new System.Net.NetworkCredential(Username, Password, Domain);
TfsTeamProjectCollection collection = new TfsTeamProjectCollection(TfsCollectionURL, credential);
collection.EnsureAuthenticated();
ICommonStructureService projectService = collection.GetService<ICommonStructureService>();
foreach (ProjectInfo project in projectService.ListProjects())
{
    ...
}

Application fails at .EnsureAuthenticated() with exception:

TF400324: Team Foundation services are not available from server ...

A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond ...

It does work from another subnet where direct access to the TFS server is allowed.

QUESTION:

How can I use HTTP proxy in my C# application for connection to TFS?

有帮助吗?

解决方案

Try adding an App.config file to set the default proxy with the following code:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.net>
        <defaultProxy enabled="true" useDefaultCredentials="true"></defaultProxy>
    </system.net>
</configuration>

其他提示

You can directly set credentials to pass the proxy:

WebProxy p = new WebProxy("proxyserver.domain.com:8080", true);
p.Credentials = new NetworkCredential("domain\\user", "password");
WebRequest.DefaultWebProxy = p;

In my scenario we have a subnet for development and those accounts/machines are not allowed to access the internet. Therefore we need to enter the upper domain proxy and those domain credentials to get access to internet.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top