Visual Studio 2010,x64机器,使用内置Web服务器通过使用内置测试框架的一组单元测试来托管WCF服务.
我有一个XML文件,我的测试需要加载才能运行.我已将此文件包含在测试项目中,并将文件设置为"content"和"始终复制到输出目录".这个文件很好地复制到bin\debug目录.
但是,当我执行测试时,xml文件不在那里.它不是在项目的bin\debug文件夹中查找,而是在测试的工作目录中查找它,C:\ Projects\SAP Reapprovals\TestResults \name_machine 2010-12-06 13_45_43\Out".文件尚未复制到那里.
有没有办法强制复制此文件,或者我是否需要从测试中完全限定引用?
TIA!
詹姆士
更新
我设置了DeploymentItem属性,但文件仍然没有复制.但这肯定看起来像我想要的......任何想法为什么不起作用?
我的测试代码:
[TestMethod]
[DeploymentItem("SAP - GS3 format.xml")]
public void TestProcessSapRoles() {
// I get a 'file not found' error here, and when
// I check the output directory, it isn't there
XElement rolesRoot = XElement.Load("SAP - GS3 format.xml");
}
Run Code Online (Sandbox Code Playgroud)
答案:
感谢CPedros,在他的帮助下,我已经放大了这一点.我运行了SysInternals的Process Monitor,看看它在哪里寻找我的xml文件.这是我发现的:
当我使用ctrl + r,ctrl + t(当前上下文中的调试测试)运行测试时,visual studio 完全忽略了该DeploymentItem属性,甚至没有尝试将文件复制到任何地方.在这种情况下,当我尝试打开它进行阅读时,我收到了"找不到文件"的异常.Visual Studio为测试创建了一个临时工作目录,但其中只有一个文件,AgentRestart.dat.
当我使用工具栏中的"运行单元测试"按钮运行测试时(不确定是什么测试选项),Visual Studio没有复制文件,而是直接从项目目录中引用它.测试通过,没有创建临时工作目录.
当我从菜单选项"run - > tests …
我在SO周围挖了一个答案,到目前为止我能找到的最好的是这里,但是这是针对静态构造函数的实例; 我只是静态使用这个类.
我的代码:
public static class MailHelper {
private static string mailHost;
static MailHelper() {
var mailSettings = ConfigurationManager.GetSection("MailSettings") as NameValueCollection;
if (null == mailSettings) {
throw new ConfigurationErrorsException("Missing Mail Settings in the configuration file");
}
mailHost = ConfigurationManager.AppSettings["mailHost"];
if (null == mailHost) {
throw new ConfigurationErrorsException("Missing mailHost setting in the configuration file");
}
}
public static void SendMail(MailMessage Message) {
...
}
}
try {
MailHelper.SendMail(Message);
}
catch (ConfigurationErrorsException exc) {
...
}
// ???
MailHelper.SendMail(Message);
. …Run Code Online (Sandbox Code Playgroud) TFS 2010,VS 2010
我们在TFS中有一种情况,即开发人员没有遵循正确的合并程序.当我将他的开发者目录与trunk进行比较时,我得到了许多在trunk中标记为不同或不同的文件.最后一次合并/登记到主干是他在2011-05-26,他最后一次登记到他的开发分支是2011-05-25.
我相信有两件事情在继续:
他没有正确更新他的dev分支(我们要求开发人员合并trunk - > dev分支并在合并dev - > trunk之前编译)
他没有正确解决合并冲突
问题是,一旦他将冲突标记为已解决,后续合并就会忽略这些差异.我需要强制合并,重新审视整个文件的差异,以便我可以手动更新他所做的每一个更改.
我怎样才能做到这一点?
在回答关于SO的另一个问题*以及随后的评论讨论时,我遇到了一个我不清楚的问题.
在我误入歧途的任何地方纠正我......
当垃圾收集器收集一个对象时,它会在一个单独的线程上调用该对象的终结器(除非终结器已经被抑制,例如通过一个Dispose()方法).在收集时,GC会挂起除触发集合的线程(除了背景集合)之外的所有线程.
不清楚的是:
*链接到原始问题:
.NET GC从终结器访问同步对象
我搜索了SO并找到了类似的问题,但没有人比较这三个问题.这让我感到惊讶,所以如果有人知道,请指点我.
有许多不同的方法来解析请求的查询字符串......"正确"方式(IMO)应该处理空值/缺失值,但也适当地解码参数值.以下哪项是最好的方法?
方法1
string suffix = Request.QueryString.Get("suffix") ?? "DefaultSuffix";
Run Code Online (Sandbox Code Playgroud)
方法2
string suffix = Request.QueryString["suffix"] ?? "DefaultSuffix";
Run Code Online (Sandbox Code Playgroud)
方法3
NameValueCollection params = HttpUtility.ParseQueryString(Request.RawUrl);
string suffix = params.Get("suffix") ?? "DefaultSuffix";
Run Code Online (Sandbox Code Playgroud)
方法4
NameValueCollection params = HttpUtility.ParseQueryString(Request.RawUrl);
string suffix = params["suffix"] ?? "DefaultSuffix";
Run Code Online (Sandbox Code Playgroud)
问题:
Request.QueryString["suffix"]如果没有指定后缀,会返回null吗?(令人尴尬的基本问题,我知道)
是否HttpUtility.ParseQueryString()提供Request.QueryString直接访问的任何额外功能?
MSDN文档列出了这个警告:
The ParseQueryString method uses query strings that might contain user input, which is a potential security threat. By default, ASP.NET Web pages validate that user input does not include script or HTML elements. …
有谁知道.NET如何处理调用超时Semaphore.WaitOne(timeout)?
我希望有一个TimeoutException,但MSDN文档没有在预期的异常列表中列出这个,我似乎无法在任何地方找到它.
提前致谢!
扩展UserPrincipal以利用其内置属性...当我们重载FindByIdentity()方法时遇到问题.
来自Microsoft的示例http://msdn.microsoft.com/en-us/library/bb384372%28VS.90%29.aspx(为简洁起见,不包括部分):
[DirectoryRdnPrefix("CN")]
[DirectoryObjectClass("inetOrgPerson")]
public class InetOrgPerson : UserPrincipal {
// Implement the overloaded search method FindByIdentity
public static new InetOrgPerson FindByIdentity(PrincipalContext context,
string identityValue) {
return (InetOrgPerson)FindByIdentityWithType(context,
typeof(InetOrgPerson),
identityValue);
}
// Implement the overloaded search method FindByIdentity
public static new InetOrgPerson FindByIdentity(PrincipalContext context,
IdentityType identityType,
string identityValue) {
return (InetOrgPerson)FindByIdentityWithType(context,
typeof(InetOrgPerson),
identityType,
identityValue);
}
}
Run Code Online (Sandbox Code Playgroud)
如果我从MSDN示例中获取确切的代码并将其粘贴到我的应用程序中,它就不起作用.调用InetOrgPerson.FindByIdentity()返回null,如下:
if (null == InetOrgPerson.FindByIdentity(principalContext, UserName)) {
throw new Exception("bah");
}
Run Code Online (Sandbox Code Playgroud)
实际上,从内部InetOrgPerson.FindByIdentity()调用FindByIdentityWithType()返回null,如下: …
c# directoryservices active-directory account-management userprincipal
需要匹配句子的第一部分,直到给定的单词.但是,该单词是可选的,在这种情况下,我想匹配整个句子.例如:
我有一句话,附有我不想要的条款.
我有一句话,我喜欢它.
在第一种情况下,我想要"I have a sentence".在第二种情况下,我想要"I have a sentence and I like it."
Lookarounds将给我第一个案例,但是一旦我尝试使其成为可选项,为了涵盖第二个案例,我得到了整个第一句话.我试过让表达懒惰......没有骰子.
适用于第一种情况的代码:
var regEx = new Regex(@".*(?=with)");
string matchstr = @"I have a sentence with a clause I don't want";
if (regEx.IsMatch(matchstr)) {
Console.WriteLine(regEx.Match(matchstr).Captures[0].Value);
Console.WriteLine("Matched!");
}
else {
Console.WriteLine("Not Matched : (");
}
Run Code Online (Sandbox Code Playgroud)
我希望的表达方式:
var regEx = new Regex(@".*(?=with)?");
Run Code Online (Sandbox Code Playgroud)
有什么建议?
提前致谢!
詹姆士
Windows服务:从配置文件中要监视的目录列表生成一组FileWatcher对象,具有以下要求:
(关于#3,我们确实在没有变化的情况下获取事件......最值得注意的是因为FileWatchers的重复事件问题)
为了做这些事情,我有两个词典 - 一个用于上传的文件,另一个用于任务本身.这两个对象都是静态的,我需要在添加/删除/更新文件和任务时锁定它们.简化代码:
public sealed class TrackingFileSystemWatcher : FileSystemWatcher {
private static readonly object fileWatcherDictionaryLock = new object();
private static readonly object runningTaskDictionaryLock = new object();
private readonly Dictionary<int, Task> runningTaskDictionary = new Dictionary<int, Task>(15);
private readonly Dictionary<string, FileSystemWatcherProperties> fileWatcherDictionary = new Dictionary<string, FileSystemWatcherProperties>();
// Wired up elsewhere
private void OnChanged(object sender, FileSystemEventArgs eventArgs) {
this.ProcessModifiedDatafeed(eventArgs);
}
private void ProcessModifiedDatafeed(FileSystemEventArgs eventArgs) {
lock (TrackingFileSystemWatcher.fileWatcherDictionaryLock) {
// Read the file and …Run Code Online (Sandbox Code Playgroud) 泛型List<T>可以包含任何类型 - 值或引用。当检查列表是否包含对象时,.Contains()使用类型 T 的默认值EqualityComparer<T>并调用.Equals()(这是我的理解)。如果没有定义 EqualityComparer,则默认比较器将调用.Equals(). 默认情况下,.Equals()调用.ReferenceEquals(), so.Contains()仅当列表包含完全相同的对象时才返回 true。
直到您需要重写.Equals()以实现值相等,此时默认比较器表示如果两个对象具有相同的值,则它们是相同的。我想不出有哪一种情况适合引用类型。
我从 @Enigmativity 听到的是,实现IEqualityComparer<StagingDataRow>将为我键入的 DataRow 提供一个默认的相等比较器,该比较器将用于代替Object\xe2\x80\x93 的默认比较器,从而允许我在StagingDataRow.Equals().
问题:
\nEqualityComparer<StagingDataRow>.Equals()而不是调用StagingDataRow.Equals()?IEqualityComparer<StagingDataRow>.GetHashCode(StagingDataRow obj)哈希什么,它应该返回与 相同的值吗StagingDataRow.GetHashCode()?IEqualityComparer<StagingDataRow>.GetHashCode(StagingDataRow obj)?我正在寻找的对象还是列表中的对象?两个都?让实例方法接受自身作为参数会很奇怪......一般来说,在重写时如何区分值相等和引用相等.Equals()?
c# ×9
asp.net ×1
c#-4.0 ×1
constructor ×1
exception ×1
finalizer ×1
gethashcode ×1
iequatable ×1
linq ×1
locking ×1
parameters ×1
regex ×1
request ×1
semaphore ×1
tfs ×1
tfs2010 ×1
timeout ×1
unit-testing ×1