如何根据 Restful wcf 服务中的暂存环境更改 uritemplate

How to change uritemplate according to the staging enviornment in Restfull wcf service

我的非产品环境有以下操作合同

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

但我需要为我的产品环境更改此 uritemplate,如下所示

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

我尝试了很多东西 none 都成功了

我试图将密钥放入 web.config 但界面不允许接受来自配置的任何内容。

你只能在编译时处理这个问题,不能在运行时处理。

我真的希望暂存和生产环境允许关于共同根的相同 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)

现在您需要确保您拥有暂存和生产的构建配置,并为每个设置正确的条件编译符号。

另请参阅:C# how can I use #if to have different compile result for debug and release?