Microsoft.SqlServer.Types 无法在 Azure 辅助角色上正常运行

Microsoft.SqlServer.Types not functioning properly on Azure Worker Role

我希望通过 EntityFramework 与 Azure 辅助角色中的 SQL 服务器数据库通信来使用 DbGeography。据我了解,DbGeography 在后台使用 Microsoft.SqlServer.Types / SqlServerSpatial110.dll,所以为了让它在 Azure 中工作,我遵循了:

http://blogs.msdn.com/b/adonet/archive/2013/12/09/microsoft-sqlserver-types-nuget-package-spatial-on-azure.aspx 并安装了 nuget 包,然后我在 WorkerRole.cs:

的 OnStart 方法中指定加载 SQL 服务器类型
public override bool OnStart()
        {
            // Load SQL Server Types
            SqlServerTypes.Utilities.LoadNativeAssemblies(AppDomain.CurrentDomain.BaseDirectory);

然后我也关注了这篇博文 https://alastaira.wordpress.com/2011/08/19/spatial-applications-in-windows-azure-redux-including-denali/,其中我明确地将 SqlServerSpatial110.dll 添加到项目并将其设置为始终复制。

所以真正的问题是 - 在部署之后,一切都按预期工作。但是,如果我暂时离开 Azure Worker 角色(约 30 分钟)并且它没有收到任何请求,我代码的 DbGeography 部分将不再起作用。

我已经确定我的问题是 EntityFramework 没有完全加载实体,而不是与 Microsoft.SqlServer.Types 有关,但我留下这个问题而不是删除万一它会帮助某人。

我不知道这样做是否正确,所以版主/聪明人,请随意编辑或做任何事情。

回复很晚,但我在寻找解决方案时发现了您的问题。如何在 Azure Functions 中加载 SQL 服务器类型。

主要问题是 运行 代码的来源路径与其他应用程序类型不同。所以你不能使用建议的:

SqlServerTypes.Utilities.LoadNativeAssemblies(Server.MapPath("~/bin")); or
SqlServerTypes.Utilities.LoadNativeAssemblies(AppDomain.CurrentDomain.BaseDirectory);

在您的 Azure 函数 运行 参数中,添加允许您检索工作文件夹的 ExecutionContext 上下文。

通过向上导航一级,您可以使用此路径加载 SQL 服务器类型:

public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]HttpRequestMessage req, TraceWriter log, ExecutionContext context)
{
    string path = context.FunctionDirectory;
    string newPath = Path.GetFullPath(Path.Combine(path, @"..\"));
    SqlServerTypes.Utilities.LoadNativeAssemblies(newPath);