从客户端访问WCF服务的泛型程序 [英] Generics program to access WCF service from client

查看:78
本文介绍了从客户端访问WCF服务的泛型程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请查看下面的代码,其中客户端正在访问WCF服务

Please look at the below code where a client is accessing WCF service

函数GetPriority

public List<Priority> GetPriority()
        {
            List<Priority> lstPriority = new List<Priority>();

            using (TmsServiceClient client = new TmsServiceClient())
            {
                try
                {
                    lstPriority =  client.GetPriority();
                }
                catch (FaultException<TMSCustomException> myFault)
                {
                    Console.WriteLine(myFault.Detail.ExceptionMessage);
                    client.Abort();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    client.Abort();                  
                }
            }
            return lstPriority;
        }

函数GetStatus:

public List<Status> GetStatus()
    {
        List<Status> lstStatus = new List<Status>();
        using (TmsServiceClient client = new TmsServiceClient())
        {
            try
            {
                lstStatus =  client.GetStatus();
            }
            catch (FaultException<TMSCustomException> myFault)
            {
                Console.WriteLine(myFault.Detail.ExceptionMessage);
                client.Abort();                   
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                client.Abort();                   
            }
        }

        return lstStatus;
    }

这两种方法都可以正常工作.可以看出,这两种方法之间有许多相似之处,并且仅在方法调用和返回类型时有所不同.这可以通用吗?如果可以,该怎么办?或任何其他更好的方法,以使catch异常块代码不应该每次都重复.

Both the methods are working fine. As can be seen that there are many similarities between the two methods and they differ only at the time of method invocation and return type. Can this be make generic? If so how to do so? Or any other better way so that the catch exception block code should not be repeated everytime.

预先感谢

推荐答案

您可以轻松地将几乎整个Get ...方法重构为通用方法.唯一真正的可变部分是要调用的客户端方法,可以使用 Func<T,TResult> .

You can easily refactor almost entire Get ... method into generic one. The only real variable part is which client method to call, which can be easily solved using Func<T,TResult>.

private List<T> Get<T>(Func<TmsServiceClient, List<T>> clientCall)
{
    List<T> results = new List<T>();
    using (TmsServiceClient client = new TmsServiceClient())
    {
        try
        {
            // invoke client method passed as method parameter
            results = clientCall(client);
        }
        catch (FaultException<TMSCustomException> myFault)
        {
            Console.WriteLine(myFault.Detail.ExceptionMessage);
            client.Abort();                   
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            client.Abort();                   
        }
    }

    return results;
}

现在您的方法实现如下:

Now your methods implementations look like this:

public List<Status> GetStatus()
{
    return Get<Status>(client => client.GetStatus());
}

public List<Priority> GetPriority()
{
    return Get<Priority>(client => client.GetPriority());
}

根据OP评论进行编辑:

Func<TmsServiceClient, List<T>> 作为参数传递给Get<T>方法是代理.委托是一种函数指针-一个用于委托稍后要执行的操作的对象(因此而得名).

Func<TmsServiceClient, List<T>> passed as a parameter to Get<T> method is a delegate. Delegate is kind of function pointer - an object you use to delegate some actions to be executed later (hence the name).

Func<TmsServiceClient, List<T>>基本上是一个委托,它接受一个输入参数(TmsServiceClient类型)并返回List<T>作为结果.

Func<TmsServiceClient, List<T>> is basically a delegate that takes one input argument (of TmsServiceClient type) and returns List<T> as a result.

现在,我们要做什么例如GetStatus?我们创建此类委托的实例(通过 lambda表达式),我们告诉"它以对我们将提供的Client对象执行 GetStatus()方法:

Now, what we do in for example GetStatus? We create an instance of such delegate (via lambda expression) - and we "tell" it to execute GetStatus() method on Client object, which we will provide:

    // this line works the same as in example above
    //                  Take client as parameter    call its .GetStatus method
    return Get<Status>((TmsServiceClient client) => client.GetStatus());

而这恰好发生在

    // invoke client method passed as method parameter
    results = clientCall(client);

行.委托执行我们要求的方法.

line. The delegate executes method we asked for.

这篇关于从客户端访问WCF服务的泛型程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