引用其他命名空间 [英] Referencing Other Namespaces

查看:116
本文介绍了引用其他命名空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

因此,我仍在将OAuth内容从C#转换为F#的旅程中.我遇到了一个有趣的问题.命名空间引用.

So I'm still on my journey to convert the OAuth stuff from C# to F#.  I have run into an interesting issue.  Namespace references.

我在IdentityConfig.fs中定义了一系列类型,这些类型在名称空间"FSharpWebKit2"中.

I have a series of types defined in IdentityConfig.fs , which is in the namespace "FSharpWebKit2".

我想使用我的AccountController中的那些类型,这些名称位于"FSharpWebKit2.Controllers"命名空间中.

I want to use those types from my AccountController, which is in the namespace "FSharpWebKit2.Controllers".

尝试引用它们时,我失败了.我已经打开了FSharpWebKit2,我尝试了FSharpWebKit2.typename,只是使用类型名称.我想念什么?

When attempting to reference them, I fail.  I have opened FSharpWebKit2, I have tried FSharpWebKit2.typename and simply just using the name of type.  What am I missing?

我尝试使用的定义类型是:ApplicationUserManager.我得到那个类型没有定义.

The type that is defined that I am attempting to use is: ApplicationUserManager.  I get that that type is not defined.

这是一些代码...

namespace FSharpWeb2.Controllers

open Microsoft.AspNet.Identity.Owin
open Microsoft.AspNet.Identity
open System.Threading.Tasks
open System.Web
open System.Web.Mvc
open FSharpWeb2

[<Authorize>]
type AccountController() as this=
    inherit Controller() 

    let _userManager : ApplicationUserManager = null

我正在尝试从C#移植

using Microsoft.AspNet.Identity.Owin;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using AwesomeAngularMVCApp.Models;

namespace AwesomeAngularMVCApp.Controllers
{
    [Authorize]
    public class AccountController : Controller
    {
        private ApplicationUserManager _userManager;
        private ApplicationSignInManager _signInManager;

        public AccountController() { }

        public AccountController(ApplicationUserManager userManager, ApplicationSignInManager signInManager)
        {
            UserManager = userManager;
            SignInManager = signInManager;
        }

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

        public ApplicationSignInManager SignInManager
        {
            get { return _signInManager ?? HttpContext.GetOwinContext().Get<ApplicationSignInManager>(); }
            private set { _signInManager = value; }
        }

这是IdentityConfig.fs文件,其中包含未被检测到的类型.

Here is the IdentityConfig.fs file containing the types which are not being detected.

namespace FSharpWeb2

open Microsoft.AspNet.Identity.Owin
open Microsoft.AspNet.Identity
open Microsoft.AspNet.Identity.EntityFramework
open System.Threading.Tasks
open System.Security.Claims
open System.Data
open Microsoft.Owin
open Microsoft.Owin.Security
open System

type ApplicationUser() as this =
    inherit IdentityUser()
    member x.GenerateUserIdentityAsync (manager : UserManager<ApplicationUser>) = 
        manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie)
//        async{   
//            //let! is a let binding with await while Async.AwaitTask converts C# Tast<t> to F# Async<a>
//            let! userIdentity = Async.AwaitTask(manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie)) 
//            return(userIdentity)
//        }

type ApplicationDbContext() =
    inherit IdentityDbContext<ApplicationUser>("DefaultConnection", throwIfV1Schema=false)
    static member Create() =
        new ApplicationDbContext()
        
type ApplicationUserManager(store : IUserStore<ApplicationUser>) =
    inherit UserManager<ApplicationUser>(store)
    static member Create(options : IdentityFactoryOptions<ApplicationUserManager>, context : IOwinContext) =
        let manager = new ApplicationUserManager(new UserStore<ApplicationUser> (context.Get<ApplicationDbContext>()), 
                        PasswordValidator = new PasswordValidator(RequiredLength = 6, 
                            RequireNonLetterOrDigit = true, 
                            RequireDigit = true, 
                            RequireLowercase = true, 
                            RequireUppercase = true))
        manager.UserValidator <- UserValidator<ApplicationUser>(manager,
                                    AllowOnlyAlphanumericUserNames = false,
                                    RequireUniqueEmail = true)
        manager.UserLockoutEnabledByDefault <- true
        manager.DefaultAccountLockoutTimeSpan <- TimeSpan.FromMinutes(5.0)
        manager.MaxFailedAccessAttemptsBeforeLockout <- 5
        let dataProtectionProvider = options.DataProtectionProvider
        if dataProtectionProvider <> null
            then
            manager.UserTokenProvider <- DataProtectorTokenProvider<ApplicationUser>(dataProtectionProvider.Create("ASP.NET Identity"));
        manager

type ApplicationSignInManager(userManager : ApplicationUserManager, authenticationManager : IAuthenticationManager) =
    inherit SignInManager<ApplicationUser, string>(userManager, authenticationManager)
        override this.CreateUserIdentityAsync(user : ApplicationUser) =
            let userManager = this.UserManager :?> ApplicationUserManager
            user.GenerateUserIdentityAsync(userManager)
        static member Create(options : IdentityFactoryOptions<ApplicationSignInManager>, context : IOwinContext) = 
            new ApplicationSignInManager(context.GetUserManager<ApplicationUserManager>(), context.Authentication)

推荐答案

最可能的解释是,AccountController类型位于项目中出现在ApplicationUserManager类型之前的文件中(如果按字母顺序包含文件,将会发生这种情况.

The most likely explanation is that the AccountController type is in a file that appears in the project before the ApplicationUserManager type (as would happen if the files were included in alphabetical order).

F#编译在执行类型解析时会按顺序从上至下以及从每个文件的上至下依次遍历项目中的文件.

F# compilation works through the files in the project in order from top to bottom, as well as from top to bottom in each file, when performing its type resolution. 


这篇关于引用其他命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