Skip to content

Commit

Permalink
Multi-target WCL to netstandard 11/20.
Browse files Browse the repository at this point in the history
This can eliminate the dependency to System.Reflection.Emit since netstandard20.
  • Loading branch information
CXuesong committed Mar 7, 2018
1 parent aa676b0 commit d5b06ad
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 22 deletions.
4 changes: 3 additions & 1 deletion UnitTestProject1/Tests/PageTestsDirty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public async Task LocalFileUploadRetryTest1()
wikiClient.RetryDelay = TimeSpan.FromSeconds(1);
wikiClient.MaxRetries = 1;
var buffer = new byte[1024 * 1024 * 2]; // 2MB, I think this size is fairly large.
// If your connection speed is too fast then, well, trottle it plz.
// If your connection speed is too fast then, well, throttle it plz.
using (var ms = new MemoryStream(buffer))
{
await Assert.ThrowsAsync<TimeoutException>(async () =>
Expand All @@ -136,6 +136,8 @@ await Assert.ThrowsAsync<TimeoutException>(async () =>
// Usually we should notify the user, then perform the re-upload ignoring the warning.
Assert.Equal(UploadResultCode.Warning, result.ResultCode);
});
// Ensure that the stream has not been disposed in this case.
ms.Seek(0, SeekOrigin.Begin);
}
}

Expand Down
13 changes: 10 additions & 3 deletions WikiClientLibrary/Client/WikiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,17 @@ namespace WikiClientLibrary.Client
/// </summary>
public class WikiClient : IWikiClient, IWikiClientLoggable, IDisposable
{

#if NETSTANDARD1_1
private const string targetFramework = ".NET Standard 1.1";
#elif NETSTANDARD2_0
private const string targetFramework = ".NET Standard 2.0";
#endif

/// <summary>
/// The User Agent of Wiki Client Library.
/// </summary>
public const string WikiClientUserAgent = "WikiClientLibrary/0.6 (.NET Portable; http://github.com/cxuesong/WikiClientLibrary)";
public const string WikiClientUserAgent = "WikiClientLibrary/0.6 (" + targetFramework + "; http://github.com/cxuesong/WikiClientLibrary)";

public WikiClient() : this(new HttpClientHandler(), true)
{
Expand Down Expand Up @@ -103,7 +110,7 @@ public string ClientUserAgent
_ClientUserAgent = value;
}
}
}
}

/// <summary>
/// Referer.
Expand Down Expand Up @@ -165,7 +172,7 @@ protected virtual HttpRequestMessage CreateHttpRequestMessage(string endPointUrl
var query = message.GetHttpQuery();
if (query != null) url = url + "?" + query;
return new HttpRequestMessage(message.GetHttpMethod(), url)
{Content = message.GetHttpContent()};
{ Content = message.GetHttpContent() };
}

private async Task<T> SendAsync<T>(string endPointUrl, WikiRequestMessage message,
Expand Down
26 changes: 12 additions & 14 deletions WikiClientLibrary/Infrastructures/HttpContentEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ private static byte[] GetContentByteArray(IEnumerable<KeyValuePair<string, strin
sb.Append('=');
Encode(sb, nameValue.Value);
}

sb.Replace("%20", "+");
return DefaultHttpEncoding.GetBytes(sb.ToString());
}
Expand Down Expand Up @@ -72,32 +73,26 @@ public KeepAlivingStreamContent(Stream stream, int bufferSize) : base(stream, bu

}

/// <summary>
/// Determines whether to dispose the underlying stream when disposing this <see cref="KeepAlivingStreamContent"/>.
/// </summary>
public bool DisposeStream { get; set; }
#if NETSTANDARD1_1

// Workaround for https://github.com/dotnet/corefx/pull/19082
// This PR hasn't been merged until .NET Core 2.0.

private static readonly Action<KeepAlivingStreamContent> disposeImpl
; // Calls System.Net.Http.HttpContent.Dispose
private static readonly Action<KeepAlivingStreamContent> disposeImpl; // Calls System.Net.Http.HttpContent.Dispose

/// <inheritdoc />
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (DisposeStream)
base.Dispose(true);
else
disposeImpl(this); // We want to bypass StreamContent::Dispose(true) implementation.
}
disposeImpl(this); // We want to bypass StreamContent::Dispose(true) implementation.
else
base.Dispose(false);
}

static KeepAlivingStreamContent()
{
var method = new DynamicMethod("$KeepAlivingStreamContent.Dispose()", null,
new[] {typeof(KeepAlivingStreamContent)}, typeof(KeepAlivingStreamContent));
new[] { typeof(KeepAlivingStreamContent) }, typeof(KeepAlivingStreamContent));
var il = method.GetILGenerator();
il.Emit(OpCodes.Ldarg_0); // this
il.Emit(OpCodes.Ldc_I4_1); // disposing
Expand All @@ -108,7 +103,10 @@ static KeepAlivingStreamContent()
null);
il.Emit(OpCodes.Ret);
disposeImpl =
(Action<KeepAlivingStreamContent>) method.CreateDelegate(typeof(Action<KeepAlivingStreamContent>));
(Action<KeepAlivingStreamContent>)method.CreateDelegate(typeof(Action<KeepAlivingStreamContent>));
}

#endif

}
}
12 changes: 8 additions & 4 deletions WikiClientLibrary/WikiClientLibrary.csproj
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard1.1</TargetFramework>
<TargetFrameworks>netstandard1.1;netstandard2.0</TargetFrameworks>
<AssemblyName>WikiClientLibrary</AssemblyName>
<PackageId>CXuesong.MW.WikiClientLibrary</PackageId>
<Version>0.6.0-intX7</Version>
<AssemblyVersion>0.6.0.17</AssemblyVersion>
<FileVersion>0.6.0.17</FileVersion>
<Copyright>Copyright (C) CXuesong 2017</Copyright>
<Description>Wiki Client Library is a .NET Standard &amp; asynchronous client library for MediaWiki sites.
<Description>
Wiki Client Library is a .NET Standard &amp; asynchronous client library for MediaWiki sites.

This portable &amp; asynchronous MediaWiki API client provides an easy and asynchronous access to commonly-used MediaWiki API. It has the following features:

Expand All @@ -19,7 +20,8 @@
* Client code has access to CookieContainer, and have chance to persist it.
* Tokens are hidden in the library functions, so that client won't bother to retrieve them over and over again.
* Query continuations are hidden by IAsyncEnumerable, which will ease the pain when using page generators.
* Other miscellaneous MediaWiki API, such as OpenSearch, Page parsing, and Patrol.</Description>
* Other miscellaneous MediaWiki API, such as OpenSearch, Page parsing, and Patrol.
</Description>
<PackageReleaseNotes>See https://github.com/CXuesong/WikiClientLibrary/releases .</PackageReleaseNotes>
<PackageTags>MediaWiki API Client</PackageTags>
<NeutralLanguage>en-us</NeutralLanguage>
Expand All @@ -41,7 +43,9 @@
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="1.1.2" />
<PackageReference Include="Newtonsoft.Json" Version="9.0.1" />
<PackageReference Include="System.Interactive.Async" Version="3.0.0" />
<PackageReference Include="System.Reflection.Emit.Lightweight" Version="4.3.0" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.1' ">
<PackageReference Include="System.Reflection.Emit.Lightweight" Version="4.3.0" />
</ItemGroup>
</Project>

0 comments on commit d5b06ad

Please sign in to comment.