【问题标题】:How fix error Endpoint not found WCF REST POST如何修复错误 Endpoint not found WCF REST POST
【发布时间】:2020-04-10 20:10:55
【问题描述】:

我有一个以字符串为输入的 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

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

我该如何解决? 提前致谢,

【问题讨论】:

    标签: c# rest wcf post


    【解决方案1】:

    正如您发布的那样,如果操作只接受String,我们应该更改ContentType。但是如果你想通过JSON数据传递一个强类型的对象参数,你可以参考下面的代码sn-ps。
    假设有下面的数据合约,

        [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);
    

    然后我们可以通过构造以下请求来测试操作。


    如果有什么我可以帮忙的,请随时告诉我。

    【讨论】:

    • 非常感谢!我现在非常了解这个问题
    【解决方案2】:

    我找到了解决办法,实际上我必须在POSTMAN的Headers中停用Content-Type:

    现在效果很好,

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-09
      • 2012-03-30
      • 2021-01-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多