improve appsettings.json in Maui client
This commit is contained in:
parent
d518860a34
commit
5be640632b
|
@ -1,18 +1,52 @@
|
||||||
<DynamicComponent Type="@ComponentType" Parameters="@Parameters"></DynamicComponent>
|
@using System.Text.Json;
|
||||||
|
@using System.Text.Json.Nodes;
|
||||||
|
|
||||||
@code {
|
@if (string.IsNullOrEmpty(message))
|
||||||
Type ComponentType = Type.GetType("Oqtane.App, Oqtane.Client");
|
{
|
||||||
private IDictionary<string, object> Parameters { get; set; }
|
<DynamicComponent Type="@ComponentType" Parameters="@Parameters"></DynamicComponent>
|
||||||
|
}
|
||||||
protected override void OnInitialized()
|
else
|
||||||
{
|
{
|
||||||
Parameters = new Dictionary<string, object>();
|
<br /><br /><center>@message</center>
|
||||||
Parameters.Add(new KeyValuePair<string, object>("AntiForgeryToken", ""));
|
}
|
||||||
Parameters.Add(new KeyValuePair<string, object>("Runtime", "Hybrid"));
|
|
||||||
Parameters.Add(new KeyValuePair<string, object>("RenderMode", "Hybrid"));
|
@code {
|
||||||
Parameters.Add(new KeyValuePair<string, object>("VisitorId", -1));
|
Type ComponentType = Type.GetType("Oqtane.App, Oqtane.Client");
|
||||||
Parameters.Add(new KeyValuePair<string, object>("RemoteIPAddress", ""));
|
private IDictionary<string, object> Parameters { get; set; }
|
||||||
Parameters.Add(new KeyValuePair<string, object>("AuthorizationToken", ""));
|
private string message = "";
|
||||||
}
|
|
||||||
|
protected override void OnInitialized()
|
||||||
|
{
|
||||||
|
Parameters = new Dictionary<string, object>();
|
||||||
|
Parameters.Add(new KeyValuePair<string, object>("AntiForgeryToken", ""));
|
||||||
|
Parameters.Add(new KeyValuePair<string, object>("Runtime", "Hybrid"));
|
||||||
|
Parameters.Add(new KeyValuePair<string, object>("RenderMode", "Hybrid"));
|
||||||
|
Parameters.Add(new KeyValuePair<string, object>("VisitorId", -1));
|
||||||
|
Parameters.Add(new KeyValuePair<string, object>("RemoteIPAddress", ""));
|
||||||
|
Parameters.Add(new KeyValuePair<string, object>("AuthorizationToken", ""));
|
||||||
|
|
||||||
|
if (MauiConstants.UseAppSettings)
|
||||||
|
{
|
||||||
|
string file = Path.Combine(FileSystem.Current.AppDataDirectory, "appsettings.json");
|
||||||
|
if (File.Exists(file))
|
||||||
|
{
|
||||||
|
using FileStream stream = File.OpenRead(file);
|
||||||
|
using StreamReader reader = new StreamReader(stream);
|
||||||
|
var content = reader.ReadToEnd();
|
||||||
|
var obj = JsonSerializer.Deserialize<JsonObject>(content)!;
|
||||||
|
if (string.IsNullOrEmpty((string)obj["Url"]) && string.IsNullOrEmpty(MauiConstants.ApiUrl))
|
||||||
|
{
|
||||||
|
message = "You Must Set The Url In Either MauiConstants.cs Or " + file;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(MauiConstants.ApiUrl))
|
||||||
|
{
|
||||||
|
message = "You Must Set The Url In MauiConstants.cs";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
13
Oqtane.Maui/MauiConstants.cs
Normal file
13
Oqtane.Maui/MauiConstants.cs
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
namespace Oqtane.Maui;
|
||||||
|
|
||||||
|
public static class MauiConstants
|
||||||
|
{
|
||||||
|
// the API service url (used as fallback if not set in appsettings.json)
|
||||||
|
public static string ApiUrl = "";
|
||||||
|
//public static string ApiUrl = "http://localhost:44357/"; // for local development (Oqtane.Server must be already running for MAUI client to connect)
|
||||||
|
//public static string apiurl = "http://localhost:44357/sitename/"; // local microsite example
|
||||||
|
//public static string apiurl = "https://www.dnfprojects.com/"; // for testing remote site
|
||||||
|
|
||||||
|
// specify if you wish to allow users to override the url via appsettings.json in the AppDataDirectory
|
||||||
|
public static bool UseAppSettings = true;
|
||||||
|
}
|
|
@ -6,19 +6,12 @@ using Oqtane.Modules;
|
||||||
using Oqtane.Services;
|
using Oqtane.Services;
|
||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using Windows.Storage.Provider;
|
|
||||||
using System.Text.Json.Nodes;
|
using System.Text.Json.Nodes;
|
||||||
|
|
||||||
namespace Oqtane.Maui;
|
namespace Oqtane.Maui;
|
||||||
|
|
||||||
public static class MauiProgram
|
public static class MauiProgram
|
||||||
{
|
{
|
||||||
// the API service url - can be overridden in an appsettings.json in AppDataDirectory
|
|
||||||
|
|
||||||
static string apiurl = "http://localhost:44357/"; // for local development (Oqtane.Server must be already running for MAUI client to connect)
|
|
||||||
//static string apiurl = "http://localhost:44357/sitename/"; // local microsite example
|
|
||||||
//static string apiurl = "https://www.dnfprojects.com/"; // for testing remote site
|
|
||||||
|
|
||||||
public static MauiApp CreateMauiApp()
|
public static MauiApp CreateMauiApp()
|
||||||
{
|
{
|
||||||
var builder = MauiApp.CreateBuilder();
|
var builder = MauiApp.CreateBuilder();
|
||||||
|
@ -33,16 +26,19 @@ public static class MauiProgram
|
||||||
builder.Services.AddBlazorWebViewDeveloperTools();
|
builder.Services.AddBlazorWebViewDeveloperTools();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
LoadAppSettings();
|
var apiurl = LoadAppSettings();
|
||||||
|
|
||||||
var httpClient = new HttpClient { BaseAddress = new Uri(GetBaseUrl(apiurl)) };
|
if (!string.IsNullOrEmpty(apiurl))
|
||||||
httpClient.DefaultRequestHeaders.UserAgent.ParseAdd(Shared.Constants.MauiUserAgent);
|
{
|
||||||
httpClient.DefaultRequestHeaders.Add(Shared.Constants.MauiAliasPath, GetUrlPath(apiurl).Replace("/", ""));
|
var httpClient = new HttpClient { BaseAddress = new Uri(GetBaseUrl(apiurl)) };
|
||||||
builder.Services.AddSingleton(httpClient);
|
httpClient.DefaultRequestHeaders.UserAgent.ParseAdd(Shared.Constants.MauiUserAgent);
|
||||||
builder.Services.AddHttpClient(); // IHttpClientFactory for calling remote services via RemoteServiceBase
|
httpClient.DefaultRequestHeaders.Add(Shared.Constants.MauiAliasPath, GetUrlPath(apiurl).Replace("/", ""));
|
||||||
|
builder.Services.AddSingleton(httpClient);
|
||||||
|
builder.Services.AddHttpClient(); // IHttpClientFactory for calling remote services via RemoteServiceBase
|
||||||
|
|
||||||
// dynamically load client assemblies
|
// dynamically load client assemblies
|
||||||
LoadClientAssemblies(httpClient);
|
LoadClientAssemblies(httpClient, apiurl);
|
||||||
|
}
|
||||||
|
|
||||||
// register localization services
|
// register localization services
|
||||||
builder.Services.AddLocalization(options => options.ResourcesPath = "Resources");
|
builder.Services.AddLocalization(options => options.ResourcesPath = "Resources");
|
||||||
|
@ -67,28 +63,36 @@ public static class MauiProgram
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private static void LoadAppSettings()
|
private static string LoadAppSettings()
|
||||||
{
|
{
|
||||||
// appsettings.json file format
|
var url = MauiConstants.ApiUrl;
|
||||||
// {
|
if (MauiConstants.UseAppSettings)
|
||||||
// "Url": "http://localhost:44357/"
|
|
||||||
// }
|
|
||||||
|
|
||||||
string file = Path.Combine(FileSystem.Current.AppDataDirectory, "appsettings.json");
|
|
||||||
if (File.Exists(file))
|
|
||||||
{
|
{
|
||||||
using FileStream stream = File.OpenRead(file);
|
string file = Path.Combine(FileSystem.Current.AppDataDirectory, "appsettings.json");
|
||||||
using StreamReader reader = new StreamReader(stream);
|
if (File.Exists(file))
|
||||||
var content = reader.ReadToEnd();
|
|
||||||
var obj = JsonSerializer.Deserialize<JsonObject>(content)!;
|
|
||||||
if (!string.IsNullOrEmpty((string)obj["Url"]))
|
|
||||||
{
|
{
|
||||||
apiurl = (string)obj["Url"];
|
using FileStream stream = File.OpenRead(file);
|
||||||
|
using StreamReader reader = new StreamReader(stream);
|
||||||
|
var content = reader.ReadToEnd();
|
||||||
|
var obj = JsonSerializer.Deserialize<JsonObject>(content)!;
|
||||||
|
if (!string.IsNullOrEmpty((string)obj["Url"]))
|
||||||
|
{
|
||||||
|
url = (string)obj["Url"];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// create template appsettings.json file
|
||||||
|
using (StreamWriter writer = File.CreateText(file))
|
||||||
|
{
|
||||||
|
writer.WriteLine("{ \"Url\": \"\" }");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return url;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void LoadClientAssemblies(HttpClient http)
|
private static void LoadClientAssemblies(HttpClient http, string apiurl)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
@ -227,7 +231,7 @@ public static class MauiProgram
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Debug.WriteLine($"Oqtane Error: Loading Client Assemblies {ex}");
|
Debug.WriteLine($"Error Loading Client Assemblies From {apiurl} - {ex}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user