简体   繁体   English

.Net Core Azure功能-无法绑定

[英].Net Core Azure Function - Cannot bind

I am do I migration to .standard to .core, but I faced a problem: 我是否要从.standard迁移到.core,但是我遇到了一个问题:

Microsoft.Azure.WebJobs.Host: Error indexing method 'FunctionName'. Microsoft.Azure.WebJobs.Host:错误索引方法'FunctionName'。 Microsoft.Azure.WebJobs.Host: Cannot bind parameter 'MyService' to type IMyService. Microsoft.Azure.WebJobs.Host:无法绑定参数“ MyService”以键入IMyService。 Make sure the parameter Type is supported by the binding. 确保绑定支持参数类型。 If you're using binding extensions (eg Azure Storage, ServiceBus, Timers, etc.) make sure you've called the registration method for the extension(s) in your startup code (eg builder.AddAzureStorage(), builder.AddServiceBus(), builder.AddTimers(), etc.). 如果您使用绑定扩展(例如Azure存储,ServiceBus,Timer等),请确保已在启动代码中调用了扩展的注册方法(例如builder.AddAzureStorage(),builder.AddServiceBus( ),builder.AddTimers()等)。

I am using a startup with autofac to resolve the dependency 我正在使用带有autofac的启动来解决依赖关系

 public sealed class Startup : IExtensionConfigProvider
    {
        private static IContainer container;

        public void Initialize(ExtensionConfigContext context)
        {
            var builder = new ContainerBuilder();

            builder.RegisterType<MyService>().As<IMyToCoreService>().InstancePerDependency();

            container = builder.Build();

            context.AddBindingRule<InjectAttribute>().BindToInput<dynamic>(inject => container.Resolve(inject.Type));
        }
    }

The NuGets are: NuGets是:

Autofac - 4.9.0
Microsoft.Azure.WebJobs.Extensions.Storage - 3.0.3
Microsoft.NET.Sdk.Functions 1.0.24

And the function: 和功能:

public static async Task RunAsync([QueueTrigger(ASERVICETOGETANAME)]string blobInformation,
            [Inject(typeof(IMyService))]IMyService myService, 
            ILogger log, CancellationToken cancellationToken)

I try to downgrade the versions of nuget and .net core, but I didnt have any success, what more can I do resolve the dependency problem? 我尝试降级nuget和.net core的版本,但没有成功,还能解决依赖问题吗?

EDIT - The Startup class is not being called. 编辑-不启动类。

I found a solution, 我找到了解决方案,

install the nugget AzureFunctions.Autofac 安装金块AzureFunctions.Autofac

add [DependencyInjectionConfig(typeof(Startup))] in your function class. 在函数类中添加[DependencyInjectionConfig(typeof(Startup))]

on the startup class, make a construct passing a string : 在启动类上,构造一个传递字符串的构造:

public Startup(string functionName)
{
    Initialize(functionName);
}

on the Initialize: 在初始化:

 public void Initialize(string functionName)
 {
      DependencyInjection.Initialize(builder =>
      {
       // your injections
      }, functionName);

  }

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

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