Microsoft 代码合同可以与 ASP.NET 网站一起使用吗?
我目前正在 ASP.NET MVC 应用程序中使用 Microsoft 代码合约,但没有任何问题,但我似乎无法让它在基本的 ASP.NET 网站上正常运行。 我不完全确定它是为此类项目而设计的(尽管这并不重要),所以我想将其介绍给每个人。
我可以很好地编译合同,但代码会跳过它们,因为我假设它尚未像在其他项目类型(即 ASP.NET MVC)中那样通过属性页启用。 我已转到 ASP.NET 网站中项目的属性页(显示一个对话框而不是典型的属性页),但它没有产生相同的菜单选项,因此没有专门的部分代码合同。
另外,我在类库项目中正确启用了 Microsoft 代码契约,我用它来将业务逻辑与网站分开。 合约编译得很好,但是当违反合约时,它会抛出一个相当无信息的 “Exception of type 'System.ExecutionEngineException' was returned”
错误,没有内部异常。 我的合同指定了一条在违规时显示的消息,但它并不属于例外情况。 它只是停止进程的执行(我相信这是 Microsoft 代码合约的默认功能)。
我找不到任何地方明确说明特定项目类型可以或不能(或不应该)与合同一起使用,所以我只是想看看是否有人遇到过这个问题。
谢谢你的帮助!
I'm currently using Microsoft Code Contracts in an ASP.NET MVC application without any issues but I can not seem to get it quite running in a basic ASP.NET Web site. I'm not entirely sure it was made to work with this type of project (although it shouldn't matter) so I wanted to bring it up to everyone.
I can compile the contracts just fine but the code skips over them since I'm assuming it hasn't been enabled through the Properties Page like you would do in other project types (ie ASP.NET MVC). I've gone to the property page of the project (which displays a dialog instead of the typical properties page) in my ASP.NET web site but it does not yield the same menu options and as such, doesn't have a section devoted to Code Contracts.
Also, I have Microsoft Code Contracts properly enabled within a class library project that I use to separate my business logic from the web site. The contracts compile fine but when a contract is violated, it throws a rather uninformative "Exception of type 'System.ExecutionEngineException' was thrown"
error with no inner exception. My contract specifies a message to display upon violation but it is nowhere within the exception. It simply halts the execution of the process (which I believe is the default functionality for Microsoft Code Contracts).
I can't find anywhere that explicitly states that a particular project type can or can't (or shouldn't) be used with Contracts so I just wanted to see if anyone has had this issue.
Thanks for any help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我遇到了同样的问题,这就是我解决它的方法:
在引用的类库中,右键单击 -> 属性-> 代码合同。
确保选中“执行合同检查”。 我将我的设置为“完整”
合同参考程序集:确保将其设置为“构建”
保存更改。
在代码中没有契约的引用类库中,将契约引用程序集设置为“不构建”。
然后在 MVC 项目中,检查代码契约“执行契约检查”。 我将我的设置为“完整”。
希望对某人有帮助。
I had the same problem and this is how I solved it:
In the Referenced Class Libraries, right click -> properties -> code contracts.
Make sure "perform contract checking" is checked. I had mine set to "Full"
Contract Reference Assembly: make sure it is set to "Build"
Save your changes.
In the Referenced Class Libraries that have no contracts in their code, set the Contract Reference Assembly to "Do Not Build".
Then in the MVC project, have the Code Contracts "perform contract checking" checked. I had mine set to "Full".
Hope that helps somebody.
这听起来不太像合同,而更像是构建/配置问题。 您是否尝试过部署预建网站? 您确定您的网站代码可以看到合同代码吗? ASP.NET 运行时是否使用 CLR 4.0,或者它是否看到早期的 Microsoft.Contracts.dll? ETC。
This sounds less like a Contracts and more like a build/config issue. Have you tried to deploy a prebuilt website? Are you sure that your website code sees the contracts code? Is the ASP.NET runtime using the CLR 4.0, or does it see the earlier Microsoft.Contracts.dll? Etc.