开发者

System.ServiceModel.ClientBase connected to Service

I have the following code:

public partial cla开发者_StackOverflow社区ss 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.


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


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");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