小编fri*_*edX的帖子

使用相同的接口调用多个类

我有一个像这样的界面

public interface IAddressProvider
{
    string GetAddress(double lat, double long);
}
Run Code Online (Sandbox Code Playgroud)

在我的消费类中,我想循环遍历具体的提供者,直到得到结果,例如(简化的):

string address;
address = _cachedAddressProvider.GetAddress(lat, long);
if(address == null)
    address = _localDbAddressProvider.GetAddress(lat, long);
if(address = null)
    address = _externalAddressProvider.GetAddress(lat, long);

return address ?? "no address found";
Run Code Online (Sandbox Code Playgroud)

然后,我可以模拟每个提供程序进行单元测试,将 null 设置为返回值以适当测试所有代码路径。

我如何将接口注入到我的消费类中(最好使用 StructureMap)以便正确解析每个具体实现?

c# structuremap unit-testing dependency-injection

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

如何在操作过滤器中记录所有查询参数?

我正在实现一个记录器,它应该通过操作过滤器捕获详细信息,以便记录用户活动和查找故障。

   public interface ISessionLogger
    {
        void LogUserActionSummary(int sessionId, string userActionType);    
        void LogUserActionDetail(int session, string userActionType, DateTime userActionStartedUtc, long actionDurationInMilliseconds, string queryParams, string referredFrom);
    }
Run Code Online (Sandbox Code Playgroud)

我想queryParams从表单集合、路由值、操作参数、json 调用和查询字符串中捕获所有键和值,这听起来像是我需要的

filterContext.Controller.ValueProvider

但似乎不可能循环遍历这个。所以在我的 FilterAttribute 中我有

public void OnActionExecuting(ActionExecutingContext filterContext)
{
    var paramList = new List<string>();
    if(filterContext.ActionParameters != null)
    {
        paramList.AddRange(filterContext.ActionParameters.Select(param => String.Format("{0}:{1}", param.Key, param.Value.ToString())));
    }
    if(filterContext.Controller.ValueProvider != null)
    {
        //loop through the valueprovider and save the key value pairs to paramList
    }

    _queryParams = string.Join(",", paramList);
}
Run Code Online (Sandbox Code Playgroud)

是否有另一种方法可以在操作过滤器的 OnActionExecuting 方法中实现此目的?

c# logging action-filter asp.net-mvc-3

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

FileSystemWatcher无法识别Excel SaveAs

我有一个使用FileSystemWatcher作为Windows服务运行的简单应用程序.文件通过excel VB Macro保存到目录中

ActiveWorkbook.SaveAs Filename:= "pathToSaveTo"
Run Code Online (Sandbox Code Playgroud)

在创建新文件时,观察者调用一个方法来处理该文件

void watcher_FileCreated(object sender, FileSystemEventArgs e)
    {
        while (true)
        {
            if (FileUploadComplete(e.FullPath))
            {
                this.ProcessOneFile(e.FullPath, e.Name);
                break;
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

当发生这种情况时,观察者应用程序从不注册事件,但手动删除并重新添加文件到文件夹会导致事件被引发.

当文件保存到目录时,有人知道如何获得预期的行为吗?

c# windows-services filesystemwatcher

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