feature: Implement Dispose for types which have disposable data (#1171)

* Initial set of dispose implementations

Not handled yet:
- Discord.Net.Websocket/Entities/SocketGuild
- Discord.Net.Tests

* Refactor DiscordSocketClient init into ctor

This way we remove an IDisposableAnalyzer warning for not disposing
the client when we set the client variable.

* Dispose of clients when disposing sharded client

* Finish implementing IDisposable where appropriate

I opted to use NoWarn in the Tests project as it wasn't really necessary
considering that our tests only run once

* Tweak samples after feedback
This commit is contained in:
Monica S
2018-11-29 01:18:16 +00:00
committed by Christopher F
parent dca6c33da3
commit 7366cd4361
31 changed files with 406 additions and 154 deletions

View File

@@ -12,4 +12,7 @@
<PackageReference Include="System.Collections.Immutable" Version="1.3.1" />
<PackageReference Include="System.Interactive.Async" Version="3.1.1" />
</ItemGroup>
<ItemGroup Condition=" '$(Configuration)' != 'Release' ">
<PackageReference Include="IDisposableAnalyzers" Version="2.0.3.3" />
</ItemGroup>
</Project>

View File

@@ -1,15 +1,21 @@
using System;
using System.IO;
namespace Discord
{
/// <summary>
/// An image that will be uploaded to Discord.
/// </summary>
public struct Image
public struct Image : IDisposable
{
private bool _isDisposed;
/// <summary>
/// Gets the stream to be uploaded to Discord.
/// </summary>
#pragma warning disable IDISP008
public Stream Stream { get; }
#pragma warning restore IDISP008
/// <summary>
/// Create the image with a <see cref="System.IO.Stream"/>.
/// </summary>
@@ -19,6 +25,7 @@ namespace Discord
/// </param>
public Image(Stream stream)
{
_isDisposed = false;
Stream = stream;
}
@@ -45,15 +52,28 @@ namespace Discord
/// The specified <paramref name="path"/> is invalid, (for example, it is on an unmapped drive).
/// </exception>
/// <exception cref="System.UnauthorizedAccessException">
/// <paramref name="path" /> specified a directory.-or- The caller does not have the required permission.
/// <paramref name="path" /> specified a directory.-or- The caller does not have the required permission.
/// </exception>
/// <exception cref="FileNotFoundException">The file specified in <paramref name="path" /> was not found.
/// <exception cref="FileNotFoundException">The file specified in <paramref name="path" /> was not found.
/// </exception>
/// <exception cref="IOException">An I/O error occurred while opening the file. </exception>
public Image(string path)
{
_isDisposed = false;
Stream = File.OpenRead(path);
}
/// <inheritdoc/>
public void Dispose()
{
if (!_isDisposed)
{
#pragma warning disable IDISP007
Stream?.Dispose();
#pragma warning restore IDISP007
_isDisposed = true;
}
}
}
}

View File

@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
@@ -7,7 +8,7 @@ namespace Discord.Net.Rest
/// <summary>
/// Represents a generic REST-based client.
/// </summary>
public interface IRestClient
public interface IRestClient : IDisposable
{
/// <summary>
/// Sets the HTTP header of this client for all requests.

View File

@@ -4,7 +4,7 @@ using System.Threading.Tasks;
namespace Discord.Net.Udp
{
public interface IUdpSocket
public interface IUdpSocket : IDisposable
{
event Func<byte[], int, int, Task> ReceivedDatagram;

View File

@@ -4,7 +4,7 @@ using System.Threading.Tasks;
namespace Discord.Net.WebSockets
{
public interface IWebSocketClient
public interface IWebSocketClient : IDisposable
{
event Func<byte[], int, int, Task> BinaryMessage;
event Func<string, Task> TextMessage;