tags:

views:

48

answers:

2

Hello,

I built a project as descripted in this URL:

http://msdn.microsoft.com/en-us/library/ms734784.aspx

I used the app.config version. But using the code-Version does not change anything (the timeout-error still occurs).

To create the ServiceHost I used the following code:

this.serviceHost = new ServiceHost(typeof(Calculator));
// Open the ServiceHostBase to create listeners and start 
// listening for messages.
this.serviceHost.Open();

On the client side I used the following code:

ChannelFactory<ICalculator> factory = new ChannelFactory<ICalculator>("netTcp_ICalculator");
ICalculator communicationChannel = this.factory.CreateChannel();
string test = communicationChannel.GetData(5);

On the last line the program waits one minute, then I get a timeout:

This request operation sent to net.tcp://localhost:8008/Calculator did not
receive a reply within the configured timeout (00:01:00).
The time allotted to this operation may have been a portion
of a longer timeout. This may be because the service is still
processing the operation or because the service was unable to
send a reply message. Please consider increasing the operation
timeout (by casting the channel/proxy to IContextChannel and
setting the OperationTimeout property) and ensure that the service
is able to connect to the client.

The class Calculator and the interface exist. Besides this timeout I get no other error. I set a breakpoint at the GetData method, but the breakpoint was not hit.

I have tried to change the portnumber used for the client from 8008 to 8009, but let the endpoint for the server at 8008. I wanted to test if the client tries to reach the server. Then I get the error that the other side is not answering (EndpointNotFoundException).

When changing the client port back to 8008 I get the Timeout error again.

Is there anything wrong with my code? How can I ensure that the server can reach the client? Client and server are in the same test application.

Thank you for your help!

EDIT:

I have now deleted the app.config settings. And tried to build the server and client by using the sourcecode. To build the server was no problem. But building the client is a problem.

There is no way to call:

CalculatorClient cc = new CalculatorClient(myBinding, myEndpointAddress);

The compiler does not know CalculatorClient.

Can I use the following instead?

NetTcpBinding myBinding = new NetTcpBinding();
myBinding.Security.Mode = SecurityMode.None;

// Create the address string, or get it from configuration.
string tcpUri = "net.tcp://localhost:8008/Calculator";

// Create an endpoint address with the address.
EndpointAddress myEndpointAddress = new EndpointAddress(tcpUri);
ChannelFactory<ICalculator> factory = new ChannelFactory<ICalculator>(myBinding, myEndpointAddress);
factory.Open();
ICalculator communicationChannel = this.factory.CreateChannel();
string test = communicationChannel.GetData(5);

I get again an exception at the last line :(

SOLVED:

Ok, the problem is solved. I needed to call the WCF host initialization via an own thread:

hostThread = new Thread(this.createService);
hostThread.Start();

Now everything works fine!

Thanks for all your help!

+3  A: 

You are not adding any endpoints to the service. You did not include the part of the example code that adds the service endpoint:

Uri tcpUri = new Uri("net.tcp://localhost:8008/Calculator");
// Create the ServiceHost.
ServiceHost sh = new ServiceHost(typeof(Calculator), tcpUri);

// Create a binding that uses TCP and set the security mode to none.
NetTcpBinding b = new NetTcpBinding();
b.Security.Mode = SecurityMode.None;

// Add an endpoint to the service.
sh.AddServiceEndpoint(typeof(ICalculator), b, "");
// Open the service and wait for calls.
sh.Open();

Edit: Same goes for your client. You have to specify an endpoint addresses

  // Create a channel factory.
  NetTcpBinding b = new NetTcpBinding();
  b.Security.Mode = SecurityMode.None;
  Uri tcpUri = new Uri("net.tcp://localhost:8008/Calculator");

  ChannelFactory<ICalculator> myChannelFactory = new ChannelFactory<ICalculator>(b,new EndpointAddress(tcpUri));

  // Create a channel.
  ICalculator calculator = myChannelFactory.CreateChannel();

Edit2: I can't currently test this code... Will give it a try tomorrow morning.

Flo
Using this code is an alternative to using the app.config settings ("The following code sets up the same endpoint using configuration:"). When I use both (your code and the app.config settings) I get an InvalidOperationException at Open() that I want to create two endpoints with the same ListenUri.
Chris
This is correct behaviour. You can not set up an endpoint in the app.config and then try to add the same endpoint in code. Disable your app.config endpoint and try the code again. Also note the missing endpoint in your client.
Flo
Thank you for you suggestions. I have completely deleted the app.config settings and created the server and client via code. But I have problems to use that client-code. Please see my edit above.
Chris
Hi Chris looks like you solved it :) If not let me know.
Flo
Yes I solved, thank you. The solution was to call the host from an extra thread. Don't know why, but this way it works.
Chris
A: 

Are you using Windows 7?

If so, you likely need to run Visual Studio as an Administrator. UAC will not let you create the service endpoint unless you are a running as an administrator.

Scott P
Yes I use Windows 7, but I have UAC disabled. Is there still a need to run it as administrator?
Chris
I don't believe I have had this problem in Windows 7...and I've been developing WCF services on it for well over a year (since the early betas.) UAC should not be disabled to host WCF services...there is likely something else wrong.
jrista
@jrista: Do you need to run VS 2010 in admin mode? I have needed to do this since running Windows 7. It could be a Win 7 configuration thing!?
Scott P
@Scott: No, I run it as a normal process. VS2008 and VS2010 are both Vista/Win7 aware, so there is no need to run them as administrator like you once had to do with VS2005. Particularly with VS2010, running and testing services has gotten very easy, since they now have an automatic host similar to the ASP.NET developer host. If you mark a WCF service project as the startup project, and run the solution, VS will automatically host the service for you in another tool. It will also bring up a general client tool for testing...allowing you to invoke any operation with any message/data contract.
jrista