如何修复错误端点未找到WCF REST POST

时间:2019-12-18 09:42:07

标签: c# rest wcf post

我有一个WCF REST服务,其中字符串作为输入,

IService.cs:

 [ServiceContract]
public interface IServiceImportAutoLiasse
{   
    [OperationContract]
    [WebInvoke(Method = "POST",BodyStyle = WebMessageBodyStyle.Bare)]     
    string PostJson(string request);
}

IService.svc.cs:

  public string PostJson(string request)
    {
    //...
    }

我验证了web.config,它做得很好:

 <services>
  <!--SOAP-->
  <service behaviorConfiguration="ServBehavior" name="SysLap.Services.Web.ImportAutoLiasse.Service">
    <endpoint address="soap" binding="basicHttpBinding" bindingConfiguration="BindingCongHttp"
      contract="SysLap.Services.Web.ImportAutoLiasse.IService" />
    <endpoint address="mex" binding="mexHttpBinding" bindingConfiguration=""
      contract="IMetadataExchange" />

    <!--REST-->
    <endpoint address="rest" behaviorConfiguration="webHttpBhavior" bindingConfiguration="BindingConfigWebHttp"
     binding="webHttpBinding" contract="SysLap.Services.Web.ImportAutoLiasse.IService" />
  </service>
</services>

然后我用POSTMAN进行测试:

https://localhost:44355/ServiceImportAutoLiasse.svc/rest/PostJson

使用JSON输入:

{
"Headers": ["Header":"CA","Header":"Pe","Header":"RU","Header":"P_AMOUNT"],
"Values": ["value":"A;2019.12;S200;100","value":"A;2019.12;S000;1" ]
}

我有错误:

The server encountered an error processing the request. The exception message is 'There was an error
        deserializing the object of type System.String. End element 'root' from namespace '' expected. Found element
        'Headers' from namespace ''.'. See server logs for more details

我收到错误消息:“找不到端点。”

我该如何解决? 预先感谢,

2 个答案:

答案 0 :(得分:1)

我找到了解决方案,实际上我必须在POSTMAN的标头中停用Content-Type:

enter image description here

现在效果很好,

答案 1 :(得分:1)

如您所发布的,如果操作仅接受ContentType,我们应该更改String。但是,如果您想通过JSON数据传递强类型对象参数,则可以参考以下代码段。
假设数据合同如下,

    [DataContract]
    public class CompositeType
    {
        bool boolValue = true;
        string stringValue = "Hello ";

        [DataMember]
        public bool BoolValue
        {
            get { return boolValue; }
            set { boolValue = value; }
        }

        [DataMember]
        public string StringValue
        {
            get { return stringValue; }
            set { stringValue = value; }
        }
}

还有服务合同

        [OperationContract]
[WebInvoke(Method = "POST",BodyStyle = WebMessageBodyStyle.Bare)]   
        CompositeType GetDataUsingDataContract(CompositeType composite);

然后我们可以通过构造以下请求来测试操作。
enter image description here
enter image description here
请随时告诉我是否有什么我可以帮助的。