首页 > 解决方案 > WCF 自托管服务,参数(字符串)断言失败

问题描述

我有一个自托管的 wcf 服务,它有三个 OperationContract,其中两个应该接受参数

 [OperationContract]
 [WebInvoke(Method = "GET",
            ResponseFormat = WebMessageFormat.Json,
            UriTemplate = "json/{id}")]
    string JSONData(string id);

基于这个参数,我执行一些逻辑并返回数据:

public string JSONData(string id)
    {
        if (id == "1")
        {
            string json = "{ \"months\":[{ \"name\":\"January\"},  { \"name\":\"Febuary\"},  { \"name\":\"March\"}  ]}";
            return json;
        }
        else
        {
            return "Id not found, invalid request";
        }
    }

我使用邮递员做这个请求:

在此处输入图像描述

但是,我填写的 id 并不重要,我总是得到:

"Id not found, invalid request"

为了增加我的困惑,以下确实有效:

 public string JSONData(string id)
    {
        return Data(id);
    }

    private string Data(string id)
    {
        // logic
        return "Data: " + id;
    }

这让我相信,与其说输入失败不如 if 语句中的断言失败。

将逻辑更改为:

  public string JSONData(string id)
    {
        if (id.Equals("name"))
        {
            return "equal";
        }
        else { 
        return Data(id);
        }
    }

    private string Data(string id)
    {
        // logic
        return "Data: " + id;
    }

if 语句中的断言仍未完成,但是我看不出我在以这种方式断言字符串的相等性时做错了什么。

帮助将不胜感激。

谢谢。

标签: c#stringwcfgetassert

解决方案


我不知道您想要完成什么,但使用 wcftestclient 我可以返回您所要求的内容。

WcfTest

这就是你在代码中调用它的方式 这就是你在代码中调用它的方式

检查此页面答案 https://stackoverflow.com/a/18589783/13760086


推荐阅读