刚刚开始编写单元测试,现在我已经被这种情况阻止了:
我有一个方法,它有一个FileStream对象,我试图传递一个"字符串".所以,我想将我的字符串转换为FileStream,我这样做:
File.WriteAllText(string.Concat(Environment.ExpandEnvironmentVariables("%temp%"),
@"/test.txt"), testFileContent); //writes my string to a temp file!
new FileStream(string.Concat(Environment.ExpandEnvironmentVariables("%temp%"),
@"/test.txt"), FileMode.Open) //open that temp file and uses it as a fileStream!
Run Code Online (Sandbox Code Playgroud)
然后关闭文件!
但是,我想必须有一些非常简单的替代方法将字符串转换为fileStream.
欢迎提出建议![注意在stackoverflow中有这个问题的其他答案,但似乎没有一个直接的解决方案]
提前致谢!
我正在Routing研究MVC 的概念,特别是在以下行:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // Route Pattern
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Default values.
);
Run Code Online (Sandbox Code Playgroud)
一切都很好,但我没有得到的意义Route Name.
这只是一个术语吗?或者有什么重要的吗?
我错过了使用它或MVC 4中使用的地方在哪里?
提前致谢.
这是UserControl我正在使用的.
this.CardHolderName.Content是label在用户控件的UI中.
public partial class PersonCredential : UserControl
{
public PersonCredential()
{
InitializeComponent();
Dispatcher.BeginInvoke( (Action) (() => {
SCLib type = new SCLib();
type.StartMonitoring();
type.CardArrived += (string ATR) => { this.CardHolderName.Content = ATR; };
};
}));
Run Code Online (Sandbox Code Playgroud)
我仍然得到错误," 调用线程无法访问此对象,因为不同的线程拥有它 "即使我正在使用它Dispatcher.BeginInvoke.
Dispatcher使用方式有什么问题吗?}
编辑:我在内容控件中实例化用户控件,代码隐藏是:
public partial class MainWindow : Window
{
PersonCredential personCredential {get;set;}
public MainWindow()
{
InitializeComponent();
var personCredential = new CoffeeShop.PersonCredential();
//create an instance of user control.
this.personCredentials.Content = personCredential;
// assign it to …Run Code Online (Sandbox Code Playgroud) 同时向我的学生解释ASP.NET MVC的概念
MVC是无国籍的.它建立在另一个无状态协议之上 - HTTP和HTTPS
但是一名学生打断了他的话,
你告诉MVC是无国籍的
无状态协议从不关心响应是否从服务器返回.但是,在ASP.NET MVC框架中,您发出请求并等待响应.由于您等待响应,因此应将其称为有状态服务.你怎么称它为无国籍服务呢?
我真的被困了,想知道该问题的答案.
有任何想法吗?
我WebAPI用来将一些数据推送到数据库.
在同一服务器中运行的Windows服务必须在数据插入时得到通知 WebAPI
我已经看到这个问题建议使用Service Broker或者SqlDependency问题似乎是在不久前提出来的.
我google了但找不到更简单的解决方案.我想做的就是when a new data is inserted to Database, a function in the the windows service should fire.
我们在WebAPI - Windows Services上下文中是否有任何替代方案或类似的解决方案?
尝试在Visual Studio 2012中定义预处理程序指令.
#define FLAG
....
#endif
Run Code Online (Sandbox Code Playgroud)
但不确定,FLAG在Visual Studio中将其包含在哪里- C#.我记得在C++项目中指定了类似的东西.
有什么想法吗 ?
在 URL 命中控制器操作之前,我有各种过滤器。
很少有过滤器具有可能引发异常的复杂逻辑。
现在,我如何捕获这些异常?
例如。我有一个过滤器来处理控制器方法中发生的异常。
[ActionFilter1]是一个过滤器,用于处理控制器方法中发生的任何异常。
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
//exception occuring here
}
Run Code Online (Sandbox Code Playgroud)
一种方法是执行以下操作:
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
try {
//exception occuring here
}
catch {
//log here
}
}
Run Code Online (Sandbox Code Playgroud)
由于存在n过滤器,我不想重复这种向每个过滤器添加try&的逻辑。catch
在这种情况下,我如何继续进行单处错误处理来处理所有这些过滤器中发生的任何异常?
我有这个sealed班,
public sealed class A
{
public string AName {get;set;}
}
Run Code Online (Sandbox Code Playgroud)
有人可以这样写一个扩展方法:
public static class Extensions
{
public static void ExtensionMethodForA (this A a)
{
Console.WriteLine("A's Extension method!");
}
}
Run Code Online (Sandbox Code Playgroud)
问题是,你如何防止这种情况?
我正在尝试打印1到1000(包括1000)的数字.
for (int i = 1; i <= 1000; i=i+1)
{
Console.WriteLine(i);
}
Run Code Online (Sandbox Code Playgroud)
但是,我确实记得我以前用过的一行代码.如下所示:
Enumerable.TheMethodGives1To1000(Console.WriteLine);
Run Code Online (Sandbox Code Playgroud)
有任何想法吗 ?
如果我们有一个带有如下方法的API控制器,这似乎是完全有效的:
[HttpPost]
public IHttpActionResult DoStuff([FromBody]ModelA modelA, [FromBody]ModelB modelB)
Run Code Online (Sandbox Code Playgroud)
注意这两个[FromBody]属性.
问题是,如何调用这样的方法?
localhost/test/DoStuff/
Run Code Online (Sandbox Code Playgroud)
发布数据:
<?xml version="1.0"?>
<ModelA>
...
</ModelA>
<ModelB>
...
</ModelB>
Run Code Online (Sandbox Code Playgroud)
似乎没有得到承认.有什么想法吗?
编辑:错误数据如下:
<?xml version="1.0"?>
<Error>
<Message>An error has occurred.</Message>
<ExceptionMessage>Can't bind multiple parameters ('model1' and 'model2') to the request's content.
</ExceptionMessage>
</Error>
Run Code Online (Sandbox Code Playgroud) c# ×10
.net ×7
asp.net-mvc ×3
.net-4.0 ×1
.net-4.5 ×1
asp.net ×1
dispatcher ×1
exception ×1
filestream ×1
routing ×1
sql-server ×1
wpf ×1