首页 > 解决方案 > 如何根据Restfull wcf服务中的暂存环境更改uritemplate

问题描述

我的非产品环境有以下运营合同

[OperationContract]
    [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "members/Empdata")]

但我需要uritemplate为我的 prod 环境更改它,如下所示

[OperationContract]
    [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "members/Empdata/Search")]

我尝试了很多东西都没有成功

我试图把钥匙放进去web.config,但界面不允许接受任何来自配置的东西。

标签: wcf

解决方案


您只能在编译时处理这个,而不是在运行时。

我真的希望暂存和生产环境确实允许针对公共根目录使用相同的 url 结构。它们是合同的一部分。

如果你真的想在代码中解决这个问题,那么你可以使用编译器指令,并通过提供正确的编译参数来控制代码中的两个变体。

[OperationContract]
#if STAGING
[WebInvoke(Method = "POST", 
           RequestFormat = WebMessageFormat.Json, 
           ResponseFormat = WebMessageFormat.Json, 
           BodyStyle = WebMessageBodyStyle.Bare, 
           UriTemplate = "members/Empdata")]
#endif
#if PRODUCTION
[WebInvoke(Method = "POST", 
           RequestFormat = WebMessageFormat.Json, 
           ResponseFormat = WebMessageFormat.Json, 
           BodyStyle = WebMessageBodyStyle.Bare, 
           UriTemplate = "members/Empdata/Search")]
#endif
public string Search(string data)

现在,您需要确保已为 Staging 和 Production 构建配置,并为每个配置设置正确的条件编译符号。

另请参阅:C# 如何使用#if 获得不同的编译结果进行调试和发布?


推荐阅读