ASP.net 页面中的 WCF 客户端

发布于 2024-07-17 00:37:46 字数 1051 浏览 3 评论 0原文

任何人都可以建议从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

九命猫 2024-07-24 00:37:46

我同意迈克尔的观点,如果可能的话,将其抽象到另一层。

但是,如果您要从 aspx 页面调用它,我只需创建一个单独的方法来调用它,返回其结果并进行清理。 将所有代码集中在一处,保持代码整洁。 只需记住在finally块中进行dispose,并且wcf代理必须转换为IDisposable才能进行dispose。

例如:

void Page_Load(object sender, EventArgs e)
{
  if(!IsPostBack)
  {
      RemoteCall();
  }
}

void RemoteCall()
{
 var client = new SearchClient();
 try
 {
     var results = client.Search(params);
     clients.Close();
 }
 catch(CommunicationException cex)
 {
   //handle
 }
 catch(Exception ex)
 {
   //handle
 }
 finally
 {
     ((IDisposable)client).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:

void Page_Load(object sender, EventArgs e)
{
  if(!IsPostBack)
  {
      RemoteCall();
  }
}

void RemoteCall()
{
 var client = new SearchClient();
 try
 {
     var results = client.Search(params);
     clients.Close();
 }
 catch(CommunicationException cex)
 {
   //handle
 }
 catch(Exception ex)
 {
   //handle
 }
 finally
 {
     ((IDisposable)client).Dispose();
 }

}
爱她像谁 2024-07-24 00:37:46

一般来说,您不应直接从表示层调用外部服务。 它会产生两个问题:第一,性能(池化、扩展等),第二,如果您需要进行身份验证,则会带来安全风险(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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文