[英]Bad Request to RESTful WCF Service Forces IIS to Recycle App Pool
I am attempting to create and host a simple RESTful WCF Service. 我正在尝试创建和托管一个简单的RESTful WCF服务。 The service works perfectly except for 1 situation. 除1种情况外,该服务均正常运行。 I attempt to perform a POST to insert a new object into my static List using the JSON request of: 我尝试执行POST,以使用以下JSON请求将新对象插入到我的静态列表中:
{"sampleItem":{"Id":1,"StartValue":2,"EndValue":3}}
If I then change the request to be: 如果然后将请求更改为:
{"sampleItemBlah":{"Id":1,"StartValue":2,"EndValue":3}}
I get a 500 response and all future POST's return a 500 error until I recycle my IIS App Pool and then it starts to work again. 我得到500响应,并且所有以后的POST都返回500错误,直到我回收IIS App Pool,然后它又开始工作。
It doesn't appear that the service is in a faulted state because I can still perform GET's and get data back. 服务似乎没有出现故障,因为我仍然可以执行GET并取回数据。 I turned on trace debugging and I do not see any errors in my log file. 我打开了跟踪调试,但是在日志文件中没有看到任何错误。
Does anyone have any ideas? 有人有什么想法吗?
Here is my Service Contract: 这是我的服务合同:
[ServiceContract]
public interface IWcfRestService
{
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
[OperationContract]
SampleItem Insert(SampleItem sampleItem);
}
[DataContract]
public class SampleItem
{
[DataMember]
public int Id { get; set; }
[DataMember]
public int StartValue { get; set; }
[DataMember]
public int EndValue { get; set; }
}
And here is my implementation: 这是我的实现:
public class WcfRestService : IWcfRestService
{
private static readonly List<SampleItem> Items = new List<SampleItem>();
public SampleItem Insert(SampleItem sampleItem)
{
return BaseInsert(sampleItem);
}
private static SampleItem BaseInsert(SampleItem sampleItem)
{
if (Items.Exists(x => x.Id == sampleItem.Id))
Items.RemoveAll(x => x.Id == sampleItem.Id);
Items.Add(sampleItem);
return sampleItem;
}
}
And finally here is my ServiceModel section of my Web.config: 最后是Web.config的ServiceModel部分:
<services>
<service behaviorConfiguration="Services.ServiceBehavior"
name="WcfRestServiceApp.WcfRestService">
<endpoint address=""
behaviorConfiguration="RESTBehavior"
binding="webHttpBinding"
contract="WcfRestServiceApp.IWcfRestService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="RESTBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="Services.ServiceBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
Any and all help is greatly appreciated. 任何帮助都将不胜感激。
It turned out that the problem was with using the tag: 原来,问题出在使用标签:
BodyStyle = WebMessageBodyStyle.Wrapped
When I removed it, the requirement to have: 当我删除它时,要求具有:
{"sampleItem":{"Id":1,"StartValue":2,"EndValue":3}}
turned into: 转换成:
{"Id":1,"StartValue":2,"EndValue":3}
This forced the cast to the correct object type and if the field wasn't present, it set the value to null or the type's default empty value. 这将强制转换为正确的对象类型,如果不存在该字段,则将值设置为null或类型的默认空值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.