.NET WCF: Persistent connection or Open/Close as needed?

I have a .NET WCF web service currently running in production right now. I would say 95% of the time it works flawlessly and as intended. However; there are times when I am throwing an exception that says.

CommunicationException: 
An unsecured or incorrectly secured fault was received from the other party. See the inner FaultException for the fault code and detail.

I have been looking around online for the cause of this, and it seems like this is happening when an exception is being thrown on the Host Service side, and it is not being handled properly and being passed back to the client. In this case my service is the client and a third party is the Host. I cannot feasibly ask the third party to change their code to handle this better.

Here is the assumption on my part: Once this exception is thrown, it closes the channel between client and host. Because I am not properly disposing of this connection, I have to wait for it to time-out before I can bring it back. This seems to be about 30 seconds, which is slightly too long to expect the users to wait for. I think I have found a possible workaround.

In my currently implementation I am instantiating my client at the start of my program and making it a global resource that can be called anywhere as needed. I am wondering if is better to use an approach like

using(WCFServiceClient client = new WCFServiceClient ())
{ 
    //More codes here 
}

and use that at each place that my client needs to communicate to the host. Essentially only having the connection opened when I needed it, and disposing of it right afterward.

The downside here, I would assume, is that there is extra overhead that is going on behind the scenes to open this connection and close it every time. So to me it comes down to whether or not the throughput is high enough to where my process would be effected by waiting for this overhead at each request. Right now I think that there is only about 12 calls to the host a minute, but the catch is they come in groups of three and the three requests all happen in the same second, so this is where the overhead might be the deal breaker.

Does anyone know what the best practice is?

1

So, I’m not sure if what I’m about to tell you is still correct but according to Mark Seeman in his book “Dependency Injection in .NET” he states that:

Although the correct usage pattern concerning ADO.NET connections should be common knowledge, it’s far less known that the same is true for WCF Clients. They should be closed as soon as we’re finished with a particular set of operations on a service.

Based on that view point, I believe you should close your connection after each request.

That being said, you’ve commented that they are always in batches of 3. It could be a worth while exercise introducing a cache/queue into your system where you collect the requests then open your connection, process all entries in the cache/queue, and then close the connection.

This combination of both aspects could give you a solution to your issue, however you could still find that the error occurs if it is generated from the server side. In which case you still need to implement some error handling into it.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật