【问题标题】:How to check wcf service is connected with client如何检查 wcf 服务是否与客户端连接
【发布时间】:2017-04-12 09:49:06
【问题描述】:

我在 C# 中创建了一个 WCF 服务。我能够通过 url http://localhost:8080/classname/function 的客户端应用程序访问它

有什么方法可以检测客户端是否从 WCF 服务连接,其中客户端只有 url http://localhost:8080 没有类名和函数名,而 WCF 服务正在侦听 localhost 的 8080 端口?

【问题讨论】:

  • 连接是什么意思?当客户端向 WCF 服务发出请求时——调用端点上的方法,服务会处理请求并发送响应。此时,WCF 服务并不关心客户端在做什么。在服务上的方法正在执行(正在执行)期间,客户端已连接。
  • 顺便说一句,它不是服务,而是连接到服务的客户端。除非你有双工模式。
  • 抱歉耽搁了,我的问题是他们是否有可能向 wcf 服务发送空请求。
  • 是的,您可以向 WCF 发送一个空字符串(假设这是空请求的定义)。
  • 你能解释一下怎么做吗?

标签: c# wcf windows-services


【解决方案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&lt;T&gt; 是一个 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。您可以设置断点以单步执行代码并根据需要进行编辑。

希望这能说明问题 - 在下面的 cmets 中留下任何问题。

【讨论】:

  • 谢谢你,我得到了我想要的信息。感谢您的支持。
  • @S.R.Bharath 很高兴我的回答解决了您的问题!
猜你喜欢
  • 1970-01-01
  • 2012-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-07
  • 1970-01-01
相关资源
最近更新 更多