小编Tra*_*lig的帖子

如何在依赖于可移植类库的库上成功运行secannotate.exe?

我正在研究Autofac项目,试图将所有常见逻辑转换为可移植类库,并为特定功能添加特定于平台的库.

我的开发机器是Windows 8企业版(64位),我安装了VS 2012 Ultimate以及所有装饰.我没有安装任何以前的.NET框架,任何其他工具或任何额外的PCL专用工具.它是一个干净的,新的虚拟机,只有基础的东西.在此配置中,所有构建和测试都运行良好.

当我尝试在依赖于其中一个可移植类库的.NET 4.5(完整配置文件)库上运行secannotate.exe时,出现错误,指示我需要mscorlib 2.0.5.0.

这是一个示例错误.PCL是Autofac.dll; .NET 4.5完整配置文件库是Autofac.Configuration.dll.

Error running annotator: Could not find referenced assembly 'Assembly(Name=mscorlib, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)'. Ensure that the reference paths and assemblies are setup correctly.
Microsoft (R) .NET Framework Security Transparency Annotator 4.0.30319.17929
Copyright (C) Microsoft Corporation.  All rights reserved.

Loaded assembly 'Autofac.Configuration' from 'C:\dev\opensource\autofac\trunk\build_output\bin\net40\Autofac.Configuration.dll'.
Resolving assembly 'Assembly(Name=mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)'.
Loaded assembly 'mscorlib' from 'C:\Windows\Microsoft.Net\assembly\GAC_64\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll'.
Loaded referenced assembly from 'C:\Windows\Microsoft.Net\assembly\GAC_64\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll'.
Using …
Run Code Online (Sandbox Code Playgroud)

.net code-access-security portable-class-library

5
推荐指数
1
解决办法
1196
查看次数

是否可以使用Castle.DynamicProxy创建异步交互?

我们基本上有一个如下所示的类,它使用Castle.DynamicProxy进行拦截.

using System;
using System.Collections.Concurrent;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Castle.DynamicProxy;

namespace SaaS.Core.IoC
{
    public abstract class AsyncInterceptor : IInterceptor
    {
        private readonly ILog _logger;

        private readonly ConcurrentDictionary<Type, Func<Task, IInvocation, Task>> wrapperCreators =
            new ConcurrentDictionary<Type, Func<Task, IInvocation, Task>>();

        protected AsyncInterceptor(ILog logger)
        {
            _logger = logger;
        }

