Fail to connect to WCF Service on my Localhost - wcf

I got the below error when trying to connect to WCF service running on my localhost using the WCF Test Client tool. I entered the end-point address as "net.tcp://localhost:19998/MyWCFService". MyWCFService is launched within Visual Studio 2017 on my local PC.
"There was no endpoint listening at net.tcp://localhost:19998/MyWCFService that
could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details."
I can verify the port 19998 is listening on my PC using the netstat.
TCP 0.0.0.0:19998 LISTENING
I have disabled all the firewall on my PC.

It turns out that my WCF service has some runtime errors that prohibits any clients to connect to it.. I have fixed the errors and i can connect now. Thanks.

It seems that the error is caused by that the service address is wrong. How do you host the service on the server side? I would like you could post more details about the server side so that give you an effective reply.
Here is my example about using NetTCPBinding, wish it is useful to you.
Server
class Program
{
static void Main(string[] args)
{
Uri uri = new Uri("net.tcp://localhost:1500");
NetTcpBinding binding = new NetTcpBinding();
binding.Security.Mode = SecurityMode.None;
using (ServiceHost sh = new ServiceHost(typeof(Calculator), uri))
{
sh.AddServiceEndpoint(typeof(ICalculator), binding,"");
ServiceMetadataBehavior smb;
smb = sh.Description.Behaviors.Find<ServiceMetadataBehavior>();
if (smb == null)
{
smb = new ServiceMetadataBehavior();
//smb.HttpGetEnabled = true;
sh.Description.Behaviors.Add(smb);
}
Binding mexbinding = MetadataExchangeBindings.CreateMexTcpBinding();
sh.AddServiceEndpoint(typeof(IMetadataExchange), mexbinding, "MEX");
sh.Open();
Console.Write("Service is ready....");
Console.ReadLine();
sh.Close();
}
}
}
[ServiceContract]
public interface ICalculator
{
[OperationContract]
int Test(int a);
}
public class Calculator : ICalculator
{
public int Test(int a)
{
return a * 2;
}
}
Result.
Feel free to let me know if there is anything I can help with.

Related

ServiceHost is not opening to start listening for messages

