我正在尝试在C#中创建线程安全属性,我想确保我在正确的路径上 - 这就是我所做的 -
private readonly object AvgBuyPriceLocker = new object();
private double _AvgBuyPrice;
private double AvgBuyPrice
{
get
{
lock (AvgBuyPriceLocker)
{
return _AvgBuyPrice;
}
}
set
{
lock (AvgBuyPriceLocker)
{
_AvgBuyPrice = value;
}
}
}
Run Code Online (Sandbox Code Playgroud)
阅读这篇文章,似乎这不是正确的做法 -
但是,这篇文章似乎暗示,
http://www.codeproject.com/KB/cs/Synchronized.aspx
有没有人有更明确的答案?
编辑:
我想为此属性执行Getter/Setter的原因是b/c我实际上希望它在设置时触发事件 - 所以代码实际上就像这样 -
public class PLTracker
{
public PLEvents Events;
private readonly object AvgBuyPriceLocker = new object();
private double _AvgBuyPrice;
private double AvgBuyPrice
{
get
{
lock (AvgBuyPriceLocker)
{
return _AvgBuyPrice;
}
}
set …
Run Code Online (Sandbox Code Playgroud) 我正在尝试学习如何使用Disruptor.NET消息框架,我找不到任何实际的例子.有很多文章都有关于它是如何工作的图片,但我找不到任何实际的内容,并向您展示如何实现这些方法.什么是一个例子?
我广泛使用了大部分线程库.我非常熟悉创建新的Threads,创建BackgroundWorkers和使用内置的.NET ThreadPool(这些都非常酷).
但是,我从未找到使用Task类的理由.我看过可能有一两个人使用它们的例子,但是这些例子不是很清楚,并且没有给出为什么人们应该使用任务而不是新线程的高级概述.
问题1:从高级别开始,什么时候使用一个有用的任务而不是其他.NET并行方法?
问题2:是否有人有一个简单和/或中等难度的例子来演示如何使用任务?
c# parallel-processing multithreading task task-parallel-library
我有一个ASP.NET MVC网站,我正在实施Application Insights.现在,我按如下方式记录跟踪事件:
private static TelemetryClient _APM;
private static TelemetryClient APM
{
get
{
if (_APM == null) { _APM = new TelemetryClient(); }
return _APM;
}
}
public static void Trace(string Message)
{
APM.TrackTrace(Message);
}
Run Code Online (Sandbox Code Playgroud)
如您所见,这将为所有跟踪维护TelemetryClient的单个静态实例.这是我们应该如何使用客户端?或者我们是否应该为每个Log创建一个新的TelemetryClient实例?
这可能是一个简单的语法问题,但我无法弄清楚.
通常,我会这样做:
public class OrderBook : IEnumerable<PriceLevel>
{
private readonly List<PriceLevel> PriceLevels = new List<PriceLevel>();
public IEnumerator<PriceLevel> GetEnumerator()
{
return PriceLevels.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return PriceLevels.GetEnumerator();
}
}
Run Code Online (Sandbox Code Playgroud)
但我没有列表,而是想使用数组 - 就像这样:
public class ArrayOrderBook : IEnumerable<PriceLevel>
{
private PriceLevel[] PriceLevels = new PriceLevel[500];
public IEnumerator<PriceLevel> GetEnumerator()
{
return PriceLevels.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return PriceLevels.GetEnumerator();
}
}
Run Code Online (Sandbox Code Playgroud)
在IEnumerator IEnumerable.GetEnumerator()
似乎精编-但市民IEnumerator<PriceLevel>
说,我需要某种形式的铸造-什么是这样做的最佳方式?
威廉
我希望能够在localhost以外的域以及任何子域上调试我的ASP.NET MVC应用程序,换句话说:
http://domain.dev http://*.domain.dev
我尝试过以下方法:
然而,没有任何工作.当我开始调试时,我得到一个页面,显示"服务不可用"或"无效的URL".
我错过了什么?
ps我正在使用Visual Studio 2013
我试图创建一个三元表达式,我收到以下错误
"无法确定条件表达式的类型,因为LiveSubscription和DisconnectedSubscription之间没有隐式转换"
同样的逻辑在if语句中起作用,但我想理解为什么它不能在三元表达式中起作用 -
以下是我要做的事情的要点:
public interface IClientSubscription
{
bool TryDisconnect();
}
public class LiveSubscription : IClientSubscription
{
public bool TryDisconnect()
{
return true;
}
}
public class DisconnectedSubscription : IClientSubscription
{
public bool TryDisconnect()
{
return true;
}
}
public class ConnectionManager
{
public readonly IClientSubscription Subscription;
public ConnectionManager(bool IsLive)
{
// This throws the exception
Subscription = (IsLive)
? new LiveSubscription()
: new DisconnectedSubscription();
// This works
if (IsLive)
{
Subscription = new LiveSubscription();
}
else
{
Subscription …
Run Code Online (Sandbox Code Playgroud) 所以我使用以下实用程序从类的实例中获取字段/属性的名称...
public static string FieldName<T>(Expression<Func<T>> Source)
{
return ((MemberExpression)Source.Body).Member.Name;
}
Run Code Online (Sandbox Code Playgroud)
这允许我执行以下操作:
public class CoolCat
{
public string KaratePower;
}
public class Program
{
public static Main()
{
public CoolCat Jimmy = new CoolCat();
string JimmysKaratePowerField = FieldName(() => Jimmy.KaratePower);
}
}
Run Code Online (Sandbox Code Playgroud)
当我需要字段名称的字符串表示时,这非常适合序列化和其他时间.
但是现在,我希望能够获得字段名称而没有类的实例 - 例如,如果我正在设置一个表并希望将列的FieldNames动态链接到类中的实际字段(因此重构)等等不会破坏它).
基本上,我觉得我还没有完全掌握如何实现这一点的语法,但我想它会看起来像这样:
public static string ClassFieldName<T>(Func<T> PropertyFunction)
{
// Do something to get the field name? I'm not sure whether 'Func' is the right thing here - but I would imagine that it is something where …
Run Code Online (Sandbox Code Playgroud) 我正在为多租户ASP.NET MVC应用程序使用OWIN身份验证.
应用程序和身份验证位于单个应用程序中的一台服务器上,但可以通过许多域和子域进行访问.例如:
www.domain.com
site1.domain.com
site2.domain.com
site3.domain.com
www.differentdomain.com
site4.differentdomain.com
site5.differentdomain.com
site6.differentdomain.com
Run Code Online (Sandbox Code Playgroud)
我想允许用户登录这些域中的任何域并使其身份验证cookie工作,无论用于访问应用程序的域是什么.
这就是我的身份验证设置:
public void ConfigureAuthentication(IAppBuilder Application)
{
Application.CreatePerOwinContext<RepositoryManager>((x, y) => new RepositoryManager(new SiteDatabase(), x, y));
Application.UseCookieAuthentication(new CookieAuthenticationOptions
{
CookieName = "sso.domain.com",
CookieDomain = ".domain.com",
LoginPath = new PathString("/login"),
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
Provider = new CookieAuthenticationProvider
{
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<UserManager, User, int>(
validateInterval: TimeSpan.FromMinutes(30),
regenerateIdentityCallback: (manager, user) => user.GenerateClaimsAsync(manager),
getUserIdCallback: (claim) => int.Parse(claim.GetUserId()))
}
});
Application.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
}
Run Code Online (Sandbox Code Playgroud)
我还在我的应用程序的根web.config中为我的应用程序显式设置了一个机器密钥:
<configuration>
<system.web>
<machineKey decryption="AES" decryptionKey="<Redacted>" validation="<Redacted>" validationKey="<Redacted>" />
</system.web>
</configuration>
Run Code Online (Sandbox Code Playgroud)
当我在domain.com和site1.domain.com之间导航时,此设置按预期工作,但现在它不允许我登录differentdomain.com. …
在Serializable类中使用SecurityPermission的重要性是什么?
在[微软网站] [1]上的一篇文章中,他们建议你编写一个Serializable类,如下所示:
[Serializable]
public class PleaseSaveMe : ISerializable
{
public readonly int Age;
public readonly string Name;
public int KarateSkills;
public PleaseSaveMe(int Age, string Name)
{
this.Age = Age;
this.Name = Name;
}
// Serialization Methods
protected PleaseSaveMe(SerializationInfo info, StreamingContext context)
{
Age = info.GetInt32("Age");
Name = info.GetString("Name");
KarateSkills = info.GetInt32("KarateSkills");
}
[SecurityPermission(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.SerializationFormatter)]
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("Age", Age);
info.AddValue("Name", Name);
info.AddValue("KarateSkills", KarateSkills);
}
}
Run Code Online (Sandbox Code Playgroud)
但是在SecurityAction.LinkDemand的文档中,它特别指出不在.NET 4.0中使用它(我正在使用它).我应该用什么呢?该属性甚至是必要的吗?
威廉
c# ×7
asp.net-mvc ×2
c#-4.0 ×2
.net ×1
asp.net ×1
attributes ×1
func ×1
ienumerable ×1
if-statement ×1
lambda ×1
logging ×1
multi-tenant ×1
owin ×1
properties ×1
reflection ×1
task ×1