/// <summary>
 /// Stops tracking the specified .NET object reference.
 /// This overload is typically invoked from .NET code by <see cref="DotNetObjectRef.Dispose"/>.
 /// </summary>
 /// <param name="dotNetObjectRef">The <see cref="DotNetObjectRef"/>.</param>
 public void ReleaseDotNetObject(DotNetObjectRef dotNetObjectRef)
 {
     lock (_storageLock)
     {
         if (_trackedIdsByRef.TryGetValue(dotNetObjectRef, out var dotNetObjectId))
         {
             _trackedRefsById.Remove(dotNetObjectId);
             _trackedIdsByRef.Remove(dotNetObjectRef);
         }
     }
 }
Ejemplo n.º 2
0
        public Task NotifiesAssociatedJsRuntimeOfDisposal() => WithJSRuntime(jsRuntime =>
        {
            // Arrange
            var objRef = DotNetObjectRef.Create(new object());

            // Act
            objRef.Dispose();

            // Assert
            var ex = Assert.Throws <ArgumentException>(() => jsRuntime.ObjectRefManager.FindDotNetObject(objRef.ObjectId));
            Assert.StartsWith("There is no tracked object with id '1'.", ex.Message);
        });
        private void EnsureDotNetObjectTracked(DotNetObjectRef dotNetObjectRef, out long dotNetObjectId)
        {
            dotNetObjectRef.EnsureAttachedToJsRuntime(_jsRuntime);

            lock (_storageLock)
            {
                // Assign an ID only if it doesn't already have one
                if (!_trackedIdsByRef.TryGetValue(dotNetObjectRef, out dotNetObjectId))
                {
                    dotNetObjectId = _nextId++;
                    _trackedRefsById.Add(dotNetObjectId, dotNetObjectRef);
                    _trackedIdsByRef.Add(dotNetObjectRef, dotNetObjectId);
                }
            }
        }
Ejemplo n.º 4
0
 /// <inheritdoc />
 public void UntrackObjectRef(DotNetObjectRef dotNetObjectRef)
 => ArgSerializerStrategy.ReleaseDotNetObject(dotNetObjectRef);
Ejemplo n.º 5
0
 public override void Write(Utf8JsonWriter writer, DotNetObjectRef <TValue> value, JsonSerializerOptions options)
 {
     writer.WriteStartObject();
     writer.WriteNumber(DotNetObjectRefKey, value.ObjectId);
     writer.WriteEndObject();
 }
Ejemplo n.º 6
0
 public Task CanAccessValue() => WithJSRuntime(_ =>
 {
     var obj = new object();
     Assert.Same(obj, DotNetObjectRef.Create(obj).Value);
 });