I’ve implemented my first WCF application.
I need one method IConsoleData GetData();
I receive CommunicationException "There was an error reading from the pipe: The channel was closed. (109, 0x6d)." in client.
When I replaced IConsoleData GetData(); to string GetData(); application become functionable.
How should I fix the code to use IConsoleData GetData()?
Server:
//public interface IConsoleData
//{
// double GetCurrentIndicator();
//}
//public class IConsoleDataImpl : IConsoleData
//{
// public double GetCurrentIndicator()
// {
// return 22;
// }
//}
[ServiceContract]
public interface IMBClientConsole
{
[OperationContract]
//IConsoleData GetData();
string GetData();
}
public class MBClientConsole : IMBClientConsole
{
//public IConsoleData GetData()
//{
// return new IConsoleDataImpl();
//}
public string GetData()
{
//return new IConsoleDataImpl();
return "hello";
}
}
class Log
{
private ServiceHost _host;
public void initialize()
{
_host = new ServiceHost(typeof (MBClientConsole),
new Uri[]
{
new Uri("net.pipe://localhost")
});
_host.AddServiceEndpoint(typeof(IMBClientConsole),
new NetNamedPipeBinding(),
"PipeReverse");
_host.Open();
System.Threading.Thread.Sleep(1000000);
// TODO: host.Close();
}
}
Client:
//public interface IConsoleData
//{
// double GetCurrentIndicator();
//}
[ServiceContract]
public interface IMBClientConsole
{
[OperationContract]
//IConsoleData GetData();
string GetData();
}
class Program
{
static void Main(string[] args)
{
ChannelFactory<IMBClientConsole> pipeFactory =
new ChannelFactory<IMBClientConsole>(
new NetNamedPipeBinding(),
new EndpointAddress(
"net.pipe://localhost/PipeReverse"));
IMBClientConsole pipeProxy =
pipeFactory.CreateChannel();
while (true)
{
string str = Console.ReadLine();
Console.WriteLine("pipe: " +
//pipeProxy.GetData().GetCurrentIndicator());
pipeProxy.GetData());
}
}
}
if you use an Interface you have to do two things:
Make your live easier and decorate the implementation with DataContract and the members of it with DataMember Attributes and use the implementation instead of the interface.
You can find a lot about this here
(and don’t begin the implementations name with “I”)
here is a rework without the known-type stuff and I think this will help you for your
first steps – no need to dig in that deep (Interfaces + KnownTypes) yet.