c#


WCF: Accessing a windows form from inside a service


How can I achieve this scenario?
I have a WCF service hosted in a windows form and whenever a client of the service calls a method on the service i want the service to be able to write a message to a textbox on the windows form.
I was thinking that i would make my WCF service a Singleton, pass my form using an Interface that the form implements, into the service and then store that instance. then when a client calls the service i can simply use the form instance to write to the textbox.
I cannot do this of course as i cannot pass a Form into a WCF service.
Any ideas or code samples?
The service instance and your Windows form are running in two separate threads, and you cannot just update a UI element on your main UI thread from the service instance.
You need to use a synchronization context and a delegate in order to properly and safely update your UI from the service thread.
See this CodeProject article - about in the middle, the author talks about the "UI thread woes". That's basically what you need to do:
SendOrPostCallback callback =
delegate (object state)
{
yourListBox.Add(state.ToString());
};
_uiSyncContext.Post(callback, guestName);
See Juval Lowy's MSDN article "WCF Synchronization Contexts" for a comprehensive introduction to the topic.
Hosting a WCF service inside a Winforms app seems like a rather bad idea to me - first of all because of all those threading issues, and secondly, it'll only ever work if the winforms app is up. Couldn't you put your WCF service into a console app or a Windows NT Service, and then just create a Winforms based monitoring app, that might check e.g. a database table for incoming request messages or something?
Marc
Take a look at this SO answer - as far as I understand it, it is basically the same question.
You can inject dependencies into WCF services: you just need to implement a custom ServiceHostFactory that wires everything up for you.

Related Links

Relationship 1:N With EF
I want to Display All Objects from AWS S3 bucket on Label on Xamarin Platform. Can you suggest me how to do it?
devexpress tabbed ui application, how to make new page load a form (visual studio)
Local resources in ASP.NET, is it possible to modify control sizes?
DateTime format conversion mm/dd/yyyy to yyyy/mm/dd
How to implement Dependency Injection on a headless IoT BackgroundTask?
How do I correctly call a post method to twitch api?
UWP - What happens if navigating away from page while async/await is running?
Xamarin custom crossplatform view (for android only)
Remove shown tooltip when new one appear c#
c# WPF UI hangs while Update data in UI thread
Entity Framework 6 Migrations - Generates “SchemaName”.“TableName”
One Web API Controller to control all models
Overloading operators in ClearScript
Captcha recognition using c# and Emgu CV
Unity 3d - Calling function from another class [duplicate]

Categories

HOME
http
alasql
ide
gap-system
livecharts
itext7
opc-ua
jshell
gnuradio
pip
pyodbc
wkhtmltopdf
theory
ndepend
kairosdb
guidewire
react-select
brightway
samba
autologin
xforms
bootstrap-datetimepicker
monad-transformers
vala
ppp
aws-certificate-manager
grunt-contrib-uglify
rancher
adobe-captivate
gulp-jshint
javapns
blockly
mplab
webspeech-api
restler
wit-ai
sampling
motherboard
strategy-pattern
scip
pygraphviz
gem-fury
office-interop
wicket-tester
vertical-alignment
jvisualvm
pyexcel
autodesk-data-management
vpython
python-venv
spring-webflow-2
hierarchical-clustering
remap
denodo
spring-cloud-bus
serialversionuid
standard-error
extjs3
photoshop-script
petapoco
tsc
acceleo
robotc
stringbuilder
ownership
glade
vga
patching
transaction-isolation
nd4j
bbedit
web-optimization
google-finance-api
web-performance
producer-consumer
biginsights
exchange-server-2007
page-flipping
dpkt
p4python
tinymce-plugins
jeet-grid
static-files
crtdbg.h
javacompiler
nsmutableurlrequest
msdropdown
scjp
switchpreference
amzi-prolog
isl
visual-studio-setup-proje
opencobol
dayofweek
windows-phone-store
mdp
kademlia
coinbase-php
bho
feincms
jquery-ui-map
parsec
mysql-connector
eeprom
towerjs
gmagick
conditional-operator
xslt-grouping
office-2003
ironpython-studio

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App