0

My issue is that I need to send a TCP message from a web page. This is not possible for security reasons, so I have written a C# function in a program that is already running as part of my production environment to send the TCP message, and I want to invoke that method via the web page. I basically need to host a web service and call a C# function in that web service from javascript in a web page. How can I do this simply?

I have looked around at solutions like asp.net (my web page is part of a GIANT web client that does not and will not be able to utilize .aspx files), and WCF (which seems like overkill for simply sending a TCP message), but none seem to really nail down my intent of calling a C# function in a project that I have already made, from a web client that is already large relatively unchangeable.

Note that what I really want to do is be able to call C# code in a project I've already made and plan to deploy, so what I'd love to be able to add REST functionality to a method there, but I am also very open to simple/smart solutions outside of my existing project to an extent.

Thanks for any guidance!

ThePartyTurtle
  • 2,276
  • 2
  • 18
  • 32
  • 2
    That `c# code` has to be running, ie hosted by something. Otherwise its nothing but a dll/exe file on disk somewhere. So you can run it as a windows service, as a console app, host it in IIS, etc. Then you can modify that code to listen over something (TCP, HTTP, whatever), then you can figure out how to call it. You can kind of tell the final answer will be fairly broad and will differ widely depending on the implementer. As such this question is not well suited for SO, I if you need more general help post it on Programmers SE or narrow your question down to a specific detail. – Igor Sep 09 '16 at 18:48
  • @igor They can include the code as a project within a solution with an ASP.NET project of some sort (web service, web site, etc.) and then reference the project from the ASP.NET project. That would let them handle the web stuff (authentication/authorization, server settings, etc.) in the ASP.NET project and keep his original C# function separate, in case they need to reference the code from another project. – maniak1982 Sep 09 '16 at 18:54
  • @maniak1982 I have tried ASP.NET, but am having trouble calling my web service from my javascript. I had also thought that scripts calling ASP.NET web clients had to be calling from and ASP.NET web page (Visual studio and tutorials seemed to point to this), so I have backed off from that approach and that's why I'm back to looking for general suggestions. Do correct me if I'm wrong, because if I can get any old JS running in a browser to talk to my ASP.NET web service then I'm good to go, I just haven't been able to thus far. – ThePartyTurtle Sep 09 '16 at 19:02
  • 1
    This question, despite the answers, is far too broad and possibly opinion based. There are literally hundreds of ways of getting this to occur, and no objectively "correct" answer. – Heretic Monkey Sep 09 '16 at 19:16
  • I understand this was broad, but have still received good direction from posters, so this forum of discussion has served me well. Will confirm answers when I look into ASP.NET further. Any answers with methods that allow me to call a C# method from my javascript are objectively correct. Part of my confusion in learning about this and trying to get it working has been the broad nature of the "solutions"/tutorials etc, it would be great to pick one I have confirmation is a good choice for my issue and work that out. – ThePartyTurtle Sep 09 '16 at 20:37

2 Answers2

3

You have a lot of options, but I would go for a web service, and my own personal preference is an ASP.NET MVC Web API. I don't know exactly which direction to send you down, but you can start from here and go further. It's possible to write a RESTful Web API using ASP.NET MVC and it's a relatively mature product with a large number of users.

http://www.asp.net/web-api

http://www.asp.net/web-api/overview/older-versions/build-restful-apis-with-aspnet-web-api

As for calling the Web API method, you can use any sort of AJAX call. I usually use jQuery's AJAX methods, myself.

