在 IIS 中部署 WCF 应用程序,但 SQL Server 数据库连接不起作用

发布于 2024-09-24 08:48:17 字数 2876 浏览 1 评论 0原文

我是 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 runBehavior, 布尔returnStream,字符串方法, DbAsyncResult 结果)位于 System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, 布尔returnStream,字符串方法) 在 System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior 行为,字符串方法)位于 System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior 行为)在 Aschrafi.MobileZollServer.Services.DatabaseHelper.GetCustomerByUsername(字符串 用户名)在

我认为我缺少数据库设置或 IIS 管理器网站设置中的某些点。对于在 IIS 中部署 WCF 以及验证 WCF 通信的一些教程或文章链接,我们将不胜感激。

提前致谢。

I am new with WCF, I am trying to deploy my WCF sample application on IIS, this application works fine in debug mode with VS2008, this application authenticate the WCF messages with following code. I am doing it like this, I have added the resulted .dlls web.config and the Service.svc in the wwwroot directory, and I have also added connection string in IIS Manager, which is

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

I am using Windows Integrated Security. I am using same connection string for connection in the database class but I get following exception,

Please guide me to deploy this application

In Validater

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();  
    }  
}  

Exception:

ExecuteReader requires an open and
available Connection. The connection's
current state is closed. at
System.Data.SqlClient.SqlConnection.GetOpenConnection(String
method) at
System.Data.SqlClient.SqlConnection.ValidateConnectionForExecute(String
method, SqlCommand command) at
System.Data.SqlClient.SqlCommand.ValidateCommand(String
method, Boolean async) at
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior
cmdBehavior, RunBehavior runBehavior,
Boolean returnStream, String method,
DbAsyncResult result) at
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior
cmdBehavior, RunBehavior runBehavior,
Boolean returnStream, String method)
at
System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior
behavior, String method) at
System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior
behavior) at
Aschrafi.MobileZollServer.Services.DatabaseHelper.GetCustomerByUsername(String
username) in

I think I am missing some point in database settings or in IIS Manager the website settings. Some tutorial or article link for WCF deployment in IIS and authenticating WCF communication would be really appreciated.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

昔梦 2024-10-01 08:48:17

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

基本上,我建议完全重写您的 GetCustomerByUsername 方法 - 使用 using(...) { ... } 块作为 SqlConnection< 的最佳实践/code> 和 SqlCommand - 类似这样:

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 的权限。这也是您选择密码的地方。

创建该登录名后,您需要在数据库中基于该登录名创建一个用户(USEGO - CREATE USER ...... - 或使用 GUI - 您的数据库 -> 安全性 -> 用户 -> 右键单击​​并选择“新用户”)。

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

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

The error message quite clearly states that your connection is not open at the time you're trying to execute your data reader.

Basically, I'd recommend completely rewriting your GetCustomerByUsername method - use the using(...) { ... } blocks as best practice around your SqlConnection and SqlCommand - something like this:

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);  
    }  
}  

By employing the using() blocks, you make sure the class instance in question will be released at the end of the { ... } block - no more need for keeping track of state and no more need for a finally clause.

Also: integrated security works fine from your desktop - but it won't work well in a hosted environment. After all: it's the IIS process that is now connection to your database - I would rather use a specific user with a password here.

Update: you can name your user anything you like - e.g. use the application's name as your user name or whatever makes sense for you. You need to create that using e.g. SQL Server Express Management Studio, and you need to create a login first using CREATE LOGIN or the GUI equivalent (under "Security" - "Logins" - right-click and pick "New Login") - this step gives that "name" the right to log into the SQL Server in the first place. This is the point where you pick a password, too.

Once you have that login created, then you need to create a user based on that login in your database (USE <database name> GO - CREATE USER ...... - or use the GUI - Your Database -> Security -> Users -> right-click and choose "New User").

Once you have all of this, your connection string will look something like:

Server=MyPC\SQLEXPRESS;Database=MySampleDb;User Id=(your user name);Pwd=(your password)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文