升级到 OData V3 的 WCF 数据服务 5.0 后,我无法进行插入或更新。唯一可能的就是选择。
编辑
重写方法 HandleException 中的异常:
异常:System.Data.Services.DataServiceException:缺少 Content-Type 标头值。在 System.Data.Services.HttpProcessUtility.ReadContentType(String contentType, String& mime, Encoding& encoding) 在 System.Data. Services.DataService 1.HandlePostOperation(RequestDescription description, IDataService dataService)
at System.Data.Services.DataService
1.ProcessIncomingRequest(RequestDescription description, IDataService dataService) 在 System.Data.Services.DataService 1.HandleNonBatchRequest(RequestDescription description)
at System.Data.Services.DataService
1.HandleRequest()
响应状态代码:400
客户端代码 我在 HTTP-Request-Header 中设置了一个令牌...
/// <summary>
/// Initializes a new instance of the <see cref="ServiceBase"/> class.
/// </summary>
/// <param name="uri">service uri</param>
protected ServiceBase(Uri uri)
{
this.Context = new Entities(uri) { MergeOption = MergeOption.OverwriteChanges };
this.Context.SendingRequest += new EventHandler<SendingRequestEventArgs>(this.ContextSendingRequest);
}
/// <summary>
/// OnSendingRequest
/// </summary>
/// <param name="sender">source</param>
/// <param name="e">event args</param>
private void ContextSendingRequest(object sender, SendingRequestEventArgs e)
{
e.RequestHeaders["token"] = "xyassdfdfdfdf";
}
Service-Code: Service解析这个Header并检查token
public XYDataService()
{
this.ProcessingPipeline.ProcessingRequest += this.ProcessingPipelineProcessingRequest;
}
/// <summary>
/// OnProcessingRequest
/// </summary>
/// <param name="sender">source</param>
/// <param name="e">event args</param>
private void ProcessingPipelineProcessingRequest(object sender, DataServiceProcessingPipelineEventArgs e)
{
var authHeader = HttpContext.Current.Request.Headers["token"];
if (string.IsNullOrEmpty(authHeader) || !authHeader.Equals("xyassdfdfdfdf"))
{
throw new DataServiceException(401, "401 Unauthorized");
}
}
谢谢你的帮助。
和我