【问题标题】:Change in UriTemplate on WCF interface causes whole service to fail.WCF 接口上 UriTemplate 的更改导致整个服务失败。
【发布时间】:2013-07-03 09:52:24
【问题描述】:

这是我第一次尝试通过托管在 Windows 服务中的 WCF 提供服务。我注意到,如果我在 UriTemplate 中做错了什么,它会完全破坏一切,我不知道为什么。

例子:

在第一个代码示例中,一切正常。该服务等待我定义的基地址并返回我期望的信息。

[ServiceContract]
public interface IService1
{
    [OperationContract]
    [WebInvoke(Method = "GET", UriTemplate = "/GetDetail?id={id}", BodyStyle = WebMessageBodyStyle.WrappedResponse, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
    MyDetail GetDetail(int id);
}

在这个例子中,我将UriTemplate = "/GetDetail?id={id}" 更改为UriTemplate = "/GetDetail/{id}",一切都中断了。该服务甚至不等待我配置的基地址。

[ServiceContract]
public interface IService1
{
    [OperationContract]
    [WebInvoke(Method = "GET", UriTemplate = "/GetDetail/{id}", BodyStyle = WebMessageBodyStyle.WrappedResponse, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
    MyDetail GetDetail(int id);
}

我不明白这种变化怎么会导致一切都失败?它不应该只是无法处理 GetDetail 调用而不破坏整个系统吗?

还要对此进行扩展,我如何将日志记录添加到我的服务中。

【问题讨论】:

    标签: c# wcf servicecontract uritemplate webinvoke


    【解决方案1】:

    使用WebGetWebInvoke 时,路径中的UriTemplate 变量必须是字符串。 当 UriTemplate 变量位于 UriTemplate 的查询部分中时,您只能将它们绑定到 int、long 等,如您的第一个示例所示。

    因此,解决您的问题的一个非常基本的方法可能是

    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        [WebGet(UriTemplate = "/GetDetail/{id}", BodyStyle = WebMessageBodyStyle.WrappedResponse, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        MyDetail  GetDetail(string id);
    }
    

    【讨论】:

    • 是的,我理解它们必须是字符串这一事实,但我不知道为什么其中一个定义不正确会导致整个 Web 服务失败?
    • 在启动时,WCF 激活验证服务声明和配置。您不能部分运行 WCF 服务。
    猜你喜欢
    • 1970-01-01
    • 2017-01-12
    • 1970-01-01
    • 2013-07-24
    • 2018-12-08
    • 1970-01-01
    • 1970-01-01
    • 2011-04-14
    • 1970-01-01
    相关资源
    最近更新 更多