小编Ser*_*diy的帖子

是否可以在条件下使用Task <bool>?

在Windows Phone 8中我有方法public async Task<bool> authentication().函数的返回类型是bool但当我尝试在if条件错误中使用其返回值时表示无法转换Task<bool>bool.

public async Task<bool> authentication()
{
    var pairs = new List<KeyValuePair<string, string>>
    {
        new KeyValuePair<string, string> ("user", _username),
        new KeyValuePair<string, string> ("password", _password)
    };

    var serverData = serverConnection.connect("login.php", pairs);

    RootObject json = JsonConvert.DeserializeObject<RootObject>(await serverData);

    if (json.logined != "false")
    {
        _firsname = json.data.firsname;
        _lastname = json.data.lastname;
        _id = json.data.id;
        _phone = json.data.phone;
        _ProfilePic = json.data.profilePic;
        _thumbnail = json.data.thumbnail;
        _email = json.data.email;
        return true;
    }
    else
        return …
Run Code Online (Sandbox Code Playgroud)

c# task-parallel-library async-await c#-5.0 windows-phone-8

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

创建Web API项目时Visual Studio 2013错误:元素<Project>下的元素<#text>无法识别

我的Visual Studio安装有问题.

为了重现它,我创建了一个新的空白解决方案然后:

  • 添加新项目..
  • ASP.NET Web应用程序
  • Web API
  • 创建项目

Visual Studio无法创建它我收到此消息:

元素<Project>下的元素<#text>无法识别.

任何想法如何解决?

c# visual-studio-2013 asp.net-web-api2

13
推荐指数
2
解决办法
8552
查看次数

在C#中使用'new'修饰符

我读到newmodifer隐藏了基类方法.

using System;

class A
{
    public void Y()
    {
        Console.WriteLine("A.Y");
    }
}

class B : A
{
    public new void Y()
    {
        // This method HIDES A.Y.
        // It is only called through the B type reference.
        Console.WriteLine("B.Y");
    }
}

class Program
{
    static void Main()
    {
        A ref1 = new A(); // Different new
        A ref2 = new B(); // Polymorpishm
        B ref3 = new B();

        ref1.Y();
        ref2.Y(); //Produces A.Y line #xx
        ref3.Y();
    }
}
Run Code Online (Sandbox Code Playgroud)

为什么 …

.net c# oop polymorphism new-operator

12
推荐指数
2
解决办法
2480
查看次数

实体框架使用LEFT JOIN语句为视图返回错误数据

我遇到了Entity Framework的奇怪行为.EF生成的DbContext对象返回与数据库中的实际数据不同的数据.

请考虑以下数据库架构: 数据库架构

Letter 数据:

Id      Value   LanguageId
------- ------- ----------
1       A       1
2       A       2
3       B       1
4       B       2
Run Code Online (Sandbox Code Playgroud)

Language 数据:

Id      Value
------- -------
1       English
2       Russian
Run Code Online (Sandbox Code Playgroud)

我也有以下简单的观点LetterLanguageView.请注意,它使用LEFT JOIN子句,因为Letter.LanguageId可能是NULL:

SELECT dbo.Letter.Value as Letter, dbo.Language.Value as Language
FROM dbo.Letter
LEFT JOIN dbo.Language ON dbo.Letter.LanguageId = dbo.Language.Id
Run Code Online (Sandbox Code Playgroud)

这个视图的结果非常简单:

Letter  Language
------- --------
A       English
A       Russian
B       English
B       Russian
Run Code Online (Sandbox Code Playgroud)

但是,当我从Entity Framework使用此视图时,我得到以下结果:

错误的数据

如您所见,该Language物业是错误的,根本没有俄语.

如果您想知道,这是用于读取此数据的代码段: …

c# sql entity-framework

10
推荐指数
1
解决办法
3131
查看次数

注入SignInManager依赖项:不能与Unity一起使用,在使用OWIN时有效

我正在向ASP.NET MVC 5 Web应用程序添加ASP.NET身份验证功能.

我在整个项目中使用Unity进行依赖注入,所以我决定注入AccountController构造函数中所需的依赖项:

public AccountController(UserManager<ApplicationUser> userManager, SignInManager<ApplicationUser, string> signInManager)
{
    _userManager = userManager;
    _signInManager = signInManager;
}
Run Code Online (Sandbox Code Playgroud)

我的Login方法实现如下(实际上,我从ASP.NET Web Application具有Individual User Accounts身份验证的项目模板中复制了该代码):

//
// POST: /Account/Login
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
    var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: true);
    // Process result and return appropriate view...
    // However, there are no authentication cookies in the response!
}
Run Code Online (Sandbox Code Playgroud)

问题是,认证工作不正常-即使我进入了正确的凭据,resultSignInStatus.Success有在响应不发送身份验证cookie.

但是,如果我使用 …

.net asp.net-mvc dependency-injection owin asp.net-identity

10
推荐指数
1
解决办法
4101
查看次数

ASP.NET MVC和Angularjs一起+ ASP.NET Web API

我想知道使用这两个世界的优点和缺点:

  • ASP.NET MVC和Angularjs在一起(没有剃刀,Angularjs会完成这项工作).
  • AngularJS(前端)+ ASP.NET Web API(后端).

我们专注于SPA/Mini-SPA,适用于具有大量服务器端业务规则和计算的中型/大型企业项目.我也专注于安全性.

