3

我有这个 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>

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

但是,当我使用WebHttpBinding和测试Google 的 Advanced Rest Client并通过它发布数据时,我收到了这个错误

服务器在处理请求时遇到错误。异常消息是'格式化程序在尝试反序列化消息时抛出异常:反序列化操作'PostComments'的请求消息正文时出错。遇到了意想不到的字符'T'。'。有关更多详细信息,请参阅服务器日志。异常堆栈跟踪是:

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

有时这个:

服务器在处理请求时遇到错误。异常消息是“反序列化操作“PostComments”的请求消息正文时出错。OperationFormatter 遇到无效的消息正文。预计会找到名称为“类型”且值为“对象”的属性。找到值'数字'。'。有关更多详细信息,请参阅服务器日志。异常堆栈跟踪是:

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

我真的不知道我错在哪里,因为这项服务昨天在谷歌的同一个休息客户端上运行良好。我必须尽快在服务器上部署此服务。请帮忙!!!!

[更新]

提琴手的输出:

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 发送的原始正文:

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

[更新 2] 我已经通过 Service Tracker 然后我发现我得到的根值有 Number 并且服务需要 Object. 所以如果它会触发一些事情,因为我现在完全空白......

4

1 回答 1

1

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

于 2014-08-07T18:20:50.530 回答