本文整理汇总了C#中IODataResponseMessage.SetHeader方法的典型用法代码示例。如果您正苦于以下问题:C# IODataResponseMessage.SetHeader方法的具体用法?C# IODataResponseMessage.SetHeader怎么用?C# IODataResponseMessage.SetHeader使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IODataResponseMessage
的用法示例。
在下文中一共展示了IODataResponseMessage.SetHeader方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Process
public override void Process(IODataRequestMessage requestMessage, IODataResponseMessage responseMessage)
{
string asyncToken = this.QueryContext.AsyncToken;
AsyncTask asyncTask = AsyncTask.GetTask(asyncToken);
if (asyncTask == null)
{
// token is invalid or expired.
throw Utility.BuildException(HttpStatusCode.NotFound);
}
else
{
if (!asyncTask.Ready)
{
ResponseWriter.WriteAsyncPendingResponse(responseMessage, asyncToken);
}
else
{
responseMessage.SetHeader(ServiceConstants.HttpHeaders.ContentType, "application/http");
responseMessage.SetHeader(ServiceConstants.HttpHeaders.ContentTransferEncoding, ServiceConstants.HttpHeaderValues.Binary);
using (var messageWriter = this.CreateMessageWriter(responseMessage))
{
var asyncWriter = messageWriter.CreateODataAsynchronousWriter();
var innerResponse = asyncWriter.CreateResponseMessage();
asyncTask.Execute(innerResponse);
}
}
}
}
示例2: ResponseBodyWriter
internal ResponseBodyWriter(bool hasMoved, IDataService service, IEnumerator queryResults, RequestDescription requestDescription, IODataResponseMessage responseMessage, ODataPayloadKind payloadKind)
{
this.hasMoved = hasMoved;
this.service = service;
this.queryResults = queryResults;
this.requestDescription = requestDescription;
this.responseMessage = responseMessage;
this.payloadKind = payloadKind;
this.encoding = HttpProcessUtility.EncodingFromAcceptCharset(this.service.OperationContext.Host.RequestAcceptCharSet);
if ((((payloadKind == ODataPayloadKind.Entry) || (payloadKind == ODataPayloadKind.Feed)) || ((payloadKind == ODataPayloadKind.Property) || (payloadKind == ODataPayloadKind.Collection))) || (((payloadKind == ODataPayloadKind.EntityReferenceLink) || (payloadKind == ODataPayloadKind.EntityReferenceLinks)) || (((payloadKind == ODataPayloadKind.Error) || (payloadKind == ODataPayloadKind.ServiceDocument)) || (payloadKind == ODataPayloadKind.Parameter))))
{
DataServiceHostWrapper host = service.OperationContext.Host;
if (WebUtil.GetEffectiveMaxResponseVersion(service.Configuration.DataServiceBehavior.MaxProtocolVersion, host.RequestMaxVersion) > RequestDescription.Version2Dot0)
{
bool isEntityOrFeed = (payloadKind == ODataPayloadKind.Entry) || (payloadKind == ODataPayloadKind.Feed);
if (WebUtil.ResponseMediaTypeWouldBeJsonLight(host.RequestAccept, isEntityOrFeed))
{
requestDescription.VerifyAndRaiseResponseVersion(RequestDescription.Version3Dot0, service);
host.ResponseVersion = RequestDescription.Version3Dot0.ToString() + ";";
}
}
}
if (this.requestDescription.TargetKind == RequestTargetKind.MediaResource)
{
this.mediaResourceStream = service.StreamProvider.GetReadStream(this.queryResults.Current, RequestDescription.GetStreamProperty(this.requestDescription), this.service.OperationContext);
}
else if (payloadKind != ODataPayloadKind.BinaryValue)
{
string requestAcceptCharSet = this.service.OperationContext.Host.RequestAcceptCharSet;
if (string.IsNullOrEmpty(requestAcceptCharSet) || (requestAcceptCharSet == "*"))
{
requestAcceptCharSet = "UTF-8";
}
if ((payloadKind == ODataPayloadKind.Value) && !string.IsNullOrEmpty(this.requestDescription.MimeType))
{
this.messageWriter = CreateMessageWriter(this.AbsoluteServiceUri, this.service, this.requestDescription.ActualResponseVersion, responseMessage, ODataFormat.RawValue);
}
else
{
this.messageWriter = CreateMessageWriter(this.AbsoluteServiceUri, this.service, this.requestDescription.ActualResponseVersion, responseMessage, this.service.OperationContext.Host.RequestAccept, requestAcceptCharSet);
}
try
{
this.contentFormat = ODataUtils.SetHeadersForPayload(this.messageWriter, payloadKind);
if ((payloadKind == ODataPayloadKind.Value) && !string.IsNullOrEmpty(this.requestDescription.MimeType))
{
responseMessage.SetHeader("Content-Type", this.requestDescription.MimeType);
}
}
catch (ODataContentTypeException exception)
{
throw new DataServiceException(0x19f, null, System.Data.Services.Strings.DataServiceException_UnsupportedMediaType, null, exception);
}
string headerValue = this.requestDescription.ResponseVersion.ToString() + ";";
responseMessage.SetHeader("DataServiceVersion", headerValue);
}
}
示例3: Process
public override void Process(IODataRequestMessage requestMessage, IODataResponseMessage responseMessage)
{
if (this.TryDispatch(requestMessage, responseMessage))
{
return;
}
if (this.QueryContext.Target.TypeKind != EdmTypeKind.Collection)
{
throw Utility.BuildException(HttpStatusCode.BadRequest, "The new resource can only be created under collection resource.", null);
}
if (this.QueryContext.Target.IsReference)
{
this.ProcessCreateLink(requestMessage, responseMessage);
return;
}
try
{
var targetEntitySet = (IEdmEntitySetBase)this.QueryContext.Target.NavigationSource;
// TODO: [lianw] Try to remove "targetEntitySet" later.
var queryResults = this.QueryContext.ResolveQuery(this.DataSource);
if (!IsAllowInsert(targetEntitySet as IEdmEntitySet))
{
throw new ODataServiceException(HttpStatusCode.BadRequest, "The insert request is not allowed.", null);
}
var bodyObject = ProcessPostBody(requestMessage, targetEntitySet, queryResults);
using (var messageWriter = this.CreateMessageWriter(responseMessage))
{
this.DataSource.UpdateProvider.SaveChanges();
// 11.4.2 Create an Entity
// Upon successful completion the service MUST respond with either 201 Created, or 204 No Content if the request included a return Prefer header with a value of return=minimal.
responseMessage.SetStatusCode(HttpStatusCode.Created);
responseMessage.SetHeader(ServiceConstants.HttpHeaders.Location, Utility.BuildLocationUri(this.QueryContext, bodyObject).OriginalString);
var currentETag = Utility.GetETagValue(bodyObject);
// if the current entity has ETag field
if (currentETag != null)
{
responseMessage.SetHeader(ServiceConstants.HttpHeaders.ETag, currentETag);
}
ResponseWriter.WriteEntry(messageWriter.CreateODataEntryWriter(targetEntitySet), bodyObject, targetEntitySet, ODataVersion.V4, null);
}
}
catch
{
this.DataSource.UpdateProvider.ClearChanges();
throw;
}
}
示例4: InitClass
public static void InitClass(TestContext testContext)
{
RequestWithApplicationJson = new ODataRequestMessageSimulator();
RequestWithApplicationJson.SetHeader(XmlConstants.HttpContentType, "aPPlicaTion/jSoN");
ResponseWithApplicationJson = new ODataResponseMessageSimulator();
ResponseWithApplicationJson.SetHeader(XmlConstants.HttpContentType, "aPPlicaTion/jSoN");
}
示例5: ProcessUpdateRequestBody
private void ProcessUpdateRequestBody(IODataRequestMessage requestMessage, IODataResponseMessage responseMessage, object targetObject, bool isUpsert)
{
if (this.QueryContext.Target.NavigationSource != null && this.QueryContext.Target.TypeKind == EdmTypeKind.Entity)
{
using (var messageReader = new ODataMessageReader(requestMessage, this.GetReaderSettings(), this.DataSource.Model))
{
var entryReader = messageReader.CreateODataEntryReader(this.QueryContext.Target.NavigationSource, (IEdmEntityType)this.QueryContext.Target.Type);
while (entryReader.Read())
{
switch (entryReader.State)
{
case ODataReaderState.EntryEnd:
var entry = (ODataEntry)entryReader.Item;
foreach (var property in entry.Properties)
{
if (Utility.IsETagProperty(targetObject, property.Name)) continue;
// the property might be an open property, so test null first
var propertyInfo = targetObject.GetType().GetProperty(property.Name);
if (propertyInfo != null)
{
if (!isUpsert && Utility.IsReadOnly(propertyInfo)) continue;
}
this.DataSource.UpdateProvider.Update(targetObject, property.Name, property.Value);
}
break;
}
}
}
}
else if (this.QueryContext.Target.Property != null && this.QueryContext.Target.TypeKind == EdmTypeKind.Complex)
{
using (var messageReader = new ODataMessageReader(requestMessage, this.GetReaderSettings(), this.DataSource.Model))
{
var property = messageReader.ReadProperty(this.QueryContext.Target.Property);
ODataComplexValue complexValue = property.Value as ODataComplexValue;
foreach (var p in complexValue.Properties)
{
if (Utility.IsETagProperty(targetObject, property.Name)) continue;
this.DataSource.UpdateProvider.Update(targetObject, p.Name, p.Value);
}
}
}
else
{
throw Utility.BuildException(
HttpStatusCode.NotImplemented,
string.Format("PATCH/PUT for '{0}' type is not supported.", this.QueryContext.Target.TypeKind),
null);
}
var currentETag = Utility.GetETagValue(targetObject);
// if the current entity has ETag field
if (currentETag != null)
{
if (!isUpsert)
{
this.DataSource.UpdateProvider.UpdateETagValue(targetObject);
}
this.DataSource.UpdateProvider.SaveChanges();
currentETag = Utility.GetETagValue(targetObject);
responseMessage.SetHeader(ServiceConstants.HttpHeaders.ETag, currentETag);
}
else
{
this.DataSource.UpdateProvider.SaveChanges();
}
ResponseWriter.WriteEmptyResponse(responseMessage);
}
示例6: ProcessCreate
private void ProcessCreate(Stream requestStream, IODataResponseMessage responseMessage)
{
try
{
// handle insert annotation
if (!this.IsAllowInsert(this.QueryContext.Target.NavigationSource as IEdmEntitySet))
{
throw new ODataServiceException(HttpStatusCode.BadRequest, "The insert request is not allowed.", null);
}
// handle content type
var contentType = this.HandleContentType((IEdmEntityType)this.QueryContext.Target.ElementType);
// handle entity
var entities = this.QueryContext.ResolveQuery(this.DataSource);
var entity = this.DataSource.UpdateProvider.Create(this.QueryContext.Target.ElementType.FullTypeName(), entities);
this.DataSource.StreamProvider.CreateStream(entity, requestStream, contentType);
this.DataSource.UpdateProvider.SaveChanges();
using (var messageWriter = this.CreateMessageWriter(responseMessage))
{
responseMessage.SetHeader(ServiceConstants.HttpHeaders.Location, Utility.BuildLocationUri(this.QueryContext, entity).OriginalString);
if (this.PreferenceContext.Return == ServiceConstants.PreferenceValue_Return_Minimal)
{
responseMessage.SetStatusCode(HttpStatusCode.NoContent);
}
else
{
responseMessage.SetStatusCode(HttpStatusCode.Created);
var edmEntitySet = (IEdmEntitySetBase)this.QueryContext.Target.NavigationSource;
ResponseWriter.WriteEntry(messageWriter.CreateODataEntryWriter(edmEntitySet), entity, edmEntitySet, ODataVersion.V4, null);
}
}
}
catch
{
this.DataSource.UpdateProvider.ClearChanges();
throw;
}
}
示例7: Process
public override void Process(IODataRequestMessage requestMessage, IODataResponseMessage responseMessage)
{
object result;
if (this.HttpMethod == HttpMethod.GET)
{
// we cannot invoke same action request multiple times, so only Functions are allowed to do the server-driven paging
this.QueryContext.InitializeServerDrivenPaging(this.PreferenceContext);
result = this.ProcessFunction();
if (this.PreferenceContext.MaxPageSize.HasValue)
{
responseMessage.AddPreferenceApplied(string.Format("{0}={1}", ServiceConstants.Preference_MaxPageSize, this.QueryContext.appliedPageSize.Value));
}
}
else
{
// TODO: currently ETag feature does not support action operation
result = this.ProcessAction(requestMessage);
}
if (result == null)
{
// Protocol 9.1.4 Response Code 204 No Content
// A request returns 204 No Content if the requested resource has the null value,
// or if the service applies a return=minimal preference. In this case, the response body MUST be empty.
ResponseWriter.WriteEmptyResponse(responseMessage);
return;
}
using (var messageWriter = this.CreateMessageWriter(responseMessage))
{
if (this.QueryContext.Target.TypeKind == EdmTypeKind.None ||
this.QueryContext.Target.TypeKind == EdmTypeKind.EntityReference ||
this.QueryContext.Target.ElementTypeKind == EdmTypeKind.EntityReference)
{
throw Utility.BuildException(HttpStatusCode.NotImplemented, "Unsupported return type in operation.", null);
}
else if (this.QueryContext.Target.TypeKind == EdmTypeKind.Entity || this.QueryContext.Target.ElementTypeKind == EdmTypeKind.Entity)
{
ODataWriter resultWriter;
IEdmNavigationSource entitySource = this.QueryContext.OperationResultSource ?? this.QueryContext.Target.NavigationSource;
if (this.QueryContext.Target.TypeKind == EdmTypeKind.Collection)
{
IEdmEntitySetBase entitySet = (IEdmEntitySetBase)entitySource;
resultWriter = messageWriter.CreateODataFeedWriter(entitySet, (IEdmEntityType)this.QueryContext.Target.ElementType);
ResponseWriter.WriteFeed(resultWriter, (IEdmEntityType)this.QueryContext.Target.ElementType, result as IEnumerable, entitySet, ODataVersion.V4, this.QueryContext.QuerySelectExpandClause, this.QueryContext.TotalCount, null, this.QueryContext.NextLink, this.RequestHeaders);
}
else
{
if (this.HttpMethod == HttpMethod.GET)
{
var currentETag = Utility.GetETagValue(result);
// if the current entity has ETag field
if (currentETag != null)
{
string requestETag;
if (Utility.TryGetIfNoneMatch(this.RequestHeaders, out requestETag) && (requestETag == ServiceConstants.ETagValueAsterisk || requestETag == currentETag))
{
ResponseWriter.WriteEmptyResponse(responseMessage, HttpStatusCode.NotModified);
return;
}
responseMessage.SetHeader(ServiceConstants.HttpHeaders.ETag, currentETag);
}
}
resultWriter = messageWriter.CreateODataEntryWriter(entitySource, (IEdmEntityType)this.QueryContext.Target.Type);
ResponseWriter.WriteEntry(resultWriter, result, entitySource, ODataVersion.V4, this.QueryContext.QuerySelectExpandClause, this.RequestHeaders);
}
}
else
{
ODataProperty property = new ODataProperty() { Name = "value", Value = ODataObjectModelConverter.CreateODataValue(result) };
messageWriter.WriteProperty(property);
}
}
}
示例8: WriteAsyncPendingResponse
/// <summary>
/// Write empty response and set the HttpStatusCode to 202 Accepted
/// </summary>
/// <param name="responseMessage"></param>
/// <param name="asyncToken"></param>
public static void WriteAsyncPendingResponse(IODataResponseMessage responseMessage, string asyncToken)
{
responseMessage.SetStatusCode(HttpStatusCode.Accepted);
responseMessage.SetHeader(ServiceConstants.HttpHeaders.DataServiceVersion, "4.0");
Uri location = new Uri(ServiceConstants.ServiceBaseUri, string.Format("{0}?{1}={2}", ServiceConstants.ServicePath_Async, ServiceConstants.QueryOption_AsyncToken, asyncToken));
responseMessage.SetHeader(ServiceConstants.HttpHeaders.Location, location.OriginalString);
if (!(responseMessage is ODataBatchOperationResponseMessage))
{
using (responseMessage.GetStream())
{
}
}
}
示例9: WriteEmptyResponse
/// <summary>
/// Write empty response and set the HttpStatusCode to 204 NoContent
/// </summary>
/// <param name="responseMessage"></param>
public static void WriteEmptyResponse(IODataResponseMessage responseMessage, HttpStatusCode statusCode = HttpStatusCode.NoContent)
{
responseMessage.SetStatusCode(statusCode);
responseMessage.SetHeader("OData-Version", "4.0");
if (!(responseMessage is ODataBatchOperationResponseMessage))
{
using (responseMessage.GetStream())
{
}
}
}
示例10: Process
//.........这里部分代码省略.........
}
if (this.PreferenceContext.TrackingChanges)
{
responseMessage.AddPreferenceApplied(ServiceConstants.Preference_TrackChanging);
}
responseMessage.SetStatusCode(HttpStatusCode.OK);
using (var messageWriter = this.CreateMessageWriter(responseMessage))
{
IEdmNavigationSource navigationSource = this.QueryContext.Target.NavigationSource;
IEnumerable iEnumerableResults = queryResults as IEnumerable;
if (this.QueryContext.Target.IsReference && this.QueryContext.Target.TypeKind == EdmTypeKind.Collection)
{
// Query a $ref collection
IList<ODataEntityReferenceLink> links = new List<ODataEntityReferenceLink>();
foreach (var iEnumerableResult in iEnumerableResults)
{
var link = new ODataEntityReferenceLink
{
Url = Utility.BuildLocationUri(this.QueryContext, iEnumerableResult),
};
links.Add(link);
}
ODataEntityReferenceLinks linksCollection = new ODataEntityReferenceLinks() { Links = links, NextPageLink = this.QueryContext.NextLink };
linksCollection.InstanceAnnotations.Add(new ODataInstanceAnnotation("Links.Annotation", new ODataPrimitiveValue(true)));
messageWriter.WriteEntityReferenceLinks(linksCollection);
}
else if (this.QueryContext.Target.IsReference && this.QueryContext.Target.TypeKind == EdmTypeKind.Entity)
{
// Query a $ref
var link = new ODataEntityReferenceLink
{
Url = Utility.BuildLocationUri(this.QueryContext, queryResults),
};
link.InstanceAnnotations.Add(new ODataInstanceAnnotation("Link.Annotation", new ODataPrimitiveValue(true)));
messageWriter.WriteEntityReferenceLink(link);
}
else if (this.QueryContext.Target.NavigationSource != null && this.QueryContext.Target.TypeKind == EdmTypeKind.Collection)
{
// Query a feed
IEdmEntitySetBase entitySet = navigationSource as IEdmEntitySetBase;
IEdmEntityType entityType = this.QueryContext.Target.ElementType as IEdmEntityType;
if (entitySet == null || entityType == null)
{
throw new InvalidOperationException("Invalid target when query feed.");
}
ODataWriter resultWriter = messageWriter.CreateODataFeedWriter(entitySet, entityType);
ResponseWriter.WriteFeed(resultWriter, entityType, iEnumerableResults, entitySet, ODataVersion.V4, this.QueryContext.QuerySelectExpandClause, this.QueryContext.TotalCount, this.QueryContext.DeltaLink, this.QueryContext.NextLink, this.RequestHeaders);
resultWriter.Flush();
}
else if (this.QueryContext.Target.NavigationSource != null && this.QueryContext.Target.TypeKind == EdmTypeKind.Entity)
{
var currentETag = Utility.GetETagValue(queryResults);
// if the current entity has ETag field
if (currentETag != null)
{
string requestETag;
if (Utility.TryGetIfNoneMatch(this.RequestHeaders, out requestETag) && (requestETag == ServiceConstants.ETagValueAsterisk || requestETag == currentETag))
{
ResponseWriter.WriteEmptyResponse(responseMessage, HttpStatusCode.NotModified);
return;
}
responseMessage.SetHeader(ServiceConstants.HttpHeaders.ETag, currentETag);
}
// Query a single entity
IEdmEntityType entityType = this.QueryContext.Target.Type as IEdmEntityType;
ODataWriter resultWriter = messageWriter.CreateODataEntryWriter(navigationSource, entityType);
ResponseWriter.WriteEntry(resultWriter, queryResults, navigationSource, ODataVersion.V4, this.QueryContext.QuerySelectExpandClause, this.RequestHeaders);
resultWriter.Flush();
}
else if (this.QueryContext.Target.Property != null && !this.QueryContext.Target.IsRawValue)
{
// Query a individual property
ODataProperty property = ODataObjectModelConverter.CreateODataProperty(queryResults, this.QueryContext.Target.Property.Name);
messageWriter.WriteProperty(property);
}
else if (this.QueryContext.Target.IsRawValue)
{
// Query a $value or $count
var propertyValue = ODataObjectModelConverter.CreateODataValue(queryResults);
messageWriter.WriteValue(propertyValue);
}
else
{
throw Utility.BuildException(HttpStatusCode.NotImplemented);
}
}
}
示例11: CreateAsyncReader
private ODataAsynchronousReader CreateAsyncReader(string payload)
{
responseStream = new MemoryStream(Encoding.Default.GetBytes(payload));
responseMessage = new InMemoryMessage { Stream = responseStream };
responseMessage.SetHeader("Content-Type", "application/http");
responseMessage.SetHeader("Content-Transfer-Encoding", "binary");
this.messageReader = new ODataMessageReader(responseMessage, new ODataMessageReaderSettings(), userModel);
return this.messageReader.CreateODataAsynchronousReader();
}
示例12: HandleBatchOperationError
/// <summary>Handles an exception when processing a batch response.</summary>
/// <param name='service'>Data service doing the processing.</param>
/// <param name="requestMessage">requestMessage holding information about the request that caused an error</param>
/// <param name="responseMessage">responseMessage to which we need to write the exception message</param>
/// <param name='exception'>Exception thrown.</param>
/// <param name='batchWriter'>Output writer for the batch.</param>
/// <param name="responseStream">Underlying response stream.</param>
/// <param name="defaultResponseVersion">The data service version to use for response, if it cannot be computed from the requestMessage.</param>
internal static void HandleBatchOperationError(IDataService service, AstoriaRequestMessage requestMessage, IODataResponseMessage responseMessage, Exception exception, ODataBatchWriter batchWriter, Stream responseStream, Version defaultResponseVersion)
{
Debug.Assert(service != null, "service != null");
Debug.Assert(exception != null, "exception != null");
Debug.Assert(batchWriter != null, "batchWriter != null");
Debug.Assert(service.Configuration != null, "service.Configuration != null");
Debug.Assert(CommonUtil.IsCatchableExceptionType(exception), "CommonUtil.IsCatchableExceptionType(exception)");
ErrorHandler handler = CreateHandler(service, requestMessage, exception, defaultResponseVersion);
service.InternalHandleException(handler.exceptionArgs);
if (requestMessage != null && responseMessage != null)
{
responseMessage.SetHeader(XmlConstants.HttpODataVersion, handler.responseVersion.ToString(2) + ";");
requestMessage.ProcessException(handler.exceptionArgs);
// if ProcessBenignException returns anything, we can safely not write to the stream.
if (ProcessBenignException(exception, service) != null)
{
return;
}
}
if (requestMessage != null)
{
responseMessage = requestMessage.BatchServiceHost.GetOperationResponseMessage();
WebUtil.SetResponseHeadersForBatchRequests(responseMessage, requestMessage.BatchServiceHost);
}
else
{
responseMessage = batchWriter.CreateOperationResponseMessage(null);
responseMessage.StatusCode = handler.exceptionArgs.ResponseStatusCode;
}
MessageWriterBuilder messageWriterBuilder = MessageWriterBuilder.ForError(
null,
service,
handler.responseVersion,
responseMessage,
handler.contentType,
null /*acceptCharsetHeaderValue*/);
using (ODataMessageWriter messageWriter = messageWriterBuilder.CreateWriter())
{
ODataError error = handler.exceptionArgs.CreateODataError();
WriteErrorWithFallbackForXml(messageWriter, handler.encoding, responseStream, handler.exceptionArgs, error, messageWriterBuilder);
}
}