1

I am developing a WCF service hosted by IIS, using VSTS2008 + C# + .Net 3.5. I find when reference the service from a client by using Add Service Reference..., client has to be able to resolve the machine name to IP address, because WSDL reference some schema file by machine name. Here is an example of a part of WSDL file, in order to parse WSDL file from client side to generate proxy, we have to be able to resolve machine name testmachine1 to related IP address,

<xsd:import schemaLocation="http://testmachine1/service.svc?xsd=xsd1" 
     namespace="http://schemas.microsoft.com/2003/10/Serialization/"/>

My question is, for some reason machine name cannot be resolved all the time (for non-technical reasons), so I want to bind to IP address of the hosting IIS server. Is it possible? If yes, appreciate if anyone could advise. Here is my current WCF web.config file, I want to know how to modify it to enable it to work with IP address,

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <services>
      <service behaviorConfiguration="Foo.WCF.ServiceBehavior"
        name="Foo.WCF.CustomerManagement">
        <endpoint address="" binding="basicHttpBinding" 
                  contract="Foo.WCF.ICustomerManagement">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" 
                  contract="IMetadataExchange" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="Foo.WCF.ServiceBehavior">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

thanks in advance, George

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
George2
  • 44,761
  • 110
  • 317
  • 455

2 Answers2

2

If your WCF service is hosted in IIS, you cannot set a separate address. You must use the URL of the virtual directory where your SVC file lives - either with a machine name (http://yourserver/virtualdir/myservice.svc) or an IP (http://123.123.123.123/virtualdir/myservice.svc).

If you use the IP to add the service reference, that IP will be used in the WSDL generated by the service import.

If you host the WCF service yourself (Windows service, console app), you can set the service address in config, and use either machine name or IP for the machine.

Marc

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Hi Marc, sorry I find something different. I have tried to access WSDL from IE by using IP address, http://123.123.123.123/service.svc?wsdl, it uses machine name to reference other import xsd file. You can have a try to reproduce my issue. This is why I am headache, without being able to resolve machine name to IP address, I cannot import the xsd file. Any comment or solutions? – George2 Aug 03 '09 at 07:12
  • 1
    OK, interesting - I've never had to do this myself, but I'm surprised. Well, one option you do have is to download the metadata yourself, assemble it into a big static WSDL, and then present that WSDL to consumers of your web service instead of letting the service create the WSDL automatically. – marc_s Aug 03 '09 at 07:14
  • Thanks Marc, I know it works. My question is whether from web.config, we have any option to bind to IP address other than machine name? You can see in web.config, I did not assign machine name or IP address, so it is by default behavior. I am not sure whether we are able to overwrite this behaior. Any comments? – George2 Aug 03 '09 at 07:16
  • 1
    It seems this is a behavior on the server side when the WSDL gets generated. As almost everything in WCF, this is extensible, e.g. you could write your own "convert this service description to a WSDL" code, but it's not trivial – marc_s Aug 03 '09 at 08:25
  • 1
    On the other hand, it might be a lot easier to just add an entry for the IP and the machine name to your "hosts" file. That way, you add a static "name resolution" entry, and connecting to the machine name will look up the IP in your "hosts" file and use the resulting IP. – marc_s Aug 03 '09 at 08:26
  • Thanks Marc, using hosts file works, but it is just a walk-around. Dues to my lab's strange DNS/firewall management rules, I cannot resolve host name all the time, so only IP address is trustable. I want to know besides writing my own extensible module to dump WSDL, do you know any easy configuration item which could solve my problem? :-) – George2 Aug 03 '09 at 09:19
  • 1
    I don't think there's anything available in WCF right now that would allow you to request IP's instead of machine names, I'm afraid. – marc_s Aug 03 '09 at 09:55
  • 1
    If you really need this, you would probably have to extend WCF and customize the way the WSDL is created at runtime from the service description. – marc_s Aug 03 '09 at 09:56
  • Thanks Marc, WCF is designed to prefer machine name other than IP address, what is the ideas behind the design? Any benefits of using machine name compared of using IP address? – George2 Aug 03 '09 at 10:01
  • 1
    A host name is more flexible. The name can stay the same when the IP address needs to change. – John Saunders Aug 03 '09 at 13:53
  • Hi John, any easy solutions to change binding to IP address according to my specific issue? – George2 Aug 03 '09 at 14:14
  • 1
    George, if I had the answer, I'd have answered. – John Saunders Aug 03 '09 at 16:10
1

I was having this same issue and seen your post while looking for answers to my own problem.

I think I may have found a solution, which was to change the IIS site binding to be that of the ip. I still don't understand why this can't be a setting in the .config file.

Here is the link to the solution that I found (http://blogs.msdn.com/wenlong/archive/2007/08/02/how-to-change-hostname-in-wsdl-of-an-iis-hosted-service.aspx).

Here is a link to my post on my issue (.NET WCF service references use server name rather than IP address causing issues when consuming).

Here is a link to my post about finding the solution (WCF (hosting service in IIS) - machine name automattically being picked up by WCF rather than IP?).

Community
  • 1
  • 1
dwhittenburg
  • 616
  • 7
  • 13