【问题标题】:Can't set ODBC source at runtime from an ASP.NET Core app hosted in IIS无法在运行时从 IIS 中托管的 ASP.NET Core 应用程序设置 ODBC 源
【发布时间】:2019-01-07 07:21:09
【问题描述】:

我有一个 ASP.NET Core 应用程序,旨在使用完整的 .NET Framework 4.6,其中包含我在 Startup.cs 中调用的 ODBC 中创建 DSN 的部分代码:

...
 public void ConfigureServices(IServiceCollection services)
        {
            services.ConfigureSqlContext(Configuration);
            services.CrearDSN(Configuration);
...

在 ServiceExtensions.cs 中:

private const short ODBC_ADD_DSN = 1; //' Add user data source 
       private const short ODBC_CONFIG_DSN = 2; //' Configure (edit) data source 
       source 
        private const int vbAPINull = 0;

    public static void CrearDSN(this IServiceCollection services, IConfiguration config)
            {
                //DSNBaseOperativa = Replace(Left((My.Settings.Servidor & My.Settings.BaseDatos & Application.ProductName), 32), "\", "") 'El nombre tiene como maximo 32 caracteres.

            //IOptions<ConnectionODBC> odbcsettings;

            string Driver = "SQL Server"; 
            int ReturnValue;
            string Attributes;

            //string name = config["SQLconnection:Server"] + config["SQLconnection:Database"] + "InteliBS";
            string name = config["SQLConnection:Server"] + config["SQLConnection:Database"] + "InteliBS";
            string DSNBaseOperativa =  name.Length > 32 ? name.Substring(0, 32).Replace( "\\", "") : name.Replace("\\", ""); //'El nombre tiene como maximo 32 caracteres.

            Attributes = "SERVER=" + config["SQLConnection:Server"] + "\0";
            Attributes += "DESCRIPTION=Temp DSN" + "\0";
            Attributes += "DSN=" + DSNBaseOperativa + "\0";
            Attributes += "DATABASE=" + config["SQLConnection:Database"] + "\0";
            //'To show dialog, use Form1.Hwnd instead of vbAPINull. 
            ReturnValue = SQLConfigDataSource(vbAPINull, ODBC_ADD_DSN, Driver, Attributes); //TODO: Poder regresar el valor en debug o pararlo en caso 0
            if (ReturnValue != 0){
                System.Diagnostics.Debug.WriteLine("Se ha cargado un DSN de ODBC: " + DSNBaseOperativa, "INFO");
                config["ConnectionODBC:DSN"] = DSNBaseOperativa;
                config["ConnectionODBC:Description"] = "Temp DSN";
                config["ConnectionODBC:Server"] = config["SQLConnection:Server"];
                config["ConnectionODBC:Database"] = config["SQLConnection:Database"];
                config["ConnectionODBC:Userid"] = config["SQLConnection:Userid"];
                config["ConnectionODBC:Password"] = config["SQLConnection:Password"];

                services.Configure<ConnectionODBC>(config.GetSection("ConnectionODBC"));
            }
            else
            {
                System.Diagnostics.Debug.WriteLine("No se pudo crear un DSN de ODBC", "INFO");
            }

        }

// Use DllImport to import the Win32 SQLConfigDataSource function.
        [DllImport("ODBCCP32.DLL", CharSet = CharSet.Ansi)]
        private static extern int SQLConfigDataSource(int hwndParent, int ByValfRequest, string lpszDriver, string lpszAttributes);

也许您想知道我为什么要配置 ODBC 而不是常规的 SQL 连接字符串。原因是遗留库需要在应用程序的某些部分中使用,我不会解释。其他一切都照常使用实体框架。

当我使用 IISExpress 从 Visual Studio 2017 运行我的应用程序时,它就像一个魅力。它创建一个没有问题的 ODBC 用户源,以防万一不存在。

但是当我尝试在 Windows Server 2012 R2 上托管它的 IIS 服务器时,它不起作用。是否有来自 IIS8 的限制来防止这种情况发生?

【问题讨论】:

  • 在 IIS 中托管时,您是否尝试过为应用程序池启用 Enable 32 bit Applicaiton 设置?
  • 没有。如果我的应用是 64 位的,如何关联?
  • 因为您的 DSN 可能是使用 32 位 ODBC 创建的。 firehousesoftware.com/webhelp/FHCADMonitor/Content/…
  • 我启用了它但它没有任何作用,64位/32位ODBC源是一样的。
  • SQLConfigDataSource 返回值是否为真?

标签: asp.net iis asp.net-core odbc iis-8


【解决方案1】:

运行 IIS 的用户没有必要的权限。

为此应用程序从应用程序池中选择其他用户解决了该问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-19
    • 2021-09-16
    • 2019-03-29
    • 2020-01-27
    • 2020-11-22
    相关资源
    最近更新 更多