代码在本地计算机上运行但不在IIS上

时间:2016-03-17 01:44:54

标签: c# asp.net iis

注意我不是这种语法的原作者,我从另一个stackoverflow用户那里找到了它,它在我的实例中工作

我正在运行IIS 7.5并托管此asp.net页面。我正在使用我的Global sax来捕获任何错误并写入文本文件以供日后查看(正在实施Elmah但这是另一天的故事)。在IIS上,这是一个Windows Server 2008计算机,此代码不会引发任何错误,页面加载但没有填充任何数据源。在我的本地计算机以及其他3台计算机(所有非服务器)上,代码按预期执行。什么阻止它在我们的服务器(生产机器)上执行? 的 GlobalAsax

void Application_Error(object sender, EventArgs e)
{
  Exception CurrentException = Server.GetLastError();
  Server.ClearError();
  if (CurrentException != null)
  {
    try
    {
        using (StreamWriter sw = new StreamWriter(Server.MapPath("ErrorLog.txt")))
        {
            sw.WriteLine(CurrentException.ToString());
            sw.Close();
        }
    }
    catch (Exception ex) { }
  }
}

这是我的C# -

protected void Page_Load(object sender, EventArgs e)
{
try
{
    if (!IsPostBack)
    {
        Page.RegisterAsyncTask(LoadDataAsync().ToPageAsyncTask());
    }
}
catch (Exception ex) { throw ex;  }
}
public Task LoadDataAsync()
{
var t1 = ExecuteSqlQueryAsync(connectionString, "Select Top 10 * from uno");
var t2 = ExecuteSqlQueryAsync(connectionString, "Select Top 10 * from tres");
var t3 = ExecuteSqlQueryAsync(connectionString, "RunStoredProcedure");
return Task.Factory.ContinueWhenAll(new[] { t1, t2, t3 }, _ =>
{
    try
    {
        this.ddluno.DataSource = t1.Result;
        this.ddluno.DataBind();
        ddltopperformers.DataSource = t2.Result;
        ddltopperformers.DataBind();
        this.gridall.DataSource = t3.Result;
    }
    catch (Exception exception) { throw exception; }
});
}
public System.Threading.Tasks.Task<DataSet> ExecuteSqlQueryAsync(string connectionString, string sqlQuery)
{
SqlConnection _sqlDatabaseConnection;
SqlCommand _sqlCommand = new SqlCommand();
SqlParameter _sqlParameter = new SqlParameter();
SqlDataAdapter _sqlDataAdapter = new SqlDataAdapter();
StringBuilder _sqlQueryBuilder = new StringBuilder();
DataSet _dataSet = new DataSet();

return System.Threading.Tasks.Task.Factory.StartNew(
() =>
{
    try
    {
        _sqlDatabaseConnection = new SqlConnection(connectionString);
        _sqlCommand = new SqlCommand(sqlQuery, _sqlDatabaseConnection);
        _sqlDatabaseConnection.Open();
        _sqlCommand.CommandTimeout = 0;
        _dataSet = new DataSet();
        _sqlDataAdapter = new SqlDataAdapter(_sqlCommand);
        _sqlDataAdapter.Fill(_dataSet, "Data");
        _sqlDatabaseConnection.Close();
        _sqlCommand.Dispose();
        _sqlDataAdapter.Dispose();
        return _dataSet;
    }
    catch (Exception exception) { throw exception; }
});
}

public static class PageAsyncTaskGenerator
{
    public static PageAsyncTask ToPageAsyncTask(this Task task, Action<IAsyncResult> onTimeout = null)
    {
        try
        {
            Func<Task> func = () => task;
            return new PageAsyncTask(
                (sender, e, cb, extraData) => func.BeginInvoke(cb, extraData),
                ar => func.EndInvoke(ar).Wait(),
                ar =>
                {
                    if (onTimeout != null)
                    {
                        onTimeout(ar);
                    }
                },
                null);
        }
        catch (Exception exception) { throw exception; }
    }
}

修改
这是我使用的连接字符串,我应该更改什么,以便它可以在我的IIS上运行?

private string connectionString = "Data Source=OnFleet;Initial Catalog=TestForCode;Integrated Security=True;MultipleActiveResultSets=True";

编辑2
尝试使用webconfig创建连接,这是我的字符串,但仍然没有连接。我错过了明显的(再次)吗?

<connectionStrings>
  <add name="DBConnection" connectionString="Data Source=OnFleet;Initial Catalog=TestForCode;Integrated Security=True;MultipleActiveResultSets=True;;User ID=IISUser;Password=nottellingSO" providerName="System.Data.SqlClient" />
</connectionStrings>

1 个答案:

答案 0 :(得分:1)

您需要通过提供有效的用户帐户来模拟IIS上的执行。 Localy您正在使用您的已登录用户帐户(也在您的Sql连接中使用),但在您发布的网站中,您不在同一个上下文中,因为它是由AppPool执行的(在您的SQL Server上没有任何权限)。 尝试在web.config文件中添加以下行:

<system.web>
  <identity impersonate="true" userName="validAccount@mydomain" password="bar"/>
</system.web>
不过,这个帐户需要在IIS_IUSRS安全组中拥有文件夹的最小权限(避免使用管理员帐户)并成为SQL Server中的valide用户帐户。

*编辑:你也可以使用一个Sql帐户(在SQL Server中创建,在目标数据库上有一些权限)

Data Source=OnFleet;Initial Catalog=TestForCode;User id=validSqlAccount;Password=myPsw;
相关问题