-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
61 lines (50 loc) · 1.77 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using System.IO;
using System.Net;
using System.Linq;
using System;
using Microsoft.AspNetCore.Http;
using System.Net.Sockets;
namespace ServiceSample
{
public class Program
{
public static void Main(string[] args)
{
// var config = new ConfigurationBuilder()
// .SetBasePath(Directory.GetCurrentDirectory())
// .AddJsonFile("hosting.json", optional: true)
// .Build();
string currentIP = GetCurrentIP2();
Console.WriteLine("CurrentIP: " + currentIP);
string url = @"http://" + currentIP + ":5000";
var host = new WebHostBuilder()
//.UseConfiguration(config)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseUrls(url)
.UseStartup<Startup>()
.Build();
host.Run();
}
private static string GetCurrentIP()
{
string name = Dns.GetHostName();
IPAddress[] ipadrlist = Dns.GetHostAddresses(name);
string ip = ipadrlist.Where(item => item.AddressFamily
== System.Net.Sockets.AddressFamily.InterNetwork).LastOrDefault().ToString();
return ip;
}
private static string GetCurrentIP2()
{
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, 0);
socket.Connect("8.8.8.8", 65530);
IPEndPoint endPoint = socket.LocalEndPoint as IPEndPoint;
string localIP = endPoint.Address.ToString();
return localIP;
}
}
}