c# - 编写通用 WCF 服务客户端配置/端点检查器?

标签 c# wcf

我有一个使用大量服务的客户端应用程序。当服务关闭或配置不正确时,并不总是立即显而易见。我拥有大部分服务的服务端代码和托管服务,但不是全部。它是客户端代理的真正混合包 - 不同的绑定(bind) (basichttp/wshttp/nettcp),一些是使用 svcutil.exe 生成的,而另一些是使用 ChannelFactory 以编程方式制作的,其中契约(Contract)位于公共(public)程序集中。但是,我始终可以访问地址、绑定(bind)和契约(Contract)。

我希望在我的客户端应用程序中有一个组件可以执行绑定(bind)/端点配置和服务可用性的基本检查(以显示在客户端的某些诊断面板中)。至少我只想知道在配置的地址处有一个端点,更好的办法是查明端点是否响应并支持客户端尝试使用的绑定(bind)。

我试过谷歌搜索,很惊讶我没有找到一个例子(也许这已经是一个不好的迹象)但我认为这不会那么难,我所要做的就是创建一个客户 channel 并尝试打开() 和 close() 捕获发生的任何异常并在必要时中止 ()。

我错了 - 特别是对于使用 BasicHttpBinding 的客户端,我可以在其中指定任何端点地址并且能够毫无异常(exception)地打开和关闭。

这是我的实现的精简版本,实际上我返回的是关于异常类型和端点地址的更详细的信息,但这是基本结构。

public class GenericClientStatusChecker<TChannel> : ICanCheckServiceStatus where TChannel : class
{
    public GenericClientStatusChecker(Binding binding, EndpointAddress endpoint)
    {
        _endpoint = endpoint;
        _binding = binding;
    }

    public bool CheckServiceStatus()
    {
        bool isOk = false;
        ChannelFactory<TChannel> clientChannelFactory = null;
        IClientChannel clientChannel = null;

        try
        {
            clientChannelFactory = new ChannelFactory<TChannel>(_binding, _endpoint);
        }
        catch
        {
            return isOk;
        }

        try
        {
            clientChannel = clientChannelFactory.CreateChannel() as IClientChannel;
            clientChannel.Open();
            clientChannel.Close();
            isOk = true;
        }
        catch
        {
            if (clientChannel != null)
                clientChannel.Abort();
        }
        return isOk;            
    }
}

[Test]
public void CheckServiceAtNonexistentEndpoint_ExpectFalse()
{
    var checker = new GenericClientStatusChecker<IDateTimeService>(new BasicHttpBinding(), new Endpointaddress("http://nonexistenturl"));
    // This assert fails, because according to my implementation, everything's ok
    Assert.IsFalse(checker.CheckServiceStatus());
}

我还尝试了一种类似的技术,使用虚拟 testclient 类实现了 ClientBase,结果相同。我想如果我知道我所有的服务契约(Contract)都实现了一个通用的 CheckHealth() 方法,这可能是可能的,但由于某些服务不在我的控制范围内,我什至无法做到这一点。

那么,是否有可能编写这样一个简单的通用通用服务检查器?如果是这样怎么办? (如果不是,为什么不呢?)

谢谢!

最佳答案

你看过 WCF Discovery 了吗?

WCF Discovery allows a client to search for a service based on different criteria including contract types, binding elements, namespace, scope, and keywords or version numbers. WCF Discovery enables runtime and design time discovery. Adding discovery to your application can be used to enable other scenarios such as fault tolerance and auto configuration.

对于第一次尝试,您可以查询端点以查看它是否支持预期的契约(Contract)。

最大的好处是您可以让客户端在运行时“发现”它想与哪个服务对话。这消除了许多您可能已经习惯看到的客户端配置错误。

关于c# - 编写通用 WCF 服务客户端配置/端点检查器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12640503/

相关文章:

c# - 如何删除代码中的所有注释?

c# - "For money, always decimal"?

c# - WPF 工具包数据列可见性绑定(bind)

c# - 在业务层中使用 [DataContract] 标记类是一个糟糕的设计吗?

c# - WCF SSL 客户端证书错误

c# - 当 SSL 不可行时,最好的 WCF 安全模式是什么

c# - 我可以使用什么库在 C# 中处理 VoIP 流?

c# - Visual Studio C#自动换行

c# - 使用 Unity 时配置 WCF 客户端的 MaxItemsInObjectGraph

使用异步任务 <T> 接口(interface)部署到 Azure (.NET 4) 的 WCF 服务