我不敢相信让某人向我展示一个简单的工作示例是如此困难.它让我相信每个人只能说他们知道如何去做,但实际上他们不会.
我将帖子简化为我想要的例子.也许这篇文章很长,让人们害怕.
为了得到这个赏金,我正在寻找一个可以在VS 2010中复制并运行的工作示例.
这个例子需要做什么.
场景1
用户A进入该站点并编辑Row1.用户B来(注意他可以看到Row1)并点击编辑Row1,在用户A完成之前,应拒绝UserB编辑该行.
情景2
用户A进入该站点并编辑Row1.用户B出现30分钟后点击编辑Row1.用户B应该能够编辑此行并保存.这是因为用户A花了太长时间来编辑行并失去了编辑权.
场景3
用户A回来了.他点击了更新行按钮,他应该受到StaleObjectException的欢迎.
我正在使用asp.net mvc和流利的nhibernate.寻找在这些中完成的例子.
我尝试了什么
我尝试构建自己的但我不能让它抛出StaleObjectException,也不能让版本号增加.我累了打开2个单独的浏览器并加载了索引页面.两个浏览器都显示相同的版本号.
public class Default1Controller : Controller
{
//
// GET: /Default1/
public ActionResult Index()
{
var sessionFactory = CreateSessionFactory();
using (var session = sessionFactory.OpenSession())
{
using (var transaction = session.BeginTransaction())
{
var firstRecord = session.Query<TableA>().FirstOrDefault();
transaction.Commit();
return View(firstRecord);
}
}
}
public ActionResult Save()
{
var sessionFactory = CreateSessionFactory();
using (var session = sessionFactory.OpenSession())
{
using (var transaction = session.BeginTransaction()) …
Run Code Online (Sandbox Code Playgroud) 我为EventLogEntry实现了一个自定义的IEqualityComparer.
public class EventLogEntryListComparison :
IEqualityComparer<List<EventLogEntry>>,
IEqualityComparer<EventLogEntry>
Run Code Online (Sandbox Code Playgroud)
对于IEqualityComparer<List<EventLogEntry>>
,GetHashCode函数非常简单.
public int GetHashCode(List<EventLogEntry> obj)
{
return obj.Sum(entry => 23 * GetHashCode(entry));
}
Run Code Online (Sandbox Code Playgroud)
但是,这会为某些条目抛出OverflowException.
"Arithmetic operation resulted in an overflow."
at System.Linq.Enumerable.Sum(IEnumerable`1 source)
at System.Linq.Enumerable.Sum[TSource](IEnumerable`1 source, Func`2 selector)
at <snip>.Diagnostics.EventLogAnalysis.EventLogEntryListComparison.GetHashCode(List`1 obj) in C:\dev\<snip>Diagnostics.EventLogAnalysis\EventLogEntryListComparison.cs:line 112
at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
at System.Collections.Generic.Dictionary`2.set_Item(TKey key, TValue value)
at <snip>.Diagnostics.EventLogAnalysis.Program.AnalyseMachine(String validMachineName) in C:\dev\<snip>.Diagnostics.EventLogAnalysis\Program.cs:line 104
at System.Threading.Tasks.Parallel.<>c__DisplayClass2d`2.<ForEachWorker>b__23(Int32 i)
at System.Threading.Tasks.Parallel.<>c__DisplayClassf`1.<ForWorker>b__c()
Run Code Online (Sandbox Code Playgroud)
在调试时尝试获得相同的错误并且无法在即时窗口中,我将代码更改为此并再见了OverflowException?
int total = 0;
foreach (var eventLogEntry in obj)
{
total += GetHashCode(eventLogEntry); …
Run Code Online (Sandbox Code Playgroud) 在我当前的项目中,我越来越多地采用 DDD/洋葱架构。我仍然不清楚的许多事情之一是应该有多少封装。用一个具体的例子更容易解释。
例子
namespace MyProject.Model
{
public class ComplexEntity
{
private int _id;
public int Id { get {return _id;} }
public ValueObjectA ValueA {get; set;}
public bool IsBool {get; set;}
public ComplexEntity(ValueObjectA a, bool isBool)
{
// Do some validation first
ValueA = a;
ValueB = b;
IsBool = isBool;
}
}
public class ValueObjectA
{
public bool IsBoolA {get; private set;}
public bool IsBoolB {get; private set;}
public ValueObjectA(bool a, bool b)
{
IsBoolA = a;
IsBoolB = …
Run Code Online (Sandbox Code Playgroud) 我在Windows服务中收到此错误.这是我以前在我的问题讨论同样的服务在这里
代码修改为使用Parallel.ForEach
(我自己的版本,因为这是一个3.5 Windows服务).并行使用的原因在于,卸载每个域只需要太长时间并且并行运行它们应该被证明更快(尽管只有一个线程正在执行每次卸载?!).
基于其他职位,我只能猜测,这是不知何故下来我用一个事实ThreadPool
Thread
来Unload
了AppDomain
秒.我只是看不出如何避免它?
public partial class SomeService : ServiceBase
{
private Manager _appDomainManager;
protected override void OnStop()
{
_appDomainManager.Dispose();
}
}
public class Manager : IDisposable
{
public void Dispose()
{
Log.Debug("Disposing");
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (_disposed) return;
if (disposing)
{
// dispose managed resources
Parallel.For(0, appdomains.Length, UnloadAppDomian);
}
_disposed = true;
}
}
private UnloadAppDomain(int appDomainIndex);
public static class Parallel35
{
public …
Run Code Online (Sandbox Code Playgroud) 这个问题围绕如何构建WCF服务以使其随着时间的推移变得容易.在没有描述问题的情况下很难得到对此的响应深度.
背景
我正在开发一个大型的WCF服务和客户端系统.服务器端"易于"更新,因为只有10个服务器运行此代码.
客户很难更新,尽管自动化程度很高,但在300,000多个WCF客户端,更新总是需要时间,并且只能在两到三周内实现高更新成功率.
数据合同
[DataContract]
public class MyContract
{
[DataMember]
public int Identity {get; set;}
[DataMember]
public string Name {get; set;}
// More members
}
Run Code Online (Sandbox Code Playgroud)
在DataContract
难以初始化并有一个标准的MyContractFactory
类初始化获得相应的实例为您的机器.
public class static MyContractFactory
{
public static MyContract GetMyContract()
{
// Complex implementation
}
}
Run Code Online (Sandbox Code Playgroud)
ServiceContracts
这DataContract
在一系列Web服务中非常常见.
namespace MyPrefix.WebServicve1
{
[ServiceContract]
public class IMyInterface1
{
[OperationContract]
public void DoSomethingWithMyContract(MyContract data);
}
[ServiceContract]
public class IMyInterface2
{
[OperationContract]
public void DoSomethingDifferentWithMyContract(MyContract data);
}
}
Run Code Online (Sandbox Code Playgroud)
客户
我的客户端是插件,基于在单独的进程或应用程序域中运行的插件,具体取决于我们在该插件中的信任级别.
实施1
我对此(默认WCF)的初始实现最终得到了 …
我有一个在 IIS 7.5 上运行的暴露于互联网的 WCF 服务,我需要保护它的安全。我想删除 HTTP 响应中的“Server”标头。
我已经实现了 IDispatchMessageInspector,代码如下。
public void BeforeSendReply(ref Message reply, object correlationState)
{
var context = WebOperationContext.Current;
if (context != null)
{
context.OutgoingResponse.Headers.Remove(
HttpResponseHeader.Server);
}
}
Run Code Online (Sandbox Code Playgroud)
但是,服务器标头仍在响应中。在调试时,我可以看到OutgoingResponse.Headers
不包含HttpResonseHead.Server
,如果我编写自己的值,它显然会被 IIS 管道中更下游的内容覆盖。
编辑1
尝试了以下方法,也不好
public class SecureServerHeaderModule : IHttpModule
{
#region Implementation of IHttpModule
public void Init(HttpApplication context)
{
context.PreSendRequestHeaders += OnPreSendRequestHeaders;
}
public void Dispose() { }
#endregion
private static void OnPreSendRequestHeaders(object sender, EventArgs e)
{
var context = HttpContext.Current;
if (context != …
Run Code Online (Sandbox Code Playgroud) 我想要一个可以从可为空的数据库列解析枚举的方法。我在下面编写了这个方法(并且必须将 T 限制为结构才能使其编译)。
它确实可以编译,但我相信它是错误的,因为枚举不是结构?如果是这样,我如何限制泛型方法说我期待一个 ValueType 你不必向我抱怨“只有不可为空的值类型可以是 'System.Nullable' 的基础
private static T? ParseEnum<T>(DataRow row, string columnName)
where T : struct
{
T? value = null;
try
{
if (row[columnName] != DBNull.Value)
{
value = (T)Enum.Parse(
typeof(T),
row[columnName].ToString(),
true);
}
}
catch (ArgumentException) { }
return value;
}
Run Code Online (Sandbox Code Playgroud) 我有一个复杂的实体(Message
),它有一个非常重要的构造函数(13个参数),其中一些参数依次为实体(Verb
).
有许多事情我需要另一个实体的存储库(例如,对于某些默认值).
使用IoC并保持代码清洁/最佳实践的好方法是什么?
简化代码(我将默认动词列表作为由静态构造函数个人构建的静态对象,如果我的Message实体需要某种IRepositry,那么它是最简单的naff示例).
private static IVerbRepository verbRepository;
static Message()
{
using (IKernel kernel = new StandardKernel())
{
verbRepository = kernel.Get<IVerbRepository>();
}
}
public Message(int id, string precis,
DateTime created, DateTime validTo,
PriorityType priority, Source source, SID createdBy,
string content = null, string helpText = null,
DateTime? validFrom = null, bool emailOnExpiry = false,
IEnumerable<SID> targetUsers = null,
IOrderedEnumerable<MessageVerb> verbs = null) : base(id)
{
Verbs = verbs ??
new List<MessageVerb>
{
new MessageVerb(
verbRepository.GetByName("Acknowledge"), true, …
Run Code Online (Sandbox Code Playgroud) 目前,我一次将一个实例从一个数据集复制到另一个。有没有一种方法可以使字符串映射保持不变?mergeInstances是水平工作的,是否有等效的垂直合并?
这是循环的一个步骤,我从多个arff文件中读取具有相同结构的数据集,将其读取到一个大数据集中。必须有一个更简单的方法。
Instances iNew = new ConverterUtils.DataSource(name).getDataSet();
for (int i = 0; i < iNew.numInstances(); i++) {
Instance nInst = iNew.instance(i);
inst.add(nInst);
}
Run Code Online (Sandbox Code Playgroud) 我一直听说C#使用懒惰评估.因此,对于某些代码,if (true || DoExpensiveOperation()
将返回true
而不执行DoExpensiveOperation()
.
在面试测试中,我看到了以下问题,
static bool WriteIfTrue(bool argument)
{
if (argument)
{
Console.WriteLine("argument is true!");
}
return argument;
}
static void Main()
{
// 1 0 0 1
WriteIfTrue((WriteIfTrue(false) & WriteIfTrue(true)) || WriteIfTrue(true));
// 1 1 0 1
WriteIfTrue((WriteIfTrue(true) || WriteIfTrue(false)) & WriteIfTrue(true));
// 0 0 0 0
WriteIfTrue((WriteIfTrue(false) & WriteIfTrue(true)) & WriteIfTrue(false));
// 1 1 0 1
WriteIfTrue((WriteIfTrue(true) || WriteIfTrue(false)) & WriteIfTrue(true));
}
Run Code Online (Sandbox Code Playgroud)
打印多少次"论证是真的!" 到屏幕?
我会说7
是正确的答案.现在,如果我坚持使用编译器并运行它,它会打印出来的10
次数!懒惰的评价哪里出错了?