nimaara / easy.common Goto Github PK
View Code? Open in Web Editor NEWA set of useful .NET utilities and C# helpers used across Easy.* projects.
License: MIT License
A set of useful .NET utilities and C# helpers used across Easy.* projects.
License: MIT License
Here is an example using UTF-16:
[Test]
public void CountLinesTest()
{
byte[] bytes = Encoding.BigEndianUnicode.GetBytes(" ਊ ");
var stream = new MemoryStream(bytes);
Assert.AreEqual(1, Easy.Common.Extensions.StreamExtensions.CountLines(stream));
}
Results in:
Expected: 1
But was: 3
The character I used is U+0a0a which is interpreted as two line feeds by CountLines. Basically any character in the form U+xx0A or U+xx0D or U+0Axx or U+0Dxx can be misinterpreted as an extra line.
I would suggest renaming the method to CountLinesUtf8 or similar.
Or pass in an Encoding / TextReader but then it won't be as fast.
I looked into trying to use this for eveonline log files, and the filecount was incorrect with compared to looping through readline on a StreamReader:
You can reference this project and the monitor.cs file
https://github.com/evetoolsblue/EveIntelReader
Using dotnet core 3.0
IDGenerator geenerate the string ID's. Is there a way to generate the long values?
Hi thanks for this library, stumbled upon it recently and it's got some great stuff in there 👍
Just an issue I encountered with a .NET Core 2 app on a Raspberry Pi running Raspbian Linux (could be an issue on other linux distro's too?)
Unhandled Exception: System.DllNotFoundException: Unable to load DLL 'Kernel32.dll': The specified module or one of its dependencies could not be found.
(Exception from HRESULT: 0x8007007E)
at Easy.Common.NativeMethods.GetSystemTimePreciseAsFileTime(Int64& filetime)
at Easy.Common.Clock..ctor()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCreateInstance(CreateInstanceCallSite createInstanceCallSite, ServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitSingleton(SingletonCallSite singletonCallSite, ServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceProvider.<>c__DisplayClass22_0.<RealizeService>b__0(ServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType)
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetService[T](IServiceProvider provider)
at MyApp.Program.<Main>d__1.MoveNext() in D:\Workspace\my-app-raspberry-pi\src\MyApp\Program.cs:line 33
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at MyApp.Program.<Main>(String[] args)
Basically when I build my Service Provider, the above error occurs, so it can't fall back to non precise time. I've worked around it by copying the Clock.cs
source code in my project and using that without GetSystemTimePreciseAsFileTime(out preciseTime);
. Am I missing a nuget package or is there some additional work required to not throw that error on startup?
Cheers
Hi @NimaAra , as title, does RestClient support gzip
var defaultHeaders = new Dictionary<string, string>
{
{"Accept", "application/json"},
{"User-Agent", "foo-bar"},
{"Accept-Encoding", "gzip" }, // <===================
};
using (IRestClient client = new RestClient(defaultHeaders, timeout: 15.Seconds()))
{
// ....
}
if it support gzip, how to unzip
thank you
reference code:
//Variance.cs:
private bool VariesImpl()
{
var isSequence = Property.PropertyType.IsSequence(out SequenceType _);
if (LeftValue is null) { return RightValue != null; }
if (RightValue is null) { return false; }
if (!isSequence) { return !Equals(LeftValue, RightValue); }
var left = ((IEnumerable)LeftValue).Cast<object>();
var right = ((IEnumerable)RightValue).Cast<object>();
return !left.SequenceEqual(right);
}
qusetion:
if (RightValue is null) { return false; }
shouldbe if (RightValue is null) { return true; }
becase, LeftValue is not null and RightValue is null
This is a great little library!
My interest in this is related to PowerShell Core development (PowerShell Core is the .Net 2.0 App that can run on almost any OS, as opposed to Windows PowerShell, which is the .Net Framework App that comes installed on Windows by default). Right now, there are a lot of basic cmdlets missing from PowerShell Core (like Get-NetIPAddress for network info), and this report can help fill-in the gaps until the PowerShell community gets around to creating some of the missing cmdlets.
Since PowerShell Core is a .Net 2.0 App, it should be able to load and use the netstandard2.0 assembly. I can load the assembly without issue, but running Generate() throws the below error:
PowerShell v6.0.0-beta.9
Copyright (C) Microsoft Corporation. All rights reserved.
PS C:\Users\testadmin> Add-Type -Path "C:\Users\testadmin\.nuget\packages\Easy.Common.2.6.1\lib\netstandard2.0\Easy.Common.dll"
PS C:\Users\testadmin> [Easy.Common.DiagnosticReport]::Generate()
Unable to generate the Diagnostic Report. Error:
System.NullReferenceException: Object reference not set to an instance of an object.
at Easy.Common.DiagnosticReport.<AddProcess>g__Format17_1(String key, Object value, <>c__DisplayClass17_0& )
at Easy.Common.DiagnosticReport.AddProcess(StringBuilder builder)
at Easy.Common.DiagnosticReport.GenerateImpl(DiagnosticReportType type)
at Easy.Common.DiagnosticReport.Generate(DiagnosticReportType type)
(As an FYI, the net45 assembly works fine in Windows PowerShell 5.1 - i.e. the .Net Framework App).
(Also, if you haven't worked with PowerShell Core before, you can grab an installer from here: https://github.com/PowerShell/PowerShell/releases)
Thanks again for making this!
IRestClient
currently relies on HttpClient
default timeout which is 100 seconds. One can currently issue a different timeout per request by executing the following:
var cts = new CancellationTokenSource(TimeSpan.FromSeconds(15));
await client.GetAsync("http://localhost", cts.Token);
This should be made easier by exposing an overload accepting a Timespan
as timeout for each of the following:
GetAsync
GetStringAsync
GetStreamAsync
GetByteArrayAsync
DeleteAsync
PostAsync
PutAsync
It is also important to note in order for this per request timeout to be effective, the default timeout must be set to infinite. So RestClient
by default will set the timeout of its internal HttpClient
to: System.Threading.Timeout.InfiniteTimeSpan
unless a different value is provided via the constructor.
Do you use Easy.Common in real world production environments and projects ?
Hello!
I commented on your blog post about this and you suggested I should contribute, so here I am, hello :)
Extending the class is simple enough, however I just wanted to get your thoughts on the IRestClient Interface. There is only one implementation in this library but could people have created their own implementations? Extending the interface could break their code which is not good.
My plan b is implement the 'missing' methods as extension methods of IRestClient - this would then not break any existing implementations of IRestClient.
What are you're thoughts?
Thanks for the great tool! It's been a great help for getting system info for cross-platform .NET programs we're writing.
I've noticed what I think is a bug in getting the processor name in Windows in .NET Core applications.
This code block checks for .NET framework or .NET standard targets and returns if the application is not targeted to one of those two. Unfortunately this means that .NET Core applications return "< INVALID >", even though lines 772-773 would work perfectly fine. Can you either remove the targeting requirement, or include .NET Core in the conditional? Or wrap in a try/catch block?
Another request - can you make methods like GetProcessorName()
and GetInstalledMemoryInGigaBytes()
public
? It would be nice to be able to display these outside of your diagnostic report.
I'd be happy to make a pull request - let me know if I should.
netcoreapp2.1 added SocketsHttpHandler
; This is a new type which leverages the rewritten HttpMessageHandler
.
One important feature of this type is the ability to set the ConnectionLifeTime without having to keep track of each endpoint and setting the ConnectionLeaseTimeout
on the ServicePoint
.
Hi all,
will be provided a method to set headers per call, in order to keep a single RestClient instance, but passing for instance different JWT Token per call (https://stackoverflow.com/questions/29801195/adding-headers-when-using-httpclient-getasync) ?
Kindly
Alessandro
Currently, in my ASP.NET Core app application level env variables aren't displayed, it would be nice if there's an option to display only application env variables.
Thanks in advance.
Hey, I saw your ID generator on your article and thought it was cool. Is it good for generating unique user IDs, though? Would I be better off using .NET's built in GUIDs?
Currently the method is checking if the given input is not Null, Empty or Whitespace, this should be changed to only check for Null.
Hi
Thanks for the effort.
I don't want to fiddle with ConnectionLeaseTimeout . So is it a better way to create a cache of httpclients based on base uri's. Each unique base uri will have it's own HttpClient. If we have 5 unique uri's we will have 5 items in the cache. URI is our key.
Do you think this is a better approach?
Thanks again!!!
DiagnosticsReport in net5?
Easy.Common/Easy.Common/Base36.cs
Line 42 in 6fcc1f3
With current implementation
Base36.Decode( Base36.Encode(10) ) == 10 // you would expect that but but the result is -1
I love the idea behind the DiagnosticReport but would it be possible to return in a JSON string rather than a "prettified" report? I love all the information but my logging system wouldn't take in that very well as a preformatted string.
Thanks.
Finding a service endpoint and then setting its ConnectionLeaseTimeout
value within the SPM for each and every full URI that RestClient
talks to is potentially wasteful, since - as you say - "the method only cares about the host, schema and port everything else is ignored".
So say the caller of RestClient
talks to a single API whose base URI is https://myapi.com/v1/
. Say you hit this API a 1000 times. RestClient
will set ConnectionLeaseTimeout
a 1000 times, but it only needs to do so once.
I reviewed the readline code, and you should probably just fix your code to do that because there is nothing to be gained here.
Hello,
The RestClient implementation seems to be leaking memory, fairly slowly in our case to be honest. Unfortunately, we have now switched to core 2.1 and httpClientFactory, which fixed the leak, so didn't look into the issue thoroughly enough. I suspect it is related to keeping the endpoints in memory. Hope this helps, let me know if I can provide any more useful info.
Hello,
Sorry for not coming up with a more succinct title.
I have wrapped the RestClient in a service that will perform 4 HTTP requests serially and need to preserve some of the headers and cookies during these calls. My service will be injected into the web API controller via constructor injection and invoked from an API route. My container is Autofac and I have set my service and RestClient to be InstancePerRequest. So now that I have that out of the way, I was wondering about how to persist headers and cookies across the 4 calls? Also wondering if you have any feedback on the life-time of the RestClient given my requirements?
Thanks in advance Nima!
-Stephen
https://github.com/NimaAra/Easy.Common/blob/master/Easy.Common/RestClient.cs
You used Hashset in AddConnectionLeaseTimeout to check whether the service pointhas been set to the ConnectionLeaseTimeout to 1 min.
But as this https://stackoverflow.com/questions/53653640/net-servicepoint-object-lifecycle says, servicePoint object will be removed from the servicepointmanager and it's resource will get released after a timeout period, so seems your implementation did not consider this.
Actually, I am wondering have you ever met the issue with the link above said, since from the code I think you will just meet same issue.
AddConnectionLeaseTimeout
is confused.
First of all, it doesn't only "add connection lease timeout". It also adds to the collection of "all of the endpoints which this instance has sent a request to", which is returned by the public Endpoints
property.
Secondly, since you note correctly regarding ServicePointManager.FindServicePoint(endpoint)
that "the method only cares about the host, schema and port everything else is ignored", then it is redundant to perform:-
ServicePointManager.FindServicePoint(endpoint).ConnectionLeaseTimeout = <value>;
for each full URI. It is only necessary to do this once for any URI's host+schema+port. For some use cases of the RestClient
class, this could result in a significant saving.
Of course, you'd need a second, purely-internal HashSet<Uri>
in which to record the shortened URIs on which you've set ConnectionLeaseTimeout
.
Hi,
Sorry to use this Issues channel as I didn't see any other ways to communicate. I am not using this library directly but I need to set this ServicePointManager.ConnectionLeaseTimeout property. However in my test environment it doesn't work as expected. Do you have any experience on this one? like how to verify behavior?
Thanks
Chunyuan
FYI - seems like this is similar to the built in abstract System.Collections.ObjectModel.KeyedCollection<TKey, TItem>
class.
https://docs.microsoft.com/en-us/dotnet/api/system.collections.objectmodel.keyedcollection-2?view=net-5.0
There is a possible exception in Easy.Common.DiagnosticReport.GetDriveDetails when enumerating the details about the connected drives if there is a mapped drive the application or current user does not have access to.
It looks like accessing either VolumeLabel and DriveFormat could throw.
Specifically here: https://github.com/NimaAra/Easy.Common/blob/master/Easy.Common/DiagnosticReport/DiagnosticReport.cs#L298
Easy workaround:
Exclude drives from the report.
DiagnosticReport report = DiagnosticReport.Generate(DiagnosticReportType.Full & ~DiagnosticReportType.Drives);
Proper fix:
Maybe the best way would be to catch the exception and substitute with '-'.
Are there any blockers to adding NotNullWhenAttribute to methods which contain null checks to remove unnecessary warnings from the compiler?
e.g.
public static bool IsNullOrEmptyOrWhiteSpace([NotNullWhen(false)] this string value) => string.IsNullOrWhiteSpace(value);
Thanks!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.