Xamarin.Android Cannot pull table from Azure database in Android 10 Q - azure

I did read that because lack of support for Netcore 2.1 the
myItemsList = await App.MobileServiceAndroid.GetTable<MyTable>().ToListAsync();
does not currently work on Android, and there is a workaround to pass an HttpClientHandler() in the constructor of the MobileServiceClient, and so I did like this:
public static MobileServiceClient MobileServiceAndroid =
new MobileServiceClient(AppConstants.AZURE_PRODUCTION_WEB_API_URL, new HttpClientHandler());
But this is incomplete,its still not working, what exactly do I have to do to make this work, any guidance is much appreciated.

From my understanding, you are using a Forms/PCL project whereas the other solution was implementing this code inside their Android project.
For you, once you add using Xamarin.Android.Net; to the class, you should be able to just do this:
public static MobileServiceClient MobileServiceAndroid =
new MobileServiceClient(AppConstants.AZURE_PRODUCTION_WEB_API_URL, new AndroidClientHandler());
Most likely you might have issues getting that using statement, for that you will have to follow steps shown here, or customized for you in the following steps:
Add the Xamarin Forms project to all your projects.
Create an interface ICustomClientHandler in the Core project
using System;
using System.Net.Http;
namespace Test
{
public interface ICustomClientHandler
{
HttpClientHandler GetHandler();
}
}
Then create a CustomClientHandler in the Droid project, which will be the Android part of the dependency service that will help you retrieve the native AndroidClientHandler
using System.Net.Http;
using Xamarin.Android.Net;
using System.Runtime.CompilerServices;
using Xamarin.Forms;
using Test;
[assembly: Xamarin.Forms.Dependency(typeof(Test.Droid.CustomClientHandler))]
namespace Test.Droid
{
public class CustomClientHandler : ICustomClientHandler
{
public HttpClientHandler GetHandler()
{
return new AndroidClientHandler();
}
}
}
Implement an iOS version as well in a similar way, but it will instead return new HttpClientHandler();
Finally, use the code as shown, in your Core project:
var clientHandler = DependencyService.Get<ICustomClientHandler>().GetHandler();
public static MobileServiceClient MobileServiceAndroid =
new MobileServiceClient(AppConstants.AZURE_PRODUCTION_WEB_API_URL, clientHandler);

Related

Programmatically open an Excel document with connection to a Tabular Model

I would like to create a spreadsheet programmatically with a connection to my Tabular Model that exists in an Azure Analysis Server when the user clicks on some button. Similar to the option available in Azure to View Tabular Model which creates an odc file which you can open in Excel. My application is hosted on Azure and I am using .NET Core 2.2 as back-end.
I am quite clueless how can I achieve this. Has anyone managed to implement such functionality?
The simplest thing is to just generate and deliver an odc file, which will open with Excel.
First create an .odc file pointing to your AAS server, per docs: Create an Office Data Connection file.
Then add that .odc file to your web app, and serve it through a controller like this:
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using System.IO;
using System.Text;
namespace WebApplication4.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class OdcController : ControllerBase
{
private IHostingEnvironment hostingEnvironment;
public OdcController(IHostingEnvironment env)
{
hostingEnvironment = env;
}
// GET api/values
[HttpGet]
public ActionResult Get()
{
var fp = Path.Combine(_env.ContentRootPath, "model.odc");
var odcText = System.IO.File.ReadAllText(fp);
//optionally modify odcText
return File(Encoding.ASCII.GetBytes(odcText), "application/octet-stream", "model.odc");
}
}
}

Owin startup bug with Umbraco Cloud - netStandard reference missing

