【问题标题】:Deploying WCF application in IIS, but SQL Server Database Connection is not working在 IIS 中部署 WCF 应用程序,但 SQL Server 数据库连接不起作用
【发布时间】:2010-09-21 05:04:11
【问题描述】:

我是 WCF 新手,我正在尝试在 IIS 上部署我的 WCF 示例应用程序,此应用程序在 VS2008 的调试模式下工作正常,此应用程序使用以下代码验证 WCF 消息。我是这样做的,我已经在wwwroot目录中添加了生成的.dlls web.config和Service.svc,并且我还在IIS管理器中添加了连接字符串,即

Server=MyPC\SQLEXPRESS;Database=MySampleDb;Integrated Security=true

我正在使用 Windows 集成安全性。我在数据库类中使用相同的连接字符串进行连接,但出现以下异常,

请指导我部署此应用程序

在验证器中

public override void Validate(string userName, string password)  
{  
       ValidateUser(userName, password);   
}  

public static bool ValidateUser(string userName, string password)  
{  
    if (!string.IsNullOrEmpty(userName))  
    {         
       ICustomer customer = GetCustomerByUsername(userName);  
       if (customer ==null)  
       {  
           throw new exception("User Not found.");  
       }  
       else  
       {  
           return true;  
       }  
    }   
    else   
    {  
        throw new FaultException("User name is required!");  
    }  
}  

public static ICustomer GetCustomerByUsername(string username)  
{  
   try  
   {  
       //ConnectionString= "Server=MyPC\SQLEXPRESS;Database=MySampleDb;Integrated Security=true";  
       OpenConnection();  

       var cmd = new SqlCommand("GetUserByUsername", _connection) { CommandType = CommandType.StoredProcedure };  

       cmd.Parameters.Add("Username", username);  

       connState = _connection.State.ToString();  

       if (_connection.State == ConnectionState.Closed)  
       {
           OpenConnection();  
       }    

       SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);  

       ICustomer customer = null;  

       customer = ExtractCustomerFromDataReader(dr)[0];  
       dr.Close();  
       return customer;  
    }  
    catch (Exception e)  
    {  
       throw new Exception(e.Message + Environment.NewLine + e.StackTrace);  
    }  
    finally  
    {  
       CloseConnection();  
    }  
}  

例外:

ExecuteReader 需要一个打开且 可用的连接。连接的 当前状态是关闭的。在 System.Data.SqlClient.SqlConnection.GetOpenConnection(字符串 方法)在 System.Data.SqlClient.SqlConnection.ValidateConnectionForExecute(字符串 方法,SqlCommand 命令)在 System.Data.SqlClient.SqlCommand.ValidateCommand(字符串 方法,布尔异步)在 System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior、RunBehavior 运行行为、 布尔返回流,字符串方法, DbAsyncResult 结果)在 System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior、RunBehavior 运行行为、 布尔返回流,字符串方法) 在 System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior 行为,字符串方法)在 System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior 行为)在 Aschrafi.MobileZollServer.Services.DatabaseHelper.GetCustomerByUsername(字符串 用户名)在

我认为我在数据库设置或 IIS 管理器的网站设置中遗漏了一些要点。非常感谢在 IIS 中部署 WCF 和验证 WCF 通信的一些教程或文章链接。

提前致谢。

【问题讨论】:

    标签: wcf authentication iis-7 database-connection


    【解决方案1】:

    错误消息非常清楚地表明在您尝试执行数据读取器时您的连接未打开。

    基本上,我建议完全重写您的 GetCustomerByUsername 方法 - 使用 using(...) { ... } 块作为您的 SqlConnectionSqlCommand 周围的最佳实践 - 像这样:

    public static ICustomer GetCustomerByUsername(string username)  
    {  
       ICustomer customer = null;
    
       try  
       {  
           using(SqlConnection _con = new SqlConnection(connectionString))
           using(SqlCommand _cmd = new SqlCommand("GetUserByUsername", _con))
           { 
               _cmd.CommandType = CommandType.StoredProcedure;  
               _cmd.Parameters.Add("Username", username);  
    
               _con.Open();
    
               using(SqlDataReader dr = cmd.ExecuteReader())
               {
                  customer = ExtractCustomerFromDataReader(dr)[0];  
                  dr.Close();  
               }
    
               _con.Close();
            }
    
            return customer;  
        }  
        catch (Exception e)  
        {  
           throw new Exception(e.Message + Environment.NewLine + e.StackTrace);  
        }  
    }  
    

    通过使用using() 块,您可以确保所讨论的类实例将在{ ... } 块的末尾释放——不再需要跟踪状态,也不再需要finally 子句.

    另外:集成安全性在您的桌面上运行良好 - 但在托管环境中无法正常运行。毕竟:现在是 IIS 进程连接到您的数据库 - 我宁愿在这里使用带有密码的特定用户。

    更新:您可以为您的用户命名任何您喜欢的名称 - 例如使用应用程序的名称作为您的用户名或任何对您有意义的名称。您需要使用例如创建它SQL Server Express Management Studio,您需要先使用 CREATE LOGIN 或 GUI 等效项(在“安全”-“登录”下-右键单击并选择“新登录”)创建登录名-此步骤给出“名称”首先登录到 SQL Server 的权限。这也是您选择密码的地方。

    创建登录后,您需要根据数据库中的登录创建用户(USE <database name> GO - CREATE USER ...... - 或使用 GUI - 您的数据库 -> 安全 -> 用户 -> 右键单击​​并选择“新用户”)。

    完成所有这些后,您的连接字符串将如下所示:

    Server=MyPC\SQLEXPRESS;Database=MySampleDb;User Id=(your user name);Pwd=(your password)
    

    【讨论】:

    • 感谢您的回答,我应该使用哪个用户。如果我必须创建新的登录名:默认架构:拥有的架构。和角色成员以及如何在 C# 代码和 IIS 管理器中查看连接字符串。
    猜你喜欢
    • 1970-01-01
    • 2011-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-03
    • 2018-05-16
    相关资源
    最近更新 更多