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

SqlCeException Not enough storage
Using Unity configuration file, how to register FluentValidation Validator Implementation with concrete in a different assembly
Call C# method from javascript drag and drop event
Customize order by in Gridview rows
how to display enum with set of special charaters as V1.0, V2.0 and V3.0
Get and object to orbit around another when button is pressed C# XNA
Automating Powerpoint Data from Microsoft Excel
How to get the value of a property of a class in a non executing assembly through reflection
why is log4net appearing in console and not text file
C# Singleton instance returns null
IOrderedQueryable Skip and Take
Bind control to dynamic collection of key-value pairs
iOS Notifications not sent using PushSharp. No event is raised either
display aspx in iframe and aspx is displaying pdf
ICollectionView's CurrentChanged event not working if we create full property
C# calendar date format

Categories

HOME
internet-explorer
youtube-api
kendo-ui-angular2
teradata
stanford-nlp
windows-server-2008
data-modeling
xss
perl6
schema
install
handlebars.js
jcodemodel
facebook-sharer
elastalert
comma
google-closure-compiler
multibinding
k-means
amazon-data-pipeline
smooth-scrolling
alert
scotty
sonarlint
descriptor
matlab-gui
aws-iot
grunt-contrib-uglify
dynamic-memory-allocation
ipython-parallel
jql
kubectl
oracle-nosql
microstrategy
mkdir
kepserverex
anypoint-studio
word-wrap
motion
origami
icu
wicket-tester
redbean
binary-decision-diagram
qt-quick
amazon-rds-aurora
weld
boost-regex
aot
hellosign
totalview
jtextarea
reloaddata
blitz3d
stripes
x12
class-design
konakart
netlink
geokit
key-value-store
hotkeys
razorengine
jmap
periodic-processing
custom-build-step
instabug
askbot
windows-kernel
lytro
first-order-logic
cyberduck
hexagonal-tiles
valdr-bean-validation
veeam
optimistic-locking
pysvn
unityvs
gnip
redmine-plugins
parallel-port
step
switchpreference
nspopupbutton
xpand
rda
yandex-api
code-smell
class-library
git-subtree
starling-framework
kuka-krl
flask-mongoengine
grunt-wiredep
google-mirror-api
sygic
grape-api
generator-expression
ntdll
gpl
antixsslibrary
cryptarithmetic-puzzle
ios6-maps
wxperl
nsundomanager
visual-leak-detector
zend-validate
session-hijacking
overlays
functional-specifications
netduino
meego-harmattan
rollover
xslt-grouping
atmega16
authkit
opengl-to-opengles

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