WCF 代理使用

发布于 2024-12-11 01:14:14 字数 1116 浏览 0 评论 0原文

此答案已发布回应此问题

现在有点超出我的想象,但是“高阶函数”应该在客户端代理类中使用吗?这是正确的用法吗?:

public class MyProxy
{
    readonly IMyService service =
        new ChannelFactory<IMyService>("IMyService").CreateChannel();

    public ResponseObject Foo(RequestObject request)
    {
        return UseService((IMyService service) =>
            service.Bar(request));
    }

    T UseService<T>(Func<IIssueTrackerService, T> code)
    {
        bool error = true;

        try
        {
            T result = code(issueTrackerChannel);
            ((IClientChannel)issueTrackerChannel).Close();
            error = false;
            return result;
        }
        finally
        {
            if (error)
            {
                ((IClientChannel)issueTrackerChannel).Abort();
            }
        }
    } 
}

我真正想要的是这里的一些指导,以及执行此操作的正确方法。

This answer was posted in response to this question.

It's a little above my head right now, but is the "higher order function" supposed to be used within a client proxy class? Is this correct usage?:

public class MyProxy
{
    readonly IMyService service =
        new ChannelFactory<IMyService>("IMyService").CreateChannel();

    public ResponseObject Foo(RequestObject request)
    {
        return UseService((IMyService service) =>
            service.Bar(request));
    }

    T UseService<T>(Func<IIssueTrackerService, T> code)
    {
        bool error = true;

        try
        {
            T result = code(issueTrackerChannel);
            ((IClientChannel)issueTrackerChannel).Close();
            error = false;
            return result;
        }
        finally
        {
            if (error)
            {
                ((IClientChannel)issueTrackerChannel).Abort();
            }
        }
    } 
}

All I'm really looking for is some guidance here, and the correct way to do this.

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

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

发布评论

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

评论(1

神爱温柔 2024-12-18 01:14:15

这实际上还不错。也许您可以改为转换为 ICommunicationObject,因为您的主机也需要相同的代码。

想想看,接近的方式就是友好的通话。请结束我的通话并将代理返回到连接池。 Abort 是“我不在乎,关闭代理,因为它已经死了,并且也将它从池中删除,因为它已经死了”。

根据您的代码,如果可能的话,您可能希望从函数调用部分中抽象出代码的“WCF 代理”部分。这样您就可以将应用程序逻辑与 WCF 代理代码分开进行单元测试。

您可能需要查看 try {} catch (CommunicationException),以便可以将 WCF 异常单独处理为应用程序级异常,而不是最后处理。

IE

try 
{
    try 
    {
        proxy.call();
        //app logic

        ((ICommunicationObject)proxy).Close();
    } 
    catch (SomeAppException)
    {
    //recover app exception
    }
}
catch (CommunicationException) 
{
    ((ICommunicationObject)proxy).Abort();
}

This is actually not to bad. Perhaps you can cast to an ICommunicationObject instead, as the same code is required for your hosts as well.

The way to think about it is close is the friendly call. Please finish my call and return the proxy to the connection pool. Abort is "I don't care, shut the proxy because it's dead and also remove it from the pool because it's dead".

Depending on your code, you might want to abstract the "WCF Proxy" parts of the code from the function call parts if it's possible. That way you can unit test your application logic separately from the WCF proxy code.

You may want to look at a try {} catch (CommunicationException) so you can treat your WCF exceptions separately to an application level exception too, instead of the finally.

i.e

try 
{
    try 
    {
        proxy.call();
        //app logic

        ((ICommunicationObject)proxy).Close();
    } 
    catch (SomeAppException)
    {
    //recover app exception
    }
}
catch (CommunicationException) 
{
    ((ICommunicationObject)proxy).Abort();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文