简体   繁体   English

我如何关闭 wcf 客户端

[英]how do i close wcf client

In my business layer, i am using WCF service.在我的业务层,我正在使用 WCF 服务。 WCF service instance can be passed in the constructor as well. WCF 服务实例也可以在构造函数中传递。 Here are the constructors of my BL.这是我的 BL 的构造函数。

    private IWCFClient wcfClient;

    public MyBL()
        :this(new WCFClient())
    {

    }

    public MyBL(IWCFClient wcfClient)
    {
        serviceClient = wcfClient;
    }

because i am declaring my wcfClient as an interface, i dont get.Close() method there.因为我将我的 wcfClient 声明为接口,所以我没有 get.Close() 方法。 I have to typecast it to close it as ((RealwCFClient)wcfClient).Close();我必须将其类型转换为((RealwCFClient)wcfClient).Close();

Is there a way i can close my client without typecasting it?有没有一种方法可以关闭我的客户而不进行类型转换? Should i expose a Close method in my interface?我应该在我的界面中公开一个 Close 方法吗?

Cast your object to ICommunicationObject and then.Close() it.将您的 object 转换为ICommunicationObject然后.Close() 它。

Note that Close() can throw an exception, in this case catch it and do.Abort()请注意,Close() 可以抛出异常,在这种情况下捕获它并 do.Abort()

In addition to the other answers, you should also consider creating a reusable WCF service client .除了其他答案之外,您还应该考虑创建一个可重用的 WCF 服务客户端 In this reusable client, you can handle the open and close, and not make the consuming class worry about it.在这个可重用的客户端中,您可以处理打开和关闭,而不用让消费 class 担心它。

You may implement IDisposable for your class and in your Dispose method close the WCF service instance.您可以为您的 class 实现 IDisposable 并在您的 Dispose 方法中关闭 WCF 服务实例。

public class Consumer
{
    public void SomeMethod()
    {
        using (WCFClient client = new WCFClient(new WCFService()))
        {
            int sum = client.Add(5, 10);
        }
    }
}

public class WCFClient : IDisposable
{
    private WCFService _service;

    public WCFClient(WCFService service)
    {
        _service = service;
    }

    public int Add(int a, int b)
    {
        return _service.Add(a, b);
    }

    public void Dispose()
    {
        if (_service != null)
            _service.Close();
    }
}

The using block in the Consumer's SomeMethod() method will ensure that the WCFClient's dispose method will be called closing the connection to the service. Consumer 的 SomeMethod() 方法中的 using 块将确保调用 WCFClient 的 dispose 方法以关闭与服务的连接。

Here is a link to MSDN doc concerning closing a client .这是有关关闭客户端的 MSDN 文档的链接。

Generally speaking dispose/close is not expected to throw an exception I use an extension method that encapsuates this functionality, here is a pretty good article providing the basic concept .一般来说 dispose/close 不会抛出异常 我使用了一个封装这个功能的扩展方法,这里有一篇很好的文章提供了基本概念

I like the verbs Use or Execute for the method name as opposed to Using, since using has the connotation of calling dispose which most people do not expect exceptions from.我喜欢方法名称的动词 Use 或 Execute 而不是 Using,因为 using 具有调用 dispose 的含义,大多数人不希望出现异常。

As long as all concrete implementers of IWCFClient will have a Close() method, then add the Close() method to the interface.只要IWCFClient的所有具体实现者都有Close()方法,然后将Close()方法添加到接口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM