我无法在本地主机上使用 Android 模拟器的类型提供程序。
当使用 F# 的类型提供程序时,这对我不起作用,因为 TypeProvider 在设计时建立连接。
当我使用 localhost 时,类型提供程序很高兴
[<Literal>]
let JsonPath = "http://localhost:48213/api/cars"
type Repository = JsonProvider<JsonPath>
但是,在尝试使用本地主机路径时,我收到连接被拒绝。有关此错误的详细信息可以找到here .
因此,我认为解决方案是使用替代 IP 地址:
当我使用 10.0.2.2 时,类型提供程序不满意
将 IP 地址上的 “localhost” 替换为 “10.0.2.2” 会导致编译错误,因为 TypeProvider 未与更新的 IP 地址建立连接。
当我尝试使用“10.0.2.2”作为替代 IP(又名:解决方法)时,以下代码将无法编译,这是模拟器工作所需的 IP:
[<Literal>] (* "10.0.2.2" is the workaround IP for local host *)
let JsonPath = "http://10.0.2.2:48213/api/cars"
type Repository = JsonProvider<JsonPath>
type Car = { Make:string ; Model:string }
let getCars() =
Repository.Load JsonPath
|> Array.toSeq
|> Seq.map(fun x -> { Make=x.Make ; Model=x.Model })
总之,如何通过本地运行的 WebAPI 服务访问文件,并在模拟器上执行类型提供程序时仍然让类型提供程序满意?
最佳答案
您在创建 JsonProvider
时提供的静态参数纯粹用于设计时,以便它为您生成类型。当您调用 Repositor.Load
时,这将是您在运行时使用的 uri。它们应该分开对待,如下所示:
[<Literal>]
let DevelopmentJsonPath = "http://localhost:48213/api/cars"
type Repository = JsonProvider<DevelopmentJsonPath>
type Car = { Make:string ; Model:string }
let runtimeUri = "http://production.mycompany.com/api/cars"
let getCars() =
Repository.Load runtimeUri
|> Array.toSeq
|> Seq.map(fun x -> { Make=x.Make ; Model=x.Model })
关于asp.net-web-api - 我无法在本地主机上使用 Android 模拟器的类型提供程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41697200/