Click here to Skip to main content
15,895,667 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a following class and the interface

C#
ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
    public class ClassA:IInterfaceA,IDisposable
    {

        ServiceHost serviceHost; // Disposable


        public string[] GetValues()
        {
            string[] returnValue = new string[]{"A","B"};
            return returnValue;
        }

        
        public virtual ClassA StartServiceHost()
        {
            using (serviceHost = new ServiceHost(this))
            {
                serviceHost.Open();
                return (ClassA)serviceHost.SingletonInstance;
            }
        }


        #region IDisposable Members

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

               protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (serviceHost != null)
                {
                    try
                    {
                        serviceHost.Close();
                        ((IDisposable)serviceHost).Dispose();
                    }
                    finally
                    {
                        serviceHost = null;
                    }
                }
            }
        }

        #endregion
    }




    [ServiceContract(SessionMode = SessionMode.Required)]
    public interface IInterfaceA
    {
        [OperationContract(IsOneWay = false)]
        string[] GetValues();

    }


Here is the client class for the same.


C#
public class ClassAClient:IDisposable
   {
       protected ChannelFactory<IInterfaceA> channelFactory;

       protected IInterfaceA server;

       protected const string EndpointName = "ClassServiceEndpoint";



       #region IDisposable Members

       bool isDisposed;

              public void Dispose()
       {
           Dispose(true);
       }

       
       protected virtual void Dispose(bool disposing)
       {
           if (!isDisposed)
           {
               if (disposing)
               {
                   // Dispose of managed resources
                   isDisposed = true;
               }
           }
           // Dispose of unmanaged resources
       }

       #endregion

              
     protected virtual void Connect()
       {
           if (server == null)
           {
               if (channelFactory == null)
               {
                   channelFactory = new ChannelFactory<IInterfaceA>(EndpointName);
               }

               if (channelFactory != null)
               {
                   server = channelFactory.CreateChannel();
               }
           }
       }

       public virtual string[] GetValues()
       {
           Connect();
           if (server != null)
           {
               try
               {
                   return server.GetValues();
               }
               catch (Exception ex)
               {
                   //Log
                   throw;
               }
           }
           return null;

       }
   }


Now when I try to host the service and use the client in the same console application. I get the following error:

"Could not connect to net.tcp://localhost:8000/ClassAService. The connection attempt lasted for a time span of 00:00:02.0150000. TCP error code 10061: No connection could be made because the target machine actively refused it 127.0.0.1:8000".

I surfed and found that it is due to port being blocked by Firewall. But its not the same with my case. I verified. It didn't work even after change the port numbers.


Here is app.config(both in same app.config)
C#
<service behaviorConfiguration="ServiceBehavior" name="Experiment.ClassA">
        <endpoint address="net.tcp://localhost:8000/ClassAService"
          behaviorConfiguration="EndPointBehaviour" binding="netTcpBinding"
          bindingConfiguration="tcpBinding" contract="Experiment.IInterfaceA">
</endpoint>
</service>


Client:


<client>

C#
<endpoint address="net.tcp://localhost:8000/ClassAService" behaviorConfiguration="EndPointBehaviour"  binding="netTcpBinding" bindingConfiguration="tcpBinding"
                contract="Experiment.IInterfaceA" name="ClassServiceEndpoint">
        <identity>
          <dns value="localhost" />
        </identity>
      </endpoint>
    </client>



Console application Host:

C#
var classAServer = new ClassA();           
            classAServer.StartServiceHost();
            Console.WriteLine("Config Service Started");
            ClassAClient clasClient = new ClassAClient();
            var value = clasClient.GetValues();
            Console.ReadLine();
Posted
Updated 4-Aug-15 3:56am
v3
Comments
Ganga Prabhu 10-Aug-15 9:17am    
Any help?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900