簡體   English   中英

代碼在本地機器上工作,但在 IIS 上不工作

[英]Code Works On Local Machine But Not On IIS

注意我不是這個語法的原作者,我發現
它來自另一個 stackoverflow 用戶,它在我的實例中工作

我正在運行IIS 7.5並托管此asp.net頁面。 我正在使用我的Global sax來捕捉任何錯誤並寫入文本文件以供以后查看(正在實施Elmah但這是另一天的故事)。 在 IIS 上,這是一台Windows Server 2008機器,此代碼不會引發任何錯誤,頁面加載但未填充任何數據源。 在我的本地機器以及其他 3 台機器(所有非服務器)上,代碼按原樣執行。 是什么阻止了它在我們的服務器(生產機器)上執行? 全球會計准則

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>

您需要通過提供有效的用戶帳戶來模擬 IIS 上的執行。 您在本地使用您登錄的用戶帳戶(也在您的 Sql Connetion 中使用)但在您發布的網站中您不在同一上下文中,因為它由 AppPool 執行(在您的 SQL Server 上沒有任何權限)。 嘗試在您的 web.config 文件中添加這一行:

<system.web>
  <identity impersonate="true" userName="validAccount@mydomain" password="bar"/>
</system.web>

順便說一句,此帳戶需要在 IIS_IUSRS 安全組中才能擁有對文件夾的最低權限(避免使用管理員帳戶)並成為 SQL Server 中的有效用戶帳戶。

*編輯:您還可以使用 Sql 帳戶(在 SQL Server 中創建,對目標數據庫具有某些權限)

Data Source=OnFleet;Initial Catalog=TestForCode;User id=validSqlAccount;Password=myPsw;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM