Unable to access over a network using WCF - c#

I have a WCF self-hosted service in a Windows Forms application. I am going to be using it so a hand-held mobile device can interact with the application at a basic level.
I have enabled basicHttp (http://localhost:8080/tagservice/basic), NetTCP (net.tcp://localhost:8888/tagservice), and basicHttpMex as endpoints.
For testing purposes, I have setup a virtual machine on the network (and repeated these on a physical machine as well).
These endpoints all work on the local machine where the service is hosted.
However when running the WCFTestClient on remote machines I get mixed results.
Using the above endpoints, I can connect to the service and see the service contracts, proving the mex (IMetadataExhange) works. But I cannot use either http or net.tcp. I get the error message
Could not connect to http://localhost:8080/tagservice/basic. TCP error code 10061: No connection could be made because the target machine actively refused it 127.0.0.1:8080.
Could not connect to net.tcp://localhost:8888/tagservice. The connection attempt lasted for a time span of 00:00:01.0014400. TCP error code 10061: No connection could be made because the target machine actively refused it 127.0.0.1:8888.
When changing localhost to the real IP address in the app.config on the host, in this case 192.168.0.61. Basic HTTP works. However net.icp fails with
"The server has rejected the client credentials."
My two questions are:
Why is the net.tcp failing? I am not given a choice to enter credentials as far as I can tell.
Given the IP address can change, how can I get the app.config to use the IP address it is assigned by the DHCP server? As once this software moves from development to production environment the IP address cannot be hardcoded as it will change.

Build and configure the client binding at runtime, so you can programmatically set the URL based on the machine information
Something like this should work
string hostName = System.Net.Dns.GetHostName();
int port = 8080;
Uri serviceUri = new Uri(string.Format("http://{0}:{1}", hostName, port.ToString()));
EndpointAddress endpoint = new EndpointAddress(serviceUri);
Then you just attach that endpoint to your client and it should all hook up.

You can replace localhost to your hostname (if IP address is dynamic) or IP address(if static) and recompile your client.

Related

SignalR - can only connect to server by IP address and not by hostname

I have a C# SignalR server (running as a windows service under a domain account), which is self-hosting via OWIN and hosting an endpoint via http on port 9001.
It is using integrated windows authentication.
The problem is that my client can only connect to it using a URI that includes the IP address of the server. If I try to use the host name (either the short name or FQDN), the connection fails with a 401.
I ran a WireShark trace and see a 401 unauthorised in the working case followed by NTLMSSP_NEGOTIATE and the usual handshake. In the failing case, I just get a 401 and it never moves on to negotiate.
Any ideas on what needs to be changed on either the client, server or on the host in order to get this to work?
Thanks!

No such host is known —> System.Net.Http.HttpRequestException:

I am developing bot solution, in which I am making REST call to get some details from other server.
In local host my code is working fine, but after publishing the code in Azure I am getting the below error:
System.Net.WebException: No such host is known No such host is known —> System.Net.Http.HttpRequestException: No such host is known —> System.Net.Sockets.SocketException: No such host is known
I am using HttpWebRequest option.
May I know what could be the reason?
I'm not sure the solution below will work for you but it looks like the solution is worth to be checked out. Here is an answer that may have useful information. Take a look at this answer and check your solution accordingly:
The problem description:
I deployed a .NET Windows Service on Azure Virtual Machine running
Windows Server with an opened port that allow me to connect to it,
this service is like a server using socket. The problem is that when I
try to connect from my PC to that hosted server it doesn't work and I
get this exception: System.Net.Sockets.SocketException: 'No such host
is known'. The port is opened and visible from my PC. Can someone tell
me why I get that exception? If I run locally all works ok.
The answer:
The Exception seems to be a DNS issue. I am not familiar with C#, from
networking, you could check the followings on your side:
Windows service is running and the port is listening on the Azure VM.
The port is allowed for outbound traffic from your PC and Inbound
traffic on your Azure VM. Check the VM firewall settings on both sides
between your PC and Azure VM. Also, you could check the NSG settings
follow this. You could use telnet vmpublicIP port on your PC CMD to
verify network connectivity.
Verify your PC can resolve the address of the hosted server if you connect
it via its DNS name. You could use the NSlookup or DIG to verify this.
If it's DNS issue, you also could add a line in hosts file (located in
%WINDIR%\system32\drivers\etc on windows) mapping the hosted server's
IP to a name. Hope this helps.
.NET Service System.Net.Sockets.SocketException: 'No such host is known'

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.

Client can connect to Server on same machine using localhost, but not when IP Address or hostname is used

I'm using VS Express 2012, setting up a simple server and client on a Windows Server 2008R2 machine with all of the latest updates installed. When I have the client try to connect to the running server using "localhost" as the host to connect to, everything works fine. When I have the client try to connect to the running server using the IP address or the actual host name, I see the "No connection could be made because the target machine actively refused it 192.168.5.159:13000 ..." error message. netstat shows my server is listening on the same port as reported in the error message (13000). I've modified the Windows Firewall setting to allow my client and server to communicate through the Windows Firewall. I've added an entry to my Hosts file associating my host name with the IP address. Any suggestions as to what else I can try? All of this is being done on a private (work) network.
Usually when you set up a server, you need to specify the hostname or IP the server listens on. When you initialize a server to use localhost, you can only use localhost to connect to it.
If you want a server to accept requests from every source you need to see if you can use a wildcard to accept all connections. For example 0.0.0.0 is used a lot.

c# socket server - deploying, starting, stopping

I'm a little confused when it comes to socket servers in terms of deploying it online.
Running locally is fine as most tutorials get you to make a server application and a client application which I can execute. Done all that for awhile now and I'm happy with it but now I want to try it using a web host.
How would I deploy the socket server to my web host and then run the server? do I just upload the program to the server and run www.mywebpage.com/mysocketserver (assuming the program is called mysocketserver.exe)
may sound like a stupid question but I'm having one of those brain dead moments.
[Edit]
Great answers guys thank you. Shame I can only mark one as the answer.
Most hosting services are website related: you upload your website to their server and the host takes care of provisioning the application.
However you haven't written a website that is served by a web server, but a socket server, which is more akin to the web server itself that runs a website.
As a result you will need a host that will allow you to install and run applications, such as a Virtual Private Server service, rent a physical server or use a cloud service such as Amazon EC2.
You usually Remote Desktop or SSH into the server you are renting (or own) to upload and start the application either as a normal application, a Service or a Daemon.
Once you have installed your socket server on the host server and started the application running you should be able to contact your service using the IP address for your server and the port that you are running your socket server on.
For example, if your ip address is 10.0.0.1 and you've written your socket server to listen on port 1234, you should be able to contact your service at address 10.0.0.1:1234.
Take into account firewalls allowing access to that port.
You will also then be able to use a DNS service such as DynDns to assign a domain name to that ip address.
You can't do that unless your socket server is using HTTP as a protocol.
You are using a port for your server, right? The ports are used to identify which application to talk with.
When you browse the web using "http://something" you really say let me get the stuff which can be found on the IP 1.2.3.4 (DNS lookup) using port 80 (which is registered for HTTP). You don't have to specify the port in the browser since all browsers know that HTTP uses port 80.
So what you are really should do is to put the socket server on your host and tell your customers/users that they can connect to your socket server at port XXX on host "www.mywebpage.com".
If you've built a client you'll just hard code the port in it or specify the default port automatically.
The problem in the Internet is the routing/name translation, so that mywebpage gets translated into the correct IP address.
Either your webserver needs a fixed public IP address or you need to use a service like dynds which will dynamically map your changing IP address to the static name.

Categories

Resources