我需要在我的服务器上的Windows服务中运行一堆可插入的进程,并希望创建一个用户界面,允许我与服务使用的每个插件进行交互.
用户界面和长时间运行的Windows服务之间通信的最常用方法是什么?我正在考虑提供一个中间位置,如数据库,并使用某种消息队列向服务发出命令.你们有没有实施过这样的方法,或者其他一些优秀的方法?你在这个过程中遇到了什么问题?
我在我的应用程序中设置了一个依赖注入容器,我认为每次WCF服务请求进入时组合容器效率都会很低.
有人可以向我解释,对于http/IIS托管的WCF服务,服务的生命周期是什么?如果我能解决这个问题,我可以就容器的存储,实例化,销毁等方面做出明智的决定.
有时微软的异常消息令人愤怒无益.我创建了一个很好的小MVC方法来呈现文本.方法体如下.当它到达"DrawString"方法时,我得到一个异常,说"参数无效".
请注意,我能说的最好的字体构造正确(我只是在10pt使用Arial),rect大小是正面且有效的看起来,画笔是白色的SolidBrush并且格式标志不影响输出;即如果我从调用中排除格式标志,我仍然会收到错误.
DrawString调用就在底部附近.
public ActionResult RenderText(
string fontFamily,
float pointSize,
string foreColor,
string backColor,
bool isBold,
bool isItalic,
bool isVertical,
string align,
string[] allText,
int textIndex)
{
// method renders a horizontal or vertical text image, taking all the text strings that will be rendered in each image
// and sizing the final bitmap according to which text would take the most space, thereby making it possible to render
// a selection of text images all at the same size. …Run Code Online (Sandbox Code Playgroud) 根据我的研究,我学到了以下内容:
TaskScheduler.UnobservedTaskException必须等待该任务被垃圾收集,然后该任务的未被观察到的异常将冒泡到该UnobservedTaskException事件.Task.Wait()它,它永远不会被调用,因为你正在阻止来自Task的即将发生的结果,因此异常将被抛出Task.Wait()而不是冒泡到UnobservedException事件.GC.Collect()手动调用通常是一个坏主意,除非你确切地知道你正在做什么,因此在这种情况下确认事情是好的,但不能作为问题的正确解决方案.问题
如果我的应用程序在垃圾收集器启动之前退出,我绝对100%无法UnobservedTaskException触发我的事件.
请注意以下代码:
class Program
{
static void Main(string[] args)
{
TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;
Task.Factory.StartNew(() =>
{
Console.WriteLine("Task started.");
throw new Exception("Test Exception");
});
Thread.Sleep(1000);
//GC.Collect();
//GC.WaitForPendingFinalizers();
}
static void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
{
File.WriteAllText(@"C:\data\TestException.txt", e.Exception.ToString());
Console.WriteLine("UNOBSERVED EXCEPTION");
}
}
Run Code Online (Sandbox Code Playgroud)
没有写入异常文件,也没有任何内容写入控制台.在应用程序退出后10-15分钟甚至更长时间可以继续,但我仍然看不到我的应用程序的遗骸被垃圾收集的证据.您可能会问,为什么不在退出时收集?好吧,我的真实场景是我的异常陷阱在Windows服务中托管的WCF服务内运行.当Windows服务关闭(因此手动调用GC.Collect())时,我无法陷阱,因为就我所见,没有任何事件.
我哪里错了?我如何确保如果WCF服务内部的某些内容最终会破坏我的Windows服务,那么我有机会在服务崩溃之前记录异常?
我有一个场景,我必须从我的CompositionContainer实例导出,但我只有一个类型可以使用; 我不知道编译时的类型,因此我无法以通常的通用方式检索导出的对象.
通常你会这样做:
_container.GetExportedObject<IMyType>();
Run Code Online (Sandbox Code Playgroud)
但就我而言,我有这个:
Type someType = ... ;
_container.HowDoIGetTheExport(someType);
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
如果我在特定服务实例和操作的上下文中执行,如何访问当前正在执行的服务实例?服务实例不从任何特定的公共基类或接口继承,并且我可以找到进入现有上下文的唯一途径:
OperationContext.Current
Run Code Online (Sandbox Code Playgroud)
但我似乎无法找到任何引用实际服务实例本身的属性,以便我可以将其转换为我应该知道的内容并对其执行操作.
如果没有探究我为什么这样做(这是无关紧要的),请告诉我是否有任何方法可以找到我想要的参考.
编辑:
[ServiceContract]
public interface IInventory
{
[OperationContract]
List<DealInfo> ListDeals(DealQueryOptions options);
}
// This is the object I will need access to the current instance of
public class Inventory : ServiceBase<Inventory>, IInventory
{
public List<DealInfo> ListDeals(DealQueryOptions options)
{
var obj = new Whatever(); // see below
}
}
public class Whatever
{
public Whatever()
{
// how do I get access to the service instance here?
// assume that in this context we are not allowed to …Run Code Online (Sandbox Code Playgroud) 我正在运行MongoDB服务器(它实际上已经运行了).该服务器具有64GB的RAM和16个内核,以及2TB的硬盘空间可供使用.
文件结构
该数据库有一个domains包含大约2000万个文档的集合.每个文档中都有相当数量的数据,但出于我们的目的,文档的结构如下:
{
_id: "abcxyz.com",
LastUpdated: <date>,
...
}
Run Code Online (Sandbox Code Playgroud)
_id字段是文档引用的域名.LastUpdated上有一个升序索引.LastUpdated每天更新数十万条记录.基本上每次新数据可用于文档时,文档都会更新,LastUpdated字段会更新为当前日期/时间.
查询
我有一种从数据库中提取数据的机制,因此可以在Lucene索引中对其进行索引.LastUpdated字段是标记对文档所做更改的关键驱动程序.为了搜索已更改的文档并翻阅这些文档,我执行以下操作:
{
LastUpdated: { $gte: ISODate(<firstdate>), $lt: ISODate(<lastdate>) },
_id: { $gt: <last_id_from_previous_page> }
}
sort: { $_id:1 }
Run Code Online (Sandbox Code Playgroud)
如果没有返回文档,则开始和结束日期将继续,并且重置_id"anchor"字段.此设置可以容忍之前已更改LastUpdated值的页面中的文档,即分页不会被先前页面中现在技术上不再位于这些页面中的文档数量错误地抵消.
问题
我想理想地一次选择大约25000个文档,但由于某种原因,查询本身(即使只选择<500个文档)也非常慢.
我跑的查询是:
db.domains.find({
"LastUpdated" : {
"$gte" : ISODate("2011-11-22T15:01:54.851Z"),
"$lt" : ISODate("2011-11-22T17:39:48.013Z")
},
"_id" : { "$gt" : "1300broadband.com" }
}).sort({ _id:1 }).limit(50).explain()
Run Code Online (Sandbox Code Playgroud)
事实上,解释(在撰写本文时)已经运行了超过10分钟并且还没有完成.我会更新这个问题,如果它完成,但当然点是查询非常慢.
我能做什么?我没有最微弱的线索,问题可能与查询有关.
编辑 55分钟后解释完成.这里是:
{
"cursor" : "BtreeCursor Lastupdated_-1__id_1",
"nscanned" : 13112,
"nscannedObjects" : 13100,
"n" : 50,
"scanAndOrder" : …Run Code Online (Sandbox Code Playgroud) 在发布x-www-form-urlencoded数据时,我很难让自定义模型绑定工作.我已经尝试过各种我能想到的方式,似乎没有任何东西可以产生预期的结果.注意发布JSON数据时,我的JsonConverters等都可以正常工作.这是我发布的时候x-www-form-urlencoded,系统似乎无法弄清楚如何绑定我的模型.
我的测试用例是我想将TimeZoneInfo对象绑定为我的模型的一部分.
这是我的模型活页夹:
public class TimeZoneModelBinder : SystemizerModelBinder
{
protected override object BindModel(string attemptedValue, Action<string> addModelError)
{
try
{
return TimeZoneInfo.FindSystemTimeZoneById(attemptedValue);
}
catch(TimeZoneNotFoundException)
{
addModelError("The value was not a valid time zone ID. See the GetSupportedTimeZones Api call for a list of valid time zone IDs.");
return null;
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是我正在使用的基类:
public abstract class SystemizerModelBinder : IModelBinder
{
public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
{
var name = GetModelName(bindingContext.ModelName);
var valueProviderResult = bindingContext.ValueProvider.GetValue(name);
if(valueProviderResult == …Run Code Online (Sandbox Code Playgroud) 我无法让我的模型将实体的Id属性表示为字符串,但是将其自动生成并由MongoDb在内部表示为本机ObjectId.
class Account
{
public string Id { get; set; }
...
}
class AccountStore
{
static AccountStore()
{
BsonClassMap.RegisterClassMap<Account>(cm =>
{
cm.AutoMap();
cm.SetIgnoreExtraElements(true);
// map Id property here
});
}
public void Save(Account account)
{
_accounts.Save(account);
}
}
Run Code Online (Sandbox Code Playgroud)
对于// map Id property here上面代码中的行,我尝试了许多不同的配置Id映射的方法,但没有一种方法有效.我尝试过的方法以及调用Save方法时抛出的相关异常是:
// Exception: No IdGenerator found.
cm.IdMemberMap
.SetRepresentation(BsonType.ObjectId);
// Exception: No IdGenerator found.
cm.IdMemberMap
.SetRepresentation(BsonType.String);
// Exception: Unable to cast object of type 'MongoDB.Bson.ObjectId' to type 'System.String'.
cm.IdMemberMap
.SetRepresentation(BsonType.ObjectId) …Run Code Online (Sandbox Code Playgroud) 在Visual Studio postbuild中,我需要运行批处理文件.该解决方案可能与Visual Studio运行的驱动器位于不同的驱动器上.在postbuild中,如何确定运行解决方案的驱动器号,以便在运行批处理文件之前更改为该驱动器?目前,我所拥有的只是:
CD $(ProjectDir)
$(ProjectDir)postbuild.bat
Run Code Online (Sandbox Code Playgroud)
问题是当目录位于不同的驱动器上时更改目录不会更改当前目录,因为您必须手动更改您所在的驱动器,例如:
E:\
CD $(ProjectDir)
$(ProjectDir)postbuild.bat
Run Code Online (Sandbox Code Playgroud)
我不能保证解决方案将会在什么驱动器上,所以我需要通过某种宏来确定驱动器,以确保postbuild.bat文件将从当前位置运行.