支持有返回值的方法以及无返回值的方法
public static void UseService<TChannel>(Action<TChannel> action)
{
var chanFactory = new ChannelFactory<TChannel>("*");
TChannel channel = chanFactory.CreateChannel();
((IClientChannel)channel).Open();
action(channel);
try
{
((IClientChannel)channel).Close();
}
catch
{
((IClientChannel)channel).Abort();
}
}
调用方法如下
int ret = -1;
try
{
WCFClientHelper.UseService<CashStateService.ICashStateService>(s => ret = s.AffiliateLogin(AuthorizeCommands.LoginAffiliate, true, true, args));
}
catch (Exception ex)
{
throw ex;
}
if(ret!=-1){
//do sth.
}