3

My purpose is:

run a self-hosting WCF service, whose base address is set at runtime according to the available network card/s; clients discover the service and get the address.

Previous answers:

I have found the same purpose and a suggested solution here

My problem:

When I run the hosting console app an exception is thrown here:

var host = new ServiceHost(typeof(WcfPingTest), new Uri("http://*:7400/WcfPing"));

Exception details: System.UriFormatException: Invalid URI: The hostname could not be parsed

If I replace * with localhost, it works, but it does not satisfy my task, that is: get the real ip address from the client, and not the hard-coded one.

I have tried also to do it inside the app.config file, but the exception is the same:

Where did I go wrong?

Additional info: C# code, .NET Framework 4.5


UPDATE: Wildcard usage works using in app.config file:

<baseAddresses><add baseAddress="http://*:7400/WcfPing" /></baseAddresses>

and in console app:

var host = new ServiceHost(typeof(WcfPingTest))

On the other hand, Uri constructor does not accept a uriString parameter with the * char inside


NEXT TOPIC: with the above source code, my clients get the uri at runtime, but this uri contains the MY_COMPUTER_NAME like http://MY_COMPUTER_NAME:7400/WcfPing, intead of

http://MY_IP_ADDRESS:7400/WcfPing

Is there any method to get the actual IP address? (I have multiple NICs for different local networks)

Community
  • 1
  • 1
pimo
  • 31
  • 4

1 Answers1

1

The exception was appeared because the uriString is invalid for Uri. About wildcard in the config file, please check that your config file is similar

<system.serviceModel>
    <services>
        <service name="Nelibur.ServiceModel.Services.JsonServicePerCall">
            <host>
                <baseAddresses>
                    <add baseAddress="http://*:9095/feedback" />
                </baseAddresses>
            </host>
            <endpoint binding="webHttpBinding"
                      contract="Nelibur.ServiceModel.Contracts.IJsonService" />
        </service>
    </services>
</system.serviceModel>

and new ServiceHost doesn't contain new Uri, maybe you just forgot about it

var host = new ServiceHost(typeof(WcfPingTest)
GSerjo
  • 4,725
  • 1
  • 36
  • 55
  • You are right. Using `` in config file and `var h = new ServiceHost(typeof(WcfPingTest.WcfPingTest))` works! However `var host = new ServiceHost(typeof(WcfPingTest), new Uri("http://*:9095/feedback"))` does not work. Probably uriString does not allow wildcard. With the above code my clients get: the `http://MY_COMPUTER_NAME:9095/feedback` and not the actual ip address like `http://MY_NIC_IP_ADDRESS:9095/feedback` – pimo Apr 03 '14 at 13:17