I have a following class and the interface
ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class ClassA:IInterfaceA,IDisposable
{
ServiceHost serviceHost;
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.
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)
{
isDisposed = true;
}
}
}
#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)
{
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)
<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>
<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:
var classAServer = new ClassA();
classAServer.StartServiceHost();
Console.WriteLine("Config Service Started");
ClassAClient clasClient = new ClassAClient();
var value = clasClient.GetValues();
Console.ReadLine();