编码不好? [英] Poor Coding?

查看:71
本文介绍了编码不好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个尝试调用Web服务的try块。网络服务

需要一个可以随时到期的令牌,所以我使用一个catch块

刷新令牌,然后再次尝试调用Web服务。这个

我的做法看起来有点差,所以只想获得第二个

意见:


有比使用goto语句更好的方法吗?

public static string GetData(string Criteria)

{


tryagain :

试试

{

CallWebService();

}

catch( SoapException SoapEx)

{

if(Soap.Ex.Message =" Bad Token")

{

RefreshLogon();

goto tryagain;

}

}

}

解决方案

" Brian P" < no@email.com>写道:

[...]
有没有比使用goto语句更好的方法?




我认为转到这是你最少的问题。投掷和

捕获异常会导致性能大幅下降,而且你肯定不想在这样的繁忙循环中做这件事。如果你可以避免它,那么
永远不应该将它们用于控制流程。


你没有完全解释CallWebService的作用。是否需要

抛出异常?并且每隔几个小时调用它真的很有意义吗?或者你可以使用计时器并且当你获得新数据时可能会引发一些

事件?


P.


Brian P< no@email.com>写道:

我有一个尝试调用Web服务的try块。 Web服务
需要一个可以随时过期的令牌,因此我使用catch块来刷新令牌,然后再次尝试调用Web服务。这个
看起来有点像我做的那样差,所以只是想得到第二个意见:

有没有比使用goto语句更好的方法?




是 - 使用while循环。你可以保留一个布尔标志,或者

break关于成功案例。这是你的代码在各方面:


bool success = false;

while(!success)

{

尝试

{

CallWebService();

success = true;

}

catch(SoapException ex)

{

if(ex.Message ==" Bad Token))

{

RefreshLogin();

}

//你的代码没有这个,但我怀疑你想保留

//如果抛出了不同的SoapException就行了...

else

{

throw;

}

}

}





while(true)

{

试试

{

CallWebService();

break;

}

catch(SoapException ex)

{

if(ex.Message ==" ; Bad Token")

{

RefreshLogin();

}

//你的代码没有''有这个,但我怀疑你想保持

//如果一个抛出了不同的SoapException ...

else

{

throw;

}

}

}


或者,您可以使用do / while,并使用continue:


而(真)

{

试试

{

CallWebService();

}

catch(SoapException ex)

{

if(ex.Message ==" Bad Token")

{

RefreshLogin();

继续;

}

//你的代码没有'没有这个,但我怀疑你想保持

//如果抛出了不同的SoapException ...

else

{

throw;

}

}

} while(false);

-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet 博客: HTTP://www.msm vps.com/jon.skeet

如果回复小组,请不要给我发邮件


Hello Paul,


是的,如果安全令牌太旧,它会抛出一个异常,但是我知道

不知道什么时候太旧了。


这是通用的,因为我将使用的Web服务大约有12个左右的方法暴露

,有些会被多次调用

a分钟,其他人每天只有一次。这里的关键是我需要能够处理太旧的问题。令牌异常并重试。


如果由我决定,那么就有办法看看令牌是否已过期,但

i didn'写网络服务。=)

- 布里恩

" Brian P" < no@email.com>写道:

[...]
有没有比使用goto语句更好的方法?


我认为goto这是你最少的问题。投掷和捕获异常会导致性能受到重大影响,而且您绝对不希望在这样的繁忙循环中执行此操作。如果你能避免它,你绝不应该将它们用于控制流程。

你没有完全解释CallWebService的作用。是否必须抛出异常?并且每隔几毫秒调用它真的很有意义,或者你可以使用计时器并在获得新数据时引发某种事件吗?

P. / blockquote>


I have a try block that attempts to call a web service. The web service
requires a token that can expire at any time, so I am using a catch block
to refresh the token and then try the call to the web service again. This
seems a little poor the way i''m doing it, so just wanted to get a second
opinion:

is there a better way than using a goto statement?
public static string GetData(string Criteria)
{

tryagain:
try
{
CallWebService();
}
catch (SoapException SoapEx)
{
if (Soap.Ex.Message = "Bad Token")
{
RefreshLogon();
goto tryagain;
}
}
}

解决方案

"Brian P" <no@email.com> wrote:

[...]
is there a better way than using a goto statement?



I think "goto" is the least of your problems here. Throwing and
catching exceptions incurs a significant performance hit, and you
definitely don''t want to be doing it in a busy loop like this. You
should never use them for control flow if you can avoid it.

You didn''t explain exactly what CallWebService does. Does it have to
throw an exception? And is it really meaningful to call it every few
milliseconds, or could you use a timer and perhaps raise some kind of
event when you get new data?

P.


Brian P <no@email.com> wrote:

I have a try block that attempts to call a web service. The web service
requires a token that can expire at any time, so I am using a catch block
to refresh the token and then try the call to the web service again. This
seems a little poor the way i''m doing it, so just wanted to get a second
opinion:

is there a better way than using a goto statement?



Yes - use a while loop. You could either keep a boolean flag, or
"break" on the success case. Here''s your code in each way:

bool success=false;
while (!success)
{
try
{
CallWebService();
success = true;
}
catch (SoapException ex)
{
if (ex.Message=="Bad Token")
{
RefreshLogin();
}
// Your code didn''t have this, but I doubt you want to keep
// going if a different SoapException has been thrown...
else
{
throw;
}
}
}

and

while (true)
{
try
{
CallWebService();
break;
}
catch (SoapException ex)
{
if (ex.Message=="Bad Token")
{
RefreshLogin();
}
// Your code didn''t have this, but I doubt you want to keep
// going if a different SoapException has been thrown...
else
{
throw;
}
}
}

Alternatively, you could use a do/while, with a continue:

while (true)
{
try
{
CallWebService();
}
catch (SoapException ex)
{
if (ex.Message=="Bad Token")
{
RefreshLogin();
continue;
}
// Your code didn''t have this, but I doubt you want to keep
// going if a different SoapException has been thrown...
else
{
throw;
}
}
} while (false);
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


Hello Paul,

Yes, it will throw an exception if the security token is too old, but i have
no idea when is "too old."

This is generic on purpose, since there are about 12 or so methods exposed
by the web service that I will be using, some will be called several times
a minute, others only once a day. The key here was that i need to be able
to handle the "too old" token exception and retry.

If it were up to me, there''d be a way to see if the token was expired, but
i didn''t write the web service.=)

--Brian

"Brian P" <no@email.com> wrote:

[...]
is there a better way than using a goto statement?


I think "goto" is the least of your problems here. Throwing and
catching exceptions incurs a significant performance hit, and you
definitely don''t want to be doing it in a busy loop like this. You
should never use them for control flow if you can avoid it.

You didn''t explain exactly what CallWebService does. Does it have to
throw an exception? And is it really meaningful to call it every few
milliseconds, or could you use a timer and perhaps raise some kind of
event when you get new data?

P.



这篇关于编码不好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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