我有一个async方法:
public async Task<string> GenerateCodeAsync()
{
string code = await GenerateCodeService.GenerateCodeAsync();
return code;
}
Run Code Online (Sandbox Code Playgroud)
我需要从同步方法中调用此方法.
如何在不必复制GenerateCodeAsync方法的情况下执行此操作以使其同步工作?
更新
然而找不到合理的解决方案
但是,我看到HttpClient已经实现了这种模式
using (HttpClient client = new HttpClient())
{
// async
HttpResponseMessage responseAsync = await client.GetAsync(url);
// sync
HttpResponseMessage responseSync = client.GetAsync(url).Result;
}
Run Code Online (Sandbox Code Playgroud) 我有一个异步方法:
public async Task<bool> ValidateRequestAsync(string userName, string password)
{
using (HttpClient client = new HttpClient())
{
HttpResponseMessage response = await client.GetAsync(url);
string stringResponse = await response.Content.ReadAsStringAsync();
return bool.Parse(stringResponse);
}
}
Run Code Online (Sandbox Code Playgroud)
我这样称呼这个方法:
bool isValid = await ValidateRequestAsync("user1", "pass1");
Run Code Online (Sandbox Code Playgroud)
我可以从同步方法调用相同的方法,而不使用await关键字?
例如:
public bool ValidateRequest(string userName, string password)
{
return ValidateRequestAsync(userName, password).Result;
}
Run Code Online (Sandbox Code Playgroud)
我认为这会导致僵局.
编辑
调用上面的方法会使调用永远不会结束.(该方法不再到达终点)
我有两个对象类
public class User
{
public Guid Id { get; set; }
public string Name { get; set; }
// Navigation
public ICollection<Product> Products { get; set; }
}
public class Product
{
public Guid Id { get; set; }
// Navigation
public User User { get; set; }
public Guid User_Id { get; set; }
public string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
当我使用dataContext加载用户时,我得到的产品列表为null(这没关系).
如果我在产品列表中添加"虚拟"关键字,
public virtual ICollection<Product> Products { get; set; }
Run Code Online (Sandbox Code Playgroud)
当我加载用户时,我也获得了产品列表.
为什么会这样?我认为"虚拟"关键字用于不加载实体,除非你明确这个(使用"包含"语句)
我想我错了
c# entity-framework lazy-loading entity-framework-4 ef-code-first
我有一个类Product和一个复杂的类型AddressDetails
public class Product
{
public Guid Id { get; set; }
public AddressDetails AddressDetails { get; set; }
}
public class AddressDetails
{
public string City { get; set; }
public string Country { get; set; }
// other properties
}
Run Code Online (Sandbox Code Playgroud)
是否可以防止从类AddressDetails内部映射"Country"属性Product?(因为我在Product课堂上永远不需要它)
像这样的东西
Property(p => p.AddressDetails.Country).Ignore();
Run Code Online (Sandbox Code Playgroud) 我正在学习DDD开发几天,我开始喜欢它了.
我(我想)了解DDD的原理,其中您主要关注业务对象,其中您有聚合,聚合根,仅用于聚合根的存储库等等.
我正在尝试创建一个简单的项目,我将DDD开发与Code First方法结合起来.
我的问题是:(我使用的是asp.net MVC)
DDD Business Objects将与Code First对象不同?即使它们可能是相同的,例如我可以拥有一个Product具有所有规则和方法的业务对象,并且我可以拥有一个Product代码优先(POCO)对象,它将只包含我需要保存在数据库中的属性.
如果问题1的答案是"真",那么我如何通知ProductPOCO对象业务对象的属性Product已被更改,我必须更新它?我正在使用"AutoMapper"或类似的东西?如果答案是"不",我就完全迷失了.
你能告诉我一个最简单的(CRUD)例子,我怎么能把这两个放在一起?
谢谢
c# asp.net-mvc domain-driven-design code-first ef-code-first
我有一个正则表达式模式,它验证三位数字
/^\d{3}$/.test("123") // true
/^\d{3}$/.test("123.") // false
Run Code Online (Sandbox Code Playgroud)
我想使用此正则表达式作为文本框的输入限制.
基本上,如果新值匹配,我允许输入字符,否则我会阻止它.
问题是没有值会匹配,因为"1"不是完全匹配,并且不允许我输入它.
是否有任何方法可以在javascript中测试regEx的部分匹配?
/^\d{3}$/.test("123") // true
/^\d{3}$/.test("12") // "partial match"
/^\d{3}$/.test("a12") // false
Run Code Online (Sandbox Code Playgroud)
编辑
\ d {3}只是一个例子.我需要使用电子邮件正则表达式或手机正则表达式作为输入限制.
"email" // true
"email@" // true
"email@@" // false
"@yahoo.com" // false
Run Code Online (Sandbox Code Playgroud)
编辑2
我有一个textBox插件,其输入限制基于正则表达式.
正则表达式可以是任何东西,十六进制颜色正则表达式,例如:(#){1}([a-fA-F0-9]){6}
我需要阻止用户插入与正则表达式不匹配的字符.
例如,如果文本框为空,则第一个允许的字符为"#".
但是如果我对正则表达式测试"#"字符,它将返回"false",因为"#"本身无效.
/^(#){1}([a-fA-F0-9]){6}$/.test("#") // false
Run Code Online (Sandbox Code Playgroud)
但与此同时,"#"部分有效,因为它尊重正则表达式格式(我应该允许用户输入)
我需要知道的是,如果我可以验证字符串是否是正则表达式的部分匹配,那么我可以允许用户键入字符.
/^(#){1}([a-fA-F0-9]){6}$/.test("#") // is a partial match, allow type
/^(#){1}([a-fA-F0-9]){6}$/.test("#0") // is a partial match, allow type
/^(#){1}([a-fA-F0-9]){6}$/.test("#00") // is a partial match, allow type
/^(#){1}([a-fA-F0-9]){6}$/.test("#000") // is a partial match, allow type …Run Code Online (Sandbox Code Playgroud) 我有一个接口类:
public interface IStartUpTask
{
bool IsEnabled { get; }
void Configure();
}
Run Code Online (Sandbox Code Playgroud)
我有多个实现相同接口的类
其中一个类看起来像这样:
public class Log4NetStartUpTask : IStartUpTask
{
public bool IsEnabled { get { return true; } }
public void Configure()
{
string log4netConfigFilePath = ConfigurationManager.AppSettings["log4netConfigFilePath"];
if (log4netConfigFilePath == null)
throw new Exception("log4netConfigFilePath configuration is missing");
if (File.Exists(log4netConfigFilePath) == false)
throw new Exception("Log4Net configuration file was not found");
log4net.Config.XmlConfigurator.Configure(
new System.IO.FileInfo(log4netConfigFilePath));
}
}
Run Code Online (Sandbox Code Playgroud)
我如何告诉Ninject我希望所有实现它的类IStartUpTask自动绑定到自己?
我找到了一个使用StructureMap的例子来做到这一点,但我不知道如何在Ninject中做到这一点.
Scan(x => {
x.AssemblyContainingType<IStartUpTask>();
x.AddAllTypesOf<IStartUpTask>();
x.WithDefaultConventions();
});
Run Code Online (Sandbox Code Playgroud) 我有一个产品的数据传输对象类
public class ProductDTO
{
public Guid Id { get; set; }
public string Name { get; set; }
// Other properties
}
Run Code Online (Sandbox Code Playgroud)
当Asp.net在JSON(using JSON.NET)或in中序列化对象时XML,它会生成ProductDTO对象.
不过,我想序列化期间改名字,从ProductDTO到Product,使用某种类型的属性:
[Name("Product")]
public class ProductDTO
{
[Name("ProductId")]
public Guid Id { get; set; }
public string Name { get; set; }
// Other properties
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
我知道有一个解决方案可以阻止mvc框架处理"favicon.ico"请求(解决方案),但我不知道为什么它首先要寻找这个图标.
我使用查找文件>整个解决方案搜索favicon.ico,没有找到任何内容.
我搜索了网站的html源代码,favicon.ico没有找到任何内容.
它在哪里?为什么浏览器试图服务它?
我有一个缓存的ActionResult.
[OutputCache(Duration = 3600, VaryByParam = "product_Id")]
public ActionResult ProductPreview(Guid product_Id)
{
// just for testing the cache
System.Threading.Thread.Sleep(4000);
return PartialView("ProductPreview", _repository.CreateProductModel(product_Id));
}
Run Code Online (Sandbox Code Playgroud)
好的部分是缓存正在运行.在第一次加载后,结果显示没有任何4秒延迟.
但是,我需要在对该产品进行一些更改时清除缓存.
我试图清除缓存这样做:
public ActionResult RemoveCache()
{
var url = Url.Action("ProductPreview", "Common");
// also tried with parameter
// var url = Url.Action("ProductPreview", "Common", new { @product_Id = "productId" });
HttpResponse.RemoveOutputCacheItem(url);
return RedirectToAction("Index");
}
Run Code Online (Sandbox Code Playgroud)
我还尝试使用ajax和整页刷新调用RemoveCache方法,并且它们都没有工作.
我能做什么?问题出在哪儿?

c# ×7
asp.net ×4
asp.net-mvc ×4
async-await ×2
asynchronous ×2
c#-5.0 ×1
caching ×1
code-first ×1
javascript ×1
lazy-loading ×1
ninject ×1
regex ×1