如何在C#中调用Web服务方法

发布于 2024-09-14 16:03:47 字数 611 浏览 1 评论 0原文

我想知道如何安全地调用 WCF Web 服务方法。这两种方法都可以接受/等效吗?有更好的办法吗?

第一种方式:

public Thing GetThing()
{
    using (var client = new WebServicesClient())
    {
        var thing = client.GetThing();
        return thing;
    }
}

第二种方式:

public Thing GetThing()
{
    WebServicesClient client = null;
    try
    {
        client = new WebServicesClient();
        var thing = client.GetThing();
        return thing;
    }
    finally
    {
        if (client != null)
        {
            client.Close();
        }
    }
}

我想确保客户端已正确关闭并处置。

谢谢

I want to know how to safely call a WCF web service method. Are both of these methods acceptable/equivalent? Is there a better way?

1st way:

public Thing GetThing()
{
    using (var client = new WebServicesClient())
    {
        var thing = client.GetThing();
        return thing;
    }
}

2nd way:

public Thing GetThing()
{
    WebServicesClient client = null;
    try
    {
        client = new WebServicesClient();
        var thing = client.GetThing();
        return thing;
    }
    finally
    {
        if (client != null)
        {
            client.Close();
        }
    }
}

I want to make sure that the client is properly closed and disposed of.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

猫七 2024-09-21 16:03:47

不建议使用 using(无双关语) 因为即使是 Dispose() 也可能引发异常。

这是我们使用的几种扩展方法:

using System;
using System.ServiceModel;

public static class CommunicationObjectExtensions
{
    public static void SafeClose(this ICommunicationObject communicationObject)
    {
        if(communicationObject.State != CommunicationState.Opened)
            return;

        try
        {
            communicationObject.Close();
        }
        catch(CommunicationException ex)
        {
            communicationObject.Abort();
        }
        catch(TimeoutException ex)
        {
            communicationObject.Abort();
        }
        catch(Exception ex)
        {
            communicationObject.Abort();
            throw;
        }
    }

    public static TResult SafeExecute<TServiceClient, TResult>(this TServiceClient communicationObject, 
        Func<TServiceClient, TResult> serviceAction)
        where TServiceClient : ICommunicationObject
    {
        try
        {
            var result = serviceAction.Invoke(communicationObject);
            return result;
        } // try

        finally
        {
            communicationObject.SafeClose();
        } // finally
    }
}

使用这两种方法:

var client = new WebServicesClient();
return client.SafeExecute(c => c.GetThing());

Using using (no pun) is not recommended because even Dispose() can throw exceptions.

Here's a couple of extension methods we use:

using System;
using System.ServiceModel;

public static class CommunicationObjectExtensions
{
    public static void SafeClose(this ICommunicationObject communicationObject)
    {
        if(communicationObject.State != CommunicationState.Opened)
            return;

        try
        {
            communicationObject.Close();
        }
        catch(CommunicationException ex)
        {
            communicationObject.Abort();
        }
        catch(TimeoutException ex)
        {
            communicationObject.Abort();
        }
        catch(Exception ex)
        {
            communicationObject.Abort();
            throw;
        }
    }

    public static TResult SafeExecute<TServiceClient, TResult>(this TServiceClient communicationObject, 
        Func<TServiceClient, TResult> serviceAction)
        where TServiceClient : ICommunicationObject
    {
        try
        {
            var result = serviceAction.Invoke(communicationObject);
            return result;
        } // try

        finally
        {
            communicationObject.SafeClose();
        } // finally
    }
}

With these two:

var client = new WebServicesClient();
return client.SafeExecute(c => c.GetThing());
极致的悲 2024-09-21 16:03:47

第二种方法稍微好一些,因为您正在应对可能引发异常的事实。如果您捕获并至少记录了特定异常,那就更好了。

但是,此代码将阻塞,直到 GetThing 返回。如果这是一个快速操作,那么它可能不是问题,但更好的方法是创建一个异步方法来获取数据。这会引发一个事件来指示完成,并且您订阅该事件来更新 UI(或您需要执行的任何操作)。

The second way is marginally better as you are coping with the fact that an exception might be raised. If you trapped and at least logged the specific exception it would be better.

However, this code will block until GetThing returns. If this is a quick operation then it might not be a problem, but otherwise a better approach is to create an asynchronous method to get the data. This raises an event to indicate completion and you subscribe to that event to update the UI (or what ever it is you need to do).

海未深 2024-09-21 16:03:47

不完全是:

Not quite:

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