Interaction between web and a C# .dll - c#

I have to call a method of a complex .dll C# library when I get a "request".
Since I don't want to program the network part (socket, protocol, etc.), I'm thinking of using an existing Web server (IIS or Apache) to handle this part (and use HTTP as protocol).
Is it possible to load the .dll in memory and call a method of it from a web server? How to do it?
If yes, is it better to use IIS on Windows, Apache or Windows, or Apache on Linux?
Is it mandatory to call this method of the C# .dll using C#/ASP? Or can it be with PHP?
It has to be very scalable, and the .dll library uses the .NET Framework.
Of course I'm thinking of exec function in PHP, but this would start a C# program again and again for each single request, which is not very good.
I would like to load the C# .dll once in memory, and have it running on the web server directly if possible, so that only "calls" to a method happen when I get a specific HTTP request.
My question has similarities to this one, however I don't believe our problems are the same.

Is it possible to load the .dll in memory and call a method of it from a web server? How to do it?
Yes, for example by programming a service layer around it using WCF or the ASP.NET Web API. Given the quirky (to say the least) interoperation of SOAP services and clients, I'd go with the latter.
You can then call your SOAP/REST/giveitaname service through PHP's cURL or any other applicable method, and your C# service will then call the code in the DLL (this part you'll have to build).
You can host both types of project easily in IIS, making it accessible through HTTP.

Related

Is it possible to pass a Socket.Handle from a C# Application to a ASP.NET Web Application running on the same server?

Is it possible to pass a Socket.Handle from a C# Application to a ASP.NET Web Application running on the same server?
I have looked around and found examples of passing C# socket to un-managed code, however this is not sufficient.
I'm very curious about why you'd want to do that.
As for the answer, no, I am 99% sure it is not possible, because unless I am very much mistaken socket handles are scoped to the process at the OS level.
EDIT:
Based on the comments, it sounds like you'd want to make the server process run a WCF service on IPC transport that the ASP.NET application can use to pass along commands to the hardware.
An added benefit is that if you use WCF and eventually need to move the ASP.NET site to another box, you can switch to TCP transport with relatively little fuss.

C# + PHP in the same application?

What im trying to do is a little different, im wondering, if its possible to create sorts of an interface, so that if a particular function is called in php (stand alone), than the arguments will be forwarded to a method in C#, and vice versa.
Depends of course on what kind of data you want to exchange, if the applications are on the same server or on two different ones etc.
I did briding between PHP and C# in some of my web based projects. What I did: create an ASP.NET MVC project which exposes a RESTful API. The methods exposed by this API are then called from PHP via HTTP (using CURL). I used JSON as a data exchange format to pass data from PHP to C# and back again. This worked good as the two applications were on different servers.
I could also imagine some kind of socket server. E. g. a background process written in C# is listening on some port, and then you connect to it via PHP´s socket functions. I did something like this to connect PHP and Java. The Java app ran as a demon process on port XXXX and was a wrapper around Apache FOP. I used PHPs socket functions to pass XML and XSLT to the Java demon which then used Apache FOP to transform the data into a pdf and returned that via the socket connection back to PHP which in turn sent the PDF file to the client requesting the page.
There are probably some other approaches, but these were my experiences so far in connecting two different technologies together.
EDIT: as an aside: PHP on a windows webserver using IIS was really not that nice to work with. Sometimes strange errors occured or certain permission related errors that were not easy to resolve. YMMV though.

Driver call Web Service

Can a driver call a C# web service or any other language?
Technically yes it can. Its just a call via the protocol that the service is using. However since you're in kernel mode, certain USER mode libraries may not be available to you so you may have to code around these yourself.
For example if the service is running on a HTTP endpoint, then you can use raw sockets to access this.
How difficult this is depends on the platform the driver is in.

Can I interact with .NET libraries through Javascript?

