Launching 2 and more WebApps - c#

I want to launch 2 or more webApp. How do I do that in .NET? I am newby to C# and .NET.
class Program
{
static void Main(string[] args)
{
launchService("localhost:4234");
launchService("localhost:4265");
}
public static void launchService(Component component)
{
using (WebApp.Start<Startup>(component.Url()))
{
Console.WriteLine("Running on {0}", component.Url());
Console.WriteLine("Press enter to exit");
Console.ReadLine();
}
}
}

This should be some thing as below:
class Program
{
static IDisposable app1;
static IDisposable app2;
static void Main(string[] args)
{
launchServices("http://localhost:4234", "http://localhost:4265");
Console.ReadLine();
app1.Dispose();
app2.Dispose();
}
public static void launchServices(string url1, string url2)
{
app1 = WebApp.Start<StartupApp1>(url1);
app2 = WebApp.Start<StartupApp2>(url2);
}
}
Following links may help you further:
http://www.asp.net/web-api/overview/hosting-aspnet-web-api/use-owin-to-self-host-web-api
OWIN cannot run multiple apps in isolation using webapp.start
Note: There may be some compilation errors/typos in above code as I have just typed it using my IPad.

Related

C# - Use Async in Main function

As per this post, the top voted answer suggested that we can use async directly in main. Or I misunderstood it?
Can't specify the 'async' modifier on the 'Main' method of a console app
My main class:
public class Program
{
public static async Task Main(string[] args)
{
ApproveEvents ap = new ApproveEvents();
List<MyModel> result = new List<MyModel>();
result = await ap.ApproveAsync();
if (result.count > 0)
{
//do something here
}
}
}
And,
public class ApproveEvents
{
public async Task<List<MyModel>> ApproveAsync()
{
//blah blah
}
}
Visual Studio 2017 is complaining about no Main method for an entry point.
How should I resolve this?
async Task Main is available in C# 7.1. You can change it in build properties (the default is the latest major version, which is 7.0)
i'd recommend you looking at this topic to help you, it speaks right into your issue.
it stated:
As I showed above, if you want to await an async operation in the
entrypoint method, you need to apply some workaround, because the
following entrypoint definition is invalid:
public static async Task Main(string[] args)
{
await BuildWebHost(args).RunAsync();
}
in order to make this work you will need to do the following workaroung:
public static void Main(string[] args)
{
BuildWebHost(args).RunAsync().GetAwaiter().GetResult();
}
or calling wait() on the task object itself:
public static void Main(string[] args)
{
BuildWebHost(args).RunAsync().Wait();
}
there is a list of valid entry points in C# 7.1, this is the up to date list:
public static void Main();
public static int Main();
public static void Main(string[] args);
public static int Main(string[] args);
public static Task Main();
public static Task<int> Main();
public static Task Main(string[] args);
public static Task<int> Main(string[] args);

In C# , in Visual Studio, using a Console Application, is there a way to make methods in a class and call them in main program using readline?

In C# , in Visual Studio, using a Console Application, is there a way to make methods in a class and call them in main program using readline?
Aka, a way to choose which methods to open when the program is running.
Easiest way is a switch statement for <4 cases, and a Dictionary for 4 or more cases.
class Program
{
private static IDictionary<string, Action> MethodMappings = new Dictionary<string, Action> {
{"Method1", Method1},
{"Method2", Method2},
...
}
public static void Main(string[] args) {
var methodCall = Console.ReadLine();
if (!MethodMappings.ContainsKey(methodCall)) {
//unrecognized command
}
MethodMappings[methodCall].Invoke();
}
private static void Method1() { ... }
private static void Method2() { ... }
}
This is very much possible using Reflection. Here is sample code to help you out:
class Program
{
public static void Hello1()
{
Console.WriteLine("\nHello 1");
}
public static void Hello2()
{
Console.WriteLine("\nHello 2");
}
static void Main(string[] args)
{
while (true)
{
String method = Console.ReadLine();
Type methodType = typeof(Program);
MethodInfo voidMethodInfo = methodType.GetMethod(method);
voidMethodInfo.Invoke(method,null);
}
}
}
For more information you can visit here.

C# code extract FROM methods

Does someone know a c# tool that could extract all code in given class from methods?
Example starting file:
public class HelloWorld
{
public static void Main()
{
Print("Hello, ");
Print("World");
PrintExclamationMark();
}
private static void Print(string text)
{
System.Console.WriteLine(text);
}
private static void PrintExclamationMark();
{
System.Console.WriteLine("!");
}
}
Result I would like to get after using the tool on given class/file:
public class HelloWorld
{
public static void Main()
{
System.Console.WriteLine("Hello, ");
System.Console.WriteLine("World");
System.Console.WriteLine("!");
}
}
Such tool could be very helpful when I extracting a lot of new methods and double checking if no code was omitted.

Run a windows form while my console application still running

I'm new at C# programming and i'm lost with a thing that could be simple.
Executing a console application, at a moment i need to call a Windows Form that will show statics of the execution but when i call the form1.ShowDialog(); this stop the console runtime.
How do i keep my console execution alive while i show a Windows form screen ?
class Program
{
static Form1 form = new Form1();
public static bool run = true;
static void Main(string[] args)
{
work();
}
public static void work()
{
form.Show();
while (run)
{
Console.WriteLine("Console still running");
}
}
}
try this it work on me
using System.Windows.Forms;
using System.Threading;
namespace ConsoleApplication1
{
class Program
{
public static bool run = true;
static void Main(string[] args)
{
Startthread();
Application.Run(new Form1());
Console.ReadLine();
}
private static void Startthread()
{
var thread = new Thread(() =>
{
while (run)
{
Console.WriteLine("console is running...");
Thread.Sleep(1000);
}
});
thread.Start();
}
}
}
Threading is like "process inside a process" in my own understanding.
See this question. You have to use Form1.Show() because Form1.ShowDialog() pauses execution until the form is closed.
Update This seems to be working (with Application.Run):-
public static Form1 form = new Form1();
public static bool run = true;
[MTAThread]
static void Main(string[] args)
{
new Thread(() => Application.Run(form)).Start();
new Thread(work).Start();
}
public static void work()
{
while (run)
{
Console.WriteLine("Console Running");
}
}

Why won't my c# program run?

I'm a uni student just starting to learn c#. I'm sure there is a simple solution to this but I have searched and I don't think know enough yet.
this is my program, note that I have not finished a few functions.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
public void welcome()
{
Console.WriteLine("Fuel Consumption Calculator "+"r/n"+"Are you using Metric 1 or Imperial 2 ?");
}
public void check()
{
string choice;
choice = Console.ReadLine();
if (choice == "1")
{
calcmetric();
}
else
{
calcimperial();
}
}
public void calcmetric()
{
}
public void calcimperial()
{
}
}
}
}
In Visual Studio I have two errors: one saying a '}' is expected after Main; and there is an error at the very end saying "type or namespace definition error".
You are declaring methods inside a method. This is wrong.
Change it:
class Program
{
static void Main(string[] args)
{
//call other methods here
welcome();
check();
//....
}
public static void welcome()
{
Console.WriteLine("Fuel Consumption Calculator "+"r/n"+"Are you using Metric 1 or Imperial 2 ?");
}
public static void check()
{
string choice;
choice = Console.ReadLine();
if (choice == "1")
{
calcmetric();
}
else
{
calcimperial();
}
}
public static void calcmetric()
{
}
public static void calcimperial()
{
}
}

Categories