Azure函数:可以编译但不能与自定义数据层库一起运行 [英] Azure Functions: Can compile but cannot run with custom datalayer library

查看:89
本文介绍了Azure函数:可以编译但不能与自定义数据层库一起运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图提出一个更好的标题,但不能.

I've tried to come up with a better title but can't.

问题是我不熟悉Azure函数,但是做了一件简单的工作,即写入SQL Azure表.现在,我已经尝试构建最简单的基于实体框架的数据层并将其上传.现在,它已编译为.Net 4.6并使用EF 6.1.3.

The issue is I am new to Azure functions but have made a simple one work that writes to a SQL Azure table. Now I've attempted to build the simplest kind of Entity Framework based Datalayer and uploaded it. Right now it is compiled as .Net 4.6 and using EF 6.1.3.

我在这里按照第二个答案使用连接字符串本指南.

I'm using a connection string as per the second answer here Second answer and have checked it is being retrieved correctly. Update - I also used this guide.

删除此{#r"D:\ home \ site \ wwwroot \ sharedbin \ TestDataLayer.dll"}会使编辑器抱怨缺少程序集,因此它正在查找有问题的dll.

Removing this {#r "D:\home\site\wwwroot\sharedbin\TestDataLayer.dll"} causes the editor to complain about missing assemblies, so it IS finding the dll in question.

但是它将无法运行-无法找到TestDataLayer.dll.

However it will not run - it cannot find TestDataLayer.dll.

我仅在门户网站编辑器中运行此程序(我尚未直接从Visual Studio Project掌握部署-别笑了:P).

I'm only running this in the portal editor (I've not yet mastered deployment direct from a Visual Studio Project - don't laugh :P).

#r "System.Configuration"
#r "System.Data.Entity"
#r "D:\home\site\wwwroot\sharedbin\TestDataLayer.dll"

using System;
using System.Collections;
using System.Configuration;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Data.Entity.SqlServer;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using System.Net;
using TestDataLayer;

public static void Run(TimerInfo myTimer, TraceWriter log)
    {
       var connection =  ConfigurationManager.ConnectionStrings["sql_connection"].ConnectionString; 
       using(var db = new SyncDbContext(connection))
            {
                var RK = new RKAzureTest()  {TestField1 = "It finally worked?" };
                db.RKAzureTests.Add(RK);
                db.SaveChanges();
            }
    }       

[DbConfigurationType(typeof(myDBContextConfig))] 
public partial class SyncDbContext : System.Data.Entity.DbContext
{
    public SyncDbContext(string cs) : base(cs) {}
    public DbSet<RKAzureTest> RKAzureTests {get;set;}

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
 //       modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
    }

}

    public  class myDBContextConfig : DbConfiguration
        {
            public myDBContextConfig()
            {
                SetProviderServices("System.Data.EntityClient", 
                System.Data.Entity.SqlServer.SqlProviderServices.Instance);
                SetDefaultConnectionFactory(new System.Data.Entity.Infrastructure.SqlConnectionFactory());
            }
        }

这是function.json:

This is the function.json:

   {

    "frameworks": {

        "net46":{
                    "dependencies": {

                        "EntityFramework": "6.1.3"
                    }
                }
    }

}

在怀疑Azure函数不支持.net 4.7.1之后,我已经将dll本身编译为.net 4.6,并通过Kudu将编译后的dll上传到了sharedbin文件夹(检查了十几遍!)

I've compiled the dll itself to .Net 4.6 after a suspicion that the Azure Functions don't support .net 4.7.1 and via Kudu uploaded the compiled dll to a sharedbin folder (checked the path a dozen times!).

这是引发的错误:

    2018-05-01T11:00:00.012 [Warning] Unable to find assembly 'TestDataLayer, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. Are you missing a private assembly file?
2018-05-01T11:00:00.012 [Error] Exception while executing function: Functions.TimerTriggerCSharp1. mscorlib: Exception has been thrown by the target of an invocation. f-TimerTriggerCSharp1__514732255: Could not load file or assembly 'TestDataLayer, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.

不太确定还剩下什么-我在应用程序设置中使用运行时版本1.0.11702,因为我发现如果使用Beta版,生活会变得更加复杂.

Not quite sure what else can be left - I'm using runtime version 1.0.11702 in the Application settings as I found life got a LOT more complicated if I went onto the Beta version.

如果有人能指出我该用例的工作指南(首先使用数据库,EF 6.1.3等),我将不胜感激.

If anyone can point me to a working guide for this use case (Database first, EF 6.1.3 etc) I'd be grateful.

任何提供的帮助都将受到感激!

Any help offered gratefully received!

谢谢:)

推荐答案

转到Azure门户,使用CMD Shell在Azure函数内部创建一个名为"bin"的文件夹,将"TestDataLayer.dll"文件上传到bin文件夹中刚刚创建.

Go to Azure Portal, create a folder called, 'bin' inside your Azure functions using CMD Shell, upload the 'TestDataLayer.dll' file to bin folder which has just been created.

#r "System.Configuration"
#r "System.Data.Entity"
#r "TestDataLayer.dll"

项目结构应该看起来像

AzureFunctionProjectName001
    bin
       TestDataLayer.dll
    run.csx
    project.json
    project.lock.json
    ...

Azure函数这次应该能够发现您的库.我相信,EntityFramework可以正常工作.

Azure functions should be able to discover your library this time. I believe, EntityFramework works just fine.

这篇关于Azure函数:可以编译但不能与自定义数据层库一起运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