ASP.net 页面中的 WCF 客户端
任何人都可以建议从 ASP.net 页面使用 WCF 服务的好模式吗? 似乎如果 Client(:ServiceModel.ClientBase) 的生命周期没有得到正确控制,我们就会抛出 PipeException 。 它当前作为 Page 类的字段存在,但在每次页面请求时都会重新实例化,而不会被清理(.Close 方法)。
我怀疑这个问题可以改写为“管理 ASP.net 页面中的有限资源”,并且可能与 ASP.net 页面的生命周期更相关。 我是 ASP.net 的新手,所以我对此的理解有点薄弱。
TIA。
编辑:一些代码(没有太多内容!)
public partial class Default : Page
{
//The WCF client... obviously, instantiating it here is bad,
//but where to instantiate, and where to close?
private readonly SearchClient client = new SearchClient();
protected void Page_Load(object sender, EventArgs e)
{
第二次编辑:以下会更好吗?
public partial class Default : Page
{
private SearchClient client;
protected void Page_Unload(object sender, EventArgs e)
{
try
{
client.Close();
}
catch
{
//gobbled
}
}
protected void Page_Load(object sender, EventArgs e)
{
client= new SearchClient();
//.....
Can anyone advise of a good pattern for using a WCF Service from an ASP.net Page? It seems that if the lifetime of the Client(:ServiceModel.ClientBase) is not properly controlled that we get PipeException thrown. It currently exists as a field of the Page class, but is being reinstantiated upon each page request without being cleaned up (the .Close method).
I suspect this question could be rephrased "Managing limited resources in an ASP.net page", and is probably more related to the lifecycle of an ASP.net page. I'm new to ASP.net, so my understanding of this is a little thin.
TIA.
EDIT: Some code (there's not much to it!)
public partial class Default : Page
{
//The WCF client... obviously, instantiating it here is bad,
//but where to instantiate, and where to close?
private readonly SearchClient client = new SearchClient();
protected void Page_Load(object sender, EventArgs e)
{
2nd Edit: Would the following be better?
public partial class Default : Page
{
private SearchClient client;
protected void Page_Unload(object sender, EventArgs e)
{
try
{
client.Close();
}
catch
{
//gobbled
}
}
protected void Page_Load(object sender, EventArgs e)
{
client= new SearchClient();
//.....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我同意迈克尔的观点,如果可能的话,将其抽象到另一层。
但是,如果您要从 aspx 页面调用它,我只需创建一个单独的方法来调用它,返回其结果并进行清理。 将所有代码集中在一处,保持代码整洁。 只需记住在finally块中进行dispose,并且wcf代理必须转换为IDisposable才能进行dispose。
例如:
I agree with Michael, abstract it out into another layer if possible.
However, if you are going to call it from your aspx page, I would just create a separate method to call it, return its results and cleanup. Keeps the code clean by having it all in one place. Just remember to dispose in your finally block, and that the wcf proxy will have to be cast to IDisposable in order to dispose.
for instance:
一般来说,您不应直接从表示层调用外部服务。 它会产生两个问题:第一,性能(池化、扩展等),第二,如果您需要进行身份验证,则会带来安全风险(DMZ 中的身份验证代码很糟糕。
即使您没有应用程序层,您应该考虑将服务调用重构为表示层中的私有服务,这将使您能够将服务的生命周期与页面的生命周期分离(正如您所说,这是有问题的)。
In general, you shouldn't call external services directly from your presentation tier. It creates two problems: first, performance (pooling, scaling, etc), and second, it creates a security risk if you need to authenticate (authentication code in your DMZ is bad.
Even if you don't have an application tier, you should consider refactoring your service call to a private service in your presentation tier. This will allow you to decouple the service's lifecycle from the page's lifecycle (which is problematic as you have stated).