通过WCF引发nullreferenceException的GetOwinContext

通过WCF引发nullreferenceException的GetOwinContext,wcf,authentication,asp.net-identity,owin,identity,Wcf,Authentication,Asp.net Identity,Owin,Identity,我已经有了一个MVC ASP.NET应用程序,我在其中使用ASP.NET标识管理身份验证 我在应用程序中创建了一个WCF服务,允许其他应用程序使用我的应用程序提供给他们的服务创建新帐户 当我调用WCF服务时,当服务尝试使用userManager属性时,我从GetOwinContext()获得一个NullReference 这是我的WCF服务实现: using Microsoft.AspNet.Identity; using System; using System.Collections.Gen

我已经有了一个MVC ASP.NET应用程序,我在其中使用ASP.NET标识管理身份验证

我在应用程序中创建了一个WCF服务,允许其他应用程序使用我的应用程序提供给他们的服务创建新帐户

当我调用WCF服务时,当服务尝试使用userManager属性时,我从GetOwinContext()获得一个NullReference

这是我的WCF服务实现:

using Microsoft.AspNet.Identity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using MyCompany.MyProject.Core.Security.Auth;
using MyCompany.MyProject.Core.Security.Auth.Models;
using MyCompany.MyProject.MvcWebHost.Services.Contracts;
using Microsoft.AspNet.Identity.Owin;
using Microsoft.Owin.Host.SystemWeb;
using System.ServiceModel;

public class AuthenticationService : IAuthenticationService
{
    private ApplicationUserManager _userManager;
    public ApplicationUserManager UserManager
    {
        get
        {
            return _userManager ?? HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>();
        }
        private set
        {
            _userManager = value;
        }
    }

    public OperationResultDTO CreateUserAccount(UserAccountDTO userDto)
    {
        OperationResultDTO result = new OperationResultDTO();

        var user = new ApplicationUser();
        user.UserName = userDto.Identifier.ToString();
        user.Email = userDto.Email;
        Task<IdentityResult> adminresult = UserManager.CreateAsync(user, userDto.Password);

        if (adminresult.IsCompleted && adminresult.IsFaulted != false)
        {
            result.IsSuccess = true;
            result.HasError = false;
        }
        else
        {
            result.IsSuccess = false;
            result.HasError = true;
            result.ErrorMessage = "This is an error message!";
        }

        return result;
    }
}
使用Microsoft.AspNet.Identity;
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用System.Threading.Tasks;
使用System.Web;
使用MyCompany.MyProject.Core.Security.Auth;
使用MyCompany.MyProject.Core.Security.Auth.Models;
使用MyCompany.MyProject.MvcWebHost.Services.Contracts;
使用Microsoft.AspNet.Identity.Owin;
使用Microsoft.Owin.Host.SystemWeb;
使用System.ServiceModel;
公共类AuthenticationService:IAAuthenticationService
{
私有应用程序用户管理器\u用户管理器;
公共应用程序管理员用户管理器
{
得到
{
返回_userManager??HttpContext.Current.GetOwinContext().GetUserManager();
}
专用设备
{
_userManager=value;
}
}
公共操作结果到CreateUserAccount(UserAccountDTO到userDto)
{
OperationResultTo result=新的OperationResultTo();
var user=new ApplicationUser();
user.UserName=userDto.Identifier.ToString();
user.Email=userDto.Email;
任务adminresult=UserManager.CreateAsync(用户,userDto.Password);
if(adminresult.IsCompleted&&adminresult.IsFaulted!=false)
{
result.issucess=true;
result.hasrerror=false;
}
其他的
{
result.issucess=false;
result.hasrerror=true;
result.ErrorMessage=“这是一条错误消息!”;
}
返回结果;
}
}

如何解决此问题?

您可以在此处看到,WCF不支持OWIN


如果仍要使用OWIN,则必须切换到REST,如果要使用WCF,则必须删除OWIN

如果我删除OWIN,应如何获取userManager?谢谢