I have a few questions regarding WCF:
– Can a program act as both client and server ?
– What’s wrong with my code :
The service:
[ServiceContract]
public interface IShout
{
[OperationContract]
String Broadcast(String message);
}
The implementation:
public class eveShout : IShout
{
public String Broadcast(String message)
{
return message + " reply";
}
}
I start the service in the form contructor:
ServiceHost s = new ServiceHost(typeof(IShout));
s.AddServiceEndpoint(typeof(IShout), new BasicHttpBinding(), "http://localhost:9189");
s.Open();
The, When I click a button on another form, I want to send a message and get a reply back.
I use the following code:
ChannelFactory<IShout> channel = new ChannelFactory<IShout>(new BasicHttpBinding(), "http://localhost:9189");
IShout shout = channel.CreateChannel();
String reply = shout.Broadcast("Test");
Note: all the code is in the same namespace.
Note: I first start the “server” (open) then the app continues.
when i run the code, the server is created. I use netstat -a to see if the port is open. when I run the command, i get 9189 is in listening state. but the code stops at the command reply = shout(“test”). and I get anexception that says
The request channel timeout while waiting for a reply after 00:00:59…
Yes, you can have an app act as both client and server.
I see a couple of things that may need correcting. First, try adding OperationContract.
Then, take the type of the class, not the interface.
Make sure you you have permission to access the namespace (s.Open() should throw an exception if it does not).
See if these suggestions help.
(oh yeh, and make Broadcast public in your class)
A quick example WindowsFormsApplication looks like this…
See if you can get something as simple as this working. That will at least prove to you that it can be done and that the problem is somewhere else.