本文整理汇总了C#中IServiceContext类的典型用法代码示例。如果您正苦于以下问题:C# IServiceContext类的具体用法?C# IServiceContext怎么用?C# IServiceContext使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
IServiceContext类属于命名空间,在下文中一共展示了IServiceContext类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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");
}
string userId, signatureHash;
DateTime timestamp;
if (!TryGetRequestedSignature(serviceContext.Request, out userId, out signatureHash, out timestamp) ||
String.IsNullOrWhiteSpace(userId) ||
String.IsNullOrWhiteSpace(signatureHash))
{
serviceContext.Response.SetStatus(HttpStatusCode.Unauthorized, Global.Unauthorized);
return BehaviorMethodAction.Stop;
}
if (!IsRequestedSignatureValid(serviceContext, signatureHash, timestamp))
{
serviceContext.Response.SetStatus(HttpStatusCode.Unauthorized, Global.Unauthorized);
return BehaviorMethodAction.Stop;
}
string hashedServerSignature = HashSignature(serviceContext.Request, userId, GenerateServerSignature(serviceContext, userId, timestamp));
return signatureHash == hashedServerSignature ? BehaviorMethodAction.Execute : BehaviorMethodAction.Stop;
}
示例2: 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");
}
if (objectType == typeof(object))
{
using (var streamReader = new StreamReader(context.Request.Body, context.Request.Headers.ContentCharsetEncoding))
{
return new DynamicXDocument(streamReader.ReadToEnd());
}
}
if (context.Request.Body.CanSeek)
{
context.Request.Body.Seek(0, SeekOrigin.Begin);
}
var reader = XmlReader.Create(new StreamReader(context.Request.Body, context.Request.Headers.ContentCharsetEncoding));
return XmlSerializerRegistry.Get(objectType).Deserialize(reader);
}
示例3: Init
public void Init(IServiceContext context)
{
Context = context;
host = context.GetPlugin<WemosPlugin>();
InitLastValues();
}
示例4: PopulateDynamicObject
private static dynamic PopulateDynamicObject(IServiceContext context)
{
dynamic instance = new DynamicResult();
foreach (string key in context.Request.QueryString.Keys)
{
IList<string> values = context.Request.QueryString.GetValues(key);
string propertyName = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(key);
try
{
if (values.Count == 1)
{
instance.Add(propertyName, values[0]);
}
else
{
instance.Add(propertyName, values);
}
}
catch (ArgumentException)
{
throw new HttpResponseException(HttpStatusCode.BadRequest, Global.InvalidDynamicPropertyName);
}
}
return instance;
}
示例5: 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");
}
if (serviceContext.Cache == null)
{
throw new InvalidOperationException(Resources.Global.UnableToInitializeCache);
}
string remoteAddress = serviceContext.Request.ServerVariables.RemoteAddress;
if (String.IsNullOrEmpty(remoteAddress))
{
return BehaviorMethodAction.Execute;
}
string cacheKey = String.Concat("throttle-", serviceContext.Request.Url.GetLeftPart(UriPartial.Path), "-", remoteAddress);
if (serviceContext.Cache.Contains(cacheKey))
{
SetStatus((HttpStatusCode) 429, Resources.Global.TooManyRequests);
return BehaviorMethodAction.Stop;
}
serviceContext.Cache.Add(cacheKey, true, DateTime.Now.AddMilliseconds(m_delayInMilliseconds), CachePriority.Low);
return BehaviorMethodAction.Execute;
}
示例6: BindObject
private object BindObject(string name, Type objectType, IServiceContext context)
{
string value = context.Request.Headers.TryGet(GetHeaderName(name));
object changedValue;
return SafeConvert.TryChangeType(value, objectType, out changedValue) ? changedValue : null;
}
示例7: 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)
{
serviceContext.Request.ResourceBag.LoggingEnabled = true;
serviceContext.Response.Output.WriteFormat("Action '{0}' executing", behaviorContext.GetMethodName()).WriteLine(2);
return BehaviorMethodAction.Execute;
}
示例8: MerchelloHelper
/// <summary>
/// Initializes a new instance of the <see cref="MerchelloHelper"/> class.
/// </summary>
/// <param name="serviceContext">
/// The service context.
/// </param>
public MerchelloHelper(IServiceContext serviceContext)
{
Mandate.ParameterNotNull(serviceContext, "ServiceContext cannot be null");
_queryProvider = new Lazy<ICachedQueryProvider>(() => new CachedQueryProvider(serviceContext));
_validationHelper = new Lazy<IValidationHelper>(() => new ValidationHelper());
}
示例9: Execute
public async Task Execute(IResult result, IServiceContext context)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
if (result == null)
{
return;
}
var asyncResult = result as IResultAsync;
if (asyncResult != null)
{
await asyncResult.ExecuteAsync(context, context.Response.GetCancellationToken());
}
else
{
result.Execute(context);
}
TryDisposeResult(result);
}
示例10: PerformQuery
/// <summary>
/// Performs a query on a collection and returns the resulting collection of
/// objects.
/// </summary>
/// <param name="context">The service context.</param>
/// <param name="collection">The collection to perform the query on.</param>
/// <returns>The resulting collection.</returns>
public virtual IEnumerable PerformQuery(IServiceContext context, IQueryable collection)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
if (collection == null)
{
return null;
}
NameValueCollection queryString = context.Request.QueryString.ToNameValueCollection();
TrySetMaxQueryResults(context, queryString);
int count;
List<object> filteredCollection = TryConvertToFilteredCollection(collection, queryString, out count);
object filteredObject = filteredCollection.FirstOrDefault(o => o != null);
Type objectType = filteredObject != null ? filteredObject.GetType() : typeof(object);
if (Attribute.IsDefined(objectType, typeof(CompilerGeneratedAttribute), false))
{
throw new HttpResponseException(HttpStatusCode.InternalServerError, Global.UnsupportedObjectTypeForOData);
}
Tuple<int, int> range = GetContentRanges(queryString, count);
TrySetContentRange(context, filteredCollection, range);
return GenerateFilteredCollection(filteredCollection, objectType);
}
示例11: 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;
}
示例12: Execute
/// <summary>
/// Executes the result against the provided service context.
/// </summary>
/// <param name="context">The service context.</param>
public virtual void Execute(IServiceContext context)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
if (Content == null)
{
return;
}
if (ClearOutput)
{
context.Response.Output.Clear();
}
SetContentType(context);
context.Response.SetCharsetEncoding(context.Request.Headers.AcceptCharsetEncoding);
OutputCompressionManager.FilterResponse(context);
context.Response.Output.Write(Content);
LogResponse();
}
示例13: 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");
}
if (context.Request.Body.CanSeek)
{
context.Request.Body.Seek(0, SeekOrigin.Begin);
}
var streamReader = new StreamReader(context.Request.Body, context.Request.Headers.ContentCharsetEncoding);
var serializer = JsonSerializerFactory.Create();
var reader = new JsonTextReader(streamReader);
if (objectType == typeof(object))
{
return serializer.Deserialize(reader);
}
return serializer.Deserialize(reader, objectType);
}
示例14: RegistrationManager
internal RegistrationManager(
IRegistrationContext registrationContext,
IModuleManager moduleManager,
IPublicRegistrationService publicRegistrationService,
ISdkInformation sdkInformation,
IEnvironmentInformation environmentInformation,
IServiceContext serviceContext,
ISecureRegistrationService secureRegistrationService,
IConfigurationManager configurationManager,
IEventBus eventBus,
IRefreshToken tokenRefresher,
ILogger logger,
IJsonSerialiser serialiser)
{
_registrationContext = registrationContext;
_moduleManager = moduleManager;
_publicRegistrationService = publicRegistrationService;
_sdkInformation = sdkInformation;
_environmentInformation = environmentInformation;
_serviceContext = serviceContext;
_secureRegistrationService = secureRegistrationService;
_configurationManager = configurationManager;
_eventBus = eventBus;
_tokenRefresher = tokenRefresher;
_logger = logger;
_serialiser = serialiser;
}
示例15: BindObject
private static object BindObject(string name, Type objectType, IServiceContext context)
{
string uriValue = context.Request.QueryString.TryGet(name);
object changedValue;
return SafeConvert.TryChangeType(uriValue, objectType, out changedValue) ? changedValue : null;
}