3

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.

G Gr
  • 6,030
  • 20
  • 91
  • 184
  • So you're opening a new VS and trying to add a service reference to `http://localhost:8001` and it gives you an error? What's the error? – CodingGorilla Mar 27 '12 at 14:58
  • To my knowledge VS won't just automatically detect arbitrary local services, only those part of a solution. Did you enter the URL in the service reference textbox? Did it work? – Grant Thomas Mar 27 '12 at 14:59
  • 2
    Do you have your service host (console app) running while trying to add a service reference from other project? – daryal Mar 27 '12 at 14:59
  • Updated my question with more info @ Gorilla its a 404 error @ Mr Disappointment I entered the url in the webservice no it did not work see updated question @daryal yes I have my host files running while I try to add them. – G Gr Mar 27 '12 at 15:07
  • also note im running in admistrator mode so I should have access at localhost – G Gr Mar 27 '12 at 15:12

2 Answers2

3

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

Min Min
  • 6,188
  • 2
  • 19
  • 17
1

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

Community
  • 1
  • 1
anAgent
  • 2,550
  • 24
  • 34