I have this method that calls and tries to open a service host to listen for request. It is able to create the service host and enable metadata publishing but it fails when it tries to open the service host.
When it reaches to host.Open() to is not able to open the service host so that i send my messages from a client.
why is my service not starting?
public void startOperator()
{
Uri baseAddress = new Uri("net.tcp://localhost/Test");
// Create the ServiceHost.
using (ServiceHost host = new ServiceHost(typeof(Operator), baseAddress))
{
// Enable metadata publishing.
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
host.Description.Behaviors.Add(smb);
// Open the ServiceHost to start listening for messages. Since
// no endpoints are explicitly configured, the runtime will create
// one endpoint per base address for each service contract implemented
// by the service.
host.Open();
Console.WriteLine("The service is ready at {0}", baseAddress);
Console.WriteLine("Press <Enter> to stop the service.");
Console.ReadLine();
// Close the ServiceHost.
host.Close();
}
any help is appreciated ..thanks
Don't use smb.HttpGetEnabled = true option as it conflicts with your net.tcp binding. After commenting it out, code works for me.

Cross domain policy file over net.tcp for WCF servicehost and Silverlight 5

I have a locally hosted WCF service and a silverlight 5 app that communicates with it. By default silverlight tries to obtain the cross domain policy file over HTTP when making calls to the WCF service. I need to change this so that the policy file is served over net.tcp port 943 instead.
I have setup a local tcp listener that serves up the policy file over port 943 and i have followed this technique whereby i make a dummy socket connection in order to obtain the policy file over tcp as it is only retrieved once per application lifetime. The tcp server is being hit as expected and i am getting SocketError property value as Success (though i must note, the first time i hit the tcp server after starting the listener, the result is always access denied).
From what i can tell, the policy file is either invalid as the silverlight application as still unable to connect or the above mentioned technique does not work with silverlight 5.
What i would like to know is if what i am doing is possible & im doing it correctly, otherwise if there is an alternative means to have the policy file successfully downloaded over tcp and removing the need for retrieving it over HTTP.
Thanks
I wrote a long post about hosting silverlight in WPF - and using WCF with a http listener here:
How can I host a Silverlight 4 application in a WPF 4 application?
Now while not directly answering your question, it does show how to create a http version of the policy file.
I have also written something that serves up a policy listener over port 943, but I can't find where I posted the source - so I'll keep digging. As far as I remember though, silverlight does a cascade find of the policy file, if it doesn't get a connection on port 80, it'll then look on port 943.
I hope this is of some help somewhere.
Ok, here is the policy listener I had for net.TCP transport i.e. not HTTP based. I presume you have sorted this by now, sorry for the delay. It may well be of use to someone else now.
I was looking for the MS thing that said they cascade from HTTP to TCP, however, I can't, and therefore have to assume it was bunk and then changed.
Either way, if you call using a net.TCP service, and want a listener for it, this code should help:
#region "Policy Listener"
// This is a simple policy listener
// that provides the cross domain policy file for silverlight applications
// this provides them with a network access policy
public class SocketPolicyListener
{
private TcpListener listener = null;
private TcpClient Client = null;
byte[] Data;
private NetworkStream netStream = null;
private string listenaddress = "";
// This could be read from a file on the disk, but for now, this gives the silverlight application
// the ability to access any domain, and all the silverlight ports 4502-4534
string policyfile = "<?xml version='1.0' encoding='utf-8'?><access-policy><cross-domain-access><policy><allow-from><domain uri='*' /></allow-from><grant-to><socket-resource port='4502-4534' protocol='tcp' /></grant-to></policy></cross-domain-access></access-policy>";
// the request that we're expecting from the client
private string _policyRequestString = "<policy-file-request/>";
// Listen for our clients to connect
public void Listen(string ListenIPAddress)
{
listenaddress = ListenIPAddress;
if (listener == null)
{
listener = new TcpListener(IPAddress.Parse(ListenIPAddress), 943);
// Try and stop our clients from lingering, keeping the socket open:
LingerOption lo = new LingerOption(true, 1);
listener.Server.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger,lo);
}
listener.Start();
WaitForClientConnect();
}
private void WaitForClientConnect()
{
listener.BeginAcceptTcpClient(new AsyncCallback(OnClientConnected), listener);
}
public void StopPolicyListener()
{
if (Client.Connected)
{
// Should never reach this point, as clients
// are closed if they request the policy
// only clients that open the connection and
// do not submit a policy request will remain unclosed
Client.Close();
}
listener.Stop();
}
public void RestartPolicyListener()
{
listener.Start();
}
// When a client connects:
private void OnClientConnected(IAsyncResult ar)
{
if (ar.IsCompleted)
{
// Get the listener that handles the client request.
TcpListener listener = (TcpListener)ar.AsyncState;
// End the operation and display the received data on
// the console.
Client = listener.EndAcceptTcpClient(ar);
// Try and stop our clients from lingering, keeping the socket open:
LingerOption lo = new LingerOption(true, 1);
Client.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger, lo);
// Set our receive callback
Data = new byte[1024];
netStream = Client.GetStream();
netStream.BeginRead(Data, 0, 1024, ReceiveMessage, null);
}
WaitForClientConnect();
}
// Read from clients.
public void ReceiveMessage(IAsyncResult ar)
{
int bufferLength;
try
{
bufferLength = Client.GetStream().EndRead(ar);
// Receive the message from client side.
string messageReceived = Encoding.ASCII.GetString(Data, 0, bufferLength);
if (messageReceived == _policyRequestString)
{
// Send our policy file, as it's been requested
SendMessage(policyfile);
// Have to close the connection or the
// silverlight client will wait around.
Client.Close();
}
else
{
// Continue reading from client.
Client.GetStream().BeginRead(Data, 0, Data.Length, ReceiveMessage, null);
}
}
catch (Exception ex)
{
throw new Exception(Client.Client.RemoteEndPoint.ToString() + " is disconnected.");
}
}
// Send the message.
public void SendMessage(string message)
{
try
{
byte[] bytesToSend = System.Text.Encoding.ASCII.GetBytes(message);
//Client.Client.Send(bytesToSend,SocketFlags.None);
Client.GetStream().Write(bytesToSend,0, bytesToSend.Length);
Client.GetStream().Flush();
}
catch (Exception ex)
{
throw ex;
}
}
}
#endregion

UDP broadcast of WCF Service address