优点和缺点意见将帮助我在脑海中找到这个问题的依据:

将ASP.NET Web API(后端),ASP.NET MVC和Angularjs一起使用是一个明智的决定吗?

c# asp.net-mvc asp.net-web-api angularjs single-page-application

10
推荐指数
1
解决办法
4469
查看次数

UserManager保持抛出System.ArgumentNullException

我想实现的标准IdAspNetUsers,从nvarcharint.我已经设法让那一方工作了.但是我的问题是当我尝试登录时,我不断从UserManager课堂上收到错误.

我的代码如下:

public class UserManager : UserManager<ApplicationUser, int>
{
    public UserManager(IUserStore<ApplicationUser, int> store) 
    : base(store) 
{ 
}
Run Code Online (Sandbox Code Playgroud)

在我登录的页面上

if (IsValid)
        {
            // Validate the user password
            var manager = Context.GetOwinContext().GetUserManager<UserManager>();
            var user = manager.Find(UserName.Text, Password.Text); //This line throws the error
            if (user != null)
            {
                IdentityHelper.SignIn(manager, user, isPersistent: false);
                Response.Redirect("~/Home.aspx");                }
            else
            {
                FailureText.Text = "Invalid username or password.";
                ErrorMessage.Visible = true;
            }
        }
Run Code Online (Sandbox Code Playgroud)

我一直得到的错误是System.ArgumentNullException:Value不能为null.参数名称:manager.还有其他人遇到过这个问题吗?在此先感谢您的帮助

堆栈跟踪

[ArgumentNullException: Value …
Run Code Online (Sandbox Code Playgroud)

c# asp.net entity-framework owin asp.net-identity-2

9
推荐指数
1
解决办法
6402
查看次数

在同步块内部和外部使用字段是否安全?

背景

我们的应用程序发送在数据库表中排队的电子邮件.我们发送了一些重复电子邮件的实例,所以我实现了一个锁,以防止多个线程同时发送电子邮件.

ReSharper警告我:

该字段有时在同步块内使用,有时在没有同步的情况下使用

为什么ReSharper告诉我这个,为什么我会担心它?

这是我的(删节)代码:

private readonly IMailQueueRepository _mailQueueRepository = new MailQueueRepository();
private static object _messageQueueLock = new object();

public void SendAllQueuedMessages(IPrincipal caller)
{
    lock (_messageQueueLock) // Prevent concurrent callers
    {
        var message = _mailQueueRepository.GetUnsentMessage();
        while (message != null)
        {
            SendQueuedMessage(message);
            message = _mailQueueRepository.GetUnsentMessage();
        }
    }
}

public void SendQueuedMessage(IMessage message)
{
    // I get the ReSharper warning here on _mailQueueRepository
    var messageAttachments = _mailQueueRepository.GetMessageAttachments(message.Id);
    // etc.
}
Run Code Online (Sandbox Code Playgroud)

.net c# resharper multithreading

8
推荐指数
2
解决办法
2954
查看次数

为什么删除项目后堆栈,队列和列表不缩小内部数组?

在.NET中StackQueue和和List(以及它们的通用实现)在内部使用数组保存项目。

添加新元素时,内部数组的大小将增加一倍Push()Enqueue()或者Add()如果已满。

问题是,为什么在删除项目时这些数据结构不内部数组减半?最好将上的阵列大小减半,或者如果阵列的大小小于四分之一,以防止浪费内存。Pop()Dequeue()Remove()

我使用以下代码检查此行为:

var s = new Stack<int>();
for (int i = 0; i <= 512; i++) s.Push(i); // Increases the array capacity to 1024.
for (int i = 0; i <= 512; i++) s.Pop();   // Doesn't decrease array capacity.

// Stack is empty, but internal array capacity is still 1024:
var fieldInfo = typeof(Stack<int>).GetField("_array", BindingFlags.NonPublic | BindingFlags.Instance); …
Run Code Online (Sandbox Code Playgroud)

.net c# arrays collections data-structures

5
推荐指数
1
解决办法
1482
查看次数

类型X有多个长度为1的构造函数.无法消除歧义

我正在使用ASP.NET Webforms项目并使用Unity for DI.该项目还使用DevExpress ASP.NET Ajax控件.

现在我们遇到了问题,似乎是DevExpress和Unity之间的冲突.

这是Unity配置

    [assembly: WebActivator.PostApplicationStartMethod(typeof(Sales.Web.App_Start.UnityWebFormsStart), "PostStart")]
namespace Sales.Web.App_Start
{
    /// <summary>
    ///     Startup class for the Unity.WebForms NuGet package.
    /// </summary>
    internal static class UnityWebFormsStart
    {
        /// <summary>
        ///     Initializes the unity container when the application starts up.
        /// </summary>
        /// <remarks>
        ///     Do not edit this method. Perform any modifications in the
        ///     <see cref="RegisterDependencies" /> method.
        /// </remarks>
        internal static void PostStart()
        {
            IUnityContainer container = new UnityContainer();
            HttpContext.Current.Application.SetContainer(container);

            RegisterDependencies(container);
        }

        /// <summary> …
Run Code Online (Sandbox Code Playgroud)

c# asp.net dependency-injection unity-container

5
推荐指数
2
解决办法
6329
查看次数