【发布时间】:2010-08-10 14:59:15
【问题描述】:
这是Choosing a Connection String based on kind of request 的后续行动,我没有得到答案,而我认为有效的方法也没有。
我有一个 web 服务,它需要根据用户从浏览器或客户端应用程序调用它来选择特定的连接字符串。
我试过了:
HttpContext.Current != null? ConnectionStrings["Website"].ConnectionString : ConnectionStrings["Client"].ConnectionString
但意识到在某些时候即使我使用客户端应用程序,也有一些 HttpContext(如果有人能解释为什么它会很棒)但 Request 下的 Browser 字段是 "Unknown"。所以,然后我尝试了:
if ( HttpContext.Current != null )
{
if ( HttpContext.Current.Request.Browser != "Unknown" )
{
//browser connection string here
}
else
//client app connection string here
}
else
//client app connection string here
这在调试时产生了奇迹,但在测试环境中,即使从客户端应用程序调用,它仍然指向浏览器连接字符串,好像在某些时候浏览器不是“未知”...
有没有更容易/更简单的方法来做到这一点?我这样做的方式看起来真的很丑。
我现在很绝望,因为我不知道为什么会这样..
【问题讨论】:
-
嗯,你需要在测试过程中记录
Browser的值,看看它到底是什么。 -
你能补充更多细节吗?您使用的是 WCF 还是 WebMethods?浏览器如何访问您的网络服务?不是通过 SOAP,我想?
-
柯克,我正在使用 WebMethods。我会尝试罗伯特的建议。
-
@Robert Harvey:我做到了,它返回“未知”......但它仍然使用服务器字符串。
-
Web 服务有自己的 HttpContext。
标签: c# sql-server web-services asmx connection-string