小编Rub*_*ink的帖子

如何处理动作过滤器?

我有以下全局过滤器,ISiteValidation和ICacheService通过Windsor容器注入并设置为Transient,因此容器不会自动处理依赖项.当站点投入生产时,这将导致资源问题.那么人们正在做什么来正确处理注入过滤器的资源?两个接口都是IDisposable,但是当Action Filter超出范围并且容器将继续保持实现时,Dispose永远不会被调用.

public class SiteValidationAttribute : ActionFilterAttribute
{
    public ISiteValidation SiteValidation { get; set; }
    public ICacheService CacheService { get; set; }

    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        if (filterContext.RequestContext.HttpContext.Request.Url != null)
        {
            string host = filterContext.RequestContext.HttpContext.Request.Url.Host;
            try
            {
                string siteId = CacheService.Get("SiteId",
                                                 () =>
                                                 SiteValidation.GetSiteId(
                                                     host));
                var siteIdCookie = new HttpCookie("_site") {Value = siteId};
                filterContext.RequestContext.HttpContext.Response.Cookies.Add(siteIdCookie);
            }
            catch (Exception)
            {
                throw new HttpException(404, String.Format("This site'{0}' was not found", host));
            }
        }

        base.OnActionExecuted(filterContext);
    }
}
Run Code Online (Sandbox Code Playgroud)

dependency-injection castle-windsor asp.net-mvc-3 asp.net-mvc-4

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

如何在f#中检查日期是否在营业时间内?

我试图在fsharp中编写一个简单的函数来检查营业时间,但它不起作用.

这就是我所拥有的:

let businessdate dts =
    let newdts = DateTime.FromOADate dts
    match newdts with
    | not DayOfWeek.Saturday & not DayOfWeek.Sunday & DateTime.Hour >= 8 & DateTime.Hour <= 20 -> 1
    | _ -> 0
Run Code Online (Sandbox Code Playgroud)

这失败了.Fsharp中的DateTime是否可能没有成员小时?我怎么能这样做?

f#

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

如何通过选择要添加的项来生成数组

我有以下数组

let evens = [| 
    for key in numbersDictionary.Keys -> 
        match numbersDictionary.[key] % 2
        | 0 -> Some(numbersDictionary.[key])
        | _ -> None |]
Run Code Online (Sandbox Code Playgroud)

我确实有None奇数的选项类型数组.如何调整上述声明所以evens成为int[]代替int option [],没有NoneS'

f#

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

F#中被歧视联盟的含义

我确实理解他们独立情境中"歧视"和"联合"的含义,但我对F#的"被歧视的联盟"感到茫然.

Fyi,英语不是我的第一语言,我也不擅长数学.所以我希望那里的人可以了解F#的这个特性.请.

我需要知道的是:

  • 这个受歧视的联盟的用例.它通常用于什么?
  • 它等同于其他OOP功能/术语.如果有的话.
  • 是否像我们使用维恩图来表示数据的设置操作?

或者你可以帮我指点链接.

f# discriminated-union

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

避免在TeamCity构建作业中为Github pull请求构建/ refs/heads/master

我有一个作业设置,使用JetBrains博客上概述的方法,即使用具有分支规范的VCS根,从每个Github拉取请求构建合并提交+:refs/heads/pull/(*/merge).这很好 - 每次提交拉取请求或使用新提交更新拉取请求时,都会触发构建作业.

但是,它也会被触发更改master,例如合并拉取请求时.我有另一个工作,构建和测试对master的更改(也将成功的构建发布到我们的临时环境),所以我不想为这些更改运行此作业.

如何排除 master VCS根目录中的更改?

git teamcity

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

混凝土.Net类的依赖注入

注入/隔离密封在dll中并且不实现接口的类的首选方法是什么?

我们使用Ninject.

假设我们有一个类"Server",我们想要注入/隔离"Server"使用的类TcpServer.

不想太具体,因为我想知道最好的方法,但让我们这样说:

public class Server 
{
    IServer _server;
    public Server(IServer server) 
    { 
        _server = server;
    }

    public void DoSomething()
    {
        _server.DoSomething();     
    }
}
Run Code Online (Sandbox Code Playgroud)

_server 在测试的情况下,应该注入TcpClient或mock

.net c# dependency-injection ninject

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

区分联盟初始化

有没有一种更简单的方法来使用F#初始化这副牌?

let create() = [ 0..12 ]

type Suit = 
    | Spades of list<int>
    | Hearts of list<int>
    | Clubs of list<int>
    | Diamonds of list<int>

