asp.net-web-api - 我无法在本地主机上使用 Android 模拟器的类型提供程序

标签 asp.net-web-api xamarin f# type-providers

我无法在本地主机上使用 Android 模拟器的类型提供程序。

当使用 F# 的类型提供程序时,这对我不起作用,因为 TypeProvider 在设计时建立连接。

当我使用 localhost 时,类型提供程序很高兴

[<Literal>]
let JsonPath =   "http://localhost:48213/api/cars"
type Repository = JsonProvider<JsonPath>

但是,在尝试使用本地主机路径时,我收到连接被拒绝。有关此错误的详细信息可以找到here .

因此,我认为解决方案是使用替代 IP 地址:

10.0.2.2

当我使用 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/

相关文章:

xamarin - 如何在 Xamarin 标签或图像中添加涟漪效果?

f# - 如何使用 Fable (F#) 进行 base64 编码?

compilation - f# 我如何编译然后释放文件 .fsx

不带 cookie 的 ASP.NET 身份外部提供商 Web API

c# - Web API 2.0 - 使用 JavaScriptConverter 自定义日期格式

android - 在 Android Activity 之间高效地传递自定义对象数据 [Mono Android]

android - Xamarin.Android 中的 rootView 高度和宽度

.net - 在路径上执行命令而不显示新窗口

.net - 当我返回类型 HttpResponseMessage 时,示例值和模型在 swagger ui 中为空

c# - ASP.NET Core 移动后端 API [错误] : This appname. 找不到 azurewebsites.net 页面