I have a simple code
byte[] buffer = Encoding.UTF8.GetBytes("abracadabra");
MemoryStream ms = new MemoryStream();
DeflateStream ds = new DeflateStream(ms, CompressionMode.Compress, false);
ms.Write(buffer, 0, buffer.Length);
DeflateStream ds2 = new DeflateStream(ms, CompressionMode.Decompress, false);
byte[] buffer2 = new byte[ms.Length];
ds2.Read(buffer2, 0, (int)ms.Length);
Console.WriteLine(Encoding.UTF8.GetString(buffer2));
And when reading from ds2, i have the following:
Stacktrace:
at (wrapper managed-to-native)
System.IO.Compression.DeflateStream.ReadZStream
(intptr,intptr,int) <0x00004>
at (wrapper managed-to-native)
System.IO.Compression.DeflateStream.ReadZStream
(intptr,intptr,int) <0x00004>
at
System.IO.Compression.DeflateStream.ReadInternal
(byte[],int,int) [0x00031] in
C:\cygwin\tmp\monobuild\build\BUILD\mono-2.6.3\mcs\class\System\System.IO.Compression\DeflateStream.cs:192
at
System.IO.Compression.DeflateStream.Read
(byte[],int,int) [0x00086] in
C:\cygwin\tmp\monobuild\build\BUILD\mono-2.6.3\mcs\class\System\System.IO.Compression\DeflateStream.cs:214
at testtesttest.MainClass.Main
(string[]) [0x00041] in
C:\Users\ilukyanov\Desktop\Cassini\GZipDemo\Main.cs:27
at (wrapper runtime-invoke)
.runtime_invoke_void_object
(object,intptr,intptr,intptr)
This application has requested the
Runtime to terminate it in an unusual
way. Please contact the application's
support team for more information.
This problem appears in Mono 2.6.1 & 2.6.3...
Is there any known way to successfully read from DeflateStream in Mono? Or maybe there are some third-party open-source assemblies with the same functionality?
You can call zlib natively using Interop with DllImport.
Only trick is to use the right size in the structures and to include the shared library in the LD_LIBRARY_PATH, if you are on a Unix platform.
Please file a bug against Mono. If you do so, it might get fixed in time for 2.6.4.
Related
I want to create a service in .NET to transfer files between SFTP servers.
What will be the best way to choose as I know WCF doesn't support SFTP, does SFTP is supported by .NET Core.
Thanks & Regards
Supratik De
I have had good experience with Renci.SshNet (https://github.com/sshnet/SSH.NET, or as NuGet package). I used it in some projects transferring files between Windows and Linux.
I did not use it with .NET Core, but it should be supported.
For a first start, you can try:
...
using System.IO;
using Renci.SshNet;
using Renci.SshNet.Sftp;
...
...
var sftpClient = new SftpClient(host, user, password);
sftpClient.Connect();
// download
using (FileStream localStream = new FileStream(localPath, FileMode.Create))
{
sftpClient.DownloadFile(hostPath, localStream);
}
// upload
using (FileStream localStream = new FileStream(localPath, FileMode.Open, FileAccess.Read))
{
sftpClient.UploadFile(localStream, hostPath);
}
...
I am porting a library to a PCL and have to find a solution for managing HTTP request credentials. I took away concrete credentials classes like CredentialCache (that is not portable) and is now only using ICredentials, so client applications may create proper credentials and just send an interface reference.
However one thing still needs to be resolved. On some platforms HttpWebRequest has a neat PreAuthenticate property that takes care of initial handshaking. Without it the client needs to catch and repsponds to 401 responses. But PreAuthenticate is not a part of most of PCL profiles, and I wonder if there is any resolution to that or the client will need to implement replacement logic itself (which is silly since this is a standard piece of code).
Thanks in advance
You will need to write the code to handle this yourself. An API will only be portable if it is available on all of the platforms you target. In this case PreAuthenticate was a new API in .NET 4.5 and Windows Store apps, so it won't be available in a Portable Class Library if you are targeting any other platforms (ie .NET 4, Silverlight, or Windows Phone).
I'm having the same problem. I tried to set Credentials to a single NetworkCredental(user, pass), but it doesn't work in WinRT, although it works in Windoes Phone. So far the only way to make it work in WinRT is to create the CredentialCache by reflection.
var credCacheType = Type.GetType("System.Net.CredentialCache, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
var credCache = credCacheType.GetConstructor(new Type[0]).Invoke(new object[0]);
var addMethod = credCacheType.GetMethod("Add", new Type[] { typeof(Uri), typeof(string), typeof(NetworkCredential) });
addMethod.Invoke(credCache, new object[] { new Uri(_server), "Digest", Credential });
request.Credentials = (ICredentials)credCache;
PreAuthenticate can be set using HttpClienHandler that HttpClient accepts as a parameter constructor, e.g.:
HttpClientHandler handler = new HttpClientHandler()
{
UseDefaultCredentials = true,
PreAuthenticate = true
};
HttpClient client = new HttpClient(handler);
I wrote a method to submit an error-report via HTTP POST. I run OSX-Lion and MonoDevelop. The code works when the application is called from MonoDevelop. When I run the .app it throws an exception:
System.TypeInitializationException: An exception was thrown by the type initializer for System.Net.WebRequest ---> System.DllNotFoundException: libc.dylib
at (wrapper managed-to-native) System.Platform:uname (intptr)
at System.Platform.get_IsMacOS () [0x00000] in <filename unknown>:0
at System.Net.WebRequest..cctor () [0x00000] in <filename unknown>:0
--- End of inner exception stack trace ---
at ch.fangorn.LIF.Access.SubmitCrash.Submit (Int32 projectId) [0x00000] in <filename unknown>:0
At the moment I call the Submit method directly not via a catch. Since it works when a debugger is attached I'm stranded. What I so far tried:
Build a mono application bundle (Create mac installer)
Build everything with Platform target x86
The beta version of Mono and MonoDevelop
The code that IMO causes the crash.
HttpWebRequest hwr = WebRequest.Create(turi) as HttpWebRequest;
string authInfo = user + ":" + password;
authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
hwr.Headers["Authorization"] = "Basic " + authInfo;
hwr.Method = "POST";
hwr.ContentType = "text/xml";
hwr.ContentLength = encData.Length;
Stream send = hwr.GetRequestStream();
send.Write(encData, 0, encData.Length);
send.Close();
hwr.GetResponse();
MonoDevelop: 2.8.5
Mono: 2.10.8
Symptom: System libraries not found (System.DllNotFoundException: libc.dylib)
when a mono created .app is started outside of MonoDevelop.
In the wrapper-script created by mono develop: MyApp.app/Contents/MacOS/MyApp
DYLD_FALLBACK_LIBRARY_PATH is set without including default. Since an empty
DYLD_FALLBACK_LIBRARY_PATH means the default this will remove default. System
libraries cannot be found.
In my opition the line should be:
export DYLD_FALLBACK_LIBRARY_PATH="$MONO_FRAMEWORK_PATH/lib:$DYLD_FALLBACK_LIBRARY_PATH:$(HOME)/lib:/usr/local/lib:/lib:/usr/lib"
Or a check if DYLD_FALLBACK_LIBRARY_PATH is empty would be even better.
I filed a bug-report: http://bugzilla.xamarin.com/show_bug.cgi?id=2727
I have two processes. One writing in a memory mapped file using .NET 4 MemoryMappedFiles and the other reading the file using FileStream in .NET 3.5 (MONO). Everything works fine when I get the permission, no problem reading or writing the file. The problem is to obtain a permission.
Writing MMF (the Writer):
MemoryMappedFile.CreateFromFile
(filenameToMap, FileMode.Create, "Something",
Marshal.SizeOf(objectToMap), MemoryMappedFileAccess.ReadWrite);
Reading MMF (the Reader):
new FileStream(filenameToMap, FileMode.Open, FileAccess.Read, FileShare.Delete | FileShare.ReadWrite);
First I start the Writer, then the Reader.
I tested it using this naive approach:
while (true)
{
try
{
new FileStream(mapPath + "map.mp", FileMode.Open, FileAccess.Read, FileShare.Delete | FileShare.ReadWrite);
break;
}
catch { }
}
After roughly 10 seconds, the Reader usually gets permission and everything runs smooth. If I would catch the exception, it would throw:
IOException: Sharing violation on path
C:\path at
System.IO.FileStream..ctor
(System.String path, FileMode mode,
FileAccess access, FileShare share,
Int32 bufferSize, Boolean anonymous,
FileOptions options) [0x00000] in
:0
Any idea why these two processes don't share the file properly from beginning?
I have a xap file that contains an unreferenced assembly: b.dll.
This assembly was put in the xap file manually (by a post build step, in which I just add the dll to zip(xap) file).
Now at runtime I want to access b.dll and call CreateInstance on it.
This is where I am stuck. How can I get an Assembly instance for b.dll from the xap file?
Thank you!
You can initialise a StreamResourceInfo object with a downloaded zip stream (Xap or otherwise).
You can then use Application.GetResourceStream to pull a stream for file from that zip using a Uri. In this case the dll which can then load with AssemblyPart and then call a CreateInstance on it:-
WebClient client = new WebClient()
client.OpenReadCompleted += (s, args) =>
{
StreamResourceInfo zip = new StreamResourceInfo(args.Result, "application/zip");
StreamResourceInfo dll = Application.GetResourceStream(zip, new Uri("b.dll", UriKind.Relative));
AssemblyPart assemblyPart = new AssemblyPart();
Assembly assembly = assemblyPart.Load(dll.Stream);
_someClassFromB = assembly.CreateInstance("b.SomeClass");
};
client.OpenReadAsync(new Uri("your.xap", UriKind.Relative));