In our current Umbraco Cloud project, we are using the Hangfire library (1.6.17) - the lib has a OWIN dependency (1.0.0).
Here is the code to call the hangfire launch:
In our current project, we are using the Hangfire library (1.6.17) - the lib has a OWIN dependency (1.0.0).
Here is the code to call the hangfire launch:
using Microsoft.Owin;
using Owin;
using Umbraco.Web;
using Hangfire;
using Hangfire.Dashboard;
using Hangfire.Annotations;
using Umbraco.Core.Models;
using Umbraco.Core;
using System.Web;
[assembly: OwinStartup(typeof(XX.Web.Core.Startup))]
namespace XX.Web.Core
{
public class Startup : UmbracoDefaultOwinStartup
{
public override void Configuration(IAppBuilder app)
{
//ensure the default options are configured
base.Configuration(app);
var cs = Umbraco.Core.ApplicationContext.Current.DatabaseContext.ConnectionString;
GlobalConfiguration.Configuration.UseSqlServerStorage(cs);
app.UseHangfireDashboard("/umbraco/backoffice/hangfire", new DashboardOptions
{
Authorization = new[] { new UmbracoUserAuthorisedFilter() },
AppPath = "/Umbraco"
});
app.UseHangfireServer();
}
}
public class UmbracoUserAuthorisedFilter : IDashboardAuthorizationFilter
{
public bool Authorize([NotNull] DashboardContext context)
{
// In case you need an OWIN context, use the next line,
// `OwinContext` class is the part of the `Microsoft.Owin` package.
//var context = new OwinContext(owinEnvironment);
// Allow all authenticated users to see the Dashboard (potentially dangerous).
//return context.Authentication.User.Identity.IsAuthenticated;
//this if you want to lock down to admins only
var userService = ApplicationContext.Current.Services.UserService;
var user = userService.GetByUsername(HttpContext.Current.User.Identity.Name);
return user.IsAdmin();
//this if you just want to make sure user is logged into backoffice
//return UmbracoContext.Current.Security.CurrentUser != null;
}
}
}
This is the default hangfire startup code to be able to use the library. The code has been working fine on 2 local machines, one Azure Web App instance but when I push this code to the Umbraco Cloud branch I get the following error:
Could not load file or assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The system cannot find the file specified.
The issue is: we are not using .net standard, both projects (web and core) are using .net framework 4.6.2
Is there any workaround for that issue ?

Where to store ODP.NET Managed Driver Connection Strings?

Finally got ODP.NET configured and the Oracle.ManagedDataAccess DLL referenced in the project.
I was testing with a TNS connection in the code behind in a WPF project (see below).
This question is probably elementary, but I can't find any good information on this, as all examples/jump-starts show embedding the connection string like this.
Is there a better (more common) way to store the connection string for ODP.NET to make it easier to maintain (i.e. it should be a configuration change that doesn't require completely rebuilding the code if it should change)? For example, similar to storing in app.config for SQL Server and IIS?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Oracle.ManagedDataAccess.Client;
using Oracle.ManagedDataAccess.Types;
namespace TEST
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private OracleConnection con;
public MainWindow()
{
InitializeComponent();
try
{
con = new OracleConnection("User Id=*****; Password=******; Data Source=******");
con.Open();
}
catch (OracleException oracleErr)
{
MessageBox.Show(oracleErr.Message);
}
finally
{
con.Close();
}
}
}
}
In case you used the tnsnames.ora from the Oracle Client for the unmanaged version then for the managed version you just have to copy the tnsnames to your project directory.

Problems using an installclass in a web setup for a web site

I am trying to create a web setup for my web site, and I want to use an installer class to do some custom stuff. I am using VS 2010, and the web site and installer is .NET 3.5.
I have added reference to the installer class project output in the Install section under Custom Actions:
I have also set /targetdir="[TARGETDIR]/" on the CustomActionData for this action.
The InstallScript project is a standard class library (dll).
There is a public class that inherits from Installer class. It overrides the Install method as I have seen been done in several online examples:
using System.Collections;
using System.Windows.Forms;
namespace InstallScript
{
public class MyWebInstaller : System.Configuration.Install.Installer
{
public override void Install(IDictionary stateSaver)
{
base.Install(stateSaver);
var targetDir = Context.Parameters["targetdir"];
if(targetDir==null) targetDir = "No TARGETDIR!";
MessageBox.Show("TARGETDIR:\t" + targetDir);
}
}
}
I would think there should be shown a message box here som time during the install, but it seems like it is never called. No error is shown either. The setup just runs through as if this code was never called.
Anyone have idea of what is wrong?
OK, I found out what was missing.
You need to specify the class with the class attribute RunInstaller(true) for the setup to pick up and actually run the code.
So the class needs to be declared like this:
[System.ComponentModel.RunInstaller(true)]
public class MyWebInstaller : System.Configuration.Install.Installer
{
...

get main window

I am trying to get the main Window of an application written C#.
Application.MainWindow Property does not work :(
uses:
using System;
using System.Windows;
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing;
using System.Collections.Generic;
using My;
using MyDialogs;
using System.Xml.Serialization;
using System.Runtime.InteropServices;
using System.Windows.Input;
using System.Windows.Media;
using System.Threading;
using System.Windows.Interop;
Do you have a line of code like this in your application anywhere?
Application.Run(new Form1());
Where Form1 is the type of the form that is created when your application starts. This is code created by default when you create a new Windows Forms application. If you want to remember that instance, you just need to store the result in a variable accessible by other classes. For example:
static class Program
{
public static Form1 MainForm;
// ...
static void Main()
{
// ...
MainForm = new Form1();
Application.Run(MainForm);
}
}
I think your application type is Windows Forms application. That follows from you post:
I have this
private static void Main(string[] args) {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
So, you can't use MainWindow object (type of System.Windows.Window), because it's using in WPF. Create new WPF project, and you can acces Application.MainWindow property.

Resources