title: Can WCF endpoint address config use 'localhost' when client is Java on another host?
body: <p><b>Short version:</b>  Can a WCF Windows Service endpoint address configuration use <code>localhost</code> as the hostname when the client is Java on a different machine?</p>
  
  <p><b>Long version:</b></p>
  
  <p>I have a WCF service that runs as Windows Service.  The clients of this service are written in Java, with proxy classes generated using <code>wsimport</code>.  When the endpoint configuration on the WCF service contains an address that includes the hostname on which the WCF service resides I can generate the Java proxy classes with <code>wsimport</code> and run the client succesfully.  Here's what the WCF config file looks like in this scenario:</p>
  
  <pre><code> &lt;service name="crOps.CompanionService" behaviorConfiguration="CompanionServiceBehavior"&gt;
      &lt;host&gt;
        &lt;baseAddresses&gt;
          &lt;add baseAddress="http://chvprdctxxa604.eu.scor.local:8432/crOpsCompanion/service"/&gt;
        &lt;/baseAddresses&gt;
      &lt;/host&gt;
      &lt;endpoint address="" binding="basicHttpBinding" contract="crOps.ICompanionService"/&gt;
      &lt;endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/&gt;
    &lt;/service&gt;
  </code></pre>
  
  <p>Here's the <code>wsimport</code> command to generate the proxy:</p>
  
  <pre><code>C:\Program Files\Java\jdk1.7.0_17\bin\wsimport.exe  -p crOps.companion -keep http://chvprdctxxa604.eu.scor.local:8432/crOpsCompanion/service?wsdl
  </code></pre>
  
  <p>This all works fine. </p>
  
  <p><b>However</b> this WCF service runs on about a dozen servers.  My current wish is to make the config file "host-agnostic", meaning that the same configuration file works on all servers.  In other words, no hostname in the config file.</p>
  
  <p>I searched around a bit for a way to agnostify the WCF config file, and found nothing.  So I decided to try <code>localhost</code> in place of the hostname.  That looks like this...</p>
  
  <pre><code>      &lt;service name="crOps.CompanionService" behaviorConfiguration="CompanionServiceBehavior"&gt;
      &lt;host&gt;
        &lt;baseAddresses&gt;
          &lt;add baseAddress="http://localhost:8432/crOpsCompanion/service"/&gt;
        &lt;/baseAddresses&gt;
      &lt;/host&gt;
      &lt;endpoint address="" binding="basicHttpBinding" contract="crOps.ICompanionService"/&gt;
      &lt;endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/&gt;
    &lt;/service&gt;
  </code></pre>
  
  <p>However when I run the <code>wsimport</code> command (listed above) I get the following error:</p>
  
  <pre><code>    parsing WSDL...
  
  
  [ERROR]         
      Unable to parse "http://localhost:8432/crOpsCompanion/service?wsdl=wsdl0" : 
      Connection refused: connect 
      line 1 of http://chvprdctxxa604.eu.scor.local:8432/crOpsCompanion/service?wsdl
  
  [ERROR]         
      Unable to parse "http://localhost:8432/crOpsCompanion/service?wsdl=wsdl0" : 
      Connection refused: connect
  
  Failed to read the WSDL document: 
      http://chvprdctxxa604.eu.scor.local:8432/crOpsCompanion/service?wsdl, because 
      1) could not find the document; 
      2) the document could not be read; 
      3) the root element of the document is not &lt;wsdl:definitions
  &gt;.
  
  
  [ERROR] failed.noservice=Could not find wsdl:service in the provided WSDL(s):
  
  http://chvprdctxxa604.eu.scor.local:8432/crOpsCompanion/service?wsdl
   At least one WSDL with at least one service definition needs to be provided.
  
  
          Failed to parse the WSDL.
  </code></pre>
  
  <p>So, what I'd like to know is: Can a WCF Windows Service endpoint address configuration use <code>localhost</code> as the hostname when the client is Java on a different machine?  If not, why not?  If so, how?</p>
  
  <p><b>24 Hours Later:</b></p>
  
  <p>Still not solved.  </p>
  
  <p>The technique propsed by Shiraz Bhaiji, to use NLB, does in fact accomplish part of my goal: one config file for all servers.  The hostname in the endpoint address would then be the "cluster name" that the cluster presents to the world and all services on all servers would have the same name.  </p>
  
  <p>The problem in my case for the NLB solution is that each server running this server must contactable individually.  It's not really a cluster type service, it's more of a local service that each one of a dozen or so servers needs to run and clients will contact each on individually.</p>
  
  <p>So on to the technique described in Yaron Naveh's reply, which is to use a local WSDL file to generate the proxies.  I am able to generate the Java proxy classes with <code>wsimport</code> but then when I use them to access the web service (which has localhost in the endpoint address) I get this exception.  Which is itself curious looking to me, it's the output of a single printStackTrace() call and seems to be an exception wrapping two others, but it doesn't look like what I'm used to seeing from a wrapped exception in a stack trace.</p>
  
  <pre><code>com.sun.xml.internal.ws.wsdl.parser.InaccessibleWSDLException: 2 counts of InaccessibleWSDLException.
  
  javax.xml.stream.XMLStreamException: Invalid WSDL http://chvprdctxxa604:8432/crOpsCompanion/service, expected {http://schemas.xmlsoap.org/wsdl/}definitions found HTML at (lineLine number = 1
  Column number = 7
  System Id = http://chvprdctxxa604:8432/crOpsCompanion/service
  Public Id = null
  Location Uri= http://chvprdctxxa604:8432/crOpsCompanion/service
  CharacterOffset = 10
  )
  
  java.io.IOException: Got Connection refused: connect while opening stream from http://localhost:8432/crOpsCompanion/service?wsdl=wsdl0
  
  at com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.tryWithMex(Unknown Source)
  at com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.parse(Unknown Source)
  at com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.parse(Unknown Source)
  at com.sun.xml.internal.ws.client.WSServiceDelegate.parseWSDL(Unknown Source)
  at com.sun.xml.internal.ws.client.WSServiceDelegate.&lt;init&gt;(Unknown Source)
  at com.sun.xml.internal.ws.client.WSServiceDelegate.&lt;init&gt;(Unknown Source)
  at com.sun.xml.internal.ws.spi.ProviderImpl.createServiceDelegate(Unknown Source)
  at javax.xml.ws.Service.&lt;init&gt;(Unknown Source)
  at crOps.companion.CompanionService.&lt;init&gt;(CompanionService.java:58)
  at crOps.companion.Main.main(Main.java:14)
  </code></pre>
  
  <p>I noticed in the stack trace that the proxy classes are trying to connect to <code>localhost</code> to retrieve the WSDL at runtime, although the proxies were generated with a WSDL that contained no reference to localhost.  However, the WSDL emitted by the WCF service DOES contain a localhost reference in the <code>&lt;wsdl:import&gt;</code> element:</p>
  
  <pre><code>&lt;wsdl:import namespace="http://crOps.CompanionService" location="http://localhost:8432/crOpsCompanion/service?wsdl=wsdl0" /&gt; 
  </code></pre>
  
  <p>So Java is trying to retrieve the WSDL in the <code>location</code> attribute and can't.</p>
  
  <p>I began wondering, would a WCF client written in C# do the same thing?  In other words, does a WCF client want to access the WSDL specified in the <code>&lt;wsdl:import&gt;</code>'s <code>location</code> attribute at runtime?  I went through the same procedure creating a WCF client proxy as I did for Java.  I used a WSDL with no reference to <code>localhost</code> to generate the proxy classes and then accessed a WCF service whose configuration file contained <code>localhost</code> in the WCF endpoint address configuration.</p>
  
  <p>This works!  A WCF client does not need to be able to retreieve the WSDL specified in the <code>location</code> attribute of the <code>&lt;wsdl:import&gt;</code> element at runtime.</p>
  
  <p>So now this has become a Java/wsimport question in my eyes.  </p>
  
  <p>My brain is tired right now.  I will come back later and clean up this wall of text to make it something that somebody would be willing to read and respond to.  </p>