如何在 C# 中创建 API?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11375076/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 17:38:57  来源:igfitidea点击:

How to create an APi in C#?

c#.netapi

提问by Vikneshwar

I have been doing programming, but now I'm facing a challenge. I'm on a project where I have to create an API in C# for my users. The basic process is that I have a C# code where I'm reading an XML file and obtaining the attributes and values that are between the tags. I achieved that and now I have to create consumable API for the same functionality. Is there any way through which I can learn to create API?

我一直在做编程,但现在我面临着挑战。我在一个项目中,我必须在 C# 中为我的用户创建一个 API。基本过程是我有一个 C# 代码,我在其中读取 XML 文件并获取标记之间的属性和值。我实现了这一点,现在我必须为相同的功能创建可使用的 API。有什么方法可以让我学习创建 API?

Basically I dont have any idea on how to move on with API. Any sort of examples would be of good use to me.

基本上我不知道如何继续使用 API。任何类型的例子对我都有用。

采纳答案by MNGwinn

An API is just a way for other programmers to interface with your code. A C# class library could be an API, as could a web service, a WCF endpoint, etc. The easiest way to make one is to create a C# Class Library project, make sure it has public methods, and compile it to a .dll which you can distribute.

API 只是其他程序员与您的代码交互的一种方式。AC# 类库可以是 API,也可以是 Web 服务、WCF 端点等。制作一个最简单的方法是创建一个 C# 类库项目,确保它具有公共方法,并将其编译为 .dll你可以分发。

As mentioned by others, the Framework Design Guidelines are useful for some rules to follow.

正如其他人所提到的,框架设计指南对于遵循一些规则很有用。

One thing to keep in mind - and I wish this would get hammered into all new software developers - is that API design is fundamentally a usability problem. You're building a UI for the users of your software, and your users are other programmers.

要记住的一件事——我希望这会被所有新的软件开发人员所重视——是 API 设计从根本上是一个可用性问题。您正在为软件的用户构建 UI,而您的用户是其他程序员。

This means that you want to follow all the general principals of UI/UX design - discoverability, making common tasks simple, etc.

这意味着您要遵循 UI/UX 设计的所有一般原则 - 可发现性、简化常见任务等。

Take advantage of XML comments to make sure the documentation is always there. Be sure that the most common tasks are easy to perform and that it's obvious how to do so. Make exceptions clean and include useful, actionable error messages. Think about what would make the consumer's life easier.

利用 XML 注释确保文档始终存在。确保最常见的任务易于执行,并且很明显如何执行。使异常干净并包含有用的、可操作的错误消息。想想什么能让消费者的生活更轻松。

回答by Asif Mushtaq

Making an API in C# is not a big deal, but making a good API is quite a challange.

在 C# 中制作 API 没什么大不了的,但是制作一个好的 API 却是一个相当大的挑战。

If you want to distribute your assembly as an API then you have to just add a Class Library Projectin your Visual Studio.

如果要将程序集作为 API 分发,则只需在 Visual Studio 中添加一个类库项目

Or if you want to expose your API as an service, then you might consider WCFand Web Api.

或者,如果您想将 API 作为服务公开,那么您可以考虑WCFWeb Api

To make a good and efficient API you have to study Framework Design Guidlines

为了制作一个好的和高效的 API,你必须学习框架设计指南

回答by Jigar Pandya

Your question is little general..

你的问题有点笼统。。

However I try to answer you. See if you want to create API that can be call via HTTP then you can do this using WCF..

不过我试着回答你。看看你是否想创建可以通过 HTTP 调用的 API,然后你可以使用 WCF 来做到这一点。

You can read about WCF on wcftutorial.net

您可以在 wcftutorial.net 上阅读有关 WCF 的信息

You can create API using DLL as well and I recommand you to read http://www.c-sharpcorner.com/UploadFile/mahesh/dll12222005064058AM/dll.aspx

您也可以使用 DLL 创建 API,我建议您阅读 http://www.c-sharpcorner.com/UploadFile/mahesh/dll12222005064058AM/dll.aspx

for a quickly start about how to Creating a DLL using Visual C#.

有关如何使用 Visual C# 创建 DLL 的快速入门。

Thanks,

谢谢,

回答by Mare Infinitus

If you want to create a API that can easily be used, it can be a challenge.

如果您想创建一个易于使用的 API,这可能是一个挑战。

First of all: Think in interfaces. Write interfaces that can be used by those in need of the functionality you provide.

首先:在接口中思考。编写可供需要您提供的功能的人使用的接口。

And: Keep it simple!

并且:保持简单!

At best, you write two projects when developing your API.

充其量,您在开发 API 时编写两个项目。

  1. The API that will be used by your users
  2. A sample project / test project that uses your API already.
  1. 您的用户将使用的 API
  2. 已经使用您的 API 的示例项目/测试项目。

That way, you will end up with an complete API and some sample that show how you think your API should be used, being worth more than any documentation, as a simple example tells more than a description can. (of course, you still shall describe what your API cando)

这样,您最终会得到一个完整的 API 和一些示例,展示您认为应该如何使用您的 API,比任何文档都更有价值,因为一个简单的示例比描述更能说明问题。(当然,你仍然应该描述你的 API可以做什么)

Provide classes in your API that can be used as they are, i.e. users of your API must not provide things that you can do inside your API.

在您的 API 中提供可以按原样使用的类,即您的 API 用户不得提供您可以在 API 中执行的操作。

Have fun!

玩得开心!

回答by Gorgsenegger

If you want to get some basic understanding of what's important I can recommend this Framework Design Guidelines book. It's quite interesting to read even if you aren't planning to design your own framework, and it does provide some good insight about why some decisions should be made and others shouldn't.

如果你想对什么是重要的有一些基本的了解,我可以推荐这本框架设计指南书。即使您不打算设计自己的框架,阅读它也很有趣,并且它确实提供了一些关于为什么应该做出某些决定而其他不应该做出的很好的见解。