简体   繁体   中英

WCF with visual studio 2012

I am new WCF programming, I did followed series of Getting Started tutorials from following link

http://msdn.microsoft.com/en-us/library/ms734712.aspx

I have hosted service in console application but when I tried to create a client and tried to add service reference I got the following exceptions.

There was an error downloading 'http: localhost:8000/GettingStarted/mex/_vti_bin/ListData.svc/$metadata'. The request failed with HTTP status 405: Method Not Allowed. Metadata contains a reference that cannot be resolved: 'http: localhost:8000/GettingStarted/mex'. There was no endpoint listening at http: localhost:8000/GettingStarted/mex 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.

code of hosting application

class Program
{
    static void Main(string[] args)
    {
        // Step 1 Create a URI to serve as the base address.
        Uri baseAddress = 
            new Uri("http://localhost:8000/GettingStarted/");

        // Step 2 Create a ServiceHost instance
        ServiceHost selfHost = 
            new ServiceHost(typeof(CalculatorService), baseAddress);

        try
        {
            // Step 3 Add a service endpoint.
            selfHost.AddServiceEndpoint(typeof(ICalculator), 
                new WSHttpBinding(), 
                "CalculatorService");

            // Step 4 Enable metadata exchange.
            var smb = new ServiceMetadataBehavior();
            smb.HttpGetEnabled = true;
            selfHost.Description.Behaviors.Add(smb);

            // Step 5 Start the service.
            selfHost.Open();
            Console.WriteLine("The service is ready.");
            Console.WriteLine("Press <ENTER> to terminate.");
            Console.WriteLine();
            Console.ReadLine();

            // Close the ServiceHostBase to shutdown.
            selfHost.Close();
        }
        catch (CommunicationException ce)
        {
            Console.WriteLine("exception: {0}", ce.Message);
            selfHost.Abort();
        }
    }
}

Now I am unable to figure out what the problem is. I am using visual studio 2012 and .net platform 4.5.

I had a similar issue as well, messing with this. Yes you seem to have followed the tutorial correctly, but if you want to connect to it and consume as a service (as in make a service reference) you must also add in the MEX service enpoint. Add this line after your selfhost.Description.Behaviors.Add(smb):

selfhost.AddServiceEndpoint(
            typeof(IMetadataExchange),
            MetadataExchangeBindings.CreateMexHttpBinding(),
            "http://localhost:8000/GettingStarted/mex");

That should enable you to connect via "Add Service Reference". Also, I have found depending on your system, you may need to run VS as admin to allow for connection to network (in case you accidentally told it no in the past).

Judging from the error message it seems that there is no service listening at the specified port. You need to have the console application which hosts the service running when you are trying to add a service reference to it.

Apparently, the service is not running which means there is no endpoint listening at the URL you are using to create the service reference.

You can host the service in IIS, or keep the console application running as Damir mentioned above.

Make sure your server is running when you are trying to access it. Also check the configuration on the server and make sure your client's endpoint matches the server's endpoint. Make sure you're using the same binding as well, while you're at it. Make sure the server is listening and the server's firewall isn't blocking you. If you made a change to your WCF service don't forget to regenerate the service reference for your client application.

Are you sure you have defined a MEX endpoint? This is what provides metadata information about your service, so that studio can generate a client proxy.

In the tutorial you linked to, it is this bit :

// Step 4 Enable metadata exchange.
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
selfHost.Description.Behaviors.Add(smb);

If you are hosting the web service in IIS, check in web.config(under behaviours section)

httpsGetEnabled is set to True

I ran into a similar problem today. However, for me it wasn't needed for me to explicitly add the Endpoints, as @iMortalitySX already said.

I had a different reason for failing: I was binding to http://0.0.0.0 , thinking the listen IP doesn't matter. Indeed, via SoapUI I was able to connect and to use the Service. But when trying to discover the service in another Visual Studio project, the discovery would fail, as VS would get the initial response, but then follow up links that contained http://0.0.0.0 and then fail.

So changing http://0.0.0.0 into the correct IP of my machine fixed my problem.

Try put the uri address in Your browser. In my case I was able to see an ExceptionDetail.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM