tags:

views:

35

answers:

2

I have the following code:

public partial class MyServiceClient : System.ServiceModel.ClientBase<...

if (m_MyClient == null)
    m_MyClient = new MyServiceClient
        ("BasicHttpBinding_IMyService", remoteAddress);

WriteOutput("Successfully connected to service");

My question is how do i know that my client actually connected to the service at this point? I would like to display a message of either failure or success.

+1  A: 

When you've created the client, and no exception like EndpointNotFoundException has occured - then you are "connected" to the service which really means: the communication channel between the client and the service is ready to be used for sending messages back and forth. That's all there is - there's nothing on the server side yet to really handle your calls (except for the channel listener which will get activated if a message arrives).

You can also check the client channel's .State property - ideally, it should be Opened at that point:

Use this if you're deriving from ClientBase<T>

m_MyClient.State == CommunicationState.Opened

or this is you're using the standard client class generated by the Add Service Reference functionality in Visual Studio:

(m_MyClient as IClientChannel).State == CommunicationState.Opened
marc_s
The following code of mine:m_MyClient = new MyServiceClient("BasicHttpBinding_IMyService", remoteAddress);never raises an exception. Only when i try to communicate with the service, do i get an exception.I would like to find out right after that code if my client actually connected?
Tamer
@Tamer: **WHAT** exception do you get??? In WCF, you're not really "connected" per se - you have a channel available to send messages - but there's no real connection like with ADO.NET or anything like that...
marc_s
Update:The following code:(m_MyClient as IClientChannel).State == CommunicationState.Opened crashes.The correct thing is:m_MyClient.State == CommunicationState.OpenedNow the interesting thing is that the CommunicationState is always created. Its never actually opened, it always remains Created. It only becomes opened after a communication actually happens. Like after i make a call to:m_MyClient.GetComments();and then i check the state, only then is it opened.
Tamer
A: 

After realizing what i mentioned in my above comment, i realized the answer to my question was as follows:

In my ServiceContract i added the following:

[OperationContract]
bool IsAlive();

Whose implentation simply looks as follows:

public bool IsAlive()
{
    return true;
}

Then changed my code as follows:

m_MyClient = new MyServiceClient("BasicHttpBinding_IMyService", remoteAddress);

try
{
    m_MyClient.IsAlive();
}
catch (EndpointNotFoundException)
{
    WriteOutput("Unable to connect to service");

    m_MyClient = null;
}

if (m_MyClient != null)
    WriteOutput("Successfully connected to service");
Tamer
This is off-topic, but now i have to wait for about a minute before the exception is fired, since this all gonna be running on a local network, how do i decrease that timeout period? I tried decreasing the receiveTimeout on my client, but it didn't work.
Tamer