1

I have a wcf service that I am self hosting in an console app.

When I run the service and deploy it to a machine (call it MyWCFRunningMachine) I can go to the "You have created a service" page. (http://MyWCFRunningMachine:8090/MyService).

But then it gives a link to the wsdl page. That link looks like this: http://localhost:8090/MyService?wsdl

So when I click on that link it tries to connect to the service using my machine rather than MyWCFRunningMachine.

If I cold type in the path to the wsdl (http://MyWCFRunningMachine:8090/MyService?wsdl) then I see a wsdl in the browser. But if I try to add a service reference I get this error:

The document was understood, but it could not be processed.
- The WSDL document contains links that could not be resolved.
- There was an error downloading 'http://localhost:8090/MyService?xsd=xsd0'.

This is also referencing localhost when it should not.

Here is the code I am using to self host my service:

public class SelfServiceHost
{
    static string StartUpUrl {get{return "http://localhost:8090/MyService";}}
    static void Main(string[] args) { StartupService(StartUpUrl); }

    public static ServiceHost StartupService(string startUpUrl)
    {
        //+ Setup the Service
        //Create a URI to serve as the base address
        Uri httpUrl = new Uri(startUpUrl);
        //Create ServiceHost
        ServiceHost host = new ServiceHost(typeof(MyService), httpUrl);
        //Add a service endpoint
        host.AddServiceEndpoint(typeof(IMyService), new WSHttpBinding(), "");
        //Enable metadata exchange
        ServiceMetadataBehavior serviceMetadataBehavior =  
             new ServiceMetadataBehavior {HttpGetEnabled = true};
        host.Description.Behaviors.Add(serviceMetadataBehavior);

        //! Turn on Debug.  Remove for production!
        host.Description.Behaviors.Remove(typeof (ServiceDebugBehavior));
        ServiceDebugBehavior serviceDebugBehavior = 
            new ServiceDebugBehavior {IncludeExceptionDetailInFaults = true};
        host.Description.Behaviors.Add(serviceDebugBehavior);

        //Start the Service
        host.Open();
        Console.WriteLine("Service is hosted at " + httpUrl);
        Console.ReadLine();

        return host;
    }
}

How can I get this to remove the localhost? (NOTE: I cannot hard code it to MyWCFRunningMachine. This service will be run on several different machines.

Do I need to go with a config file that I change as I move machines? (I have stayed away from a config file because I did not want to set one up for my console app, but if it is the only way, then I will do it.)

1 Answer 1

1

I think you are correct in using a config file to hold the machine name

"http://" + MACHINE_NAME + ":8090/MyService"

After the service has been installed, change that value in the config file and restart the service to bring it in.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.