Im have a lab-environment in VMware with a WS2008R2-server and a W7-client. Im trying to broadcast a WCF-service-address from the server and receive this in the client. Im using System.Net.Sockets in C# .NET and I can successfuly send data from the server. I looks okay with WinDump at least. But when I try to receive this on the client it fails. I cant understand where the problem is..? The client can communicate with the server in other ways and with my WCF-service if I manually enter its address. I have turned of my firewalls in the lab-environment just in case.
[Update]
I checked WinDump on my client-vm and the same udp-message showes up here as well so it seem to be able to receive the broadcast. But why arent the ReceieveFrom-method returning anything? Have I setup the client socket wrong? Should it bind to the Any-address or to its local ip? Neither works...
[/Update]
Heres the server-code:
public static class MulticastServer
{
static Socket socket;
static IPEndPoint ep = new IPEndPoint(IPAddress.Broadcast, 9050);
public static void Open()
{
socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true);
}
public static void Send(string message)
{
socket.SendTo(Encoding.ASCII.GetBytes(message), ep);
}
public static void Close()
{
socket.Close();
}
}
And the client:
public static class MulticastClient
{
public static string ReceiveOne()
{
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPEndPoint ep = new IPEndPoint(IPAddress.Any, 9050);
socket.Bind(ep);
byte[] data = new byte[1024];
EndPoint e = (EndPoint)ep;
int i = socket.ReceiveFrom(data, ref e);
socket.Close();
return Encoding.ASCII.GetString(data, 0, i);
}
}
I'm not sure where your current problem is but by reading your question I immediately knew that you are reinventing a wheel. Upgrade to .NET 4.0 and use WCF Discovery which is exactly for this purpose - UDP based searching for service with given contract and UDP based announcements about services. Moreover it is based on WS-Discovery protocol so I guess it should be interoperable. Isn't it better than custom solution?

Get client IP address for WCF, post operation

I am trying to determine the client's IP address following this link: http://www.danrigsby.com/blog/index.php/2008/05/21/get-the-clients-address-in-wcf/
In .Net 3.0 there was not a reliable way to obtain the address of the
client connecting to a WCF service. In .Net 3.5 a new property was
introduced called RemoteEndpointMessageProperty. This property gives
you the IP address and port that the client connection came into the
service on. Obtaining the this information is pretty straight forward.
Just pull it from the IncomingMessageProperties of the current
OperationContext by the RemoteEndpointMessageProperty.Name and access
the Address and Port properties.
> [ServiceContract] public interface IMyService {
> [OperationContract]
> string GetAddressAsString(); }
>
> public class MyService : IMyService {
> public string GetAddressAsString()
> {
> RemoteEndpointMessageProperty clientEndpoint =
> OperationContext.Current.IncomingMessageProperties[
> RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
>
> return String.Format(
> "{0}:{1}",
> clientEndpoint.Address, clientEndpoint.Port);
> } }
Things to note:
This property only works on http and tcp transports. On all other
transports such as MSMQ and NamedPipes, this property will not be
available.
The address and port are reported by the socket or http.sys
of the service’s machine. So if the client came in over a VPN or some
other proxy that modified the address, that new address will be
represented instead of the client’s local address. This is desirable
and important because this is the address and port that the service
sees the client as, not as the client sees itself as. This also means
that there could be some spoofing going on. A client or something in
between the client and server could spoof an address. So, do not use
the address or port for any security decisions unless you add in some
other custom checking mechanisms.
If you are using duplexing on your
service, then not only will the service have this property populated
for the client, but the client will also have this property populated
for the service for each call from that service.
I have operationContracts of WebInvoke/Post and WebGet. The code works when the client request is a WebGet. But when the client request is a WebInvoke, I will get the WCF host IP. Any solution? Thanks.
Here is the interface
[OperationContract]
[WebGet(UriTemplate = RestTemplate.hello_get)]
Stream hello_get();
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = RestTemplate.hello_post)]
Stream hello_post();
// Code for getting IP
private string getClientIP()
{
//WebOperationContext webContext = WebOperationContext.Current;
OperationContext context = OperationContext.Current;
MessageProperties messageProperties = context.IncomingMessageProperties;
RemoteEndpointMessageProperty endpointProperty =
messageProperties[RemoteEndpointMessageProperty.Name]
as RemoteEndpointMessageProperty;
return endpointProperty.Address;
}
public Stream hello_get()
{
string ip = getClientIP();
...
}
public Stream hello_post()
{
string ip = getClientIP();
...
}
Have you tried using the HttpContext? It's not available in all WCF modes but this could work depending on your environment:
if (HttpContext.Current != null)
{
Trace.WriteLine(
"Who's calling? IP address: '{0}', Name: '{1}', User Agent: '{2}', URL: '{3}'.",
HttpContext.Current.Request.UserHostAddress, HttpContext.Current.Request.UserHostName,
HttpContext.Current.Request.UserAgent, HttpContext.Current.Request.Url);
}

WCF: Net.TCP multiple bindings, same port, different IP Addresses

