我有這樣的WCF服務:WCF是給400錯誤請求的錯誤在POST方法
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
工作和測試谷歌的高級REST客戶端和公佈數據這個錯誤:
服務器遇到錯誤處理請求。 消息的例外情況是:'格式化程序在嘗試對 進行反序列化時拋出異常:對操作'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(MESSA geRpc & RPC)在 System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc & RPC)處 System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage41 System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc & RPC) (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.ProcessMessage11 System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc & RPC)(MessageRpc & rpc)at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime。ProcessMessage1(MessageRpc & RPC)在System.ServiceModel.Dispatcher.MessageRpc.Process(布爾 isOperationContextSet)
並且有時這樣的:
服務器遇到錯誤處理請求。 消息的例外情況是'請求消息正文反序列化錯誤 操作'PostComments'錯誤。 OperationFormatter遇到無效的 消息正文。預計會找到名稱爲'type'且值爲 'object'的屬性。發現值'數字'。'。查看服務器日誌獲取更多詳細信 的異常堆棧跟蹤是:
在 System.ServiceModel.Dispatcher.OperationFormatter.DeserializeRequest(消息 消息,對象[]參數)在 System.ServiceModel.Dispatcher.DemultiplexingDispatchMessageFormatter.DeserializeRequest(消息 消息,對象[ ]參數)在 System.ServiceModel.Dispatcher.UriTemplateDispatchFormatter.DeserializeRequest(消息 消息,則在 System.ServiceModel.Dispatcher.Dispatch在 System.ServiceModel.Dispatcher.DispatchOperationRuntime.DeserializeInputs(MessageRpc & RPC)對象[]參數) OperationRuntime.InvokeBegin(MessageRpc & RPC)處System.ServiceModel.Dispatcher System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc & RPC)在 System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc & RPC)。 MessageRpc.Process(布爾 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
原體,其谷歌正在發送:
{
"Id": "818744",
"FullName": "Abhishek",
"Email": "[email protected]",
"Location": "Jaipur",
"Comments": "asdkjfjk sdnfjlksdjlk dfljkfsd",
"Type": "Style"
}
[UPDATE 2] 我已經通過服務追蹤器,然後我發現,我得到的數值和服務的根值是期待對象。 因此,如果它會觸發的東西,因爲我現在完全是空白...
請使用客戶端發佈您提交給服務的請求。 – shambulator
我正在使用Google的Rest客戶端 請求URL是http://192.168.1.2:801/Service.svc/PostComment 然後在參數中我發送的值ItemId,FullName,Email等 –
請使用類似的東西[Fiddler](http://www.fiddler2.com/fiddler2/)捕獲Google客戶端發送給您的服務的實際請求(包括標頭和POST內容),然後使用這些詳細信息更新您的問題。 – shambulator