        void IInterceptor.Intercept(IInvocation invocation)
        {
            if (!typeof(Task).IsAssignableFrom(invocation.Method.ReturnType))
            {
                InterceptSync(invocation);
                return;
            }

            try
            {
                CheckCurrentSyncronizationContext();
                var method = invocation.Method;

                if ((method != null) && typeof(Task).IsAssignableFrom(method.ReturnType))
                {
                    var taskWrapper = GetWrapperCreator(method.ReturnType);
                    Task.Factory.StartNew(
                        async () => …
Run Code Online (Sandbox Code Playgroud)

asynchronous castle-dynamicproxy

5
推荐指数
1
解决办法
1349
查看次数

Autofac:如何使用多个类型参数注册泛型?使用EFGenRepo <T,TKey>注册IGenRepo <T,TKey>

我正在尝试构建一个通用的repo并使用autofac进行测试.我有以下界面:

public interface IGenRepo<T, TKey> where T : class
{
    IQueryable<T> Items { get; }
    T find(TKey pk);
    RepoResult delete(TKey pk);
    RepoResult create(T item);
    RepoResult update(T item);
    RepoResult save();
}
Run Code Online (Sandbox Code Playgroud)

这是实现该接口的类:

public class EFGenRepo<T, TKey> : IGenRepo<T, TKey> where T : class
{
    private PortalEntities context = new PortalEntities();

    public IQueryable<T> Items { get { return context.Set<T>().AsQueryable<T>(); } }

    public T find(TKey pk){}
    public RepoResult delete(TKey pk){}
    public RepoResult create(T item){}
    public RepoResult update(T item){}
    public RepoResult save(){}
    private RepoResult …
Run Code Online (Sandbox Code Playgroud)

c# autofac entity-framework-6 .net-4.5.2 asp.net-mvc-5.2

4
推荐指数
1
解决办法
1524
查看次数

如果依赖项仅在单个方法中使用,我应该注入它还是使用服务位置?

我对依赖注入有点陌生,在工作时遇到了问题。

假设我有一个类“Employee”,它有一个方法,这个方法说“Promote”在最罕见的情况下也会有条件地被调用。

“Promote”方法使用“ValueAddition”对象,现在最好的做法是通过构造函数和用户全局对象注入该对象,还是应该解决方法本身的依赖关系?

推荐的最佳实践是什么?或者任何关于已解决依赖的生命周期的指针都会有帮助。

constructor dependency-injection lazy-loading

4
推荐指数
1
解决办法
841
查看次数

在StartUp中使用Autofac解决每用户/每请求的依赖性

我正在尝试解决StartUpASP.Net WebApi 2项目的类中的依赖项.Autofac用于配置依赖注入.方案如下:

  • 不同用户看到不同的数据.每个用户都拥有允许该用户查看某些数据的某些权限.
  • 正在使用包含多个边界的域驱动架构.不允许在域和数据访问层中进行跨境调用.这很重要,因为为了获得用户所允许数据的所有代码,我们需要进行一些跨境调用.
  • 我们希望过滤数据访问层内的数据,以便仅为每个用户检索允许的数据.这里不允许跨境通话.
  • 为了实现这一点,我想从用户可以看到的记录中注入所有代码的列表.
  • 为此,我尝试使用Autofac注册工厂,该工厂根据用户创建此列表.我们打电话给工厂StuffFactory和列表吧StuffModel.

现在的问题是所有这些都需要注册为InstancePerRequest.抛出一个InvalidOperatonException,这听起来很合理,因为请求生命周期范围无法访问StartUp

从请求实例的作用域中看不到具有匹配"AutofacWebRequest"的标记的作用域.这通常表示SingleInstance()组件(或类似场景)正在请求注册为每HTTP请求的组件.在Web集成下,始终从DependencyResolver.Current或ILifetimeScopeProvider.RequestLifetime请求依赖项,从不从容器本身请求.

允许的东西在容器中注册的代码:

private IContainer RegisterAllowedStuff(IContainer container)
{
    var builder = new ContainerBuilder();

    builder.Register(x => GetAllowedStuffForUser(container))
        .As<StuffModel>()
        .InstancePerRequest();

    builder.Update(container);
    return container;
}

private static StuffModel GetAllowedStuffForUser(IContainer container)
{
    var stuffFactory = container.Resolve<IStuffFactory>();
    return stuffFactory.CreateStuffModel(Helper.GetParsedUserName());
}
Run Code Online (Sandbox Code Playgroud)

我有点被困在如何从这里前进.对于我完全监督的Autofac问题,是否有一个简单的解决方案?有人可能更清楚我是如何实现这一点的吗?提前致谢!

c# dependency-injection autofac asp.net-web-api

3
推荐指数
1
解决办法
1240
查看次数

Log4net附加程序始终为空

我在我的webapi项目中使用log4net(使用autofac和owin)。我将此添加到我的控制器

LogManager.GetLogger(typeof(NotificationController));
Run Code Online (Sandbox Code Playgroud)

但是我已经看到没有加载附加程序。在我的配置中我有这个

 <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
...    
<log4net>
        <root>
          <level value="ALL" />
          <appender-ref ref="aiAppender" />
          <appender-ref ref="TraceAppender" />
          <appender-ref ref="FileAppender" />
        </root>
        <appender name="aiAppender" type="Microsoft.ApplicationInsights.Log4NetAppender.ApplicationInsightsAppender, Microsoft.ApplicationInsights.Log4NetAppender">
          <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%message%newline" />
          </layout>
        </appender>
        <appender name="TraceAppender" type="log4net.Appender.TraceAppender">
          <layout type="log4net.Layout.PatternLayout">
            <param name="ConversionPattern" value="%d [%t] %-5p %m%n" />              
          </layout>
        </appender>
        <appender name="FileAppender" type="log4net.Appender.FileAppender">
          <file value="log\server.log" />
          <appendToFile value="true" />
          <layout type="log4net.Layout.PatternLayout">
            <param name="ConversionPattern" value="%d [%t] %-5p %m%n" />
          </layout>
        </appender>
      </log4net>
Run Code Online (Sandbox Code Playgroud)

我没有构建错误,如果我调用此代码,我的代码将运行无错误

logger.Fatal("Error");
Run Code Online (Sandbox Code Playgroud)

但是我没有日志。

更新

将此添加到启动类,但不会更改

public class Startup
{
    private …
Run Code Online (Sandbox Code Playgroud)

c# log4net asp.net-web-api owin

3
推荐指数
1
解决办法
2453
查看次数

依赖注入:根据环境调用不同的服务

我正在构建一个 .net core 3.1 Web 应用程序,并且正在尝试内置依赖项注入。

我想根据应用程序运行的运行时环境注入不同的服务,我认为我可以使用一个属性来定义该服务是否适合该环境,例如:

public void ConfigureServices(IServiceCollection services)
{
    ...

    services.AddTransient<IOperation, OperationDevelopment>();
    services.AddTransient<IOperation, OperationStaging>();
    services.AddTransient<IOperation, OperationProduction>();

    ...
}


public interface IOperation
{
    Guid OperationId { get; }
}

[Development]
public class OperationDevelopment : IOperation
{
}

[Staging]
public class OperationStaging : IOperation
{
}

[Production]
public class OperationProduction : IOperation
{
}
Run Code Online (Sandbox Code Playgroud)

我该怎么办,跳过注册?全部注册然后解决合适的服务?我错过了什么吗?

如果.net core DI太基础了,我应该使用什么?

谢谢

c# inversion-of-control .net-core

1
推荐指数
1
解决办法
3531
查看次数