How to retrieve PC name of client from C# webservice - c#

I have a C# web service running on a separate server machine. I would like to be able to retrieve the client machine name when using a specific method on the web service. I have searched online and people have mentioned that this would be difficult to retrieve client machine names if the clients were connected to a router.
Update: If there is no way to get around the router issue, how would I be able to get the client's information (e.i. ip address, machine name, .... anything)

HttpContext.Current.Request.UserHostAddress
HttpContext.Current.Request.UserHostName

I don't think this is really possible. If the user is behind a router all you're going to get is whatever the router reports.

I don't know of a way to retrieve the client PC name from the web service. However, you could send the name as a parameter to your web service. We send the value of Environment.MachineName as a parameter to one of our web services. This will make the information available on in the web service.
Of course, we only have our WinForms client accessing the web service. You may not be able to do the same thing.

Related

Can't find the device thats self hosting a web api

I'm using Owin and Topshelf to selfhost an ASP.net Web Api.
I am able to access it through localhost and 127.0.0.1 on the device that is self hosting it.
The idea is that any device that comes onto the network and has my computer or mobile application installed is able to communicate with that web api to request/update/remove data from the database hanging behind.
The issue i am having is that i have no way of finding out on what internal ip the service is actually running. I have no control/view over the network that it's installed onto (except through my code), this means i don't know what the ip or hostname might be, only the port number (on any other device that isn't hosting it).
I tried adding custom url's like shown below
StartOptions options = new StartOptions();
options.Urls.Add("http://localhost:6969");
options.Urls.Add("http://127.0.0.1:6969");
options.Urls.Add(string.Format("http://{0}:6969", Environment.MachineName));
options.Urls.Add("http://+:6969");
Obviously localhost and 127.0.0.1 won't work for a remote device on the same network. The issue with the MachineName is that i don't have a say in the name of the machine either, meaning this could be anything. I assumued the wildcard would allow me to send a request on port 6969 and the host would catch it, this was sadly not the case.
This leaves me in a bit of an issue, i now have a self hosted Web api, on a machine with a random ip and name, and it needs to be accessed by other devices on the same network, The users are not able to manually enter ip, and i can't force a name onto the host computer. This all leaves me in a situation where im not sure wat else i could try, or if its even possible to reach the host computer without human interference.
Edit: I did come across something along the lines of a
network broadcast address
Which should in someway allow me to send out a message from a device, and let the server respond with its ip. Not sure how i would apply this to my self hosted web api though.
Basically, the idea is to have your web API application register its address to a service, such as DNS, which may be updating the IP address of a DNS A record. Then clients will query this service or DNS, to resolve the address to your application.

Expose WCF service hosted in IIS to users

I have a server physically located in the US. The OS is Windows Server 2008 R2 SP1.
I create a WCF service and host it in IIS on the server.
Now, while I'm physically in Germany, I want to write a WCF client to the WCF service. First of all, I need to create a WCF client proxy.
All the tutorials I've seen imply that the WCF service and WCF client are located on the same machine. I.e., the tutorials use "localhost" everywhere. In my case it doesn't work.
I know the IP of my server but I don't know what settings should be done to allow any programmer to use the WCF service. I guess it requires special customization of the endpoint at the WCF service and some settings at the IIS to make the WCF accessible by anybody.
Can you please give a piece of advice in this regard?
Thanks for any suggestions!
Two approaches are available :
Create a service reference using the add reference , enter your service address should be something like : http://IpAdress/service.svc , or if you have your wsdl file on your local disk , just enter your wsdl file location in the add service address bar. This will generate the proxy class.
If you have the service contract(interface) just do it programatically using ChannelFactory , you must know your binding and endpoint address as well.
If have any questions you're welcome

Windows Service communication, sockets, c#

i'm writing a server side application and a windows service which need to be installed on a remote host
the service returns CPU usage to the serverside application - this is NOT the problem
the serverside application deploys the service on the remote host. - also, not the problem
THE PROBLEM:
but how do i setup a socket connection between the two, when i dont want to hardcode the ip address? (for scalability) - do i need to use multicast or is there some devious way of doing this? is there another solution than using sockets? -
i'm new to writing Windows Services.
I need help figuring out how to communicate between the server and the service without hardcoding IP-addr.
thank you in advance.
MY SOLUTION:
I created a windows service, that reads a xml file with the IP and port of the Server application. so when i deploy my client application, i also create a xml file with the network information.
Regards Alex
A lot of communication platforms now use network discovery; there's an article on codeproject that goes into detail about using network discovery.
The problem was, I did not know what machine name the server was running on, in fact I wanted this to be flexible, and selectable by the user.
Seems relevant to http://www.codeproject.com/Articles/16113/Retreiving-a-list-of-network-computer-names-using
Have you thought of using WCF?
http://msdn.microsoft.com/en-us/library/ms731082.aspx
Regarding multicast, you can have a WCF server multicast announce it's availability on a network; see http://msdn.microsoft.com/en-us/library/dd456782.aspx
another solution could be to create the service as a console application with arguments (endpoint ip address) and then just deploy it with the arguments on the remote host

Calling a webservice on one website from another website; both hosted on the same server

I have a server serving multiple websites under one IP with bindings on specific URLs.
However, one of the sites I am hosting needs to pull data off a webservice (.asmx file) located in another IIS website on the same server. The code works fine locally but when we push the code to our server, it seems not to be able to locally resolve the lookup. (getting the dreaded 'No connection could be made because the target machine actively refused it'. I think what's happening is it tries to use the endpoint address and do a DNS lookup, but because the DNS lookup ends up resolving to the same IP, it actually pulls the traffic via in-memory or pipes or something and not network, which causes IIS not to bind to the correct site, and so the whole thing goes to pot.
Is there a way I can use the windows hosts file or other mechanism to fix this apparent DNS issue? Perhaps someone might shed light on this and maybe its not DNS at all. Thanks!
UPDATE - Here is a quick breakdown of the domains in question. Nothing is specifically coded or pointing to 'localhost'. Everyhing is using fully qualified domain names.
DomainA.com is a website that, when a form is submitted, calls a web service from DomainB.com. Both DomainA.com and DomainB.com are 'web sites' set up in IIS on the same server bound to the same external IP address. What I believe to be the problem is that DomainA.com, when the form is submitted, does a DNS lookup for DomainB.com, and when it finds that the domain is the same IP address, does not use the domain name when making the requests to DomainB.com. Without the domain name being used/sent, IIS doesn't know which site to connect to and the 'target machine actively refuses' the connection.
Do you have to location to your webservice in a configuration file?
Take a look at the proxy class you generated to consume it.
The host is usually hardcoded into that file.
If you didn't use your production host to create that file(or service reference), it is pointing to your development webservice. This won't be accessible from your production machine.

Best way to contact Desktop application remotely via Internet

I want to pass certain parameters to a desktop application remotely via the internet. I don't want my application to contact the server repeatedly, because many such applications can bring the server down easily. Is there a way to initiate the connection from the server? How can I identify the applications, as there will many of them running on many computers somewhere around the globe. I don't know where to start - I'm trying to do this in C# and ASP.NET/PHP on the server-side. Please give some advice.
Is there a way to initiate the connection from the server?
No. Not without having the client contact with the server first, informing it with the IP address, port to use etc... Which the server will need to keep for each client, hoping that they don't change (or get updated when they do change).
Long polling by the client is the right solution for what you are doing, even if you don't want to use it.
There are many different ways you could approach this, just thinking out of the box, both your app and the server could utilize a different mechanism for transferring the settings. I'm not recommending any of these methods, please don't shoot me down, they are all just ideas.
As an example, your server could connect out using FTP and output the updated settings to an FTP server on each PC. You could install something like Filezilla on each machine which runs your app. You'd obviously need to configure port forwarding on the router to allow the server FTP access.
You could use email. Setting up an email account where your server can login to send out the settings. Your app could possibly login to the same email account possibly even a single Gmail account to retrieve the settings.
Another idea would be to use a file sharing service like Dropbox, Google Drive or similar and where the settings could be shared. Obviously this would involve learning any API and I'm not sure if there are any restrictions on this approach.
The last idea and probably my preferred approach would be to host a web service and database on a remote server, both your server and the applications would connect to the same service to transfer the settings. This approach is obviously firewall/router friendly as all the clients connect out to the web service to collect the required data.
Hope this helps?

Categories

Resources