WCF error - Can't get service endpoint up

1

I started working with WCF recently, and I'm having a problem that I just don't have a clue how to solve. I start a WCF Service using Service Host, but when I use the URI in a browser it doesn't show the contract of the service, and it gives an exception when I try to connect to it using a ChannelFactory.

I created the project in Visual Studio 2017, and didn't do anything to the config file, excpet changing the base address. Both the service interface and implementation are in the root project "folder", and I've tried disabling the firewall and even my antivirus, but nothing seems to work.

App.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
    </startup>
    <system.serviceModel>
        <behaviors>
            <serviceBehaviors>
                <behavior name="">
                    <serviceMetadata httpGetEnabled="true" />
                    <serviceDebug includeExceptionDetailInFaults="false" />
                </behavior>
            </serviceBehaviors>
        </behaviors>
        <services>
            <service name="TaskExecutor.Exec">
                <endpoint address="" binding="basicHttpBinding" contract="TaskExecutor.IExec">
                    <identity>
                        <dns value="localhost" />
                    </identity>
                </endpoint>
                <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
                <host>
                    <baseAddresses>
                        <add baseAddress="http://localhost:8001/TaskExecutor/Exec/" />
                    </baseAddresses>
                </host>
            </service>
        </services>
    </system.serviceModel>
</configuration>

Service interface:

namespace TaskExecutor
{
    [ServiceContract]
    public interface IExec
    {
        [OperationContract]
        void DoWork();
    }
}

Service implementation:

namespace TaskExecutor
{
    public class Exec : IExec
    {
        public void DoWork()
        {
            Console.WriteLine("Doing work.");
        }
    }
}

Program launching the service:

using (ServiceHost host = new ServiceHost(typeof(Exec)))
{
    host.Open();
    Console.WriteLine("exec service started at {0}", host.BaseAddresses[0]);
}

Console.WriteLine("Press any key to end...");
Console.ReadLine();

After launching the program display the message:

exec service started at http://localhost:8001/TaskExecutor/Exec/
Press any key to end...

The service client code is the following:

EndpointAddress endpoint = new EndpointAddress("http://localhost:8001/TaskExecutor/Exec/");
BasicHttpBinding binding = new BasicHttpBinding();

ChannelFactory<IExec> channelFactory = new ChannelFactory<IExec>(binding, endpoint);
IExec proxy = channelFactory.CreateChannel();

proxy.DoWork();

And it gives the exception:

System.ServiceModel.EndpointNotFoundException occurred
  HResult=0x80131501
  Message=There was no endpoint listening at http://localhost:8001/TaskExecutor/Exec/ that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.

Inner Exception 1:
WebException: Unable to connect to the remote server

Inner Exception 2:
SocketException: No connection could be made because the target machine actively refused it

I seriously don't know what to do, and any help would be amazing.

Thank you very much!

c#
web-services
wcf
asked on Stack Overflow Dec 3, 2017 by gqmartins

2 Answers

0

You have exposed metadata but didnt bind it to the service.Here's how you have to.

<behaviors>
            <serviceBehaviors>
                <behavior name="metadadiscovery">
                    <serviceMetadata httpGetEnabled="true" />
                    <serviceDebug includeExceptionDetailInFaults="false" />
                </behavior>
            </serviceBehaviors>
        </behaviors>

Now bind the behavior to the service.

 <services>
        <service name="TaskExecutor.Exec" behaviorConfiguration="metadadiscovery">
            <endpoint address="" binding="basicHttpBinding" contract="TaskExecutor.IExec">
           <identity>
                        <dns value="localhost" />
                    </identity>
                </endpoint>
                <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
                <host>
                    <baseAddresses>
                        <add baseAddress="http://localhost:8001/TaskExecutor/Exec/" />
                    </baseAddresses>
                </host>
            </service>
        </services>

Now when you type the address in the browser you should be able to see wsdl. http://localhost:8001/TaskExecutor/Exec/

answered on Stack Overflow Dec 3, 2017 by Hameed Syed
0

So I figured out what was wrong, It was the code starting the service. instead of what I have It should be:

using (ServiceHost host = new ServiceHost(typeof(Exec)))
{
    host.Open();
    Console.WriteLine("exec service started at {0}", host.BaseAddresses[0]);
    Console.WriteLine("Press any key to end...");
    Console.ReadLine();
}
answered on Stack Overflow Dec 3, 2017 by gqmartins

User contributions licensed under CC BY-SA 3.0