I've run into a problem. I'm a little new at WCF so any help would be greatly appreaciated.
Here's my code:
public static void StartHosts()
{
try
{
// Create a new host
ServiceHost host = new ServiceHost(typeof(ServerTasks));
List<IPAddress> ips = new List<IPAddress>(Dns.GetHostAddresses(Dns.GetHostName()));
if (IPAddress.Loopback != null)
ips.Add(IPAddress.Loopback);
ips.RemoveAll(i => i.AddressFamily != AddressFamily.InterNetwork);
foreach (var ip in ips)
{
string uri = string.Empty;
// Formulate the uri for this host
uri = string.Format(
"net.tcp://{0}:{1}/ServerTasks",
ip.ToString(),
ServerSettings.Instance.TCPListeningPort
);
// Add the endpoint binding
host.AddServiceEndpoint(
typeof(ServerTasks),
new NetTcpBinding(SecurityMode.Transport) { TransferMode = TransferMode.Streamed },
uri
);
}
// Add the meta data publishing
var smb = host.Description.Behaviors.Find<ServiceMetadataBehavior>();
if (smb == null)
smb = new ServiceMetadataBehavior();
smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
host.Description.Behaviors.Add(smb);
host.AddServiceEndpoint(
ServiceMetadataBehavior.MexContractName,
MetadataExchangeBindings.CreateMexTcpBinding(),
"net.tcp://localhost/ServerTasks/mex"
);
// Run the host
host.Open();
}
catch (Exception exc)
{
DebugLogger.WriteException(exc);
}
}
An exception is thrown on the line: 'host.Open();'
The exception is:
System.InvalidOperationException
A registration already exists for URI 'net.tcp://192.168.1.45:4329/ServerTasks'.
What I'm trying to do is bind to all the network addresses on the machine so that the client applications can reach the service from whatever network they see it on. When I run this code it finds and attempts to set up a binding for about 5 different IPs, including 127.0.0.1.
192.168.1.45 is the second IP that it attempts to bind to. At the point that it throws the exception I can see (using netstat) that the program has bound to the first IP in the list on port 4329. There isn't anything bound to port 4329 on the address mentioned in the exception.
Sorry there's not a lot of details, I wanted to give a concise post. If anyone needs any more info I'll be happy to supply it.
Note: I've tried setting PortSharingEnabled to true for the NetTcpBinding that gets created inside the foreach loop, but I still experienced the same error.
Any help or advise would be greatly appreaciated!
Thanks
Thanks for the info Corazza!
I've figured out how to accomplish this. I was going about this all the wrong way.
My ultimate goal was to have the service listening on every IP Address available on the machine. Trying to bind to each address individually is the wrong way of doing this.
Instead, I only needed to bind the service to the machine's Host Name (not 'localhost') and WCF automatically listens on all adapters.
Here's the corrected code:
public static void StartHosts()
{
try
{
// Formulate the uri for this host
string uri = string.Format(
"net.tcp://{0}:{1}/ServerTasks",
Dns.GetHostName(),
ServerSettings.Instance.TCPListeningPort
);
// Create a new host
ServiceHost host = new ServiceHost(typeof(ServerTasks), new Uri(uri));
// Add the endpoint binding
host.AddServiceEndpoint(
typeof(ServerTasks),
new NetTcpBinding(SecurityMode.Transport)
{
TransferMode = TransferMode.Streamed
},
uri
);
// Add the meta data publishing
var smb = host.Description.Behaviors.Find<ServiceMetadataBehavior>();
if (smb == null)
smb = new ServiceMetadataBehavior();
smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
host.Description.Behaviors.Add(smb);
host.AddServiceEndpoint(
ServiceMetadataBehavior.MexContractName,
MetadataExchangeBindings.CreateMexTcpBinding(),
"net.tcp://localhost/ServerTasks/mex"
);
// Run the host
host.Open();
}
catch (Exception exc)
{
DebugLogger.WriteException(exc);
}
}
Mel,
While I've never tried this before myself, here's some samples to look at that I've heard before. You may want to create your binding object first and then add the same instance to the AddServiceEndpoint method, just a thought so you're not creating new bindings every time as I remember reading somewhere that netTCPBindings should be a 1:1 relationship with the address (even though you're using different addresses).
I don't think you have to worry about port sharing as your opening up multiple ports.
Here's a sample of what you may want to accomplish with multiple ports.
http://www.aspfree.com/c/a/Windows-Scripting/WCF-and-Bindings/2/
Here's a good sample for using portsharing on the NetTcpBinding.
http://blogs.msdn.com/drnick/archive/2006/08/08/690333.aspx
-Bryan
Looks like i'm bit late :). But anyway - there is quite simple way to make Wcf listen all available network interfaces "net.tcp://0.0.0.0:8523/WCFTestService".