简体   繁体   English

FSharp.Data.SqlCient在本地工作,但不在生产服务器上工作

[英]FSharp.Data.SqlCient works locally but not on production server

I am having an issue with FSharp.Data.SqlClient. 我在FSharp.Data.SqlClient中遇到问题。 I am building an Asp.net Core full framework 4.6 web app. 我正在构建一个Asp.net Core完整框架4.6 Web应用程序。 The main project is in C# which references several projects that are in F#. 主项目在C#中,它引用了F#中的几个项目。 Each of these F# projects use FSharp.Data.SqlClient for data access. 这些F#项目中的每一个都使用FSharp.Data.SqlClient进行数据访问。 When running the app locally everything works fine, but when I deploy it to azure I get the error "Instance Failure" when the F# code attempts to execute a query. 在本地运行应用程序时,一切正常,但是当我将其部署到Azure时,当F#代码尝试执行查询时,出现错误“实例失败”。

I have a suspicion that in some way it has to do with how the connection strings are being consumed or maybe some sort of runtime conflict between FSharp.Data.SqlClient and Entity Framework. 我怀疑它在某种程度上与连接字符串的消耗方式或FSharp.Data.SqlClient与实体框架之间的某种运行时冲突有关。 Entity framework is in the main project to handle the membership data and nothing else. 实体框架在主项目中,用于处理成员资格数据,仅此而已。 I have to specifically add a connection string to a config file in the main project so that the referenced F# projects can access the database at runtime. 我必须在主项目中的配置文件中专门添加一个连接字符串,以便引用的F#项目可以在运行时访问数据库。 Entity framework consumes it's data string via the AppSettings.Json file. 实体框架通过AppSettings.Json文件使用其数据字符串。 I am unaware if there is a better way to wire this up, but again, this works perfectly when I run it locally but not on the server that it's been deployed to. 我不知道是否有更好的方法可以将其连接起来,但是同样,当我在本地运行它(而不是在已部署到它的服务器上)运行它时,这可以完美地工作。
Is there something that I need to enable or change code-wise for the app to work on the production server? 我需要启用或更改代码方式才能使应用程序在生产服务器上工作吗?

Here's a view of my data strings. 这是我的数据字符串的视图。 In my F# project I have an app.config file: 在我的F#项目中,我有一个app.config文件:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <connectionStrings>
    <add name="DefaultConnection" connectionString="Data 
        Source=server code here" 
        providerName="System.Data.SqlClient" />
  </connectionStrings>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7" />
  </startup>
</configuration>

In that same F# project I have another file that I use to access the runtime connection string: 在同一个F#项目中,我有另一个文件用于访问运行时连接字符串:

module internal DbAdmin

open FSharp.Data
open FSharp.Configuration

module Admin = 

// runtime connection string
type private Config = AppSettings<"App.config">
let rtConnection = Config.ConnectionStrings.DefaultConnection

In the main C# project I have an app.config file: 在主要的C#项目中,我有一个app.config文件:

<configuration>
   <runtime>
      <gcServer enabled="true"/>
   </runtime>
   <connectionStrings>
       <add name="DefaultConnection" connectionString="Data Source=server 
            code here" providerName="System.Data.SqlClient"/>

  </connectionStrings>
</configuration>

and in the appsettings.json is configured as this: 并在appsettings.json中将其配置为:

{
  "ConnectionStrings": {

    "DefaultConnection": "Data Source=Server code Here"
  }
}

this is the actual query code: 这是实际的查询代码:

type Select_AllArticles = 

    SqlCommandProvider<
            "
                select * from article.vw_AllArticlesAndDetails
            ", Admin.connectionString, ConfigFile = Admin.configFile
        >

 try
    succeedWithMsg             
        (Select.Select_AllArticles.Create(Admin.rtConnection).Execute() |> 
            Seq.toList)
        (createGoodMsg(OK("Some Success Message.")))
  with
      | ex -> fail (createBadMsg(DbError(ex.Message + " --selectAllArticles")))

**Update: ** After remote debugging the app, this is the entire error message it gives **更新:**在远程调试应用程序之后,这是它给出的完整错误消息

A network-related or instance-specific error occurred while establishing a connection to SQL Server. 建立与SQL Server的连接时发生与网络相关或特定于实例的错误。 The server was not found or was not accessible. 服务器未找到或无法访问。 Verify that the instance name is correct and that SQL Server is configured to allow remote connections. 验证实例名称正确,并且已将SQL Server配置为允许远程连接。 (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified) (提供者:SQL网络接口,错误:26-指定服务器/实例时出错)

This is odd to me as the connection string in use is the same one used by Entity Framework which seems to access the data base just fine. 这对我来说很奇怪,因为使用的连接字符串与Entity Framework所使用的连接字符串相同,似乎可以很好地访问数据库。

New Update Ok, I think I figured out what's going on. 新更新好的,我想我知道发生了什么事。 I believe it is definitely a config issue. 我相信这绝对是配置问题。 So as I stated before, in development I had to create a config file in order to create a connection string that FSharp.Data.SqlClient could connect to. 如前所述,在开发中,我必须创建一个配置文件才能创建FSharp.Data.SqlClient可以连接到的连接字符串。 I couldn't figure out how to get it to connect to the connection string in the appsettings.json file. 我不知道如何使它连接到appsettings.json文件中的连接字符串。 Perhaps someone can explain that to me, as that may be the best solution. 也许有人可以向我解释一下,因为这可能是最好的解决方案。 Anyhow, I followed up on my assumption that the connection string inside of the config file wasn't getting updated on deployment by manually inserting the production server connection string, and then deploying the app. 无论如何,我遵循以下假设:手动插入生产服务器连接字符串,然后部署应用程序,因此在部署时不会更新配置文件内部的连接字符串。 Sure enough the issue was gone, and everything worked normally. 果然问题已经解决,一切正常。 So, now the question is, what's the best way to get FSharp.Data.SqlClient connected to the connection string correctly in a core app that utilizes a appsettings.json file? 因此,现在的问题是,在利用appsettings.json文件的核心应用程序中,使FSharp.Data.SqlClient正确连接到连接字符串的最佳方法是什么? How do I go about handling this issue? 我该如何处理此问题? I need someone to walk me through it, as I'm new to this. 我需要一个人来指导我,因为我是新来的。

Current Status 当前状态

So after realizing that it is indeed a config issue, the question now is how do I properly retrieve the connection settings from the appsettings.json file via my F# projects. 因此,在意识到这确实是一个配置问题之后,现在的问题是如何通过我的F#项目从appsettings.json文件中正确检索连接设置。 Using the FSharp.Data json provider, I need to figure out how to properly locate the appsettings.json file for both production and development use. 使用FSharp.Data json提供程序,我需要弄清楚如何正确定位appsettings.json文件以供生产和开发使用。 How can an F# project locate a file in the main project? F#项目如何在主项目中找到文件? Am I just overly complicating things? 我只是在使事情变得过于复杂吗?

You appear to be looking for App.config to find the connection string. 您似乎正在寻找App.config来查找连接字符串。 That usually gets replaced with a different .dll.config file when deploying, though that doesn't appear to be the issue. 部署时通常会用其他.dll.config文件替换该文件,尽管这似乎不是问题。 Do you have a DefaultConnection in you Azure App Service connection strings that is incorrect? 您的Azure App Service连接字符串中是否有不正确的DefaultConnection? Other than that, I would suggest parsing the connection string from the appsettings.json rather than the app.config file. 除此之外,我建议从appsettings.json而不是app.config文件中解析连接字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM