Tải bản đầy đủ (.pdf) (137 trang)

Transitioning to NET core on red hat enterprise linux

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (3.03 MB, 137 trang )


Programming



Transitioning to .NET Core on
Red Hat Enterprise Linux
Don Schenck


Transitioning to .NET Core on Red Hat Enterprise Linux
by Don Schenck
Copyright © 2017 O’Reilly Media. All rights reserved.
Printed in the United States of America.
Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North,
Sebastopol, CA 95472.
O’Reilly books may be purchased for educational, business, or sales
promotional use. Online editions are also available for most titles
( For more information, contact our
corporate/institutional sales department: 800-998-9938 or

Editors: Nan Barber and Susan Conant
Production Editor: Melanie Yarbrough
Copyeditor: Gillian McGarvey
Proofreader: Jasmine Kwityn
Interior Designer: David Futato
Cover Designer: Karen Montgomery
Illustrator: Rebecca Demarest
September 2016: First Edition



Revision History for the First Edition
2016-09-20: First Release
2016-10-21: Second Release
2016-12-06: Third Release
The O’Reilly logo is a registered trademark of O’Reilly Media, Inc.
Transitioning to .NET Core on Red Hat Enterprise Linux, the cover image,
and related trade dress are trademarks of O’Reilly Media, Inc.
While the publisher and the author have used good faith efforts to ensure that
the information and instructions contained in this work are accurate, the
publisher and the author disclaim all responsibility for errors or omissions,
including without limitation responsibility for damages resulting from the use
of or reliance on this work. Use of the information and instructions contained
in this work is at your own risk. If any code samples or other technology this
work contains or describes is subject to open source licenses or the
intellectual property rights of others, it is your responsibility to ensure that
your use thereof complies with such licenses and/or rights.
978-1-491-97055-3
[LSI]


What to Expect from This Book
This book is targeted at — but not limited to — .NET developers with any
level of experience. Starting with a basic “Hello World” console application,
it moves along through ASP.NET, MVC, and Entity Framework applications,
eventually arriving at a full-featured application running in a Linux container.
Because the code in this book is limited to C#, your existing knowledge of
.NET and C# will largely determine the speed at which you progress through
this volume. More experienced developers will be interested in the few key
differences when developing for Linux (and there are quite a few), whereas
beginning developers can use this book to learn more advanced programming

techniques — the fact that those techniques are related to .NET Core is just
icing on the cake.
If you’ve spent most of your time writing code in a language other than C#,
with Linux as your native operating system, don’t fret. Though knowledge of
C# is highly recommended, even a novice developer with a desire to learn
can use this volume, perhaps in concert with another manual that guides the
reader in C# development.


Some Assumptions
This book assumes some previous experience with the .NET Framework.
Again, while it’s technically not necessary to have prior knowledge when you
begin reading, you will need to be familiar with .NET (either by experience
or education) to understand what’s happening. Again, not to worry — the
knowledge needed isn’t as deep as you might fear. Grasp a few key concepts
and you’ll be good to go.
For the purposes of simplicity, the Linux distribution used in the examples
will be Red Hat Enterprise Linux (RHEL). Any adjustments necessary for
other Linux distributions will be pointed out as we go.


Your Environment
To follow along with the narrative and code, you need to install the Red Hat
Container Development Kit (CDK) and clone or download a repository from
GitHub. Step-by-step instructions are provided in Chapter 1.


Formatting
The following typographical conventions are used in this book:
Text will be in the typeface you are reading now.

// Code is formatted like this:
using System;
namespace ConsoleApplication
{
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}
Command-line commands will be shown like this

Italic
Indicates new terms, URLs, email addresses, filenames, and file
extensions.
Constant width
Used for program listings, as well as within paragraphs to refer to
program elements such as variable or function names, databases, data
types, environment variables, statements, and keywords.
Constant width bold
Shows commands or other text that should be typed literally by the user.
Constant width italic
Shows text that should be replaced with user-supplied values or by
values determined by context.


TIP
This element signifies a tip or suggestion.


NOTE
This element signifies a general note.

WARNING
This element indicates a warning or caution.


.NET Core Is the Future
In November 2014, Microsoft announced that the .NET Framework would be
transitioned to an open source development model. This announcement was
met with surprise and open arms by the .NET community. Although
Microsoft had, up to that moment, done an admirable job of soliciting input
from developers about .NET features and bugs, making the source available
meant a much higher level of engagement. The standard open source model
of using a GitHub repo — with issues, milestones, pull requests, and the
related discussions — meant that anyone, anywhere could have instant and
transparent access to the development team. Developers could participate in
discussions and research defects and workarounds. Schedules and roadmaps
would now be available to everyone, leading to more realistic expectations.
Surprise announcements would give way to planned milestones.
Meanwhile, developers outside the .NET world saw this as an opportunity to
bring their open source experience and knowledge to one of the more
pervasive software development platforms. The free exchange of ideas, code
reviews, and sense of community found its place inside the Microsoft-centric
developer’s world. The infusion of non-.NET developers meant that “dot
netters” could gain a broader view of development in general. This is a very
good thing.
This is the new model. This is the future.
.NET Core is not just another .NET Framework. Rather, it is the future of

.NET. Microsoft has stated that the previous framework, 4.6.1, will still be
supported, and bug fixes will be applied, but any new .NET features will be
reserved for .NET Core. Using .NET Core as your basis for future work is in
line with Microsoft’s stated intentions. While migrating existing code to
.NET Core may or may not be a good idea, future development should be
based on .NET Core if possible. In short, this is the .NET Framework. Period.
Full stop.


What’s Missing
Because some parts of .NET are either specific to Windows or outdated, not
everything made the journey from proprietary software to the open source
model and .NET Core 1.0.
Here’s a list of some of the things that didn’t make the transition:
MWF (Windows Forms)
Web forms
WCF (SOAP)
In addition, some things haven’t been ported yet — perhaps the biggest being
SignalR. Microsoft has publicly stated that SignalR will be ported to .NET
Core.

NOTE
From this point on, unless the difference is worth noting, references to .NET CoreCLR,
CoreFX, and the like, will be simplified to “.NET”.


What’s New
.NET is more modular now, which is a definite benefit from starting over
when designing the framework. Parts that were previously included by
default are now excluded by default. Even what may seem to be the most

basic items, such as the ability to render static files in an ASP.NET page, are
now optional. This means that you need to be more expressive in your
coding, but it also results in performance improvements. The trade-off is
worth the cost: writing code is easier than optimizing code.


Chapter 1. Setting Up Your
Environment
This chapter will guide you through setting up the environment used in the
examples in this book. It includes a Linux Virtual Machine (VM) running on
a Windows 10 machine, and a clone or download of a GitHub repository
(repo). The VM in this setup will use the Red Hat Container Development
Kit (CDK), which includes an image of Red Hat Enterprise Linux (RHEL),
Oracle’s Virtual Box, and Vagrant.


Your Red Hat Account
Before you can download the RHEL image and the CDK, you need to
register at the Red Hat Developer website. This is free; all you need is an
email address and a password. You can opt out of the Red Hat Developers
newsletter if you wish, but I suggest keeping it — at least initially.
This gives you access to all the tools and help, including the CDK (with
RHEL), forums, knowledge base, and more.
Keep your username and password handy; you’ll need them every time you
start your VM in order to register with Red Hat and enable your subscription.


Installing the CDK
The Red Hat CDK includes a copy of RHEL 7 that’s been preloaded with the
docker command and OpenShift. It runs the RHEL VM using Vagrant and

Virtual Box. Fortunately, if you’re not familiar with these technologies, the
CDK makes the process of installing them transparent. Basically, you
download, install, and run.
Once you’ve registered and logged in, select the TECHNOLOGIES menu
and the Red Hat Container Development Kit option, as shown in Figure 1-1.

Figure 1-1. Select Red Hat Container Development Kit


This takes you to the Container Development Kit page.
At the top, click the GET STARTED button to reveal your choice of
Windows or Mac/Linux assets. From there, simply follow the screen prompts
to download the CDK, as shown in Figure 1-2.

Figure 1-2. Container Development Kit download page

After the CDK installer is downloaded (the installer is development-suite1.0.0-GA-bundle-installer.exe, which is about 1.4 GB in size), open it and
follow the prompts to install the CDK on your Windows machine. You’ll
need your Red Hat username and password to get started, as you can see in
Figure 1-3.


Figure 1-3. Sign in to start the CDK installation


Disable Hyper-V
Before you can use Vagrant, you’ll need to disable Hyper-V on your
Windows machine. Use the Windows Features Control Panel applet to do
this, as shown in Figure 1-4.


Figure 1-4. Disable Hyper-V

If you need to switch between enabling and disabling Hyper-V, there’s a
helpful guide at Scott Hanselman’s blog.


Start Your VM
Now that you’ve installed everything, you can start the VM. Assuming that
you used the default values during the installation, open a command prompt,
navigate to the proper folder, and launch the VM using the vagrant up
command:
PS C:\DevelopmentSuite\cdk\components\rhel\rhel-ose> vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configurat...
default: Adapter 1: nat
default: Adapter 2: hostonly
==> default: Forwarding ports...
default: 22 => 2222 (adapter 1)
==> default: Running 'pre-boot' VM customizations...
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few...
default: SSH address: 127.0.0.1:2222
default: SSH username: vagrant
default: SSH auth method: password
default: Warning: Connection timeout. Retrying...
default: Warning: Remote connection disconnect. Retrying...
==> default: Machine booted and ready!
==> default: Registering box with vagrant-registration...

default: Would you like to register the system now (defau...
default: username: **redacted**
default: password:
[default] GuestAdditions 5.0.26 running --- OK.
==> default: Checking for guest additions in VM...
==> default: Configuring and enabling network interfaces...
==> default: Copying TLS certificates to C:/DevelopmentSuite/...
==> default: Rsyncing folder: /cygdrive/c/DevelopmentSuite/cdk/...
==> default: Mounting SMB shared folders...
default: C:/shared => /shared
==> default: Machine already provisioned. Run `vagrant provis...
==> default: flag to force provisioning. Provisioners marked...
==> default: Running provisioner: shell...
default: Running: inline script
==> default: Running provisioner: shell...
default: Running: inline script
==> default:
==> default: Successfully started and provisioned VM with 2...
==> default: To modify the number of cores and/or available...


==> default: VM_CPU respectively VM_MEMORY.
==> default: You can now access the OpenShift console on: https...
==> default:
==> default: To use OpenShift CLI, run:
==> default: $ vagrant ssh
==> default: $ oc login 10.1.2.2:8443
==> default:
==> default: Configured users are (<username>/):
==> default: openshift-dev/devel

==> default: admin/admin
==> default:
==> default: If you have the oc client library on your host,...
PS C:\DevelopmentSuite\cdk\components\rhel\rhel-ose>


RUN AS ADMINISTRATOR
Get in the habit of opening the command prompt — I suggest using PowerShell — by
using the “Run as Administrator” option because you’ll need administrator privileges later
when you are editing and debugging from Windows.

Now you can use ssh to get into the VM. The easiest option is to use the
vagrant ssh command, which will open an ssh session inside the VM, as
shown in Figure 1-5.

Figure 1-5. Using vagrant SSH to start a bash session on the VM

At this point, the VM is set up and running and you’re ready to begin your
transition to .NET on Linux.


Getting the Source Code
Once you are inside the VM, you can go ahead and fetch the source code
associated with this book. You can do this by running the following
command:
git clone />
You’ll now have a directory, NetOnLinuxBook, that contains subdirectories
for this book.

NOTE

When applicable, at the beginning of each chapter, you will see a note directing you to the
source code from the repo that is associated with that chapter.


Chapter 2. Getting Started: From
Zero to “Hello World” in One
Chapter


×