Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1084655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:35:26+00:00 2026-05-16T22:35:26+00:00

I am new with WCF, I am trying to deploy my WCF sample application

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T22:35:26+00:00Added an answer on May 16, 2026 at 10:35 pm

    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)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was new to WCF, i was trying to build a sample application using
I am totally new to this and trying to host the simplest WCF service
I'm fairly new to WCF and I'm trying to figure out how streaming works.I
Im totally new to WCF. Trying to create a Silverlight application with WCF services.
I am new to WCF so I think this is pretty basic. I have
I'm trying to use the new WCF Web API Preview 6 with Basic Authentication.
I'm fairly new to WCF but I have a WCF Service hosted in IIS
I am new to Web/WCF/Services - so bear with me :) I am trying
This is a strange problem that I am having with WCF trying to send
I'm trying to get going with a new WCF service. The service was working

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.