Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 455
Ahsan Iqbal Avatar asked Oct 26 '12 18:10

Ahsan Iqbal


People also ask

How do I add WCF to Visual Studio?

Reference the WCF serviceOn the File menu, point to Add and then click New Project. In the New Project dialog box, expand the Visual Basic or Visual C# node, select Windows, and then select Windows Forms Application. Click OK to open the project. Right-click WindowsApplication1 and click Add Service Reference.

Which .NET framework supports WCF?

WCF support was added to the platform with support for . NET Core 3.1, . NET 5, and . NET 6 in 2022.

Is WCF supported in .NET standard?

WCF server APIs are not supported on . NET Standard or . NET Core/. NET 5+, so there's no good way to migrate such an app forward.

Is WCF still in use?

As long as the Windows operating system continues to include . NET Framework, WCF will continue to function.


1 Answers

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).

like image 192
iMortalitySX Avatar answered Sep 28 '22 08:09

iMortalitySX