小编Sim*_*ane的帖子

MSTEST中的DataTestMethod和DataRow属性

我在Microsoft视频中看到有关这些属性的Visual Studio更新2.但是,我找不到关于它们的任何其他信息,也无法使用它们构建项目.

有没有人知道这些属性或如何让它们工作?

[DataTestMethod]
[DataRow("a", "b")]
[DataRow(" ", "a")]
public void TestMethod1(string value1, string value2)
{
    Assert.AreEqual(value1 + value2, string.Concat(value1, value2));
}
Run Code Online (Sandbox Code Playgroud)

c# unit-testing mstest

39
推荐指数
3
解决办法
3万
查看次数

从ASP.NET Core集成测试中的WebApplicationFactory <T>获取服务

我想使用ASP.NET CoreWebApplicationFactory<T>中的Integration tests中详细说明 来设置我的测试.

在我的一些测试之前,我需要使用在真正的Startup类中配置的服务进行设置.我遇到的问题是我看不到从工厂获得服务的方法.

我可以从factory.Server使用中获取服务,factory.Host.Services.GetRequiredService<ITheType>();除非在调用factory.Server之前为null factory.CreateClient();.

我有什么方法可以使用工厂获得服务吗?

谢谢.

asp.net-core-mvc asp.net-core asp.net-core-webapi asp.net-core-2.1

13
推荐指数
3
解决办法
2313
查看次数

GroupPrincipal.GetMembers在组(或子组,如果递归)包含ForeignSecurityPrincipal时失败

对于遇到同样问题的人来说,这不是一个问题.

发生以下错误:

