Application insights working without config - azure-application-insights

I'm building a small library that use Application Insights.
Is there a possibility Application Insights working without ApplicationInsights.config file?
I tried remove and add the modules manually at constructor, but it didn't work.
Edited
I did something like that:
dependencies = new DependencyTrackingTelemetryModule();
dependencies.Initialize(configuration);
exceptionTelemetryModule = new UnhandledExceptionTelemetryModule();
exceptionTelemetryModule.Initialize(configuration);
unobservedExceptionTelemetry = new UnobservedExceptionTelemetryModule();
unobservedExceptionTelemetry.Initialize(configuration);
serverTelemetryChannel = new ServerTelemetryChannel();
serverTelemetryChannel.DeveloperMode = true;
serverTelemetryChannel.Initialize(configuration);
azureInstanceMetadataTelemetry = new AzureInstanceMetadataTelemetryModule();
azureInstanceMetadataTelemetry.Initialize(configuration);
var developer = new DeveloperModeWithDebuggerAttachedTelemetryModule();
developer.Initialize(configuration);
configuration.TelemetryInitializers.Add(new OperationCorrelationTelemetryInitializer());
client = new TelemetryClient(configuration);

A minimalistic setup can be done like this.
using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.DependencyCollector;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel;
..
..
..
private static void setupApplicationInsights()
{
// Setup Channel, Initializers, and Sampling
// Nugets Required: "Microsoft.ApplicationInsights", "Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel"
var channel = new ServerTelemetryChannel();
var config = TelemetryConfiguration.Active;
config.InstrumentationKey = "putikey";
channel.Initialize(config);
TelemetryConfiguration.Active.TelemetryChannel = channel;
//Setup TelemetryInitializers...
config.TelemetryInitializers.Add(new OperationCorrelationTelemetryInitializer());
//Setup Sampling
config.TelemetryProcessorChainBuilder.UseAdaptiveSampling();
// Setup modules...
// Nugets : Microsoft.ApplicationInsights.DependencyCollector
DependencyTrackingTelemetryModule dep = new DependencyTrackingTelemetryModule();
dep.Initialize(config);
}

Related

Generate QR code image stream or bytes for Xamarin Forms

I am struggling big time with generating QR barcodes as a byte[] or Stream (something that I can use on a XAML image source)
ZXING.NET
I've tried with Zxing.Net but I find the documentation is not great.
In fact, when installing in the xamarin forms class library I am able to compile, but as soon as I add some code to write barcodes I get a compilation error saying that
Can not resolve reference: `zxing`, referenced by `MyXamarinFormsClassLibrary`. Please add a NuGet package or assembly reference for `zxing`, or remove the reference to `Sasw.AforoPass`. 0
Something funky is going on with that library.
And I'm doing a simple example such as:
var options = new QrCodeEncodingOptions
{
DisableECI = true,
CharacterSet = "UTF-8",
Width = 250,
Height = 250
};
var writer = new BarcodeWriter<Image>();
writer.Format = BarcodeFormat.QR_CODE;
writer.Options = options;
var result = writer.Write("foo");
MyImage.Source = result.Source;
QRCoder
It's another nuget library. I've successfully used for dotnet core applications, but it does not seem to be compatible with Xamarin Forms (or Mono, or whatever). It says that the platform is not supported. Probably because it uses System.Drawing.Common?
ZXing.Net.Mobile.Forms
I've used this other library which underneath it uses ZXing.Net. What I don't like is that I don't know if there's any way to generate qr codes without relying on Xaml or the ZXingBarcodeImageView.
I managed to generate QR Codes that way as a workaround, but I hit another wall. See https://github.com/Redth/ZXing.Net.Mobile/issues/908 in which I describe the problems I have to embed the ZXingBarcodeImageView in a carousel inside a popup.
So basically I wanted to go back to the roots, and simply have a working example with the latest version of ZXing.Net (or an alternative, if it exists) that I am able to use in Xamarin Forms.
Most of the examples I find talk about BarcodeWriter but there is no such a class anymore. There is a generic one BarcodeWriter<TUnknownType>and a BarcodeWriterGeneric but as I said, I could not compile anything using Zxing.Net library and with through ZXing.Net.Mobile the images I generate are always empty.
Any help or "modern" code sample (ideally with an alternative) would be much appreciated
UPDATE 1
In other words, I'm looking to have in Xamarin Forms something similar to this code that I had using QrCoder library.
public class QrCodeService
: IQrCodeService
{
public Stream GetQrCode(Guid id, string mimeType = "image/jpeg")
{
if (mimeType is null)
{
throw new ArgumentNullException(nameof(mimeType));
}
var qrGenerator = new QRCodeGenerator();
var qrCodeData = qrGenerator.CreateQrCode(id.ToString(), QRCodeGenerator.ECCLevel.Q);
var qrCode = new QRCode(qrCodeData);
var qrCodeImage = qrCode.GetGraphic(20);
var myImageCodecInfo = GetEncoderInfo(mimeType);
var myEncoder = Encoder.Quality;
var myEncoderParameters = new EncoderParameters(1);
var myEncoderParameter = new EncoderParameter(myEncoder, 50L);
myEncoderParameters.Param[0] = myEncoderParameter;
var stream = new MemoryStream();
qrCodeImage.Save(stream, myImageCodecInfo, myEncoderParameters);
stream.Position = 0;
return stream;
}
private static ImageCodecInfo GetEncoderInfo(string mimeType)
{
var encoders = ImageCodecInfo.GetImageEncoders();
foreach (var encoder in encoders)
{
if (encoder.MimeType == mimeType)
{
return encoder;
}
}
throw new KeyNotFoundException($"Encoder for {mimeType} not found");
}
}
The solution uses QrCoder library and it works fine for Xamarin Forms as follows.
private byte[] GetQrImageAsBytes()
{
var randomText = Guid.NewGuid().ToString();
var qrGenerator = new QRCodeGenerator();
var qrCodeData = qrGenerator.CreateQrCode(randomText, QRCodeGenerator.ECCLevel.L);
var qRCode = new PngByteQRCode(qrCodeData);
var qrCodeBytes = qRCode.GetGraphic(20);
return qrCodeBytes;
}
Here a working sample with this implementation
QrCoder library can do this, but instead of using as in the main page of the project:
QRCodeGenerator qrGenerator = new QRCodeGenerator();
QRCodeData qrCodeData = qrGenerator.CreateQrCode("The text which should be encoded.", QRCodeGenerator.ECCLevel.Q);
QRCode qrCode = new QRCode(qrCodeData); // This point onwards is problematic
Bitmap qrCodeImage = qrCode.GetGraphic(20); // This will throw not supported exception
Switch the last 2 lines to:
var qrCode = new PngByteQRCode(qrCodeData);
byte[] imageByteArray = qrCode.GetGraphic(20);
You'll get byte array instead, but you can convert it into Stream or whatever you like afterwards.

