Is it possible to use the HttpClient class in MonoDroid? - xamarin.android

I'm trying to use the HttpClient class on a MonoDroid project but it looks like the System.Net.http namespace it's not valid.
I try to add a reference in the project to System.Net.http.dll but it doesn't seem to be available in the references list.
Any idea?
Thks

HttpClient is a .NET 4.5 class, which is not available yet in Mono for Android. Mono itself added supported for it in version 3.0, but Mono for Android is still based on Mono 2.10. I know Xamarin is working on updating Mono for Android (and MonoTouch) to Mono 3.0 now, but as far as I know there's no release date set yet.

I know it is an old thread but I just saw that Xamarin has finally given System.Net.Http in Xamarin.Android 4.8, so thought to share it with you too.
Thanks.

You can't use HttpClient (yet!), but you can still use the System.Net.HttpWebRequest object, which does actually do what HttpClient can provide convenient wrappers for (especially when hitting up a Web API controller).
Here's a sample from a current project I'm working on (it's using the monodroid port of NewtonSoft.Json, not the standard System.Runtime.Serialization.Json) :
private void AddARecord()
{
var cartesian = new Cartesian()
{
Description = "next item blah",
X = 5,
Y = 10,
Z = 15,
};
string json = JsonConvert.SerializeObject(cartesian);
var request = new HttpWebRequest(new Uri(_url)) {ContentType = "application/json", Method = "POST"};
var sw = new StreamWriter(request.GetRequestStream(), System.Text.Encoding.ASCII);
sw.Write(json);
sw.Close();
request.BeginGetResponse(ProcessJsonResponseForSingleResult, request);
}
...the Web API controller I'm hitting does something arbitrary, saves the object I just sent, and then tweaks the description so I know it works. Then it sends the tweaked object back...
And then the callback ProcessJsonResponseForSingleResult looks like
private void ProcessJsonResponseForSingleResult(IAsyncResult ar)
{
var request = (HttpWebRequest)ar.AsyncState;
var response = request.EndGetResponse(ar);
using (var outputStream = new StreamReader(response.GetResponseStream(), System.Text.Encoding.ASCII))
{
var jsonString = outputStream.ReadToEnd();
Log.Info("PJRFSR", string.Format("JSON string: {0} - deserialising...", jsonString));
var cartesian = JsonConvert.DeserializeObject<Cartesian>(jsonString);
RunOnUiThread(() => UpdateUiTextView(cartesian.Description));
}
}
Yeah, I know, it uses the BeginAsync/EndAsync pattern which I don't like any more either, but it does work if you just need to get something done.

Related

Enabling Single Sign On in TEdgeBrowser

I have some C# code that shows how to enable Single Sign On in WebView2.
The TEdgeBrowser doesn't expose any of the properties that the C# code uses. In particular the interface defined in the Winapi.WebView2 unit for ICoreWebView2EnvironmentOptions doesn't have the functions for getting or setting AllowSingleSignOnUsingOSPrimaryAccount defined in it. I believe that this is because it was created from WebView2.tlb on 07/05/2020 whereas the property was added in the version released September 10, 2020.
What options do I have? Do I need to create my own version of WebView2 from the latest tlb and then duplicate the code in the Vcl.Edge unit to get a component with the SSO option enabled?
I don't need a visual component - I'd be happy to create the browser in code.
The C# code is:
private async void Form1_Load(object sender, EventArgs e)
{
var browser = new WebView2();
var options = new CoreWebView2EnvironmentOptions();
options.AllowSingleSignOnUsingOSPrimaryAccount = true;
var environment = await CoreWebView2Environment.CreateAsync(options: options).ConfigureAwait(false);
await browser.EnsureCoreWebView2Async(environment).ConfigureAwait(false);
Invoke((MethodInvoker)(() =>
{
browser.Dock = DockStyle.Fill;
this.Controls.Add(browser);
browser.Source = new Uri(https://example.com);
}));
}
Try WebView4Delphi instead. WebView4Delphi is fully updated to the latest WebView2 version and it supports all the WebView2 interfaces.
You only have to add this line before the GlobalWebView2Loader.StartWebView2 call :
GlobalWebView2Loader.AllowSingleSignOnUsingOSPrimaryAccount := True;
The demos use the initialization section of the main unit to create GlobalWebView2Loader and set the properties. If you use the SimpleBrowser demo as a template for your application then you would have to add the previous line here.
That property in GlobalWebView2Loader is used for all the browsers that share the same ICoreWebView2Environment which is the default behavior.
In case you need to create a browser with an independent ICoreWebView2Environment then you have to set this property before the TWVBrowserBase.CreateBrowser call :
MyWVBrowser.AllowSingleSignOnUsingOSPrimaryAccount := True;
MyWVBrowser would be an instance of TWVBrowser.

Using DataConnectionDialog

When I am attempting to use the DataConnectionDialog from NuGet (version 1.2), I receive the Advanced Settings dialog for setting up the Database Connection. Is there some Setting I have missed or additional library to retreive?
Code:
using System;
using Microsoft.Data.ConnectionUI;
DataConnectionDialog dcd = new DataConnectionDialog();
DataSource.AddStandardDataSources(dcd);
dcd.SelectedDataSource = DataSource.SqlDataSource;
dcd.SelectedDataProvider = DataProvider.SqlDataProvider;
DataConnectionDialog.Show(dcd);
Output:
What I want (this comes from the datasource wizard in Visual Studio Community 2015):
I happened to stumble on the same issue. From my main form, I called an async method using Task.Factory.StartNew. This method tries to open the Data Connection Dialog but it would show the Advance Settings dialog box instead.
During troubleshooting, I replaced the DataConnectionDialog with a OpenFileDialog and this gave me a ThreadStateException which pointed me towards the solution.
To solve it, I had to put the code in a separate function, e.g. AskConnectionString and call it using Control.Invoke.
e.g.
public void btnConnString_Click(object sender, EventArgs e)
{
_connectionString = (string)this.Invoke(AskConnectionString);
}
public string AskConnectionString()
{
DataConnectionDialog dcd = new DataConnectionDialog();
DataSource.AddStandardDataSources(dcd);
dcd.SelectedDataSource = DataSource.SqlDataSource;
dcd.SelectedDataProvider = DataProvider.SqlDataProvider;
DataConnectionDialog.Show(dcd);
return dcd.ConnectionString;
}

Get TFS version programmatically

How can I get the TFS version programmatically?
I am trying to get the version that shows up in the TFS Administration console.
I tried the following code, but it returns the server version as "Server Version: Dev14.M89-Part7", that doesn't seem correct.
var server = new TfsTeamProjectCollection(new Uri("http://tfs2015:8080/tfs"));
server.EnsureAuthenticated();
var serverVersion = server.ServerDataProvider.ServerVersion;
Console.WriteLine("Server Version: {0}", serverVersion);
I guess I am looking at the wrong property...
I'm using the Microsoft.TeamFoundation.Server.dll version number then the table from this link.
Unfortunately there is not some uniform method that you can call that will simply tell you “You are communicating with version X of TFS”. In order to determine what version of the server you are talking to we are going to use the principals about querying for services along with some knowledge about what services were available in each release.
Check this blog:http://blogs.msdn.com/b/taylaf/archive/2010/01/05/determining-the-tfs-server-version-using-client-apis.aspx
Another approach can be to pick the version number from a DLL, but requires to reach the server via PSExec, CIFS/SMB or Powershell Remoting.
The C# code should be something like
using (var tfsBaseKey = Registry.LocalMachine.OpenSubKey(#"SOFTWARE\Microsoft\TeamFoundationServer"))
{
var versionKeys = tfsBaseKey.GetSubKeyNames();
double dummy;
double maxVersion = versionKeys.Max(x => double.TryParse(x, out dummy) ? dummy : 0.0);
var latestVersionKey = maxVersion.ToString("#.0");
using (var tfsKey = tfsBaseKey.OpenSubKey(latestVersionKey))
{
string tfsInstallPath = tfsKey.GetValue("InstallPath").ToString();
string refAssemblyPath = Path.Combine(tfsInstallPath, #"Application Tier\Web Services\bin\Microsoft.TeamFoundation.Server.Core.dll");
var refAssembly = Assembly.ReflectionOnlyLoadFrom(refAssemblyPath);
var fileVer = refAssembly.GetCustomAttributes(typeof(AssemblyFileVersionAttribute), false).FirstOrDefault() as AssemblyFileVersionAttribute;
return fileVersion.Version;
}
}

F# passing null reference to ref parameter

I am using a third party API that rather clumsily makes use of ref parameters to produce outputs. Personally I really hate this design of an API but it's what I have available to me right now. I've had to hide the datatypes of the API slightly due to proprietary code but this should be irrelevant to the problem at hand.
Anyway in C# I can pass a null reference as a ref parameter successfully as follows:
IDataType tl = null;
bool success = api.myFunction(ref tl);
However in F# the following will not work
let mutable tl : IDataType = null //null reference assignment in F#
let success = api.myFunction(&tl) //& means ref in F#
It returns a null reference exception error. No such error is returned in C#.
Has anyone experiences this before? I am thinking it must be a bug in the API itself which is relatively ancient design.
**Edit: This should be closed, I believe the answer does not lie in the F# code but in the API as it's already a number of known bugs similar to this.
Quick and dirty prototyping of your API in C#
namespace API
{
public interface IDataType { void Hi(); }
public class API: IDataType {
public void Hi() { System.Console.WriteLine("Hi!"); }
public bool MyFunction(ref IDataType iface) {
iface = new API();
return true;
}
}
}
and then using it from F# exactly your way while staying within the same CLR:
let mutable iface : API.IDataType = null
if API.API().MyFunction(&iface) then iface.Hi()
works without any problem.
So, indeed, your problem is specific to your given API and has nothing to do with the form of its use from F#.
Using a ref cell is also an option here. Does this work?
let tl = ref null
let success = api.myFunction(tl)
The problem was with the API being compiled in .NET 2.0 which works fine under C# but not F#.

How to Communiccate between JD Edwards Enterprise one 9.0 and .Net

I have problem that how to communicate Jd Edwards Enterprise one 9.0 with .Net,and my project manager told me to look at the "fatclient" as it acts as a middle ware between these two,but there is no luck in my search,
Thanks in advance
Responding to this old post. Hopefully, this will help someone who has a similar need:
Take a look at our product LynX Business Integrator. It is Oracle Validated and it allows you to create integration processes natively in C# and publish it as a web service. So, you can write code like this:
private bool CallAddressBookBsfn(BusinessDocument businessDocument, Transaction transaction)
{
AddressBookMaster abm = businessDocument.document.input.AddressBook;
// create an instance of the Address Book Master Business function
// note the use of JDE Transactions
AddressBookMasterMBF bsfn = new AddressBookMasterMBF(transaction);
// set parameters - most of this code is auto-generated
bsfn.DpmnAddressBookNumber.InValue = (long)abm.AddressNumber;
bsfn.DpszSearchType.InValue = abm.AddressType;
bsfn.DpszAlphaName.InValue = abm.Name;
bsfn.DpszAddressLine1.InValue = abm.AddressLine1;
bsfn.DpszAddressLine2.InValue = abm.AddressLine2;
bsfn.DpszAddressLine3.InValue = abm.AddressLine3;
bsfn.DpszAddressLine4.InValue = abm.AddressLine4;
bsfn.DpszPostalCode.InValue = abm.ZipCodePostal;
bsfn.DpszCity.InValue = abm.City;
bsfn.DpszState.InValue = abm.State;
bsfn.DpszCountry.InValue = abm.Country;
bsfn.DpcActionCode.InValue = 'A';
bsfn.DpcUpdateMasterFile.InValue = '1';
// execute the business function
if (bsfn.Execute() != BusinessFunctionResult.Success)
{
// get errors
return false;
}
// assign output
businessDocument.document.output.AddressNumber = bsfn.DpmnAddressBookNumber.OutValue;
businessDocument.document.output.AddressNumberSpecified = true;
return true;
}
Take a look at our YouTube Channel at http://www.youtube.com/user/aelliuslynx and our product page at http://www.aellius.com/products/lynx-business-integrator
I am using old version 8.0 and if by "communication" you mean to be able from a .NET application to run JDE BSFN directly, then i am gonna dissapoint you but there isn't any way i know of.
Maybe things have changed in 9.0 but i doubt it.
Personally whenever i want to communicate with our JDE (AS400 based) i am using:
Frontend
-.NET Web Api services
-C# winforms apps
-ASP.NET
Backend
-Custom Dlls for Business logic and Data Access Layers.

Resources