What is the best workaround for the WCF client `using` block issue?

前端 未结 26 2464
余生分开走
余生分开走 2020-11-22 00:03

I like instantiating my WCF service clients within a using block as it\'s pretty much the standard way to use resources that implement IDisposable:

26条回答
  •  佛祖请我去吃肉
    2020-11-22 00:15

    I used Castle dynamic proxy to solve the Dispose() issue, and also implemented auto-refreshing the channel when it is in an unusable state. To use this you must create a new interface that inherits your service contract and IDisposable. The dynamic proxy implements this interface and wraps a WCF channel:

    Func createChannel = () =>
        ChannelFactory
            .CreateChannel(new NetTcpBinding(), new EndpointAddress(uri));
    var factory = new WcfProxyFactory();
    var proxy = factory.Create(createChannel);
    proxy.HelloWorld();
    
    
    

    I like this since you can inject WCF services without consumers needing to worry about any details of WCF. And there's no added cruft like the other solutions.

    Have a look at the code, it's actually pretty simple: WCF Dynamic Proxy

    提交回复
    热议问题