WCF在Post方法中给出400错误请求的错误

时间:2012-09-27 05:05:52

标签: c# wcf post

我有这个WCF服务:

IService.cs

public interface IService
{
   [OperationContract]
   [WebInvoke(Method = "POST", UriTemplate = "/PostComments", BodyStyle = WebMessageBodyStyle.Wrapped,
    RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
   string PostComments(PostComment comment);

 [DataContract]
public class PostComment
{
    private string Id;
    private string FullName;
    private string Email;
    private string Location;
    private string Comments;
    private string Type;

    [DataMember]
    public string id { get { return Id; } set { Id = value; } }
    [DataMember]
    public string fullname { get { return FullName; } set { FullName = value; } }
    [DataMember]
    public string email { get { return Email; } set { Email = value; } }
    [DataMember]
    public string location { get { return Location; } set { Location = value; } }
    [DataMember]
    public string comments { get { return Comments; } set { Comments = value; } }
    [DataMember]
    public string type { get { return Type; } set { Type = value; } }
}

Service.svc.cs

public string PostComments(PostComment commnt)
        {
            int ItemId;
            string Comments, FullName, Location, Email, Type;

            ItemId = Convert.ToInt32(commnt.id);
            Type = commnt.type;
            Comments = commnt.comments;
            FullName = commnt.fullname;
            Location = commnt.location;
            Email = commnt.email;
            int i = 0;

            if (Type == "Style")
            {
                adp = new SqlDataAdapter("insert into tblComment(intId,strComments,strFullName,strLocation,strEmail,dtPosted,blnApprove) values("+ItemId+",'"+Comments+"','"+FullName+"','"+Location+"','"+Email+"',GetDate(),1)", offcon);
                adp.Fill(ds1,"StComment");
                DataTable dt = ds1.Tables["StComment"];
                i++;
            }
            else if (Type == "Article")
            {
                adp = new SqlDataAdapter("insert into tblNewsComment(intArticleId,strComments,strFullName,strLocation,strEmail,dtPosted,blnApprove) values(" + ItemId + ",'" + Comments + "','" + FullName + "','" + Location + "','" + Email + "',GetDate(),1)", offcon);
                adp.Fill(ds1, "ArtComment");
                DataTable dt = ds1.Tables["ArtComment"];
                i++;
            }
            if (i > 0)
            {
                return "Comment Successfully Submitted.";
            }
            else
            {
                return "Comment falied to Submit.";
            }
        }

web.config档案:

<?xml version="1.0"?>
<configuration>
<connectionStrings>
  <add name="con" connectionString="Data Source=.;Initial Catalog=PatrikaData;Integrated Security=SSPI;"/>  
</connectionStrings>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
    <httpRuntime requestPathInvalidCharacters="&lt;,&gt;,*,%,:,\"/>
  </system.web>
  <system.serviceModel>
    <services>
      <service behaviorConfiguration="ServiceBehaviour" name="WcfService.Service">
        <endpoint address="" behaviorConfiguration="web" binding="webHttpBinding"
          contract="WcfService.IService" />
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehaviour">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="web">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment/>
    <bindings>
      <webHttpBinding>
        <binding name="web"
                 maxBufferPoolSize="1500000"
                 maxReceivedMessageSize="1500000"
                 maxBufferSize="1500000">
          <readerQuotas
                maxArrayLength="656000"
                maxBytesPerRead="656000"
                maxDepth="32"
                maxNameTableCharCount="656000"
                maxStringContentLength="656000"
            />
        </binding>
      </webHttpBinding>
    </bindings>
  </system.serviceModel>  
</configuration>

现在,如果我正在使用BasicHttpBinding并使用 WCF测试客户端进行测试,那么一切正常。

但是,当我使用WebHttpBinding并使用 Google的高级休息客户端进行测试并通过该帖子发布数据时,我得到此错误

  

服务器在处理请求时遇到错误。例外   消息是'格式化程序在尝试时抛出异常   反序列化消息:反序列化请求主体时出错   操作消息'PostComments'。遇到意外的角色   'T'。'。请参阅服务器日志以获取更多详异常堆栈跟踪是:

     

在   System.ServiceModel.Dispatcher.OperationFormatter.DeserializeRequest(消息   消息,对象[]参数)at   System.ServiceModel.Dispatcher.DemultiplexingDispatchMessageFormatter.DeserializeRequest(消息   消息,对象[]参数)at   System.ServiceModel.Dispatcher.UriTemplateDispatchFormatter.DeserializeRequest(消息   消息,对象[]参数)at   System.ServiceModel.Dispatcher.CompositeDispatchFormatter.DeserializeRequest(消息   消息,对象[]参数)at   System.ServiceModel.Dispatcher.DispatchOperationRuntime.DeserializeInputs(MessageRpc&安培;   rpc)at   System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc&安培;   rpc)at   System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc&安培;   rpc)at   System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage41(MessageRpc&安培;   rpc)at   System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc&安培;   rpc)at   System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc&安培;   rpc)at   System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc&安培;   rpc)at   System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc&安培;   rpc)at   System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(MessageRpc&安培;   rpc)at   System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc&安培;   rpc)at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean   isOperationContextSet)

有时这个:

  

服务器在处理请求时遇到错误。例外   消息是'反序列化请求消息正文时出错   操作'PostComments'。 OperationFormatter遇到无效   邮件正文。预计会找到名为“type”和value的属性   '宾语'。找到价值'数字'。'。请参阅服务器日志以获取更多详   异常堆栈跟踪是:

     

在   System.ServiceModel.Dispatcher.OperationFormatter.DeserializeRequest(消息   消息,对象[]参数)at   System.ServiceModel.Dispatcher.DemultiplexingDispatchMessageFormatter.DeserializeRequest(消息   消息,对象[]参数)at   System.ServiceModel.Dispatcher.UriTemplateDispatchFormatter.DeserializeRequest(消息   消息,对象[]参数)at   System.ServiceModel.Dispatcher.DispatchOperationRuntime.DeserializeInputs(MessageRpc&安培;   rpc)at   System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc&安培;   rpc)at   System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc&安培;   rpc)at   System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc&安培;   rpc)at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean   isOperationContextSet)

我真的不知道我错在哪里,因为这项服务昨天在google的同一个客户端工作正常。 我必须尽快在服务器上部署此服务。 请帮助!!!!

[UPDATE]

Fiddler的输出:

HTTP / 1.1 400错误请求

Server: ASP.NET Development Server/10.0.0.0
Date: Thu, 27 Sep 2012 10:33:24 GMT
X-AspNet-Version: 4.0.30319
Content-Length: 3513
Cache-Control: private
Content-Type: text/html
Connection: Close

Google发送的Raw Body:

{
    "Id": "818744",
    "FullName": "Abhishek",
    "Email": "ab@gma.com",
    "Location": "Jaipur",
    "Comments": "asdkjfjk sdnfjlksdjlk dfljkfsd",
    "Type": "Style"
}

[更新2] 我已经通过服务跟踪器,然后我发现我得到的数字和服务的根值是期待对象。 所以,如果它会引发某些事情,因为我现在完全空白......

1 个答案:

答案 0 :(得分:1)

您发布的json的所有键都以大写字母开头,您的公共属性(标有小写属性)以小写字母开头。您应该让它们匹配或添加使用[Datamember(name =&#34; key&#34;)]以确保它们正确映射。