System.DirectoryServices.AccountManagement.PrincipalOperationException: An error (87) occurred while enumerating the groups. The group's SID could not be resolved. 
at System.DirectoryServices.AccountManagement.SidList.TranslateSids(String target, IntPtr&#91;&#93; pSids) 
at System.DirectoryServices.AccountManagement.SidList.ctor(List`1 sidListByteFormat, String target, NetCred credentials) 
at System.DirectoryServices.AccountManagement.ADDNLinkedAttrSet.TranslateForeignMembers()
Run Code Online (Sandbox Code Playgroud)

运行以下代码并且组或子组包含ForeignSecurityPrincipal时:

private static void GetUsersFromGroup()
{
    var groupDistinguishedName = "CN=IIS_IUSRS,CN=Builtin,DC=Domain,DC=com";
    //NB: Exception thrown during iteration of members rather than call to GetMembers.    
    using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "Domain", "Username", "Password"))
    {
        using (GroupPrincipal groupPrincipal = GroupPrincipal.FindByIdentity(ctx, IdentityType.DistinguishedName, groupDistinguishedName))
        {                    
            using (var searchResults = groupPrincipal.GetMembers(true))//Occurs when false also. …
Run Code Online (Sandbox Code Playgroud)

c# directoryservices active-directory

12
推荐指数
1
解决办法
1万
查看次数

ASP.NET Core:防止单个操作的自动 HTTP 400 响应

我喜欢ASP.NET Core 2.1 新的自动 HTTP 400 响应功能,它在大多数情况下都运行良好。

但是,在一个操作中,我需要在验证有效负载之前进行一些预处理。我有一个自定义验证器,它需要模型中的两个值来执行验证。其中一个值在路径中,所以我想从路径中的模型上设置该值然后验证。

我不想关闭所有操作的功能:

public void ConfigureServices(IServiceCollection services)
{
    services.Configure<ApiBehaviorOptions>(options =>
    {
        options.SuppressModelStateInvalidFilter = true;
    });
}
Run Code Online (Sandbox Code Playgroud)

有什么办法可以仅针对个人操作将其关闭?

编辑:

我尝试修改 InvalidModelStateResponseFactory 但它没有解决我的问题,因为我仍然需要进入控制器操作:

services.Configure<ApiBehaviorOptions>(options =>
{
    options.InvalidModelStateResponseFactory = actionContext =>
    {
        var ignore = actionContext.ActionDescriptor.FilterDescriptors.Any(fd => fd.Filter is SuppressModelStateInvalidFilterAttribute);
        if (ignore)
        {
            // Can only return IActionResult so doesn't enter the controller action.
        }

        return new BadRequestObjectResult(actionContext.ModelState);
    };
});

[AttributeUsage(AttributeTargets.Method)]
public class SuppressModelStateInvalidFilterAttribute : FormatFilterAttribute
{
}
Run Code Online (Sandbox Code Playgroud)

编辑:

这是我在 asp.net 核心 repo 上提出的问题的链接,以防万一我得到任何地方 - …

asp.net asp.net-core

7
推荐指数
3
解决办法
1825
查看次数

在System.DirectoryServices.AccountManagement中调用UserPrinciapl.GetGroups时出现奇怪的间歇性错误处理错误

背景

我们有一个用C#编写的asp.net 4.0 Web应用程序,它调用用C#编写的.net 3.5 Web服务.Web服务传递用户标识,并根据用户所属的活动目录组返回数据列表.

Web服务使用.net 3.5版本的System.DirectoryServices.AccountManagement来获取用户所属组的Sids.

对UserPrincipal.GetGroups的调用会间歇性地失败并出现以下错误.在发生之间存在很长的时间段但是当它确实发生时它反复发生几分钟.问题出现在不同的AD用户身上.

这个例外的堆栈跟踪对我们没有任何意义.我们花了很多时间在Reflector/ILSpy中查看Microsoft AD代码,但无法超越对IADsPathName.Retrieve的调用.

例外

System.NotSupportedException: Specified method is not supported.
at System.Web.HttpResponseStream.get_Position()
at System.Drawing.UnsafeNativeMethods.ComStreamFromDataStream.Seek(Int64 offset, Int32 origin)
at System.DirectoryServices.AccountManagement.UnsafeNativeMethods.IADsPathname.Retrieve(Int32 lnFormatType)
at System.DirectoryServices.AccountManagement.ADStoreCtx.LoadDomainInfo()
at System.DirectoryServices.AccountManagement.ADStoreCtx.get_DnsForestName()
at System.DirectoryServices.AccountManagement.ADStoreCtx.GetGroupsMemberOf(Principal p)
at System.DirectoryServices.AccountManagement.Principal.GetGroupsHelper()
at System.DirectoryServices.AccountManagement.Principal.GetGroups()
at Data.SoftwarePublishingItemData.GetSids(String requestedForUserId)
at Data.SoftwarePublishingItemData.GetSoftwarePublishingItems(IDatabaseContext dbContext, GetSoftwarePublishingItemsSettings settings, XBXmlDocument parameters)
at Web.GetSoftwarePublishingItems.GetFlexiFieldData(String xml)
Run Code Online (Sandbox Code Playgroud)

代码重现

请注意,CauseNotSupportedException方法模仿我们的应用程序中没有运行的代码,但是在我们无法控制的环境中的其他地方的代码中.

class Program
{
    static void Main(string[] args)
    {
        CauseNotSupportedException();

        string samAccountName = "domain.user";

        using (var principalContext = new PrincipalContext(ContextType.Domain))
        {
            using (var userPrincipal = UserPrincipal.FindByIdentity(principalContext, IdentityType.SamAccountName, …
Run Code Online (Sandbox Code Playgroud)

.net c# error-handling active-directory

6
推荐指数
1
解决办法
2514
查看次数

获取更新语句中的最新rowversion/timestamp值 - Sql Server

我正在使用rowversion列来处理乐观并发,并希望在我完成更新时获取新的rowversion值,以便我的数据层具有最新值,并且可以执行另一个更新并获得并发异常(除非记录有被别人更新了).

我在更新后只是在数据层中进行了操作,但这不是非常有效或完全可靠.

对于下表:

CREATE TABLE PurchaseType
(
    PurchaseTypeCode nvarchar(20) NOT NULL PRIMARY KEY CLUSTERED (PurchaseTypeCode),
    Name nvarchar(50) NOT NULL,
    TS rowversion NOT NULL
)
Run Code Online (Sandbox Code Playgroud)

我试过了:

CREATE PROCEDURE PurchaseType_UpdateWithGet
@PurchaseTypeCode nvarchar(20),
@Name nvarchar(50),
@TS rowversion OUTPUT    
AS

UPDATE PurchaseType
SET Name = @Name
WHERE PurchaseTypeCode = @PurchaseTypeCode
 AND TS = @TS

SELECT @TS = TS FROM PurchaseType WHERE PurchaseTypeCode = @PurchaseTypeCode
GO
Run Code Online (Sandbox Code Playgroud)

但由于没有从其他人的更新获得rowverion值的可能性,并不完全满意.然后我在rowversion文档中遇到了OUTPUT语句(http://msdn.microsoft.com/en-us/library/ms182776.aspx/http://msdn.microsoft.com/en-us/library/ms177564.aspx并试过这个:

CREATE PROCEDURE PurchaseType_UpdateWithOutput
@PurchaseTypeCode nvarchar(20),
@Name nvarchar(50),
@TS rowversion OUTPUT    
AS

DECLARE @Output TABLE (TS BINARY(8)) …
Run Code Online (Sandbox Code Playgroud)

t-sql sql-server concurrency optimistic-concurrency

6
推荐指数
1
解决办法
4546
查看次数