let spades = create() |> Spades
let hearts = create() |> Hearts
let clubs = create() |> Clubs
let diamonds = create() |> Diamonds
Run Code Online (Sandbox Code Playgroud)

具体来说,我想简化这四种套装的初始化.有更简单的方法吗?

我可以列举这个有区别的联盟的类型并将其分配给一些"甲板"结构吗?

注意:

我是F#的新手.如果这个问题似乎无知,请原谅我.

f#

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

如何在 Windows 服务中初始化 Serilog?

我有以下问题。我希望能够Serilog在 Windows 服务中使用,但我不知道它应该在哪里初始化:

目前我在我的初始化它Main

using Serilog;

public static void RunService() {
    Log.Logger = new LoggerConfiguration()
      .WriteTo.RollingFile([some path])
      .CreateLogger();

    MyService daemon = new MyService();
    Log.Information("Service initialized")

    ServiceBase[] services;
    services = new ServiceBase[] {
        service
    };
    Log.Information("Before running the service");
    ServiceBase.Run(services);
}

static void Main(string[] args) {
   RunService();
}
Run Code Online (Sandbox Code Playgroud)

服务

public class MyService:ServiceBase{
   protected override void OnSessionChange(SessionChangeDescription changeDescription) {
    Log.Information("session changed");
   } 
   protected override void OnStart(string[] args) {
     Log.Information("Started service");
   }
}
Run Code Online (Sandbox Code Playgroud)

因此,为了SerilogMain运行服务集合的服务和目标服务中使用它应该如何完成?

.net windows-services initialization serilog

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

使用Autofac注入接口的特定实例

我使用ASP.NET MVC 3和Autofac进行依赖注入.我正在使用AutoMapper进行映射.

我有一个IMapper类,我用于所有模型视图映射.所以我的任何映射类都可以实现这个接口.在下面的控制器中,构造函数接收一个IMapper实例,并且在我的用户控制器中它可能会收到一个不同的实例,可能是userMapper.回到下面的代码,我有一个名为NewsMapper的类,它实现了IMapper.如何设置依赖注入,以便此控制器必须接收NewsMapper的实例?请记住,我可能有另一个名为UserMapper的映射器.

我有以下控制器:

public class NewsController
{
   private INewsService newsService;
   private IMapper newsMapper;

   public NewsController(INewsService newsService, IMapper newsMapper)
   {
      if (newsService == null)
      {
         throw new ArgumentNullException("newsService");
      }

      if (newsMapper == null)
      {
         throw new ArgumentNullException("newsMapper");
      }

      this.newsService = newsService;
      this.newsMapper = newsMapper;
   }
}
Run Code Online (Sandbox Code Playgroud)

我的global.asax.cs中有以下配置:

var builder = new ContainerBuilder();
builder.RegisterControllers(Assembly.GetExecutingAssembly());
builder.RegisterType<NewsService>().As<INewsService>();
builder.RegisterType<NewsRepository>().As<INewsRepository>();
Run Code Online (Sandbox Code Playgroud)

更新:

我的IMapper界面:

public interface IMapper
{
   object Map(object source, Type sourceType, Type destinationType);
}
Run Code Online (Sandbox Code Playgroud)

我的NewsMapper类:

public class NewsMapper : IMapper
{
   static NewsMapper()
   { …
Run Code Online (Sandbox Code Playgroud)

asp.net-mvc dependency-injection autofac automapper asp.net-mvc-3

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

存储库"生成器"的依赖注入

我做了我的研究,但是我找不到任何我想做的具体例子.

我成功地将Ninject实现到了我的MVC项目中.一切都很完美.但是,我想做最后一步.

到现在为止,我一直在这样工作(正常的DI模式):

public class myController : Controller
{  
    private iMyInterface myRepository;

    public myController(iMyInterface myRepository)
    {
        this.myRepository = myRepository;
    }        

    public ActionResult list(){
        return view(myRepository.getMyList())
    }        

    // rest o the code ...
}
Run Code Online (Sandbox Code Playgroud)

我的问题是; 有办法做这样的事情吗?(存储库"Generator")

public class myController : Controller
{  
    private iMyInterface myRepository = specialClass.GetMyRepository();

    public ActionResult list(){
        return view(myRepository.getMyList()) }

    // rest o the code ...    
}
Run Code Online (Sandbox Code Playgroud)

我知道我正在写一个无意义的代码,但我的想法是能够做类似的事情.

有什么建议?

asp.net-mvc dependency-injection ninject

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