Run Workflow with nested XAML-activity

I need to run workflow XAML, but that workflow keeps references to other XAMLs. When I'm trying to run the workflow by
ActivityXamlServicesSettings settings = new ActivityXamlServicesSettings
{
CompileExpressions = true
};
return ActivityXamlServices.Load(stream, settings);
I get next error from Load method:
CacheMetadata for activity 'MyNamespace.MyMainActivity' threw 'System.Xaml.XamlObjectWriterException: Cannot create unknown type '{clr-namespace:MyNamespace}MyNestedActivity'.
How can I solve it?
I think you have to convert your internal xamls to dll (assembly) file. And load the assembly file while reading/loading the parent xaml.
System.Xaml.XamlXmlReaderSettings xmlsettings = new System.Xaml.XamlXmlReaderSettings();
if(dllFile != null) {
Assembly wfAssembly = Assembly.Load(dllFile);
xmlsettings.LocalAssembly = wfAssembly;
}
System.IO.StringReader stringReader = new System.IO.StringReader(xaml);
XamlXmlReader reader = new XamlXmlReader(stringReader, xmlsettings);
ActivityXamlServicesSettings settings = new ActivityXamlServicesSettings { CompileExpressions = true };
Activity activity = System.Activities.XamlIntegration.ActivityXamlServices.Load(reader, settings);
hope this helps.

Sqlite support for openfl

