2
votes

I have a WCF service hosted on my local machine as windows service. Now I'm trying to add a service reference in the client (again on my local dev machine) but I'm getting an error

Metadata contains a reference that cannot be resolved: 'net.tcp://localhost:8523/Service1'. Could not connect to net.tcp://localhost:8523/Service1. The connection attempt lasted for a time span of 00:00:02.0011145. TCP error code 10061: No connection could be made because the target machine actively refused it

I checked that the Windows firewall doesn't block port 8523. I even created a new rule in Windows firewall to allow run 8523 port. But when I'm running netstat -sp tcp I can't seem to find port 8523. But I can see Serice1 service's state is set to START in Services. This is the config files

Service Library

<system.serviceModel>
  <services>
    <service name="WcfServiceLibrary1.Service1">
      <endpoint 
           address="" 
           binding="netTcpBinding" bindingConfiguration=""
           contract="WcfServiceLibrary1.IService1">
         <identity>
           <dns value="localhost" />
         </identity>
      </endpoint>
      <endpoint 
           address="mex" 
           binding="mexTcpBinding" bindingConfiguration=""
           contract="IMetadataExchange" />
      <host>
        <baseAddresses>
          <add baseAddress="net.tcp://localhost:8523/Service1" />
        </baseAddresses>
      </host>
    </service>
  </services>
  <behaviors>
    <serviceBehaviors>
      <behavior name="">
        <serviceMetadata httpGetEnabled="false" />
        <serviceDebug includeExceptionDetailInFaults="false" />
      </behavior>
    </serviceBehaviors>
  </behaviors>
</system.serviceModel>

Config in the windows service project looks identical.

4
Is that service, with identical configuration, working as expected if it's hosted in a console application? Did you check the event log for possible error messages from the windows service? You could also try to completely disable the firewall, just to make sure that it does not create trouble.Clemens
once you start the service are you sure it doesn't stop again through an error (Clemens suggestion with the event log will show that up). Refreshing the services view may show it has stopped againRichard Blewett
@Clemens I checked event logs, no error message exists. It just shows up information that service successfully started. I disabled windows firewall completely, unfortunately still got the same error.Michael
@RichardBlewett As I said in above comment, Service is running succesfully, and remains running without any problem.Michael
Just because I can't see 8523 port in the active tcp connection lists, while my service is running, and I can see no error message in the event log, is that possible that something wrong with configuration, and it prevents service to run under 8523 port ??? Is there any way to see what port is running under my service, from Services ?Michael

4 Answers

4
votes

Michael, this is a net.tcp binding that I typically use for a WCF service.

    <bindings>
        <netTcpBinding>
            <binding name="TcpBinding"
                     receiveTimeout="Infinite"
                     sendTimeout="Infinite"
                     maxBufferSize="2147483647"
                     maxReceivedMessageSize="2147483647">
                <reliableSession inactivityTimeout="Infinite"/>
                <security mode="None"/>
            </binding>
        </netTcpBinding>
    </bindings>

Try to add it to your configuration:

<service name="WcfServiceLibrary1.Service1">   
    <endpoint    
        address=""    
        binding="netTcpBinding" bindingConfiguration="TcpBinding"   
        contract="WcfServiceLibrary1.IService1">   
    ...

Also, my services are running under ServiceAccount.LocalSystem.

Check

netstat -ap tcp

if the service is listening.

EDIT: my Service class below. Note that the current directory of the windows service is set programatically to the BaseDirectory, i.e. the directory of the executable.

public class Service : ServiceBase
{
    public static void Main()
    {
        Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
        ServiceBase.Run(new Service());
    }

    ...
}
1
votes

You've specified a behavior but haven't given it a name. Give the behavior a name and point to it using behaviorConfiguration in your service.

<service name="WcfServiceLibrary1.Service1" behaviorConfiguration="svc1behavior">

...

<serviceBehaviors>
<behavior name="svc1behavior">
  <serviceMetadata httpGetEnabled="false" />
  <serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
0
votes

Check that Net.Tcp listener adapter service is running (although if it works while hosting the service in console application, it should be running).

To find the port your service is using,

  1. Open Task Manager
  2. Select View -> Select Columns -> Check PID -> OK
  3. Find your service process name, note the PID
  4. Run the followind command from the command prompt: netstat -ano | findstr *PID*
0
votes

Had the same issue. After a lot of troubleshooting I found out I was missing a reference to the service in Web.config in the service project, so I added this to Web.config:

<system.serviceModel>
  <services>
    <service name="serviceName" behaviorConfiguration="BasicServiceBehavior" >
      <endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange"/>
      <endpoint address="" binding="basicHttpBinding" bindingConfiguration="BasicServiceBasicHttpBinding" contract="(I<nameofservice>)"/>
    </service>
  </services>
</system.serviceModel>

After this a rebuilt the solution (which created the interface) and republished. Then I could finally add a service reference from my other project.