jjzjj

contract_model

全部标签

c# - 代码契约(Contract)的好处

我为什么要使用像这样的代码契约Contract.Requires(x!=null,"x");而不是旧的if(x!=null){}elsethrow...除了简洁还有其他好处吗? 最佳答案 根据MSDN:Thebenefitsofcodecontractsincludethefollowing:Improvedtesting:Codecontractsprovidestaticcontractverification,runtimechecking,anddocumentationgeneration.Automatictesting

c# - 确保 IEnumerable 不为空的契约(Contract)

给定的代码staticpublicintQ(){returnEnumerable.Range(0,100).Select(i=>i).First();}发出以下警告:warning:CodeContracts:requiresunproven:Any(source)如果我删除.Select()子句,它就会消失。但我不清楚我到底需要什么.Ensure才能满足cccheck。 最佳答案 你能用这段代码避免警告吗?varres=Enumerable.Range(0,100).Select(i=>i).Take(1);//executeon

c# - DropCreateDatabaseIfModelChanges EF6 导致 System.InvalidOperationException : The model backing the context has changed

迁移到EntityFramework6后,我在构建服务器上执行单元测试时遇到错误。我正在使用DropCreateDatabaseIfModelChanges初始化程序。当我将其更改为MigrateDatabaseToLatestVersion时一切正常,但我想坚持使用之前的初始化程序。我得到的错误是:System.InvalidOperationException:System.InvalidOperationException:Themodelbackingthe'AppContext'contexthaschangedsincethedatabasewascreated.Consi

c# - 自定义验证属性 : Comparing two properties in the same model

有没有一种方法可以在ASP.NETCore中创建自定义属性,以使用ValidationAttribute验证一个日期属性是否小于模型中的其他日期属性。假设我有这个:publicclassMyViewModel{[Required][CompareDates]publicDateTimeStartDate{get;set;}[Required]publicDateTimeEndDate{get;set;}=DateTime.Parse("3000-01-01");}我正在尝试使用这样的东西:publicclassCompareDates:ValidationAttribute{publi

c# - 用于 Salesforce 出站消息传递的契约(Contract)优先 WCF

我正在考虑为SalesforceOutboundMessaging实现监听器应用程序。walkthrough使用已弃用的ASMXWeb服务实现它。代码是使用带有/serverInterface开关的wsdl.exe生成的。这是SalesforceOutboundMessaging的wsdl。Processanumberofnotifications.NotificationServiceImplementationtldr是我需要实现NotificationBinding,以便Salesforce可以在他们的系统上发生事件时调用我的网络服务。我后来意识到svcutil本身并不支持契约优

c# - 与 Contract.Requires<T> 相比抛出异常?

我想知道我是应该抛出异常还是调用Contract.Requires例如:publicstaticvoidFunction(Stringstr){if(str==null)thrownewArgumentNullException("str","Inputstringcannotbenull.");//...}对比publicstaticvoidFunction(Stringstr){Contract.Requires(str!=null,"Inputstringcannotbenull.");//...}自Contract.Requires不需要CONTRACTS_FULL符号我也可以

c# - 我是否错误地实现了这个简单的契约(Contract)?

这是我的代码:publicclassRegularPolygon{publicintVertexCount;publicdoubleSideLength;publicRegularPolygon(intvertexCount,doublesideLength){Contract.Requires(vertexCount>=3);VertexCount=vertexCount;SideLength=sideLength;}[ContractInvariantMethod]privatevoidRegularPolygonInvariants(){Contract.Invariant(Ve

c# - 如何安全地混契约(Contract)步和异步代码?

这个问题在这里已经有了答案:Writeawelldesignedasync/non-asyncAPI(3个答案)关闭2年前。我有这个完全同步的库。它公开了同步方法,我有客户在使用它。我将底层实现更改为异步,并为任何想要使用它的人公开了异步方法。但是现在我有很多重复的代码。异步代码似乎表现更好。我希望现有客户能够利用它,并且我希望消除代码重复。是否有任何安全的方法来保持同步签名并调用异步实现?我特别害怕在调用.Result和.Wait时出现死锁。

c# - 在一个 WCF 服务中托管多个契约(Contract)

这个问题在这里已经有了答案:RunWCFServiceHostwithmultiplecontracts(8个答案)关闭9年前。是否可以在一个WCF服务中托管多个服务契约(Contract)?如果是这样,如何?我一直在谷歌上搜索,有些帖子说你可以做到(但不是怎么做),而其他人则说这是不可能的。当我运行服务器时,出现以下错误:Thecontractname'ConsoleAppWcfCommon.IBarService'couldnotbefoundinthelistofcontractsimplementedbytheservice'ConsoleAppWcfServer.FooSer

c# - 不能在同一个契约(Contract)中有两个同名的操作(异步和非)

当以下服务被激活时,我得到以下异常(不能在同一个契约(Contract)中有两个具有相同名称的操作,方法ExecuteAsync和Execute)。[ServiceContract]publicinterfaceIMyService{[OperationContract]byte[]Execute(MyRequestrequest);[OperationContract]TaskExecuteAsync(MyRequestrequest);}如果您使用svcutil.exe创建服务引用,我想这很有意义,因为基于任务的操作是自动为您创建的。但是,我不想添加服务引用,而只是使用标准的Cha