本文整理汇总了C#中IServiceContext.GetHttpContext方法的典型用法代码示例。如果您正苦于以下问题:C# IServiceContext.GetHttpContext方法的具体用法?C# IServiceContext.GetHttpContext怎么用?C# IServiceContext.GetHttpContext使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IServiceContext
的用法示例。
在下文中一共展示了IServiceContext.GetHttpContext方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: BindObject
private static object BindObject(string name, Type objectType, IServiceContext context)
{
string value = context.GetHttpContext().Request.Form.Get(name);
object changedValue;
return SafeConvert.TryChangeType(value, objectType, out changedValue) ? changedValue : null;
}
示例2: OnMethodExecuting
/// <summary>
/// Called before a service method is executed.
/// </summary>
/// <param name="serviceContext">The service context.</param>
/// <param name="behaviorContext">The "method executing" behavior context.</param>
/// <returns>A service method action.</returns>
public override BehaviorMethodAction OnMethodExecuting(IServiceContext serviceContext, MethodExecutingContext behaviorContext)
{
if (serviceContext == null)
{
throw new ArgumentNullException("serviceContext");
}
if (behaviorContext == null)
{
throw new ArgumentNullException("behaviorContext");
}
if (behaviorContext.Resource == null || m_validator == null)
{
return BehaviorMethodAction.Execute;
}
IReadOnlyCollection<ValidationError> validationErrors;
if (!m_validator.IsValid(behaviorContext.Resource, out validationErrors))
{
serviceContext.GetHttpContext().Items[ResourceValidator.ValidationErrorKey] = new ResourceState(validationErrors);
}
return BehaviorMethodAction.Execute;
}
示例3: OnMethodAuthorizing
/// <summary>
/// Called during the authorization process before a service method or behavior is executed.
/// </summary>
/// <param name="serviceContext">The service context.</param>
/// <param name="behaviorContext">The "method authorizing" behavior context.</param>
/// <returns>A service method action.</returns>
public override BehaviorMethodAction OnMethodAuthorizing(IServiceContext serviceContext, MethodAuthorizingContext behaviorContext)
{
if (serviceContext == null)
{
throw new ArgumentNullException("serviceContext");
}
var ranges = IPAddressRange.GetConfiguredRanges(m_sectionName).ToList();
if (ranges.Count == 0)
{
return BehaviorMethodAction.Stop;
}
bool isAllowed = false;
foreach (var range in ranges)
{
if (range.IsInRange(serviceContext.GetHttpContext().Request.UserHostAddress))
{
isAllowed = true;
break;
}
}
return isAllowed ? BehaviorMethodAction.Execute : BehaviorMethodAction.Stop;
}
示例4: OnMethodExecuting
/// <summary>
/// Called before a service method is executed.
/// </summary>
/// <param name="serviceContext">The service context.</param>
/// <param name="behaviorContext">The "method executing" behavior context.</param>
/// <returns>A service method action.</returns>
public override BehaviorMethodAction OnMethodExecuting(IServiceContext serviceContext, MethodExecutingContext behaviorContext)
{
if (serviceContext == null)
{
throw new ArgumentNullException("serviceContext");
}
serviceContext.GetHttpContext().Items[ServiceCallConstants.MaxQueryResults] = AllowOverride ? (MaxResults * -1) : MaxResults;
return BehaviorMethodAction.Execute;
}
示例5: BindArray
private static object BindArray(string name, Type objectType, IServiceContext context)
{
Type elementType = objectType.GetElementType();
string[] values = context.GetHttpContext().Request.Form.GetValues(name) ?? new string[0];
var changedValues = Array.CreateInstance(elementType, values.Length);
for (int i = 0; i < values.Length; i++)
{
object changedArrayValue;
changedValues.SetValue(SafeConvert.TryChangeType(values[i], elementType, out changedArrayValue) ? changedArrayValue : null, i);
}
return changedValues;
}
示例6: OnMethodExecuted
/// <summary>
/// Called after a service method is executed.
/// </summary>
/// <param name="serviceContext">The service context.</param>
/// <param name="behaviorContext">The "method executed" behavior context.</param>
public override void OnMethodExecuted(IServiceContext serviceContext, MethodExecutedContext behaviorContext)
{
if (serviceContext == null)
{
throw new ArgumentNullException("serviceContext");
}
HttpContextBase httpContext = serviceContext.GetHttpContext();
if (httpContext == null)
{
throw new ArgumentException(Resources.Global.MissingHttpContext, "serviceContext");
}
httpContext.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
httpContext.Response.Cache.SetValidUntilExpires(false);
httpContext.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
httpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
httpContext.Response.Cache.SetNoStore();
}
示例7: FormatRequest
/// <summary>
/// Deserializes HTTP message body data into an object instance of the provided type.
/// </summary>
/// <param name="context">The service context.</param>
/// <param name="objectType">The object type.</param>
/// <returns>The deserialized object.</returns>
/// <exception cref="HttpResponseException">If the object cannot be deserialized.</exception>
public virtual object FormatRequest(IServiceContext context, Type objectType)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
if (objectType == null)
{
throw new ArgumentNullException("objectType");
}
TryValidateFormValues(context.GetHttpContext());
object resource = InitializeResource(objectType);
if (resource == null)
{
return null;
}
NameValueCollection formData = PopulateFormData(context);
if (formData.Count == 0)
{
return resource;
}
dynamic dynamicResource = resource as DynamicResult;
if (dynamicResource != null)
{
PopulateDynamicResourceData(dynamicResource, formData);
return dynamicResource;
}
PopulateResourceData(resource, formData);
return resource;
}
示例8: ArgumentNullException
void ISecureServiceBehavior.OnMethodAuthorizing(IServiceContext serviceContext, MethodAuthorizingContext behaviorContext)
{
if (serviceContext == null)
{
throw new ArgumentNullException("serviceContext");
}
if (OnMethodAuthorizing(serviceContext, behaviorContext) == BehaviorMethodAction.Stop)
{
HttpStatusCode statusCode = serviceContext.Response.GetStatusCode();
if (statusCode != HttpStatusCode.Unauthorized && statusCode != m_statusCode)
{
throw new HttpResponseException(m_statusCode, m_statusDescription);
}
throw new HttpResponseException(statusCode, serviceContext.Response.GetStatusDescription());
}
HttpCachePolicyBase cache = serviceContext.GetHttpContext().Response.Cache;
cache.SetProxyMaxAge(new TimeSpan(0L));
cache.AddValidationCallback(CacheValidationHandler, new CacheValidationHandlerData(serviceContext, behaviorContext));
}
示例9: TrySetMaxQueryResults
private static void TrySetMaxQueryResults(IServiceContext context, NameValueCollection queryString)
{
object maxResultString = context.GetHttpContext().Items[ServiceCallConstants.MaxQueryResults] ??
Rest.Configuration.Options.ODataSettings.MaxResults.ToString(CultureInfo.InvariantCulture);
int maxQueryResults = Convert.ToInt32(maxResultString, CultureInfo.InvariantCulture);
if (maxQueryResults < 0)
{
if (!String.IsNullOrEmpty(queryString[TopKey]))
{
return;
}
queryString[TopKey] = (maxQueryResults * -1).ToString(CultureInfo.InvariantCulture);
}
else if (maxQueryResults > 0)
{
string topValue = queryString[TopKey];
if (!String.IsNullOrEmpty(topValue))
{
int top;
if (!Int32.TryParse(topValue, out top))
{
return;
}
if (top > 0 && top < maxQueryResults)
{
maxQueryResults = top;
}
}
queryString[TopKey] = maxQueryResults.ToString(CultureInfo.InvariantCulture);
}
}