What's the simplest & most efficient way of telling if your code is executing on the client or the server? - c#

I have an ASP.NET (C#) class that is used both on the client and server.
Depending on whether the code is being executed on the client or the server, it needs to behave differently.
What is the simplest & most efficient way of telling if the code is executing on the client or the server?
Thanks!

unless you are running Silverlight 2 or Silverlight 3 There is no way for Asp.Net to run C# code in the client (the users' browser)

It's executing on the server. Unless you are using Silverlight, C# is probably not run on the client.

Unless we're talking Silverlight here, there's not much choice: C# code is always executed on the server, JavaScript is executed on the client.

By your tags I see your talking about a web application not a client-server winforms app.
If it is in javascript, html and css, or silverlight it's happening in the browser. If it is happening in C#, it's happening on the server.

Related

What is a proper way to communicate from my website to the .NET (C#) and vice-versa?

I'm using CefSharp as a webbrowser framework in a Visual Studio C# Form application project.
I read in the CefSharp documentation that I can communicate from the .NET to my website using CefSharp Javascript Injection.
chromeBrowser.ExecuteScriptAsync("document.body.style.background = 'red';");
This line of code will change the background of my document to red.
I'm pretty sure this is not a proper way to establish a communication from the .NET to the website. (I would want for example to send data that the .NET project has to the website so the website can update the MySql database and this seems like a very fragile way to do it)
So I continued to google and I stumbled upon this. It says it
allows for communication between JavaScript and .Net.
Fair enough. I tried to read and understand what's going on but it's quite complex.
My question is: Is this the proper way to establish a communication from my website to the .NET project and from the .NET project to my website? Is there a simpler way?
The thing you've shown is just a communication between JavaScript and .NET
To actually communicate with the website you should use:
Web Sockets: Real-time communication between your server and client.
HTTP: You can use usual HTTP requests in C# to send requests and receive responses.
JavaScript: Run the JavaScript code that will request stuff with CefSharp tools.

How to develop Smart Client Application

Hi.
I have several questions. I developed an application using Visual Studio 2010. The application is developed using Windows Form and the program was wrote using C#.
This application needs to be run on a server. There are several clients connected to the server that will access and use that application through the browser. I know this can be done using Smart Client technique. But I don't really understand how to do it. My questions are:
Is there any syntax/class/methods/function that I need to include in my application? How to use it?
What settings/configuration do I need to set up so that the client computer can access the application in the server through their browser?
I know this can be done using ClickOnce but I don't know how. Can anybody tell me or show me the steps that I need to do to implement this?
Is there any syntax/class/methods/function that I need to include in my application? How to use it?
There are a few - but this is a large area. I would point you at couple:
WCF
ASP.NET Web API.
I would advise to use the later as it appears to be where the modern development is heading (at least to me).
Put simply, you will write your own web server, host it either in your WinForms application or migrate your WinForms to be a web application and host it on IIS, for example. Your web server will expose some API, which will likely to be based on HTTP protocol. A client application will hit web URLs. This will be a request-response paradigm.
Because this is a large area, I cannot name you exact classes, but have a look at ASP.NET site for samples.
What settings/configuration do I need to set up so that the client computer can access the application in the server through their browser?
It depends on the technology. Usually it's pretty simple - get it from tutorials and samples. In most cases this will be *.config file XML code and some minor bootstrapping in .cs files.
I know this can be done using ClickOnce but I don't know how. Can anybody tell me or show me the steps that I need to do to implement this?
ClickOnce is a deployment tool. You probably don't need that at this point.

C# Program working with PHP server

Just curious about the best way to go about this. I want to make a webpage that shows the amount of time it takes to ping all the machines on my network. I want a C# program to do the pinging and report it to the webpage. I want to be able to click "Re-ping" on the webpage and the program rerun the test. What is the best way to go about this?
The biggest issue is how to get things from the user to the C# program.
I can make the program post on the server, and the server show the user, like below.
User <----> PHP Host <---- C# Program
However how could I get data to go the other way?
PHP Host ---?-->C# Program
Am I thinking about this all wrong?
I do want the pinging and other things to be handled by a C# program as it has far more complex functionality (threading, connections, etc) than a PHP host that is interpreted as it is requested.
Is it possible that I could let the C# program return pages to users and avoid a PHP server all-togeather?
Any ideas are welcomed.
You have many options. If you want to avoid using a PHP server the simplest would probably be to write a simple http server similar to this. Other options include using ASP.NET with a C# backend, or a webservice, which would be accessed from the PHP script or directly by a client application.

C# Web Server for displaying Console Output?

I have just finished writing my c# console application, and I am contemplating embedding a web server into it (probably this one http://webserver.codeplex.com). I don't do much in the way over advanced web coding though, so I am not sure if I can do what I need to.
Basically, I would like to allow users to view the console output of my application in realtime just by visiting the site being served by my application. If I understand correctly, to do something like this it would require AJAX, which a simple C# Web Server wouldn't be able to handle.
Is this correct or is there an easy way to do this I am missing?
How to re-route console output
You will need to write your own TextWriter and make Console use it via Console.SetOut. This writer should notify connected web clients, as well as the original Console.Out.
How to host a COMET-like server
You can use HttpListener and some basic async programming to do this. If you wrap the HttpListenerContext.Response.OutputStream in a StreamWriter (with AutoFlush set to true) and set HttpListenerContext.Response.SendChunked to true clients will receive partial results - this means you can even do it in an IFRAME.
You will need to add rights to the URL for yourself if UAC is enabled:
netsh http add urlacl url=http://+:9090/ user=domain\username
Code?
I couldn't resist it; I have written a (poorly tested and mostly incomplete) sample.
That is incorrect.
AJAX is a client side technique relying on JavaScript. As long as the web server can respond to an HTTP request, it can server AJAX content (whatever that might mean:-).
I would use this UtilDev Cassini as me embedded web server of choice, it is based on the code from the embedded Visual Studio dev server and can run pretty much anything that runs in IIS. AJAX is a browser technology not a server technology, the server just sees http requests the same as any other. My last point would that it seems slightly odd to embed a web server in a console application. It would be more usual to do this with a windows service. Have you considered converting the console app into a windows service?

Connecting C# Back-end with PHP frontend

I have a code written in C# I would like to use as the back-end of a site I'm building.
I would prefer not to build the site front-end in ASP.NET (which integrates nicely with C#), and to use PHP or Python instead.
Is that reasonable? Should I re-consider using ASP.NET?
How can I achieve that?
Just use asp.net mvc framework for the frontend instead of plain asp.net. It's easy to learn. And if you know php it will be easy to you undestand asp.net mvc.
I don't see the reasons if you are using c# backend use php frontend. For sure you can create service layer on c# and communicate with php through it, but it does not make sence for me.
You can do whatever you like. Personally i wouldnt use php because i dont know very much php.
But you can do it, you could expose a soap web service and there are libraries that will let php talk to it.
No one here will be able to tell you what you haven't already told us. Asp.Net will probably be easier because of how everything integrates and you can share classes etc - but that does not mean you HAVE to use it.
Both of them are fairly passive server side technologies that present html to browsers though. why do you need 2 servers?
You have to ask why you are doing it .. if you are playing and want to learn then of course you can do it just to see how it all works. But if you are on a commercial project then id suggest that you dont need both a php and a c# server ... or if you do perhaps you want to go asp.net for your web server and if you need another layer of services behind then use WCF if you want to go a microsoft route. Howver it is usually possible to host all services in the same IIs instance.
You can do this i have done this for a web site my self use a database server or files,
http://dtpgroup.co.uk/
Your C# application can connection to your store save the info it needs to then php can read them if it file just use a formatted file E.G
if your using Database php can connect to MySQL or MSSQL so your C# application can use MySQL if you know what your doing in C# if not and your more comfortable in php then use MSSQL (also php have the superior documentation )
I work in both frequently
Ugh, in normal instances, reading data with C# writing it to files and loading up with PHP sound slow, inefficient and down wright crazy. I believe these terms are being used wrongly.
Client Server - user machine - database great for private networks where you connect to the DB without going over the internet
vs n-Tier
Client - Browser programming html, css, javascript connects to middleware over the internet
Middleware - inside your firewall, connects browser to database could be called part of backend - php and C# are middleware languages
Database final (generally 3rd) tier
With php and c# you are creating multiple middleware layers
why why why would you do this for a web app pick one
now if you have a web app with PHP and sneakerware in house client server apps that are controlled ie shipping, accounting that are not exposed - maybe but you have added complexity that you would not need (generally)
Gary

Categories