WCF REST-服务:传递国家/地区代码

发布于 2024-12-19 02:16:47 字数 284 浏览 2 评论 0原文

我有一个 WCF REST 服务,由在不同国家/地区使用的移动应用程序使用。它接受并返回 JSON,我使用 StructureMap。

我的想法是为所有国家/地区创建一个服务实例,但我需要知道哪个国家/地区正在调用该服务,并在服务中对此执行一些逻辑(例如,确定要使用的连接字符串)。

但是,我想避免每个服务请求都必须传递国家/地区代码。我在这里有什么选择?

例如,我可以为每个国家/地区设置一个端点吗?但在这种情况下,我如何知道使用什么端点/国家/地区代码来调用服务?

也许还有其他可能性?

I have a WCF REST-service that is used by a mobile application used in different countries. It's accepting and returning JSON, and I use StructureMap.

The idea is to create one service instance for all countries, but I need to know what country is calling the service and do some logic on that in the service (for example, determine the connection string to be used).

however, I want to avoid that country code has to be passed with each service request. What are my options here?

Can I, for example, have one endpoint for each country? But in that case, how can I know what endpoint/country code was used to call the service?

Maybe other possibilities?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不及他 2024-12-26 02:16:47

多个端点可能是一个解决方案,但您需要一种可靠的方法来确定实际使用了哪个端点。鉴于 WCF REST 服务的“断开连接”性质(我的意思是使用非 WCF 类型进行通信并仅使用 WCF 属性),这将要求您编写一个 WebHostFactory 来指定创建服务时的国家/地区对于给定的端点。您可以检查 WebOperationCurrent.Current 实例以访问隐藏在方法签名中的信息。例如:

Uri requestRoot = WebOperationContext.Current.IncomingRequest.UriTemplateMatch.BaseUri;
if (requestRoot.PathAndQuery.Contains("en-us")) {
    // use english locale
}
else if (requestRoot.PathAndQuery.Contains("de-de")) {
    // use german locale
}

您需要更好的策略来确定实际的国家/语言,但基本思想是在多个路径下重新托管相同的服务,并使用当前的 WebOperationContext 检查请求中的这些路径实例。

编辑
根据上面的评论,我想补充一点,您可以使用 WebOperationContext 访问当前请求的 UserAgent 字符串。因此,您还可以检查这些信息,寻找有关所请求语言的线索。但请记住,这些“隐式”信息只是线索,但并不能清楚地表明用户想要什么。

Multiple endpoints could be a solution, but you need a reliant way of determining which endpoint was actually used. Given the "disconnected" nature of WCF REST services (by that I mean the usage of non WCF types to do the communication and just using the WCF attributes), this would require you to write a WebHostFactory that specifies the country on creation of the service for a given endpoint. What you could to is inspect the WebOperationCurrent.Current instance to get access to information hidden from your method signature. For example:

Uri requestRoot = WebOperationContext.Current.IncomingRequest.UriTemplateMatch.BaseUri;
if (requestRoot.PathAndQuery.Contains("en-us")) {
    // use english locale
}
else if (requestRoot.PathAndQuery.Contains("de-de")) {
    // use german locale
}

You would need a better strategy to determine the actual country/language, but the basic idea is to re-host the same service under multiple paths and inspect those paths within the request using the current WebOperationContext instance.

Edit
From the comments above, I would like to add that you have access to the UserAgent string for the current request using the WebOperationContext. So you could also inspect those information looking for a clue about the requested language. But keep in mind that those "implicit" information are only clues but never clear indications about what the user wants.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文