I'm trying to drop in sqlite support for saving the score and some flags. I need to open the db if it exists, then init game values based on db. If the db does not exist, I need to create/init it. The code below compiles but crashes for unknown reason.
package mygame;
<snip imports>
import sys.db.Types;
class ScoreDB extends sys.db.Object {
public var id : SId;
public var dbscore1 : SInt;
public var dbsound : SInt;
public var dbscore2 : SInt;
}
class mygame extends Sprite {
<snip var defines>
public function new () {
// start sqlite code
sys.db.Manager.initialize();
// db does exist
// then read values
// currentScore = score1.dbscore1;
// doSound = score1.dbsound;
// doScore = score1.dbscore2;
// db does not exist:
var cnx = sys.db.Sqlite.open("mybase.db");
sys.db.Manager.cnx = cnx;
sys.db.TableCreate.create(ScoreDB.manager);
var score1 = new ScoreDB();
score1.id = 0;
score1.dbscore1 = 0;
score1.dbsound = 0;
score1.dbscore2 = 0;
score1.insert();
currentScore = 0;
doSound = 0;
doScore = 0;
cnx.close();
// end sqlite code
super ();
initialize ();
construct ();
newGame ();
}
I actually just solved the same issue.
The problem is that the app is essentially a .zip file and cannot be edited. You need to create (and later access) the DB in the app storage directory.
To access the directory use following code:
private static var localStoragePath:String = openfl.utils.SystemPath.applicationStorageDirectory;
There is a known bug that the IDE's don't show the SystemPath class, but don't mind it, it will compile without problem.
Later, with your common tools you can read and write the directory, create new folders etc.
Here's a quick test, to make sure it works and doesn't crash:
// creating a test folder
sys.FileSystem.createDirectory(openfl.utils.SystemPath.applicationStorageDirectory + "/testFolder");
var form:TextFormat = new TextFormat(null, 22, 0xFFFFFF);
// getting contents of the storage dir
var arr = sys.FileSystem.readDirectory(openfl.utils.SystemPath.applicationStorageDirectory);
var tf:TextField = new TextField();
tf.defaultTextFormat = form;
tf.width = Lib.current.stage.stageWidth;
tf.height = Lib.current.stage.stageHeight;
tf.multiline = true;
tf.wordWrap = true;
tf.text = arr.toString();
addChild(tf);
as you'll see, the newly created folder is there. You can delete the line that creates the folder and you'll see it's safe.
Oh, an don't forget to add Android permissions in the XML file:
<android permission="android.permission.WRITE_EXTERNAL_STORAGE"/>
As far as I know there is no sqlite definitions in openfl. And the ones you use are just normal cpp target definition. I suppose the problem is they don't work on android. Even more: I'm quite sure the api definitions are ok, but it tries to load dll with a wrong name, which probably kills your app without even letting out an error. Try to look into implementation(it is short and easy to understand) and change the dll name.

Execute BizUnit 4.0 XML Test Case

Hi i have created a an xml test case in bizunit 4.0 called test1.xml
The problem I have is how to run the test. All the examples i find are for bizunit 2.0 and 3.0, but not for 4.0. In the examples i found it says to do this:
BizUnit.BizUnit bizUnit = new BizUnit.BizUnit("testl.xml");
bizUnit.RunTest();
but i get a note saying it is deprecated.
I'm using .net unit tests and calling bizunit from code
Here's an example
// create list of files
TestGeneratedFiles = new System.Collections.Generic.List<String>();
var TestCase = new BizUnit.Xaml.TestCase { Name = "Interface128UnitTestSetup" };
//Create the file validate step
var testStep = new FileReadMultipleStep
{
DirectoryPath = inputPath,
SearchPattern = InputTemplate,
ExpectedNumberOfFiles = 1,
Timeout = 3000,
DeleteFiles = false
};
//Create an XML Validation step
//This will check against the XSD for the document
var validation = new XmlValidationStep();
var schemaSummary = new SchemaDefinition
{
// test deployment copies this file to test directory
XmlSchemaPath = #"Publish_Employee.xsd",
XmlSchemaNameSpace = "http://AMC.Oracle.Employee"
};
validation.XmlSchemas.Add(schemaSummary);
TestCase.ExecutionSteps.Add(testStep);
// run the test case
var bizUnit = new BizUnit.BizUnit(TestCase);
bizUnit.RunTest();
For me this worked (w/o warning):
TestCase tc = TestCase.LoadFromFile("test1.xml");
BizUnit.BizUnit bizUnit = new BizUnit.BizUnit(tc);
bizUnit.RunTest();

Pause and resume download in flex?

Is it possible in an air application to start a download, pause it and after that resume it?
I want to download very big files (1-3Gb) and I need to be sure if the connection is interrupted, then the next time the user tries to download the file it's start from the last position.
Any ideas and source code samples would be appreciated.
Yes, you would want to use the URLStream class (URLLoader doesn't support partial downloads) and the HTTP Range header. Note that there are some onerous security restrictions on the Range header, but it should be fine in an AIR application. Here's some untested code that should give you the general idea.
private var _us:URLStream;
private var _buf:ByteArray;
private var _offs:uint;
private var _paused:Boolean;
private var _intervalId:uint;
...
private function init():void {
_buf = new ByteArray();
_offs = 0;
var ur:URLRequest = new URLRequest( ... uri ... );
_us = new URLStream();
_paused = false;
_intervalId = setInterval(500, partialLoad);
}
...
private function partialLoad():void {
var len:uint = _us.bytesAvailable;
_us.readBytes(_buf, _offs, len);
_offs += len;
if (_paused) {
_us.close();
clearInterval(_intervalId);
}
}
...
private function pause():void {
_paused = true;
}
...
private function resume():void {
var ur:URLRequest = new URLRequest(... uri ...);
ur.requestHeaders = [new URLRequestHeader("Range", "bytes=" + _offs + "-")];
_us.load(ur);
_paused = false;
_intervalId = setInterval(500, partialLoad);
}
if you are targeting mobile devices, maybe you should take a look at this native extension: http://myappsnippet.com/download-manager-air-native-extension/ it supports simultaneous resumable downloads with multi-section chunks to download files as fast as possible.

Resources