G Gr
G Gr

Reputation: 6080

Web Service not detected?

I am trying to host this service below which runs fine yet when I open a new project in a a different visual studio runtime and try to add a web service it cant find anything? Not at the address specified or anything on the local machine? The code below only seems to work when I run it in the same solution?

namespace Students
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create the address for the service
            Uri address = new Uri("http://localhost:8001");
            // Create the binding for the service
            WSHttpBinding binding = new WSHttpBinding();
            // Create the service object
            StudentService service = new StudentService();
            // Create the host for the service
            ServiceHost host = new ServiceHost(service, address);
            // Add the endpoint for the service using the contract, binding and name
            host.AddServiceEndpoint(typeof(IStudentService),
                                    binding,
                                    "students");

            // Open the host
            host.Open();
            Console.WriteLine("Student service started");
            Console.WriteLine("Press return to exit");
            Console.ReadLine();
            // Close the host
            host.Close();
        }
    }


}

The error I get when I try to add it from a new project (seperate to the current solution) is this:

The HTML document does not contain Web service discovery information.
Metadata contains a reference that cannot be resolved: 'http://localhost:8001/'.
There was no endpoint listening at http://localhost:8001/ that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.
The remote server returned an error: (404) Not Found.
If the service is defined in the current solution, try building the solution and adding the service reference again.

Which when I downloaded this case study (starter project) it had no web or app config files anywhere it just hosted from the console apps.

Also note I have the service running when I try to add the web service.

Upvotes: 3

Views: 9212

Answers (2)

anAgent
anAgent

Reputation: 2780

You will need to create/update your app.config file with the information about your service. Check out: http://msdn.microsoft.com/en-us/library/ms734765.aspx

Read more here: https://stackoverflow.com/a/4660531/1220302

Upvotes: 1

Min Min
Min Min

Reputation: 6218

Add Metadata Exchange behavior in your ServiceHost.

namespace Students
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create the address for the service
            Uri address = new Uri("http://localhost:8001");
            // Create the binding for the service
            WSHttpBinding binding = new WSHttpBinding();
            // Create the service object
            StudentService service = new StudentService();
            // Create the host for the service
            ServiceHost host = new ServiceHost(service, address);
            // Add the endpoint for the service using the contract, binding and name
            host.AddServiceEndpoint(typeof(IStudentService),
                                    binding,
                                    "students");

            // Add metadata exchange
            ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
            smb.HttpGetEnabled = true;
            host.Description.Behaviors.Add(smb);

            // Open the host
            host.Open();
            Console.WriteLine("Student service started");
            Console.WriteLine("Press return to exit");
            Console.ReadLine();
            // Close the host
            host.Close();
        }
    }
}

http://wcftutorial.net/WCF-Self-Hosting.aspx

Upvotes: 3

Related Questions