WCF with visual studio 2012

I am new WCF programming, I followed a series of tutorials on getting started at the following link

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

I hosted the service in a console application, but when I tried to create a client and tried to add a link to the service, I received the following exceptions.

An error occurred while loading the 'http: localhost: 8000 / GettingStarted / mex / _vti_bin / ListData.svc / $ metadata'. Request failed with HTTP status 405: method not allowed. The metadata contains a link that cannot be resolved: 'http: localhost: 8000 / GettingStarted / mex'. There was no endpoint listening at http: localhost: 8000 / GettingStarted / mex that could receive the message. This is often caused by an invalid address or SOAP action. See InnerException, if available, for more information. The remote server responded with an error: (404) Not found. If the service is defined in the current solution, try creating a solution and add the link to the service again.

hosting code

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 can’t understand what the problem is. I am using visual studio 2012 and .net platform 4.5.

+6
source share
8 answers

I also had a similar problem, messing around with this. Yes, you seem to be following the tutorial correctly, but if you want to connect to it and use it as a service (as in the case of a service link), you should also add MEX services to the access point. Add this line after your selfhost.Description.Behaviors.Add (smb):

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

This will allow you to connect through the "Add service link." In addition, I found depending on your system, you may need to run VS as an administrator to allow network connectivity (in case you did not accidentally say so in the past).

+8
source

Judging by the error message, it seems that there is no service on the specified port. You need to have a console application that hosts the service that is running when you try to add a service link to it.

0
source

Apparently, the service is not working, which means that you don’t listen to the endpoint at the URL you are using to create help for the service.

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

0
source

Make sure your server is running when you try to access it. Also check the configuration on the server and make sure your client endpoint matches the server endpoint. Make sure that you use the same binding as you. Make sure the server is listening and the server firewall is not blocking you. If you make changes to your WCF service, be sure to restore the service link for your client application.

0
source

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

In the tutorial you are attached to, this bit :

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

If you host the web service in IIS, go to web.config (section "Behavior")

httpsGetEnabled set to True

0
source

Today I had a similar problem. However, for me there was no need to explicitly add endpoints, as @iMortalitySX already said.

I had another reason for the failure: I became attached to http://0.0.0.0 , assuming that the listening IP address did not matter. Indeed, through SoapUI I was able to connect and use the Service. But when you try to discover the service in another Visual Studio project, the discovery will fail because VS will get an initial response, followed by subsequent links containing http://0.0.0.0 , and then crash.

Therefore, changing http://0.0.0.0 to the correct IP address of my machine fixed my problem.

0
source

Try putting uri address in your browser. In my case, I was able to see ExceptionDetail.

-1
source

All Articles