maniak1982
  • 707
  • 2
  • 7
  • 23
  • 3
    This. I was about to recommend exactly the same myself. – Ben Harrison Sep 09 '16 at 18:49
  • I have also looked into jQuery's AJAX and I really liked how it seemed to work, but I have hit a road block calling my ASP.NET web service methods. I guess that's why I backtracked and asked such a broad question. I was also under the impression that my web page that wanted to call the web service had to be an ASP.NET web page, and not just any old script running in a broswer (my web client is not an ASP.NET web page). – ThePartyTurtle Sep 09 '16 at 18:57
  • 1
    You can literally call it from any old script, so long as you send the request to the right endpoint and it contains valid data. I like to test my AJAX calls right in the Chrome F12 development console. Remember to take a look at the errors you receive, and that you can debug on IIS (in the project web properties, you can change the server to Local IIS). Look for answers to the error messages that you get and don't be afraid to use trial and error. – maniak1982 Sep 09 '16 at 19:00
  • Hmm good suggestions, I was just getting internal server errors. That could've been because I was calling it wrong or due to how I running the service (will debugging in visual studio host the service correctly in lieu of formal deployment in IIS?). Anyways, thanks for that confirmation, something about the docs I've been reading had me thinking that my page needed to be a part of some ASP.NET web page workflow. – ThePartyTurtle Sep 09 '16 at 19:05
  • 1
    The Web section of the properties window has a Create Virtual Directory button that creates a virtual directory on your localhost IIS. If Local IIS is selected as the server, every build will deploy to localhost. – maniak1982 Sep 09 '16 at 19:11
  • 1
    Have been checking this out more as I have time. It looks like the build is deploying to local host properly, and when I try to hit that url from JS with jQuery's AJAX, I'm getting the "Cross origin requests are only supported for protocol schemes..." error. Thanks again for the direction, going to go check that out now. Hopefully that means it at least can hit the web service, if not consume it properly yet. – ThePartyTurtle Sep 13 '16 at 16:52
  • 1
    Yes, you're hitting the web service correctly but you haven't configured it correctly for cross-site scripting. It's seeing that you are accessing it from a different domain and kicking you back out. If I recall correctly that's on POST requests only and not GET. If you need to make calls from one site to another, look into CORS. I never did get deep into that myself but you will need to tell the site that it can accept requests from your domains (or sub-domains, if that is the case). – maniak1982 Sep 13 '16 at 16:54
  • Ooops, I am actually looking to hit the service from the same domain, the same host actually. I resolved that error, which was due to a badly constructed URL. Now I am getting simply a net::ERR_CONNECTION_REFUSED error, which I need to check out. I think I could be missing some configuration in my ASP.NET web service, although nothing is immediately obvious to me. – ThePartyTurtle Sep 13 '16 at 17:00
  • Sorry, doing a kind of train-of-though comment string here for clarity/future page visitors. Looks like I need to fix some things with my web service, as per some extra error information I just found in Chrome debugger, specifically "No 'Access-Control-Allow-Origin' header is present in requested resource". – ThePartyTurtle Sep 13 '16 at 17:10
  • If you're not calling it from localhost (that is, the same machine), I'm not surprised. https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS – maniak1982 Sep 13 '16 at 18:33
2

The best way will be direct call ajax to your app through a soap/rest web service.

There are also some alternatives for calling server apps from website:

You have as many options as server side services in the world. The simplest, in my opinion, way is to make an ajax call to a php file which will run a call to your C# program. On the other hand it won't be the best solution in case of performance. You should definitely consider using NodeJS as it is a perfect technology to make a server side 'server' that will handle tasks for server and works well with browser calls (through ajax or socket.io). If your TCP message isn't too complex you can rewrite it in Node, or just call the C# from the Node app.

PHP solution:

Just use a file that will execute your program using for instance http://php.net/manual/en/function.exec.php

NodeJS:

there is a great gist which shows how to send tcp messages: https://gist.github.com/tedmiston/5935757

Ajax calls:

You can use either pure javascript (http://www.w3schools.com/ajax/ajax_xmlhttprequest_send.asp) or some libraries like jQuery http://api.jquery.com/jquery.ajax/

Socket.io:

simple socket.io example: What is an example of the simplest possible Socket.io example?

Beware: you need to secure the access, leaving ANY way to make an unauthorized call to a server application is highly insecure.

Community
  • 1
  • 1
user40194
  • 41
  • 4
  • I will look into these methods! I had previously investigated a library called WebTCP that provided methods for sending TCP from web browsers through a NodeJS application. The issue is that we (my team) don't picture or want to run NodeJS as part of the operating dev/demo environment for our software, definitely not for production. The lighter weight the solution the better though for the time being, so I will take another look at NodeJS and also at PHP! Thanks. – ThePartyTurtle Sep 09 '16 at 20:40