本文整理汇总了C#中Microsoft.AspNet.SignalR.Hosting.HostContext类的典型用法代码示例。如果您正苦于以下问题:C# HostContext类的具体用法?C# HostContext怎么用?C# HostContext使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
HostContext类属于Microsoft.AspNet.SignalR.Hosting命名空间,在下文中一共展示了HostContext类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SendUrlTriggersReceivedEvent
public void SendUrlTriggersReceivedEvent()
{
var tcs = new TaskCompletionSource<string>();
var request = new Mock<IRequest>();
var form = new NameValueCollection();
form["data"] = "This is my data";
var qs = new NameValueCollection();
qs["connectionId"] = "1";
request.Setup(m => m.QueryString).Returns(new NameValueCollectionWrapper(qs));
request.Setup(m => m.ReadForm()).Returns(Task.FromResult<INameValueCollection>(new NameValueCollectionWrapper(form)));
request.Setup(m => m.LocalPath).Returns("/test/echo/send");
var counters = new Mock<IPerformanceCounterManager>();
var heartBeat = new Mock<ITransportHeartbeat>();
var json = JsonUtility.CreateDefaultSerializer();
var hostContext = new HostContext(request.Object, null);
var transportConnection = new Mock<ITransportConnection>();
var traceManager = new Mock<ITraceManager>();
counters.SetupGet(m => m.ConnectionsConnected).Returns(new NoOpPerformanceCounter());
traceManager.Setup(m => m[It.IsAny<string>()]).Returns(new System.Diagnostics.TraceSource("foo"));
var transport = new Mock<ForeverTransport>(hostContext, json, heartBeat.Object, counters.Object, traceManager.Object)
{
CallBase = true
};
transport.Object.Received = data =>
{
tcs.TrySetResult(data);
return TaskAsyncHelper.Empty;
};
transport.Object.ProcessRequest(transportConnection.Object).Wait();
Assert.Equal("This is my data", tcs.Task.Result);
}
示例2: AbortUrlTriggersConnectionAbort
public void AbortUrlTriggersConnectionAbort()
{
var request = new Mock<IRequest>();
var qs = new NameValueCollection();
request.Setup(m => m.QueryString).Returns(qs);
request.Setup(m => m.Url).Returns(new Uri("http://test/echo/abort"));
string abortedConnectionId = null;
var counters = new Mock<IPerformanceCounterManager>();
var heartBeat = new Mock<ITransportHeartbeat>();
var json = new JsonNetSerializer();
var hostContext = new HostContext(request.Object, null);
var transportConnection = new Mock<ITransportConnection>();
var traceManager = new Mock<ITraceManager>();
traceManager.Setup(m => m[It.IsAny<string>()]).Returns(new System.Diagnostics.TraceSource("foo"));
transportConnection.Setup(m => m.Send(It.IsAny<ConnectionMessage>()))
.Callback<ConnectionMessage>(m =>
{
abortedConnectionId = m.Signal;
var command = m.Value as Command;
Assert.NotNull(command);
Assert.Equal(CommandType.Abort, command.CommandType);
})
.Returns(TaskAsyncHelper.Empty);
var transport = new Mock<ForeverTransport>(hostContext, json, heartBeat.Object, counters.Object, traceManager.Object)
{
CallBase = true
};
transport.Object.ConnectionId = "1";
transport.Object.ProcessRequest(transportConnection.Object).Wait();
Assert.Equal("c-1", abortedConnectionId);
}
示例3: Invoke
public Task Invoke(IDictionary<string, object> environment)
{
var serverRequest = new ServerRequest(environment);
var serverResponse = new ServerResponse(environment);
var hostContext = new HostContext(serverRequest, serverResponse);
// Add CORS support
var origins = serverRequest.RequestHeaders.GetHeaders("Origin");
if (origins != null && origins.Any(origin => !String.IsNullOrEmpty(origin)))
{
serverResponse.ResponseHeaders["Access-Control-Allow-Origin"] = origins;
serverResponse.ResponseHeaders["Access-Control-Allow-Credentials"] = AllowCredentialsTrue;
}
hostContext.Items[HostConstants.SupportsWebSockets] = LazyInitializer.EnsureInitialized(
ref _supportWebSockets,
ref _supportWebSocketsInitialized,
ref _supportWebSocketsLock,
() => environment.SupportsWebSockets());
hostContext.Items[HostConstants.ShutdownToken] = environment.GetShutdownToken();
hostContext.Items[HostConstants.DebugMode] = environment.GetIsDebugEnabled();
serverRequest.DisableRequestBuffering();
serverResponse.DisableResponseBuffering();
_connection.Initialize(_resolver, hostContext);
return _connection.ProcessRequest(hostContext);
}
示例4: TransportDisconnectBase
protected TransportDisconnectBase(HostContext context, ITransportHeartbeat heartbeat, IPerformanceCounterManager performanceCounterManager, ITraceManager traceManager)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
if (heartbeat == null)
{
throw new ArgumentNullException("heartbeat");
}
if (performanceCounterManager == null)
{
throw new ArgumentNullException("performanceCounterManager");
}
if (traceManager == null)
{
throw new ArgumentNullException("traceManager");
}
_context = context;
_heartbeat = heartbeat;
_counters = performanceCounterManager;
// Queue to protect against overlapping writes to the underlying response stream
WriteQueue = new TaskQueue();
_trace = traceManager["SignalR.Transports." + GetType().Name];
}
示例5: ProcessRequest
public override Task ProcessRequest(HostContext context)
{
string alterWhen = "____Never____";
int statusCode = 200;
if (!String.IsNullOrEmpty(context.Request.QueryString["alterWhen"]))
{
alterWhen = context.Request.QueryString["alterWhen"];
}
if (!String.IsNullOrEmpty(context.Request.QueryString["statusCode"]))
{
statusCode = Int32.Parse(context.Request.QueryString["statusCode"]);
}
var owinRequest = new OwinRequest(context.Environment);
if (owinRequest.Path.Value.Contains("/" + alterWhen))
{
var response = new OwinResponse(context.Environment);
// Alter status code
response.StatusCode = statusCode;
using (var sw = new StreamWriter(response.Body))
{
sw.WriteLine("Hello world");
sw.Flush();
}
return TaskAsyncHelper.Empty;
}
return base.ProcessRequest(context);
}
示例6: Invoke
public Task Invoke(IDictionary<string, object> environment)
{
var serverRequest = new ServerRequest(environment);
var serverResponse = new ServerResponse(environment);
var hostContext = new HostContext(serverRequest, serverResponse);
string origin = serverRequest.RequestHeaders.GetHeader("Origin");
if (_configuration.EnableCrossDomain)
{
// Add CORS response headers support
if (!String.IsNullOrEmpty(origin))
{
serverResponse.ResponseHeaders.SetHeader("Access-Control-Allow-Origin", origin);
serverResponse.ResponseHeaders.SetHeader("Access-Control-Allow-Credentials", "true");
}
}
else
{
string callback = serverRequest.QueryString["callback"];
// If it's a JSONP request and we're not allowing cross domain requests then block it
// If there's an origin header and it's not a same origin request then block it.
if (!String.IsNullOrEmpty(callback) ||
(!String.IsNullOrEmpty(origin) && !IsSameOrigin(serverRequest.Url, origin)))
{
return EndResponse(environment, 403, "Forbidden");
}
}
// Add the nosniff header for all responses to prevent IE from trying to sniff mime type from contents
serverResponse.ResponseHeaders.SetHeader("X-Content-Type-Options", "nosniff");
hostContext.Items[HostConstants.SupportsWebSockets] = LazyInitializer.EnsureInitialized(
ref _supportWebSockets,
ref _supportWebSocketsInitialized,
ref _supportWebSocketsLock,
() => environment.SupportsWebSockets());
hostContext.Items[HostConstants.ShutdownToken] = environment.GetShutdownToken();
hostContext.Items[HostConstants.DebugMode] = environment.GetIsDebugEnabled();
serverRequest.DisableRequestCompression();
serverResponse.DisableResponseBuffering();
_connection.Initialize(_configuration.Resolver, hostContext);
if (!_connection.Authorize(serverRequest))
{
// If we failed to authorize the request then return a 403 since the request
// can't do anything
return EndResponse(environment, 403, "Forbidden");
}
else
{
return _connection.ProcessRequest(hostContext);
}
}
示例7: LongPollingTransport
public LongPollingTransport(HostContext context, IDependencyResolver resolver)
: this(context,
resolver.Resolve<IJsonSerializer>(),
resolver.Resolve<ITransportHeartbeat>(),
resolver.Resolve<IPerformanceCounterManager>(),
resolver.Resolve<ITraceManager>())
{
}
示例8: ForeverTransport
protected ForeverTransport(HostContext context, IDependencyResolver resolver)
: this(context,
resolver.Resolve<IJsonSerializer>(),
resolver.Resolve<ITransportHeartbeat>(),
resolver.Resolve<IPerformanceCounterManager>(),
resolver.Resolve<ITraceManager>())
{
}
示例9: ForeverFrameTransportEscapesTagsWithPersistentResponse
public void ForeverFrameTransportEscapesTagsWithPersistentResponse(string data, string expected)
{
var request = MockRequest();
var response = new CustomResponse();
var context = new HostContext(request.Object, response);
var fft = new ForeverFrameTransport(context, new DefaultDependencyResolver());
AssertEscaped(fft, response, GetWrappedResponse(data), expected);
}
示例10: WebSocketTransport
public WebSocketTransport(HostContext context,
IJsonSerializer serializer,
ITransportHeartbeat heartbeat,
IPerformanceCounterManager performanceCounterWriter,
ITraceManager traceManager)
: base(context, serializer, heartbeat, performanceCounterWriter, traceManager)
{
_context = context;
}
示例11: ForeverFrameTransportEscapesTags
public void ForeverFrameTransportEscapesTags(string data, string expected)
{
var request = new Mock<IRequest>();
var response = new CustomResponse();
var context = new HostContext(request.Object, response);
var fft = new ForeverFrameTransport(context, new DefaultDependencyResolver());
AssertEscaped(fft, response, data, expected);
}
示例12: AvoidDeadlockIfCancellationTokenTriggeredBeforeSubscribing
public void AvoidDeadlockIfCancellationTokenTriggeredBeforeSubscribing()
{
var response = new Mock<IResponse>();
response.Setup(m => m.CancellationToken).Returns(CancellationToken.None);
var request = new Mock<IRequest>();
var qs = new NameValueCollection();
qs["connectionId"] = "1";
request.Setup(m => m.QueryString).Returns(qs);
request.Setup(m => m.Url).Returns(new Uri("http://test/echo/connect"));
var counters = new Mock<IPerformanceCounterManager>();
var heartBeat = new Mock<ITransportHeartbeat>();
var json = new JsonNetSerializer();
var hostContext = new HostContext(request.Object, response.Object);
var transportConnection = new Mock<ITransportConnection>();
var traceManager = new Mock<ITraceManager>();
traceManager.Setup(m => m[It.IsAny<string>()]).Returns(new System.Diagnostics.TraceSource("foo"));
Func<PersistentResponse, object, Task<bool>> callback = null;
object state = null;
var disposable = new DisposableAction(() =>
{
callback(new PersistentResponse() { Terminal = true }, state);
});
transportConnection.Setup(m => m.Receive(It.IsAny<string>(),
It.IsAny<Func<PersistentResponse, object, Task<bool>>>(),
It.IsAny<int>(),
It.IsAny<object>())).Callback<string, Func<PersistentResponse, object, Task<bool>>, int, object>((id, cb, max, st) =>
{
callback = cb;
state = st;
})
.Returns(disposable);
var transport = new Mock<ForeverTransport>(hostContext, json, heartBeat.Object, counters.Object, traceManager.Object)
{
CallBase = true
};
var wh = new ManualResetEventSlim();
transport.Object.BeforeCancellationTokenCallbackRegistered = () =>
{
// Trip the cancellation token
transport.Object.End();
};
// Act
Task.Factory.StartNew(() =>
{
transport.Object.ProcessRequest(transportConnection.Object);
wh.Set();
});
Assert.True(wh.Wait(TimeSpan.FromSeconds(2)), "Dead lock!");
}
示例13: WebSocketTransport
public WebSocketTransport(HostContext context,
IDependencyResolver resolver)
: this(context,
resolver.Resolve<JsonSerializer>(),
resolver.Resolve<ITransportHeartbeat>(),
resolver.Resolve<IPerformanceCounterManager>(),
resolver.Resolve<ITraceManager>(),
resolver.Resolve<IConfigurationManager>().MaxIncomingWebSocketMessageSize)
{
}
示例14: LongPollingTransport
public LongPollingTransport(HostContext context,
JsonSerializer jsonSerializer,
ITransportHeartbeat heartbeat,
IPerformanceCounterManager performanceCounterManager,
ITraceManager traceManager,
IConfigurationManager configurationManager)
: base(context, jsonSerializer, heartbeat, performanceCounterManager, traceManager)
{
_configurationManager = configurationManager;
}
示例15: TestLongPollingTransport
private TestLongPollingTransport(
HostContext context,
JsonSerializer json,
ITransportHeartbeat heartBeat,
IPerformanceCounterManager counters,
ITraceManager traceManager,
IConfigurationManager configuarionManager)
: base(context, json, heartBeat, counters, traceManager, configuarionManager)
{
}