2
votes

I used this link to learn WCF programming

I used the sample code in that link that was for connection between client and server. Now I want to convert that console applications to WPF but I get this error when I run client (of course I run server before running client) :

There was no endpoint listening at net.pipe://localhost/PipeReverse that could accept the message. This is often caused by an incorrect address or SOAP action.

Also I should say that sample code does not have any app.config file.

In MainWindow.xaml file for client :

[CallbackBehavior( ConcurrencyMode=ConcurrencyMode.Multiple,UseSynchronizationContext=false )]
public partial class MainWindow : Window,ICallbacks
{
    public void MyCallbackFunction(string callbackValue)
    {
        Dispatcher dispatcher=Dispatcher.CurrentDispatcher;
        dispatcher.BeginInvoke( new Action(
        () => textBox1.Text = callbackValue ) );
        //     MessageBox.Show( "Callback Received: {0}",callbackValue );
    }
    IStringReverser _channel;
    [ServiceContract( SessionMode = SessionMode.Required,
    CallbackContract = typeof( ICallbacks ) )]
    public interface IStringReverser
    {
        [OperationContract]
        string ReverseString(string value);
    }


    public MainWindow()
    {
        InitializeComponent();
        MainWindow myCallbacks = this;
        var pipeFactory = new DuplexChannelFactory<IStringReverser>(
           myCallbacks,
           new NetNamedPipeBinding(),
           new EndpointAddress(
              "net.pipe://localhost/PipeReverse" ) );
        ThreadPool.QueueUserWorkItem( new WaitCallback(
            (obj) =>
            {
                _channel = pipeFactory.CreateChannel();
            } ) );
    }

    private void button1_Click(object sender,RoutedEventArgs e)
    {
      _channel.ReverseString( "Hello World" );
    }
}
public interface ICallbacks
{
    [OperationContract( IsOneWay = true )]
    void MyCallbackFunction(string callbackValue);
}

In MainWindow.xaml file for server :

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }


    private void button1_Click(object sender,RoutedEventArgs e)
    {
        using (ServiceHost host = new ServiceHost(
       typeof( StringReverser ),
        new Uri[]{
      new Uri("net.pipe://localhost")
        } ))
        {
            host.AddServiceEndpoint( typeof( IStringReverser ),
              new NetNamedPipeBinding(),
              "PipeReverse" );

            host.Open();

            MessageBox.Show( Properties.Resources.Service_is_available__Press__ENTER__to_exit_ );
            host.Close();
        }
    }
}

[ServiceContract(SessionMode = SessionMode.Required,
                 CallbackContract = typeof( ICallbacks ) )]
public interface IStringReverser
{
    [OperationContract]
    string ReverseString(string value);
}

public interface ICallbacks
{
    [OperationContract( IsOneWay = true )]
    void MyCallbackFunction(string callbackValue);
}

public class StringReverser : IStringReverser
{
    public string ReverseString(string value)
    {
        char[] retVal = value.ToCharArray();
        int idx = 0;
        for (int i = value.Length - 1;i >= 0;i--)
            retVal[idx++] = value[i];

        ICallbacks callbacks =
     OperationContext.Current.GetCallbackChannel<ICallbacks>();

        callbacks.MyCallbackFunction( new string( retVal ) );

        return new string( retVal );
    }
}
1
Do your server and your client run on the same machine? The NetNamedPipe binding only works on the same machine..marc_s
Arash, are you writing the Interface for the service twice? IStringReverser should only be written once in a separate dll so the exact interface can be shared by both the server and the client. The fully qualified namespace can make a difference, it's not just about the name of the interface alone.Despertar
Yes they are in same machine,also i tested with NetTcpBinding too but no successArash
@Despertar:Can you explain me more?Arash
Also have you been able to connect to a service before without all the duplexing? If not you want to start with a barebone minimal example so that know you can connect. Then go back and add the duplex and callbacks on top of that. Secondly if you host with TCP it would be easier to verify it is listening by typing in cmd.exe netstat -ano | findstr 1234 where 1234 is the port you are hosting on. That is the first thing you want to verify is that the server is even listening for connections before you try to connect to it.Despertar

1 Answers

2
votes

The Problem Solved So Easy,

All The Problem was that i mistakenly close connection after click on button in server!