搭建一个舒适的 .NET Core 开发环境

最近,一直在往.Net Core上迁移,随着工作的深入,发现.Net Core比.Net Framework好玩多了。不过目前还在windows下开发,虽然VisualStudio是宇宙第一神器,但是最近经常莫名其妙的重启,让我对它有些不放心了,干脆在MacOS搭建一个开发环境,还附带装13效果:)

  1. 首先安装.Net Core SDK
  2. 安装VS Code及插件
  3. 安装Git
  4. 配置Nuget源,推荐使用博客园的镜像
  5. 克隆代码,然后编译
  6. 由于涉及到跨域等问题,该项目必须使用域名地址寄宿
    public class Program
    {
        public static void Main(string[] args)
        {
            var host = new WebHostBuilder()
                .UseKestrel()
                .UseContentRoot(Directory.GetCurrentDirectory())
                .UseUrls("http://project.domain.com:9000")
                .UseIISIntegration()
                .UseStartup<Startup>()
                .Build();

            host.Run();
        }
    }
  1. 因此需要手动设置host:打开Finder,Go -> Go to Folder: /private/etc/ 找到hosts文件,添加我们的域名解析(如下最后一行):
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1	localhost
255.255.255.255	broadcasthost
::1             localhost
127.0.0.1	project.domain.com
  1. dotnet run跑一下,使用project.domain.com:9000访问

  2. 由于调用了其他项目的API,而在MacOS中没有部署,所以报错了。而其他项目目前还是没有迁移,所以只能开虚拟机寄宿他们。

  3. 我们使用VMWare Fusion运行Windows10,设置Windows的上网方式为NAT,这样还有个问题,那就是Windows的IP地址是动态分配的,所以下一步就是固定Windows的IP地址。

  4. 打开终端,进入/Library/Preferences/VMware Fusion/vmnet8,使用vi dhcpd.conf打开文件

subnet 192.168.199.0 netmask 255.255.255.0 {//子网掩码
        range 192.168.199.128 192.168.199.254;//IP地址分布范围
        option broadcast-address 192.168.199.255;//默认网关
        option domain-name-servers 192.168.199.2;//DNS
        option domain-name localdomain;
        default-lease-time 1800;                # default is 30 minutes
        max-lease-time 7200;                    # default is 2 hours
        option netbios-name-servers 192.168.199.2;
        option routers 192.168.199.2;
}
host vmnet8 {
        hardware ethernet 00:50:56:C0:00:08;
        fixed-address 192.168.199.1;
        option domain-name-servers 0.0.0.0;
        option domain-name "";
        option routers 0.0.0.0;
}

打开windows10的控制面板,设置当前的网络的属性中的TCP/IPV4的属性,根据上面的注释,依次填空,注意IP地址不能超过上面注释中的范围。
最后在Mac中ping一下虚拟机的地址,确认可以访问。
如此,就能固定虚拟机的IP地址了,现在只要把需要用到的项目的地址在Hosts文件中设置好即可。

##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1	localhost
255.255.255.255	broadcasthost
::1             localhost
127.0.0.1	project.domain.com
192.168.199.133 login.domain.com
192.168.199.133 common.domain.com

折腾了这么久,终于可以在Mac下愉快的玩耍了。


现在有个问题很尴尬,我们已经在hosts文件中,把域名指向了开发环境,那么我们自己要访问生产环境的话,还需要更改hosts,这时候就可以发挥shell脚本的功能了。

个人比较喜欢powershell,推荐各位童鞋尝试一下。

首先把/private/etc/hosts复制到~/Documents/shell/hosts/original/hosts,这个用于恢复正常环境,然后再复制一份到~/Documents/shell/hosts/modified/hosts,这一个用于开发环境。
然后再~/Documents/shell/下新建两个powershell脚本,如下:

Copy-Item /Users/当前用户名称/Documents/shell/hosts/modified/hosts /private/etc/hosts
Write-Host "It's workspace!"

保存为To_workspace.ps1

Copy-Item /Users/当前用户名称/Documents/shell/hosts/original/hosts /private/etc//hosts
Write-Host "It's relax time!"

保存为To_relaxtime.ps1

使用方式

切换到开发环境,打开终端,输入:powershell回车,把To_workspace.ps1直接拖进去回车。
同样的,把To_relaxtime.ps1拖进去回车就会恢复正常状态。

posted @ 2017-02-21 23:06  蝌蝌  阅读(1285)  评论(4编辑  收藏  举报