如何检查wcf服务是否与客户端连接

时间:2017-04-12 09:49:06

标签: c# wcf windows-services

我在c#中创建了一个WCF服务。我可以通过网址http://localhost:8080/classname/function

从客户端应用访问它

有没有办法检测客户端是否从WCF服务连接,其中客户端单独使用url http://localhost:8080而没有类名和函数名称,其中WCF服务正在localhost处监听8080端口?

1 个答案:

答案 0 :(得分:1)

根据您解释如何向WCF服务发送空字符串消息的请求,我已经开发了VS2017解决方案并上传到GITHUB

该解决方案包含3个项目,2个位于WCF服务端(类库和控制台应用程序),1个用于客户端。

WCF服务端

首先我们定义一个ServiceContract,它将有一个OperationalContract来接收字符串消息并返回一个布尔值:

[ServiceContract]
public interface IClientConnectionService
{
    [OperationContract]
    bool Connect(string message);
}

接下来,我们有一个实现此ServiceContract

的类
public class ClientConnectionService : IClientConnectionService
{
    public bool Connect(string message)
    {
        /*
         * As per your comment on http://stackoverflow.com/questions/43366101/how-to-check-wcf-service-is-connected-with-client?noredirect=1#comment74005120_43366101
         * the message should be empty, however you can pass string.
         * Once you are done with processing you can return true or false depending upon how you want to carry out 
         * this operation.
         */
        return true;
    }
}

接下来,我们有WCF服务主机管理器(基于控制台的应用程序只是为了托管这个WCF服务)

class Program
{
    static void Main(string[] args)
    {
        using (ServiceHost host = new ServiceHost(typeof(ClientConnectionService)))
        {
            host.Open();
            Console.WriteLine($"{host.Description.Name} is up and listening on the URI given below. Press <enter> to exit.");
            PrintServiceInfo(host.Description);
            Console.ReadLine();
        }
    }

    private static void PrintServiceInfo(ServiceDescription desc)
    {
        foreach (ServiceEndpoint nextEndpoint in desc.Endpoints)
        {
            Console.WriteLine(nextEndpoint.Address);
        }

    }
}

它的职责是保持WCF服务监听在配置文件中定义的net.tcp端口上的请求:

  <system.serviceModel>
    <services>
      <service name="StackOverflow.Wcf.Services.ClientConnectionService">
        <endpoint
           address="net.tcp://localhost:9988/ClientConnectionService/"
            binding="netTcpBinding"
            contract="StackOverflow.Wcf.Services.Contracts.IClientConnectionService"
          ></endpoint>
      </service>
    </services>
  </system.serviceModel>

一旦完成,我们就有了一个正在运行的WCF服务。现在让我们将注意力转向将使用此服务的客户端。

WCF客户端

这只是一个控制台应用程序,它具有WCF服务的引用,它创建了代理类来调用服务上的方法。

public class ClientConnectionServiceProxy : ClientBase<IClientConnectionService>
{
    public bool Connect(string message)
    {
        return base.Channel.Connect(message);
    }
}

请注意,我们已从服务端使用IClientConnectionService接口/合同。 ClientBase<T>是WCF框架类。

这是使用上面定义的代理类调用WCF服务的程序类。

class Program
{
    static void Main(string[] args)
    {
        using (ClientConnectionServiceProxy proxy = new ClientConnectionServiceProxy())
        {
            bool isCallSuccessful = proxy.Connect(string.Empty);
        }
    }
}

这是客户端配置:

  <system.serviceModel>
    <client>
      <endpoint
          address="net.tcp://localhost:9988/ClientConnectionService/"
           binding="netTcpBinding"
           contract="StackOverflow.Wcf.Services.Contracts.IClientConnectionService"
          ></endpoint>
    </client>
  </system.serviceModel>

如何运行

GITHUB下载源代码后,打开VS2017中的StackOverflow.Wcf.sln文件(这就是我用来开发它的内容 - 不确定是否可以在VS2015中打开它)并点击F5。您可以设置断点来逐步执行代码并根据需要进行编辑。

希望这说清楚 - 在下面的评论中留下任何问题。

相关问题