简体   繁体   中英

How to build two binaries .net core 2 console app

I am new in .Net core and c sharp. i would like to build two binaries in the same namespace. It's two console apps, one server and one client. I'd like to share some objects like "Scores" and think like that. Actually I have created two apps (via the "dotnet new console" command) but i can change and start again. I am working inside a docker container with a simple text editor (emacs). No visual studio or visual studio code there :)

It is possible or not ?

edit : Okay, I will try to be more precise. I have to create two programs: one is a server and the other is client. Clients have to be connected to play a game (multiplayer). Does I have to create two distinct project (with their own namespaces/types/tests) or I can a simple project (just call "dotnet new console" command once) and share a same namespace, share some types between the server and the client (like a score class) and tests ? here is my curent directory (client and server are two directories created via the "donet new console" command :

├── README.md
├── client
│   ├── client.csproj
│   ├── obj
│   │   ├── client.csproj.nuget.cache
│   │   ├── client.csproj.nuget.g.props
│   │   ├── client.csproj.nuget.g.targets
│   │   └── project.assets.json
│   └── srcs
│       ├── Program.cs
│       └── Types
│           └── user.cs
├── docs
│   ├── rfc
│   │   └── donotremove
│   ├── text
│   │   └── donotremove
│   └── uml
│       └── donotremove
├── server
│   ├── obj
│   │   ├── project.assets.json
│   │   ├── server.csproj.nuget.cache
│   │   ├── server.csproj.nuget.g.props
│   │   └── server.csproj.nuget.g.targets
│   ├── server.csproj
│   └── srcs
│       ├── Program.cs
│       └── Types
│           └── server.cs
└── tests
    └── tests.cs

If I understood the question correctly, all u have to do is to change the namespace on every class.

For example, lets say that one of your binary is called Yourappname.Server and other is called Yourappname.Client . And if we assume that the common namespace that u need to share is Yourcompany.Yourapp . Then simply change the namespace of your classes to Yourcompany.Yourapp .It will do the work.

One more tip..you even can import classes in other namespaces using import statement using the using statement or can use the fully-qualified class name with the namespace.

using Yourcompany.Yourapp;

or

Yourcompany.Yourapp.Score score =new Yourcompany.Yourapp.Score();

Anyhow in both of the scenarios, you need to add references to binaries.You can do that using the .csproj file.

<ItemGroup>
    <ProjectReference Iclude="path to .csproj" />
</ItemGroup>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM