0

I am doing a project and have a C# library but I need to call it in C code on Linux. What's the most efficient way to do this? Performance is the first consideration.

Of course, I can make a C# service and use TCP to talk. But I wonder if it's the best way...

Thank you!

user1165560
  • 331
  • 4
  • 21
  • 3
    If performance is the first consideration, then rewriting the library in C would be the best ***performance***-wise. The penalty, of course, is in ease of development, and maintenance if you need to maintain a C version and a .NET version. *Personally*, I'd go the service route. It's what SOA was designed for - reusable, cross-platform pluggable components. But I'm lazy. I'd hate maintaining two lines OR rewriting something that works. – David Jun 26 '12 at 18:04
  • Thanks for replying. Rewriting is not quite possible. I just had a look at [mono-c](http://www.mono-project.com/Embedding_Mono?title=Special:Upload&wpDestFile=Callback.png). Not sure if it's better than service option. Are there any other methods? – user1165560 Jun 26 '12 at 18:29
  • @user1165560 of course mono. Is there any other way to run C# code on Linux ? – Alex F Jun 26 '12 at 21:30
  • @Jasper Yes. Running C# code on Linux requires mono. But I am asking what's the best way to call C# library on Linux. I could run the C# code as service using TCP and I can call it within mono runtime. I wonder which is most efficient or there are any other ways. – user1165560 Jun 27 '12 at 17:31

2 Answers2

1

You can also use CoreRT to create native libraries from C# code and call them from C. You can find an example here

Edit: CoreRT has moved to runtimelab feature NativeAOT. Updated link.

Fabian
  • 1,100
  • 11
  • 14
0

I think the most standard solution is to have your C# service export a "web services" remote API, which the C code can then access via the standard Linux web services packages. In effect these support a form of RPC, but with web pages encoding the request and also the reply. The advantage of using web services is generality; your solution will port to any platform and will work from any language, not just C.

Another way to go is to use one of the new open-source packages from Google, Facebook, Twitter, etc. A bunch of the cloud vendors are open-sourcing their RPC infrastructures because many customers pose your exact question and need these solutions. Those will definitely be faster and more powerful in other ways too: they include powerful performance visualization tools and debuggers of various kinds. Microsoft has a remote method invocation technology too. So these are an option, perhaps less general but definitely faster. So you then have to ask how important speed is, for this particular path...

The bottom line then is that there are maybe five packages you could use. No idea which is winning or best!

Ken Birman
  • 1,088
  • 8
  • 25