We've built a set of .NET libraries (in C#) which are used to interact with an outside resource. The outside resource provide bindings in Java and .NET, so it was necessary to build our libraries in one of those languages and we're officially a Windows shop.
We also use PHP/Javascript for a lot of our front-end web applications. Is it possible for these PHP/Javascript web applications to interact with our .NET libraries? We have both IIS and Apache web servers if that makes a difference. Communication would need to go in both directions.
The most obvious approach is exposing your .NET libraries as web services on a web server (either IIS or Apache/Mono). This allows both JS and PHP to consume the same service endpoints (JS via AJAX, PHP vs web service calls).
If extreme performance is a concern - for example, your PHP apps need to make heavy calls into the .NET libraries as if they are native, then web services are probably not the best approach. For that, you might want to look into making COM Callable Wrappers for your .NET libraries and consuming them from PHP.
Although Ajax is the usual approach, another idea for you, in case it might help, would be to use Silverlight on the client side. That way, you would embed your .NET classes into a Silverlight application, which would then be downloaded to the client where it would run inside the browser. Silverlight apps can easily interact bi-directionaly with JavaScript, as well as with remote resources.
For example, you can mark a .NET method with the [ScriptableMember] attribute, which will expose it to JavaScript.
You can pass data via AJAX calls where you communicate to and from the client via JSON. For client side data set manipulation jLinq is a great library for performing Linq queries on JSON objects.
If you are looking for a good place to start with web services, JSON and ASP.Net webforms Dave Ward's posts on Encosia are really top notch.

C# & PHP: Simple example of PHP Web Service used by C#

Could anyone give me a small and simple example of how to do this? Or some good pointers on how to get started.
I would like to create a C# client that can send a file or some text or xml or whatever, to a web service or something similar written in PHP, where the PHP Web Service stores it in a file or a database or something like that. Just not sure how to get started.
I guess the first step would be to create the php web service. And then it perhaps would be quite easy to use it in C#, since I could probably pretty much use the "Add Web Reference" button in vs and then go from there?
You can take a look at this tutorial showing how to develop a web service using php. The .NET client will be pretty straightforward as you mentioned.
I'm not versed in the C# aspect of your question, which you intend to be the consumer. But if you don't mind using a library to expedite the process of getting the webservice up and running, you can set one up very quickly using Zend Framework. Check out the documentation for setting up a Zend_Rest_Server.
Just a thought... if the service only has to support simple operations like "upload a file", perhaps avoid WSDL all together? I assume PHP can handle raw requests - so you can use the oh-so-complex .NET client:
using (WebClient client = new WebClient())
{
client.UploadFile("http://some/upload.php", "foo.bar");
client.UploadString("http://some/upload.php", "some text");
}
etc. Likewise, you can often easily use POX / REST without the complexity of a formal web-service contract. The above code just does a simple HTTP request with the file-contents/string in the body.
you are right. all you need to do is to create a simple web service in PHP, which accepts the request/xml/file and then stores it in a database. then you can use the webservice using any technology, C# for sure.
in order to write a web service in PHP, I think it is better to choose your webservice type in the first step. it can be a REST web service, or SOAP, or XML-RPC. it depends on the level of complexity of your application.
if your application is as simple as just throwing a string to the webservice or a file URL, so then the webservice can store that string, or fetch the file from the URL and stores it in database, I recomment REST. because it is simpler. as simple as just writing a simple PHP script that uses it's HTTP parameters as input.
but incase you want it to be more secure or have more complex needs, It is easy to have SOAP services in PHP. if you wanted to work with SOAP, PHP has an extension (php-soap) which you can install and use the built-in functionality of PHP-SOAP. if you are using an older version of PHP that does not support the extension, or your application will be hosted where php-soap is not installed, there is pure PHP implementaion of SOAP protocol, named nusoap. it is open source and works very well.
http://sourceforge.net/projects/nusoap/
I have made work in PHP NuSOAP webservice and my C# mobile client fetching data from web services.Below is the link
http://khanmubeen.wordpress.com/2010/11/18/web-services-with-php-nusoap-c-mobile-client/
In case you like to know more from me welcome to inbox me at mubeen44us at gmail.com

Categories