502 (Bad Gateway) and Http clients

In the office I get all sorts of interesting questions about our transports. I’ve realized that I really should share the answers with those of you kind enough to be following along. Today’s tidbit deals with an issue encountered on the Microsoft corp-net, but there’s no reason it can’t happen outside of this deployment, especially if ISA Firewall Client is installed.

The issue is with HTTP-based Indigo clients, and manifests itself as follows:

System.ServiceModel.ProtocolException: The remote server returned an unexpected response: (502) ProxyError (The URL is invalid. The request was not entered correctly. Enter the correct URL and try again.) -> System.Net.WebException: The remote server returned an error: (502) bad gateway.

The likely culprit is your proxy settings, which by default we pick up from the system. If you have autoproxy setup, then your Indigo client will possibly be running an autoproxy detection script, and doing all sorts of other “magic” that can interfere with accessing local sites. To correct this, you have a few options:

  • Set useDefaultWebProxy=false in your binding (available on basicHttpBinding, wsHttpBinding, and httpTransportBindingElement). If you don’t want to use any proxy at all, then this is a viable option for you
  • Fix your system proxy settings (i.e. turn off autoproxy if necessary, etc). This is more the solution if you’re having global HTTP issues
  • Specify a proxy (and optionally proxy authentication scheme) in your binding. You need to do this in conjunction with diabling useDefaultWebProxy so that your settings actually take effect.

8 thoughts on “502 (Bad Gateway) and Http clients

  1. Pingback: Yasser Shohoud

  2. Hao Xu

    502 can also be due to the firewall if the server is running on the same machine. If the on-machine server is listening at a port blocked by the firewall, the proxy server won’t be able to connect. It will ultimately time out after several failed TCP SYN packets and return 502 to client.

    Reply
  3. Pingback: kennyw.com » Blog Archive » Configuration Http Proxies in WCF

  4. Pingback: mattonsoftware.com : .NET Resources

  5. Harvinder Bholowasia

    I am trying to get the Metadata of a service using MetadataExchangeClient, from a Self-Hosted (Windows Service) WCF service. I am getting the following error:-
    System.InvalidOperationException was caught
    Message=”Metadata contains a reference that cannot be resolved: ‘http://harvinder-test.csfidc.com/Session40/SessionManagerAdmin.svc?xsd=xsd1’.”
    Source=”System.ServiceModel”
    StackTrace:
    at System.ServiceModel.Description.MetadataExchangeClient.MetadataRetriever.Retrieve(TimeoutHelper timeoutHelper)
    at System.ServiceModel.Description.MetadataExchangeClient.ResolveNext(ResolveCallState resolveCallState)
    at System.ServiceModel.Description.MetadataExchangeClient.ResolveNext(ResolveCallState resolveCallState)
    at System.ServiceModel.Description.MetadataExchangeClient.GetMetadata(MetadataRetriever retriever)
    at System.ServiceModel.Description.MetadataExchangeClient.GetMetadata(Uri address, MetadataExchangeClientMode mode)
    at Microsoft.ConnectedServices.MV.DebugService.SessionChannelFactory._getKerberosEndpoint(String server) in C:MVInstallLocalsrcMVServerMicrosoft.ConnectedServices.MV.DebugServiceSessionChannelFactory.cs:line 107

    The inner-exception is The remote server returned an error: (502) Bad Gateway.

    The code that I used to get the Metadata is:-
    private static ServiceEndpoint _getKerberosEndpoint(string server)
    {
    string ssuffix = System.Configuration.ConfigurationManager.AppSettings[“SessionManagerAdminAddressSuffix”];
    WSHttpBinding mexBinding = new WSHttpBinding(SecurityMode.None);
    mexBinding.MaxReceivedMessageSize = Int32.MaxValue;
    MetadataExchangeClient mexClient = new MetadataExchangeClient(mexBinding);
    mexClient.ResolveMetadataReferences = true;
    MetadataSet metadataSet = null;
    WsdlImporter importer = null;
    ServiceEndpoint endpointToReturn = null;

    //Get the ISessionMgrAdmin Metadata
    Uri wsdlUri = new Uri(“http://” + server + “/” + ssuffix + “?wsdl”);
    metadataSet = mexClient.GetMetadata(wsdlUri, MetadataExchangeClientMode.HttpGet);
    importer = new WsdlImporter(metadataSet);
    ServiceEndpointCollection secCollection = importer.ImportAllEndpoints();

    WSHttpBinding currentBinding = null;
    foreach (ServiceEndpoint ep in secCollection)
    {
    currentBinding = ep.Binding as WSHttpBinding;
    if (currentBinding == null || currentBinding.Security.Message.ClientCredentialType != MessageCredentialType.Windows)
    continue;
    endpointToReturn = ep;
    break;
    }

    return endpointToReturn;
    }

    But when I try to get the Metadata of the same service, using a ConsoleApplication (using the same piece of code, as in the Windows Service) – I am able to retrieve the ServiceEndpoint correctly!!
    Can someone please tell me what might be going wrong?

    Reply
  6. Kenny

    What account are each of these applications running as? It sounds like the domain account for the windows service does not have the correct proxy settings

    Reply
  7. Pingback: BizTalk, WCF and the Bad Gateway « Santosh Benjamin’s Weblog

  8. Pingback: Azure Service Bus Relay – 502 Bad Gateway after Service Restart - Windows Azure Blog

Leave a Reply to Hao Xu Cancel reply

Your email address will not be published. Required fields are marked *