小编Ota*_*ake的帖子

在WebApi中使用Unity解决每个请求的dbcontext

我正在努力使这项工作.我已经安装了Unity和Unity.AspNet.WebApi软件包(v 3.5.1404),并且下面附带了激活码

public static class UnityWebApiActivator
{
    /// <summary>Integrates Unity when the application starts.</summary>
    public static void Start() 
    {
        var container = UnityConfig.GetConfiguredContainer();
        var resolver = new UnityHierarchicalDependencyResolver(container);

        GlobalConfiguration.Configuration.DependencyResolver = resolver;

        // DynamicModuleUtility.RegisterModule(typeof(UnityPerRequestHttpModule));
    }

    /// <summary>Disposes the Unity container when the application is shut down.</summary>
    public static void Shutdown()
    {
        var container = UnityConfig.GetConfiguredContainer();
        container.Dispose();
    }
}
Run Code Online (Sandbox Code Playgroud)

我的类型注册看起来像这样:

 public static void RegisterTypes(IUnityContainer container)
        {
            container.RegisterType<IAuditService, AuditService>(
                new PerThreadLifetimeManager(),
                new InjectionConstructor(new SecurityDbContext()));
        }
Run Code Online (Sandbox Code Playgroud)

到目前为止,我已经尝试过PerThreadLifetimeManager和TransientLifetimeManager但没有成功.我也得到了Unity.Mvc包并尝试使用msdn建议的PerRequestLifetimeManager,但没有运气.它总是给我相同的dbcontex实例.

我宁愿不包含任何MVC依赖,因为这纯粹是WebApi,但是当我尝试使用Unity.Mvc时,我也结束了一些http运行时错误.

任何人都有一个很好的建议/示例来解决每个请求使用Unity在WebApi中的dbcontext,最好没有任何mvc依赖?

c# entity-framework dependency-injection asp.net-web-api

14
推荐指数
1
解决办法
2万
查看次数

实体框架代码First ReadOnly Entity

有没有办法将实体标记为只读而不指定任何密钥?

entity-framework ef-code-first

11
推荐指数
2
解决办法
1万
查看次数

这些C++结构的C#等价物是什么

typedef union _Value {
    signed char    c; 
    unsigned char  b; 
    signed short   s; 
    unsigned short w; 
    signed long    l; 
    unsigned long  u; 
    float          f; 
    double        *d; 
    char          *p; 
} Value;


typedef struct _Field {
 WORD    nFieldId;
 BYTE    bValueType;
 Value Value;
} Field;


typedef struct _Packet {
 WORD    nMessageType;
 WORD    nSecurityType;
 BYTE    bExchangeId;
 BYTE    bMarketCenter;
 int     iFieldCount;
 char    cSymbol[20];
    Field FieldArr[1];

} Packet;
Run Code Online (Sandbox Code Playgroud)

这些C++结构的C#等价物是什么?

我正在将一些代码从C++迁移到C#,并且在迁移这些结构时遇到问题.我曾尝试过一些东西,但我总是遇到编组问题.

c# c++ struct marshalling

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