There is such a client code:

private void btChangeState_Click(object sender, RoutedEventArgs e) { if (!IsConnected) { Exception error = null; if (!TryConnect($"net.tcp://{tbIP.Text}:{tbPort.Text}/JobService", out Proxy, out error)) { MessageBox.Show(error.Message, "Error!", MessageBoxButton.OK, MessageBoxImage.Error); return; } } else { Proxy.Close(); } IsConnected = !IsConnected; Jobs.Clear(); } private bool TryConnect(string endpoint, out JobServiceClient proxy, out Exception error) { try { var callback = new ClientCallback(); callback.GiveJob += Callback_GiveJobAsync; var ctx = new InstanceContext(callback); proxy = new JobServiceClient(ctx); proxy.Endpoint.Address = new EndpointAddress(endpoint); proxy.Connect(); error = null; return true; } catch (Exception e) { proxy = null; error = e; return false; } } 

Why does this error fall when I call Connect? "This type of CollectionView does not support changes to its SourceCollection from a stream other than the Dispatcher stream." No additional streams are used in this case, everything is basically.

Now, it’s not the client on which the error pops up to debug, but the server, where the Connect function via the proxy is actually called. Campaign error is there. But why she pops up, I still do not understand ... In the service contract there is a collection of public ObservableCollection<ClientView> Clients { get; } = new ObservableCollection<ClientView>(); public ObservableCollection<ClientView> Clients { get; } = new ObservableCollection<ClientView>(); . In the Connect function, a ClientView object is created and added to it. And it is with this addition (according to the debugger) that the error falls, which is simply transmitted to the client. But everything happens in one thread, there are no extra threads there! Where does the mistake come from?

    2 answers 2

    As it turned out by experience, the problem was that the button that starts the server had an asynchronous handler. It is not completely clear how, but somehow he influenced this, since it was worth making it synchronous, as the error disappeared.

      Still check again. In appearance, it seems that this is a common problem for WPF applications — changing the UI of components outside the main UI thread. Perhaps you change the object in the callback, and callback is called in another thread. This can be easily verified. Try calling a callback using the variable component Dispatcher:

        component.Dispatcher.Invoke(callback) 
      • I do not think so. And the code did not help, the same error with him. - PECHAPTER
      • WCF calls callback methods where necessary, the problem is not that - Pavel Mayorov
      • @Pavel Mayorov even did not understand anything ... Who is calling? Where is calling? Why does it cause? What is the problem??? - PECHAIRTER