.net core websockets obtain DbContext - c#

I am testing out .net core and making a small sample app using .net core + websockets to push some data into my app. I want to save this data in the database using a dbcontext.
However I have issues getting the dbcontext in my websocket handler. So how can I create a dbcontext to use.
My startup Configure method contains this:
...
app.Map("/ws", WSHandler.Map);
...
This is my WSHandler class that implements actually reading from the incoming connection. I need a DbContext here that I can use to read/write from the database.
/// <summary>
/// Handler for an incoming websocket client
/// </summary>
public class WSHandler {
/// <summary>
/// Max size in bytes of an incoming/outgoing message
/// </summary>
public const int BufferSize = 4096;
/// <summary>
/// The socket of the current connection
/// </summary>
WebSocket socket;
/// <summary>
/// Constructor, assign socket to current instance and adds socket to ConnectedClients.
/// </summary>
/// <param name="socket"></param>
WSHandler(WebSocket socket) {
this.socket = socket;
}
/// <summary>
/// Configure app to use websockets and register handler.
/// </summary>
/// <param name="app"></param>
public static void Map(IApplicationBuilder app) {
app.UseWebSockets();
app.Use((WSHandler.Acceptor);
}
/// <summary>
/// Accept HttpContext and handles constructs instance of WSHandler.
/// </summary>
/// <param name="hc">The HttpContext</param>
/// <param name="n">Task n</param>
/// <returns></returns>
static async Task Acceptor(HttpContext hc, Func<Task> n) {
if (hc.WebSockets.IsWebSocketRequest == false) {
return;
}
var socket = await hc.WebSockets.AcceptWebSocketAsync();
var h = new WSHandler(socket);
await h.Loop();
}
/// <summary>
/// Wait's for incoming messages
/// </summary>
/// <returns></returns>
async Task Loop() {
var buffer = new Byte[BufferSize];
ArraySegment<Byte> segment = new ArraySegment<byte>(buffer);
while (this.socket.State == WebSocketState.Open) {
WebSocketReceiveResult result = null;
do {
result = await socket.ReceiveAsync(segment, CancellationToken.None);
} while (result.EndOfMessage == false);
// do something with message here. I want to save parse and save to database
}
}
}

Since there is some interests in this post I have added the solution I used.
You have access to all services via the HttpContext. So what I did is get the context options from this service and then create context when I need them. Be aware though that long living contexts are not advised and if an error occurs a DbContext is no longer useable. In the end I implemented a different database caching layer and write to that instead of using DbContext itself in the websocket handler.
Here is the code above extended to create DbContexts. I have not tested it since right now I don't have visual studio available...
<summary>
/// Handler for an incoming websocket client
/// </summary>
public class WSHandler {
/// <summary>
/// Max size in bytes of an incoming/outgoing message
/// </summary>
public const int BufferSize = 4096;
/// <summary>
/// The socket of the current connection
/// </summary>
WebSocket socket;
/// <summary>
/// The options to create DbContexts with.
/// </summary>
DbContextOptions<AssemblyServerContext> ctxOpts;
/// <summary>
/// Constructor, assign socket to current instance and adds socket to ConnectedClients.
/// </summary>
/// <param name="socket"></param>
/// <param name="ctxOpts"></param>
WSHandler(WebSocket socket, DbContextOptions<AssemblyServerContext> ctxOpts) {
this.ctxOpts = ctxOpts;
this.socket = socket;
}
/// <summary>
/// Configure app to use websockets and register handler.
/// </summary>
/// <param name="app"></param>
public static void Map(IApplicationBuilder app) {
app.UseWebSockets();
app.Use((WSHandler.Acceptor);
}
/// <summary>
/// Accept HttpContext and handles constructs instance of WSHandler.
/// </summary>
/// <param name="hc">The HttpContext</param>
/// <param name="n">Task n</param>
/// <returns></returns>
static async Task Acceptor(HttpContext hc, Func<Task> n) {
if (hc.WebSockets.IsWebSocketRequest == false) {
return;
}
var socket = await hc.WebSockets.AcceptWebSocketAsync();
var ctxOptions = hc.RequestServices.GetService<DbContextOptions<AssemblyServerContext>>();
var h = new WSHandler(socket, ctxOptions);
await h.Loop();
}
/// <summary>
/// Wait's for incoming messages
/// </summary>
/// <returns></returns>
async Task Loop() {
var buffer = new Byte[BufferSize];
ArraySegment<Byte> segment = new ArraySegment<byte>(buffer);
while (this.socket.State == WebSocketState.Open) {
WebSocketReceiveResult result = null;
do {
result = await socket.ReceiveAsync(segment, CancellationToken.None);
} while (result.EndOfMessage == false);
// do something with message here. I want to save parse and save to database
using (var ctx = new AssemblyServerContext(ctxOpts)) {
}
}
}
}

Related

Is there a way to dynamically assert the Responses with CompareToSet in SpecFlow

I have two Data Service EnquiryType and EnquirySource the Common Step between them is when I try to assert the response for both in there feature files, currently I am distinguishing the Scenario Step with the Data Service name, but I want to separate out the assertion step and use that for both Data Service.
Current Logic
/// <summary>
/// The step that gets all of the enquiry types.
/// </summary>
/// <returns></returns>
[Given(#"I request all enquiry types")]
public async Task RequestAllEnquiryTypes()
{
try
{
var results = await EnquiryTypesApi.GetEnquiryTypes(enquiryTypeData);
enquiryTypes = new List<EnquiryTypeModel>(results.Results);
FeatureSteps.statusCode = results.StatusCode;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
/// <summary>
/// Makes sure that the retrieved enquiries types match the expected data.
/// </summary>
/// <param name="data">The expected enquiry type data.</param>
[Then("The following Enquiry Type result is returned:")]
public void ThenTheFollowingResultIsReturned(TechTalk.SpecFlow.Table data)
{
data.CompareToSet<EnquiryTypeModel>(enquiryTypes);
}
/// <summary>
/// The step that gets all of the enquiry sources.
/// </summary>
/// <returns></returns>
[Given(#"I request all enquiry sources")]
public async Task RequestAllEnquirySources()
{
try
{
var results = await EnquirySourcesApi.GetEnquirySources(enquirySourceData);
enquirySources = new List<EnquirySourceModel>(results.Results);
FeatureSteps.statusCode = results.StatusCode;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
/// <summary>
/// Makes sure that the retrieved enquiries sources match the expected data.
/// </summary>
/// <param name="data">The expected enquiry source data.</param>
[Then("The following Enquiry Source result is returned:")]
public void ThenTheFollowingResultIsReturned(TechTalk.SpecFlow.Table data)
{
data.CompareToSet<EnquirySourceModel>(enquirySources);
}
What I want to achieve
/// <summary>
/// Makes sure that the Result match the expected data.
/// </summary>
/// <param name="data">The expected data.</param>
[Then("The following result is returned:")]
public void ThenTheFollowingResultIsReturned(TechTalk.SpecFlow.Table data)
{
data.CompareToSet(dynamicObject);
}

UnixEpochDateTimeConverter - Cannot get the value of a token type 'Number' as a string

I'm trying to convert milliseconds timestamp to a DateTime, but it throws an exception at reader.GetString():
System.InvalidOperationException: 'Cannot get the value of a token type 'Number' as a string.'
It means I'm trying to read it as a string when it is a value. If I replace it with reader.GetInt64() or reader.GetDouble(), it works, but the reason I'm writing that question here is because I took this class from one of dotnet's open source projects on GitHub and I doubt that I really need to change the class. I believe the problem could be in my JsonSerializerOptions.
JSON response
https://pastebin.com/9AjwSp5L (Pastebin because it exceeds SO's limits)
Snippet
using System.Globalization;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
namespace QSGEngine.Web.Platforms.Converters
{
/// <summary>
/// Used for deserializing json with Microsoft date format.
/// </summary>
internal sealed class UnixEpochDateTimeConverter : JsonConverter<DateTime>
{
private static readonly DateTime EpochDateTime = new(1970, 1, 1, 0, 0, 0);
private static readonly Regex Regex = new("^/Date\\(([^+-]+)\\)/$", RegexOptions.CultureInvariant);
public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var formatted = reader.GetString();
var match = Regex.Match(formatted!);
return !match.Success || !long.TryParse(match.Groups[1].Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out var unixTime) ?
throw new JsonException() : EpochDateTime.AddMilliseconds(unixTime);
}
public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
{
var unixTime = Convert.ToInt64((value - EpochDateTime).TotalMilliseconds);
var formatted = FormattableString.Invariant($"/Date({unixTime})/");
writer.WriteStringValue(formatted);
}
}
}
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using Ardalis.GuardClauses;
using QSGEngine.Web.Platforms.Extensions;
using RestSharp;
namespace QSGEngine.Web.Platforms.Binance;
/// <summary>
/// Binance REST API implementation.
/// </summary>
internal class BinanceRestApiClient : IDisposable
{
/// <summary>
/// The base point url.
/// </summary>
private const string BasePointUrl = "https://api.binance.com";
/// <summary>
/// The key header.
/// </summary>
private const string KeyHeader = "X-MBX-APIKEY";
/// <summary>
/// REST Client.
/// </summary>
private readonly IRestClient _restClient = new RestClient(BasePointUrl);
/// <summary>
/// Initializes a new instance of the <see cref="BinanceRestApiClient"/> class.
/// </summary>
/// <param name="apiKey">Binance API key.</param>
/// <param name="apiSecret">Binance Secret key.</param>
public BinanceRestApiClient(string apiKey, string apiSecret)
{
Guard.Against.NullOrWhiteSpace(apiKey, nameof(apiKey));
Guard.Against.NullOrWhiteSpace(apiSecret, nameof(apiSecret));
ApiKey = apiKey;
ApiSecret = apiSecret;
}
/// <summary>
/// The API key.
/// </summary>
public string ApiKey { get; }
/// <summary>
/// The secret key.
/// </summary>
public string ApiSecret { get; }
/// <summary>
/// Gets the total account cash balance for specified account type.
/// </summary>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public AccountInformation? GetBalances()
{
var queryString = $"timestamp={GetNonce()}";
var endpoint = $"/api/v3/account?{queryString}&signature={AuthenticationToken(queryString)}";
var request = new RestRequest(endpoint, Method.GET);
request.AddHeader(KeyHeader, ApiKey);
var response = ExecuteRestRequest(request);
if (response.StatusCode != HttpStatusCode.OK)
{
throw new Exception($"{nameof(BinanceRestApiClient)}: request failed: [{(int)response.StatusCode}] {response.StatusDescription}, Content: {response.Content}, ErrorMessage: {response.ErrorMessage}");
}
var jsonSerializerOptions = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
NumberHandling = JsonNumberHandling.AllowReadingFromString
};
var deserialize = JsonSerializer.Deserialize<AccountInformation>(response.Content, jsonSerializerOptions);
return deserialize;
}
/// <summary>
/// If an IP address exceeds a certain number of requests per minute
/// HTTP 429 return code is used when breaking a request rate limit.
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
private IRestResponse ExecuteRestRequest(IRestRequest request)
{
const int maxAttempts = 10;
var attempts = 0;
IRestResponse response;
do
{
// TODO: RateLimiter
//if (!_restRateLimiter.WaitToProceed(TimeSpan.Zero))
//{
// Log.Trace("Brokerage.OnMessage(): " + new BrokerageMessageEvent(BrokerageMessageType.Warning, "RateLimit",
// "The API request has been rate limited. To avoid this message, please reduce the frequency of API calls."));
// _restRateLimiter.WaitToProceed();
//}
response = _restClient.Execute(request);
// 429 status code: Too Many Requests
} while (++attempts < maxAttempts && (int)response.StatusCode == 429);
return response;
}
/// <summary>
/// Timestamp in milliseconds.
/// </summary>
/// <returns>The current timestamp in milliseconds.</returns>
private long GetNonce()
{
return DateTime.UtcNow.ToTimestamp();
}
/// <summary>
/// Creates a signature for signed endpoints.
/// </summary>
/// <param name="payload">The body of the request.</param>
/// <returns>A token representing the request params.</returns>
private string AuthenticationToken(string payload)
{
using var hmac = new HMACSHA256(Encoding.UTF8.GetBytes(ApiSecret));
var computedHash = hmac.ComputeHash(Encoding.UTF8.GetBytes(payload));
return BitConverter.ToString(computedHash).Replace("-", "").ToLowerInvariant();
}
/// <summary>
/// The standard dispose destructor.
/// </summary>
~BinanceRestApiClient() => Dispose(false);
/// <summary>
/// Returns true if it is already disposed.
/// </summary>
public bool IsDisposed { get; private set; }
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
/// <param name="disposing">If this method is called by a user's code.</param>
private void Dispose(bool disposing)
{
if (IsDisposed) return;
if (disposing)
{
}
IsDisposed = true;
}
/// <summary>
/// Throw if disposed.
/// </summary>
/// <exception cref="ObjectDisposedException"></exception>
private void ThrowIfDisposed()
{
if (IsDisposed)
{
throw new ObjectDisposedException($"{nameof(BinanceRestApiClient)} has been disposed.");
}
}
}
using System.Text.Json.Serialization;
using QSGEngine.Web.Platforms.Converters;
namespace QSGEngine.Web.Platforms.Binance;
/// <summary>
/// Information about the account.
/// </summary>
public class AccountInformation
{
/// <summary>
/// Commission percentage to pay when making trades.
/// </summary>
public decimal MakerCommission { get; set; }
/// <summary>
/// Commission percentage to pay when taking trades.
/// </summary>
public decimal TakerCommission { get; set; }
/// <summary>
/// Commission percentage to buy when buying.
/// </summary>
public decimal BuyerCommission { get; set; }
/// <summary>
/// Commission percentage to buy when selling.
/// </summary>
public decimal SellerCommission { get; set; }
/// <summary>
/// Boolean indicating if this account can trade.
/// </summary>
public bool CanTrade { get; set; }
/// <summary>
/// Boolean indicating if this account can withdraw.
/// </summary>
public bool CanWithdraw { get; set; }
/// <summary>
/// Boolean indicating if this account can deposit.
/// </summary>
public bool CanDeposit { get; set; }
/// <summary>
/// The time of the update.
/// </summary>
[JsonConverter(typeof(UnixEpochDateTimeConverter))]
public DateTime UpdateTime { get; set; }
/// <summary>
/// The type of the account.
/// </summary>
public string? AccountType { get; set; }
/// <summary>
/// List of assets with their current balances.
/// </summary>
public IEnumerable<Balance>? Balances { get; set; }
/// <summary>
/// Permission types.
/// </summary>
public IEnumerable<string>? Permissions { get; set; }
}
/// <summary>
/// Information about an asset balance.
/// </summary>
public class Balance
{
/// <summary>
/// The asset this balance is for.
/// </summary>
public string? Asset { get; set; }
/// <summary>
/// The amount that isn't locked in a trade.
/// </summary>
public decimal Free { get; set; }
/// <summary>
/// The amount that is currently locked in a trade.
/// </summary>
public decimal Locked { get; set; }
/// <summary>
/// The total balance of this asset (Free + Locked).
/// </summary>
public decimal Total => Free + Locked;
}
internal static class DateTimeExtensions
{
/// <summary>
/// To Unix Timestamp in milliseconds.
/// </summary>
/// <param name="datetime">The <see cref="DateTime"/>.</param>
/// <returns>The unix timestamp</returns>
public static long ToTimestamp(this DateTime datetime) => new DateTimeOffset(datetime).ToUnixTimeMilliseconds();
}
Let's try to make your converter a bit more forgiving:
public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if(reader.TryGetInt64(out long x))
return EpochDateTime.AddMilliseconds(x);
var formatted = reader.GetString();
var match = Regex.Match(formatted!);
return !match.Success || !long.TryParse(match.Groups[1].Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out var unixTime) ?
throw new JsonException() : EpochDateTime.AddMilliseconds(unixTime);
}

Entity Framework : deleting child entities

I am using EF, but I have lazy loading disabled. Instead I am using eager loading, so I created my own service:
/// <summary>
/// Generic service for entity framework
/// </summary>
/// <typeparam name="T">An entity model</typeparam>
public class Service<T> : IService<T> where T : class
{
// Create our private properties
private readonly DbContext _context;
private readonly DbSet<T> _dbEntitySet;
/// <summary>
/// Default constructor
/// </summary>
/// <param name="context">The database context</param>
public Service(DbContext context)
{
// Assign our context and entity set
_context = context ?? throw new ArgumentNullException("context");
_dbEntitySet = context.Set<T>();
}
/// <summary>
/// Gets all the entities
/// </summary>
/// <param name="includes">Option includes for eager loading</param>
/// <returns></returns>
public IQueryable<T> List(params string[] includes)
{
// Create a query
IQueryable<T> query = _dbEntitySet;
// For each include, append to our query
if (includes != null)
foreach (var include in includes)
query = query.Include(include);
// Return our query
return query;
}
/// <summary>
/// Creates an entity
/// </summary>
/// <param name="model"></param>
public void Create(T model) => _dbEntitySet.Add(model);
/// <summary>
/// Updates an entity
/// </summary>
/// <param name="model"></param>
public void Update(T model) => _context.Entry<T>(model).State = EntityState.Modified;
/// <summary>
/// Removes an entity
/// </summary>
/// <param name="model"></param>
public void Remove(T model) => _context.Entry<T>(model).State = EntityState.Deleted;
/// <summary>
/// Saves the database context changes
/// </summary>
/// <returns></returns>
public async Task SaveChangesAsync()
{
try
{
// Save the changes to the database
await _context.SaveChangesAsync();
}
catch (DbEntityValidationException ex)
{
// Retrieve the error messages as a list of strings.
var errorMessages = ex.EntityValidationErrors.SelectMany(x => x.ValidationErrors).Select(x => x.ErrorMessage);
// Join the list to a single string.
var fullErrorMessage = string.Join("; ", errorMessages);
// Combine the original exception message with the new one.
var exceptionMessage = string.Concat(ex.Message, " The validation errors are: ", fullErrorMessage);
// Throw a new DbEntityValidationException with the improved exception message.
throw new DbEntityValidationException(exceptionMessage, ex.EntityValidationErrors);
}
catch (DbUpdateException ex)
{
throw;
}
}
/// <summary>
/// Executes a stored procedure in sql
/// </summary>
/// <param name="procedure">The name of the sproc</param>
/// <param name="parameters">the sql params for the sproc</param>
/// <returns></returns>
public DbRawSqlQuery<T> ExecuteProcedure(string procedure, List<SqlParameter> parameters)
{
var results = _context.Database.SqlQuery<T>($"exec {procedure} { CreateQueryStringFromParams(parameters) }");
return results;
}
/// <summary>
/// Dispose
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Creates the input string to run sprocs in sql with EF by converting the sql params into a nice string
/// </summary>
/// <param name="parameters"></param>
/// <returns></returns>
private static string CreateQueryStringFromParams(IEnumerable<SqlParameter> parameters)
{
var response = "";
var list = parameters as IList<SqlParameter> ?? parameters.ToList();
var length = list.Count;
for (var i = 0; i < length; i++)
{
response += $"{list[i].ParameterName}=\"{list[i].Value}\"";
if (i != length - 1)
response += ", ";
}
return response;
}
/// <summary>
/// Disposes of any attached resources
/// </summary>
/// <param name="disposing">A boolean indicating whether the object is being disposed</param>
protected virtual void Dispose(bool disposing)
{
// If we are disposing, dispose of our context
if (disposing)
_context.Dispose();
}
}
Each service then inherits this class:
/// <summary>
/// Handles all Group related methods
/// </summary>
public class GroupService : Service<Group>, IGroupService
{
/// <summary>
/// The default constructor
/// </summary>
/// <param name="unitOfWork"></param>
public GroupService(DbContext context) : base(context)
{
}
/// <summary>
/// Lists groups by category
/// </summary>
/// <param name="categoryId">The id of the category</param>
/// <param name="includes"></param>
/// <returns></returns>
public IQueryable<Group> List(int categoryId, params string[] includes) => List(includes).Where(m => m.CategoryId == categoryId);
/// <summary>
/// Gets a single Group by id
/// </summary>
/// <param name="id">The id of the Group</param>
/// <returns></returns>
public async Task<Group> GetAsync(int id, params string[] includes) => await List(includes).Where(model => model.Id == id).SingleOrDefaultAsync();
}
Each "entity" has a class similar to this GroupService.
I also have providers for each entity type too and here is my delete method:
/// <summary>
/// Delete a Group
/// </summary>
/// <param name="id">The Group id</param>
/// <returns></returns>
public async Task<bool> DeleteAsync(int id)
{
// Get our model
var model = await _service.GetAsync(id, "Questions");
// For each question, remove from the database
if (model.Questions != null)
foreach (var question in model.Questions.ToList())
if (!await _questionProvider.Value.DeleteAsync(question.Id, false))
throw new Exception("Failed to delete the questions");
// Update our Questions
model.Questions = null;
// Save our model
_service.Remove(model);
// Save the database changes
await _service.SaveChangesAsync();
// Return true
return true;
}
As you can see, I just pull back the questions.
If there are some questions, then I invoke the questionProvider's delete method, which is very similar:
/// <summary>
/// Delete a question
/// </summary>
/// <param name="id">The question id</param>
/// <param name="saveChanges">Saves the changes to the database after the delete (default true)</param>
/// <returns></returns>
public async Task<bool> DeleteAsync(int id, bool saveChanges = true)
{
// Get our model
var model = await _service.GetAsync(id, "Answers");
// For each answer, delete from the database
if (model.Answers != null)
foreach (var answer in model.Answers.ToList())
if (!await _answerProvider.Value.DeleteAsync(answer.Id, false))
throw new Exception("Failed to delete the answers");
// Update our Answers
model.Answers = null;
// Save our model
_service.Remove(model);
// Save the database changes
if (saveChanges) await _service.SaveChangesAsync();
// Return true
return true;
}
As you can see, I do not save the context changes until all children have been removed. Now I must point out that I am not removing the child directly from the parent. Instead I am removing the entity from it's own collection and then setting the property to null. After it is all done I save the changes, but I am getting this error:
The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a relationship, the related foreign-key property is set to a null value. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted.
Does anyone know how I can delete the entities in a method similar to what I am trying to achieve?
This was rather easy to fix after reading some other people with similar issues. I just changed one line of code in my Service.cs from:
public void Remove(T model) => _context.Entry<T>(model).State = EntityState.Deleted;
to:
public void Remove(T model) => _dbEntitySet.Remove(model);
And that worked.
Update
I also found that the eager load needs to include all the navigation properties that will be affected.
So if I was to delete a Question which has Answers and Answers have Formulas, the Get invocation would have to be:
var model = await _service.GetAsync(id, "Answers.Formulas");
If you don't include that, you will get an error.

access Session variable from aspx into UserControl

I have a Session["Name"] in my aspx.cs file
how to access it from the UserControl codebehind on the same page
Thanks,
try like this:
string name= HttpContext.Current.Session["Name"].ToString();
or
string name=Session["Name"].ToString();
You need to create an object from that control.
Without see a little bit of the code, it would be hard to tell.
Maybe the session is timing out
Is there an error happing that causes the session state to be clear, or an logout
Modifying the web.config or many files can cause the application to restart.
Finally I have to ask:
- Is session disabled in some way? Does it work between other pages? Maybe it is disabled for Ajax requests?
- Is is maybe a magic string error.
Personally, to avoid magic string errors, I use a Session Wrapper:
/// <summary>
/// The session manager
/// </summary>
public sealed class SessionManager
{
#region ISessionManager Members
/// <summary>
/// Clears the session.
/// </summary>
public void ClearSession()
{
HttpContext.Current.Session.Clear();
HttpContext.Current.Session.Abandon(); //Abandon ends the entire session (the user gets a new SessionId)
}
/// <summary>
/// Gets or sets the current employe.
/// </summary>
/// <value>The current employe.</value>
public EmployeDto CurrentEmploye
{
get { return Get<EmployeDto>(); }
set { Add(value); }
}
/// <summary>
/// Gets or sets the parameters.
/// </summary>
/// <value>The parameters.</value>
public IList<ParameterDto> Parameters
{
get { return Get<List<ParameterDto>>() ?? new List<ParameterDto>(); }
set { Add(value); }
}
#endregion
#region Methods
/// <summary>
/// Adds the specified key.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="value">The value.</param>
/// <param name="key">The key.</param>
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
/// <exception cref="System.ArgumentNullException">key</exception>
/// <exception cref="System.Exception">Session elements cannot be added when session is disabled.</exception>
public static bool Add<T>(T value, [CallerMemberName] string key = null)
{
if (key == null) throw new ArgumentNullException("key");
HttpContext current = HttpContext.Current;
if (current == null)
{
return false;
}
if (current.Session.Mode == SessionStateMode.Off)
{
throw new Exception("Session elements cannot be added when session is disabled.");
}
current.Session.Add(key, value);
return true;
}
/// <summary>
/// Gets the specified key.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key">The key.</param>
/// <returns>``0.</returns>
/// <exception cref="System.ArgumentNullException">key</exception>
/// <exception cref="System.Exception">Session elements cannot be added when session is disabled.</exception>
public static T Get<T>([CallerMemberName] string key = null) where T : class
{
if (key == null) throw new ArgumentNullException("key");
HttpContext current = HttpContext.Current;
if (current.Session.Mode == SessionStateMode.Off)
{
throw new Exception("Session elements cannot be added when session is disabled.");
}
return current.Session[key] as T;
}
/// <summary>
/// Gets the specified key.
/// </summary>
/// <param name="key">The key.</param>
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
/// <exception cref="System.ArgumentNullException">key</exception>
/// <exception cref="System.Exception">Session elements cannot be added when session is disabled.</exception>
public static bool Get([CallerMemberName] string key = null)
{
if (key == null) throw new ArgumentNullException("key");
HttpContext current = HttpContext.Current;
if (current.Session.Mode == SessionStateMode.Off)
{
throw new Exception("Session elements cannot be added when session is disabled.");
}
bool result = false;
bool.TryParse(current.Session[key].ToString(), out result);
return result;
}
/// <summary>
/// Removes the specified key.
/// </summary>
/// <param name="key">The key.</param>
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
/// <exception cref="System.ArgumentNullException">key</exception>
/// <exception cref="System.Exception">Session elements cannot be added when session is disabled.</exception>
public static bool Remove(string key)
{
if (key == null) throw new ArgumentNullException("key");
HttpContext current = HttpContext.Current;
if (current == null)
{
return false;
}
if (current.Session.Mode == SessionStateMode.Off)
{
throw new Exception("Session elements cannot be added when session is disabled.");
}
current.Session.Remove(key);
return true;
}
#endregion
}
My example uses reflection to define the key name as the same as the property name, but you can use constants instead. It also checks if the Session is disabled, which might help in debugging your case.
Try this as well:
HttpContext.Current.Session["Name"]

How to get parent AppDomain?

I need to do something like this in c# (pseudo):
static var ns = new Non_Serializable_Nor_Marshal()
var app = new AppDomain();
app.execute(foo)
void foo()
{
var host = AppDomain.Current.Parent; //e.g. the original one
host.execute(bar)
}
void bar()
{
ns.Something();
}
IOW I have a non serializeable nor marshal object in one appdomain.
I want to create a second domain and execute foo(). From within that second domain I want to execute bar() on the original domain.
How do I pass the original domain to the child one?
If you don't want to use interop, you can also use a little trick using AppDomainManager. You can basically automatically 'wire' the 'primary' domain into any domains automatically - albiet the way I do it means you discard your real primary domain.
Here is the class that does all the magic:
/// <summary>
/// Represents a <see cref="AppDomainManager"/> that is
/// aware of the primary application AppDomain.
/// </summary>
public class PrimaryAppDomainManager : AppDomainManager
{
private static AppDomain _primaryDomain;
/// <summary>
/// Gets the primary domain.
/// </summary>
/// <value>The primary domain.</value>
public static AppDomain PrimaryDomain
{
get
{
return _primaryDomain;
}
}
/// <summary>
/// Sets the primary domain.
/// </summary>
/// <param name="primaryDomain">The primary domain.</param>
private void SetPrimaryDomain(AppDomain primaryDomain)
{
_primaryDomain = primaryDomain;
}
/// <summary>
/// Sets the primary domain to self.
/// </summary>
private void SetPrimaryDomainToSelf()
{
_primaryDomain = AppDomain.CurrentDomain;
}
/// <summary>
/// Determines whether this is the primary domain.
/// </summary>
/// <value>
/// <see langword="true"/> if this instance is the primary domain; otherwise, <see langword="false"/>.
/// </value>
public static bool IsPrimaryDomain
{
get
{
return _primaryDomain == AppDomain.CurrentDomain;
}
}
/// <summary>
/// Creates the initial domain.
/// </summary>
/// <param name="friendlyName">Name of the friendly.</param>
/// <param name="securityInfo">The security info.</param>
/// <param name="appDomainInfo">The AppDomain setup info.</param>
/// <returns></returns>
public static AppDomain CreateInitialDomain(string friendlyName, Evidence securityInfo, AppDomainSetup appDomainInfo)
{
if (AppDomain.CurrentDomain.DomainManager is PrimaryAppDomainManager)
return null;
appDomainInfo = appDomainInfo ?? new AppDomainSetup();
appDomainInfo.AppDomainManagerAssembly = typeof(PrimaryAppDomainManager).Assembly.FullName;
appDomainInfo.AppDomainManagerType = typeof(PrimaryAppDomainManager).FullName;
var appDomain = AppDomainManager.CreateDomainHelper(friendlyName, securityInfo, appDomainInfo);
((PrimaryAppDomainManager)appDomain.DomainManager).SetPrimaryDomainToSelf();
_primaryDomain = appDomain;
return appDomain;
}
/// <summary>
/// Returns a new or existing application domain.
/// </summary>
/// <param name="friendlyName">The friendly name of the domain.</param>
/// <param name="securityInfo">An object that contains evidence mapped through the security policy to establish a top-of-stack permission set.</param>
/// <param name="appDomainInfo">An object that contains application domain initialization information.</param>
/// <returns>A new or existing application domain.</returns>
/// <PermissionSet>
/// <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="ControlEvidence, ControlAppDomain, Infrastructure"/>
/// </PermissionSet>
public override AppDomain CreateDomain(string friendlyName, Evidence securityInfo, AppDomainSetup appDomainInfo)
{
appDomainInfo = appDomainInfo ?? new AppDomainSetup();
appDomainInfo.AppDomainManagerAssembly = typeof(PrimaryAppDomainManager).Assembly.FullName;
appDomainInfo.AppDomainManagerType = typeof(PrimaryAppDomainManager).FullName;
var appDomain = base.CreateDomain(friendlyName, securityInfo, appDomainInfo);
((PrimaryAppDomainManager)appDomain.DomainManager).SetPrimaryDomain(_primaryDomain);
return appDomain;
}
}
And you need to alter your Main() (application entry) slightly:
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main(string[] args)
{
new Program().Run(args);
}
void Run(string[] args)
{
var domain = PrimaryAppDomainManager.CreateInitialDomain("PrimaryDomain", null, null);
if (domain == null)
{
// Original Main() code here.
}
else
{
domain.CreateInstanceAndUnwrap<Program>().Run(args);
}
}
Now at any point you can get PrimaryAppDomainManager.PrimaryDomain to get a reference to the primary domain, just remember that it isn't the inital domain created by the .Net runtime - it's one we create immediately.
You can look at the comments in my blog post for an way to get the .Net runtime to hook this in for you automatically using the app.config.
Edit: I forgot to add the extension method I use, here it is:
/// <summary>
/// Creates a new instance of the specified type.
/// </summary>
/// <typeparam name="T">The type of object to create.</typeparam>
/// <param name="appDomain">The app domain.</param>
/// <returns>A proxy for the new object.</returns>
public static T CreateInstanceAndUnwrap<T>(this AppDomain appDomain)
{
var res = (T)appDomain.CreateInstanceAndUnwrap(typeof(T));
return res;
}
You could try referencing mscoree and then using its methods. I have used this in one of my projects.
mscoree will keep track of your AppDomains without any input.
/// <summary>
/// Returns the primary application domain.
/// </summary>
/// <returns>The primary application domain.</returns>
public static AppDomain GetPrimaryAppDomain()
{
return GetAppDomain(Process.GetCurrentProcess().MainModule.ModuleName);
}
/// <summary>
/// Returns the application domain with the given friendly name.
/// </summary>
/// <param name="friendlyName">The friendly name of the application domain.</param>
/// <returns>The application domain with the given friendly name.</returns>
/// <exception cref="System.ArgumentNullException">Thrown if friendlyName is null.</exception>
public static AppDomain GetAppDomain(string friendlyName)
{
if (friendlyName == null)
{
throw new ArgumentNullException("friendlyName");
}
IntPtr handle = IntPtr.Zero;
CorRuntimeHostClass host = new CorRuntimeHostClass();
try
{
host.EnumDomains(out handle);
object domain = null;
while (true)
{
host.NextDomain(handle, out domain);
if (domain == null)
{
return null;
}
AppDomain appDomain = (AppDomain)domain;
if (appDomain.FriendlyName == friendlyName)
{
return appDomain;
}
}
}
finally
{
host.CloseEnum(handle);
Marshal.ReleaseComObject(host);
host = null;
}
}
(Adapted from http://www.dolittle.com/blogs/einar/archive/2007/05/18/cross-appdomain-singleton.aspx)

Categories

Resources