【发布时间】:2012-09-18 19:52:45
【问题描述】:
我有这个 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="<,>,*,%,:,\"/>
</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 的 Advanced Rest Client 进行测试并通过它发布数据时,我会收到 此错误:
服务器在处理请求时遇到错误。例外 消息是'格式化程序在尝试 反序列化消息:反序列化请求正文时出错 操作“PostComments”的消息。遇到了意想不到的角色 'T'。有关更多详细信息,请参阅服务器日志。异常堆栈跟踪是:
在 System.ServiceModel.Dispatcher.OperationFormatter.DeserializeRequest(消息 消息,对象 [] 参数)在 System.ServiceModel.Dispatcher.DemultiplexingDispatchMessageFormatter.DeserializeRequest(消息 消息,对象 [] 参数)在 System.ServiceModel.Dispatcher.UriTemplateDispatchFormatter.DeserializeRequest(消息 消息,对象 [] 参数)在 System.ServiceModel.Dispatcher.CompositeDispatchFormatter.DeserializeRequest(消息 消息,对象 [] 参数)在 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(Boolean isOperationContextSet)
有时会这样:
服务器在处理请求时遇到错误。例外 消息是'反序列化请求消息正文时出错 操作“发表评论”。 OperationFormatter 遇到无效的 邮件正文。期望找到名称为“类型”和值的属性 '目的'。找到值'数字'。'。有关更多详细信息,请参阅服务器日志。 异常堆栈跟踪是:
在 System.ServiceModel.Dispatcher.OperationFormatter.DeserializeRequest(消息 消息,对象 [] 参数)在 System.ServiceModel.Dispatcher.DemultiplexingDispatchMessageFormatter.DeserializeRequest(消息 消息,对象 [] 参数)在 System.ServiceModel.Dispatcher.UriTemplateDispatchFormatter.DeserializeRequest(消息 消息,对象 [] 参数)在 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.ProcessMessage31(MessageRpc& rpc) 在 System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)
我真的不知道我错在哪里,因为这项服务昨天在谷歌的同一个休息客户端上运行良好。 我必须尽快在服务器上部署此服务。 请帮忙!!!!
[更新]
提琴手的输出:
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. 所以如果它会触发一些东西,因为我现在完全空白......
【问题讨论】:
-
请使用客户端将您提交的请求发布到服务。
-
我正在使用 Google 的 Rest 客户端,请求 URL 是 192.168.1.2:801/Service.svc/PostComment,然后在参数中我发送值 ItemId、FullName、Email 等。
-
请使用 Fiddler 之类的内容来捕获 Google 客户端发送到您的服务的实际请求,包括标头和 POST 内容,然后使用这些详细信息更新您的问题。
-
看看你的插入语句。您正在为 SQL 注入攻击敞开大门
-
数据契约序列化器是否区分大小写?您的公共属性为小写,内部字段为大写,JSON 字段名称为大写