How to create an APi in C#?

61,459

Solution 1

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.

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.

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

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.

Solution 2

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

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

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

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

Solution 3

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..

You can read about WCF on wcftutorial.net

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

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

Thanks,

Solution 4

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

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.

  1. The API that will be used by your users
  2. A sample project / test project that uses your API already.

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 can do)

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.

Have fun!

Share:
61,459
Vikneshwar
Author by

Vikneshwar

For Details about me visit about.me/s.vikneshwar S. Vikneshwar www.askoutchennai.com, www.enchantingchennai.com

Updated on July 09, 2022

Comments

  • Vikneshwar
    Vikneshwar almost 2 years

    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?

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

  • Vikneshwar
    Vikneshwar almost 12 years
    Asif: So far from my reasearch i have found it. The thing you are talking about is .ddl right? Im clear with that. But my point is that if i create the dll and include it in any other project it should work efficiently. So thats wat im tyring to do. Any necessary steps related to the dll that can help me will be really apreciated.
  • Vikneshwar
    Vikneshwar almost 12 years
    Thanks u Jigar, it seems good. Ill try.
  • Asif Mushtaq
    Asif Mushtaq almost 12 years
    @Vikneshwar: i think you need to study Framework Design Guidelines, believe me its worth reading it. If you don't have time for that you can go the Brad Abrams blog and read some articles.
  • Jigar Pandya
    Jigar Pandya almost 12 years
    Yes thanks for appreciation, you can do upvote (stackoverflow.com/privileges/vote-up) or accept answer... thanks
  • Vikneshwar
    Vikneshwar almost 12 years
    Thank u MNGwinn. I think i got idea. Thanks for ur timely help.