gpt4 book ai didi

wcf - WCF 代理生命周期的最佳实践 - 或者多久关闭一次 WCF 代理?

转载 作者:行者123 更新时间:2023-12-03 01:15:02 26 4
gpt4 key购买 nike

我一直在开发一个使用 WCF 访问服务器端逻辑和数据库的 WPF 应用程序。

我从一个 WCF 客户端代理对象开始,我反复使用它来调用服务器上的方法。使用代理一段时间后,服务器最终会抛出异常: System.ServiceModel.EndpointNotFoundException:http://.../Service/BillingService.svc 上没有可以接受消息的终结点监听。这通常是由不正确的地址或 SOAP 操作引起的。有关更多详细信息,请参阅 InnerException(如果存在)。 ---> System.Net.WebException: 无法连接到远程服务器 ---> System.Net.Sockets.SocketException: 无法执行套接字上的操作,因为系统缺乏足够的缓冲区空间或因为队列已满完整

我认为这是因为每个服务调用都会打开一个从代理到服务器的新套接字并且从不关闭它们。最终服务器被淹没并开始拒绝请求。

经过短暂的搜索,我确定需要定期 Close() 代理。我发现的样本非常小。 This one提供了一些有用的提示,但并没有真正回答问题。我也 seen recommendations避免使用 using() 模式(并应用 try/catch/finally 代替),因为代理的 Dispose 方法可能会抛出异常(恶心)。

看起来推荐的模式是这样的:

[TestClass]
public class WCFClientUnitTest
{
BillingServiceClient _service;

[TestMethod]
public void TestGetAddressModel()
{
List<CustomerModel> customers = null;

try
{
_service = new BillingServiceClient();
customers = _service.GetCustomers().ToList();
}
catch
{
_service.Abort();
_service = null;
throw;
}
finally
{
if ((_service != null) &&
(_service.State == System.ServiceModel.CommunicationState.Opened))
_service.Close();
_service = null;
}

if (customers != null)
foreach (CustomerModel customer in customers)
{
try
{
_service = new BillingServiceClient();
AddressModel address = (AddressModel)_service.GetAddressModel(customer.CustomerID);

Assert.IsNotNull(address, "GetAddressModel returned null");
}
catch
{
_service.Abort();
_service = null;
throw;
}
finally
{
if ((_service != null) &&
(_service.State == System.ServiceModel.CommunicationState.Opened))
_service.Close();
_service = null;
}
}
}

所以我的问题仍然围绕着我应该让客户端代理保持事件状态多长时间?我应该为每个服务请求打开/关闭它吗?这对我来说似乎太过分了。我的性能不会受到重大影响吗?

我真正想做的是创建并打开一个 channel ,并在该 channel 上进行短暂的重复、简短、连续的服务调用。然后很好地关闭 channel 。

顺便说一句,虽然我还没有实现它,但我很快就会向服务添加安全模型(SSL 和 ACL),以限制谁可以调用服务方法。 this post 的答案之一提到重新协商身份验证和安全上下文会使为每个服务调用重新打开 channel 变得浪费,但只是建议避免构建安全上下文。

<小时/>

编辑 11/3/2010:这似乎很重要,所以我将其添加到问题中......

回应Andrew Shepherd's评论/建议,我在监视 netstat -b 输出的同时关闭 TrendMicro AntiVirus 并重新运行单元测试。 Netstat 能够记录 WebDev.WebServer40.exe 拥有的开放端口的显着增长。绝大多数端口处于 TIME_WAIT 状态。微软says客户端关闭连接后,端口可能会停留在 NET_WAIT 状态...

NOTE: It is normal to have a socket in the TIME_WAIT state for a long period of time. The time is specified in RFC793 as twice the Maximum Segment Lifetime (MSL). MSL is specified to be 2 minutes. So, a socket could be in a TIME_WAIT state for as long as 4 minutes. Some systems implement different values (less than 2 minutes) for the MSL.

这让我相信,如果每个服务调用在服务器上打开一个新套接字,并且因为我在紧密循环中调用该服务,我可以很容易地淹没服务器,导致它耗尽可用套接字并在然后生成我上面提到的异常。

因此,我需要选择以下两条路径之一:1)尝试批量服务调用,以便它们重用服务器端套接字2) 更改我的服务契约(Contract),以便我可以通过更少的调用返回更大的数据 block 。

第一个选择对我来说似乎更好,我将继续追求它。我将发回我的发现并欢迎进一步的评论、问题和解答。

最佳答案

(发布完全不同的第二个答案)

如果我们谈论“最佳实践”,那么 WCF 的真正最佳实践是拥有“粗粒度方法”。如果客户端在循环中多次调用某个方法,则应将整个业务逻辑移至服务本身。

例如。

[DataContract]
class CustomerAndAddress
{
[DataMember]
CustomerModel Customer;

[DataMember]
AddressModel Address;
}

[ServiceContract]
class BillingService
{
[OperationContract]
CustomerAndAddress[] GetAllCustomersAndAddresses();
}

或者,在现实世界中更有可能:

[ServiceContract]
class BillingService
{
[OperationContract]
CustomerReportData FetchCustomerReportInfo(CustomerReportParameters parameterSet);
}

话虽如此,我仍然有兴趣看看您是否可以实现您正在尝试的目标。

关于wcf - WCF 代理生命周期的最佳实践 - 或者多久关闭一次 WCF 代理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4047524/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com