我使用this链接来学习WCF编程
我在该链接中使用了用于客户端和服务器之间连接的示例代码。现在我想将该控制台应用程序转换为WPF,但是当我运行客户端时我得到了这个错误(当然我在运行客户端之前运行服务器):
没有端点侦听net.pipe:// localhost / PipeReverse可以接受该消息。这个 通常是由不正确的地址或SOAP操作引起的。
另外我应该说示例代码没有任何app.config
文件。
在客户的MainWindow.xaml
文件中:
[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);
}
在服务器的MainWindow.xaml
文件中:
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 );
}
}
答案 0 :(得分:2)
问题解决得那么容易,
所有问题是我在点击服务器上的按钮后错误地关闭了连接!