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

Design pattern for redirecting dependencies
MVC3 Pre-Loading many images in view
how do i list all possible directories and paths to subfolders ignoring files
SynchronizationContext.Post not working inside BackgroundWorker
Download File from Given URL
Insert time data into SQL Server with C#
can i have two connection string in two different web config in one asp.net project
How to convert from int to int?, in C#?
Execute multiple command (using BCP) with same ProcessStartInfo instance in C#
How to make my program check for an installation to have completed and then “come back to life”
Getting a thread's visible window's title
generated PDF Error - the font 'Algerian' contains a bad /BBox
int to float pointers
In T4 code generation, how can I grab a type from a referenced assembly?
string with special characters conversion
How many chars one line can contain?(C# printing text)

Categories

HOME
asterisk
max
gcc
javafx
swift3
mobile
sql-server-2014
salesforce
thermal-printer
browser-cache
aggregation-framework
servicenow
symfony-3.2
sitemap
angularjs-ng-repeat
google-my-business-api
aws-sdk-cpp
desktop-application
ng2-charts
popup
openscad
smooth-scrolling
loader
udeploy
jq
custom-component
git-branch
ajaxcontroltoolkit
ejb-3.1
android-navigation-drawer
functional-dependencies
chartist.js
pydub
utc
proof-general
pypy
edsdk
mplab
visual-studio-debugging
log4net-configuration
azure-vm-scale-set
leaderboard
stdin
lex
mediastream
file-descriptor
definition
ocpjp
activeandroid
opos
remap
stanford-nlp-server
ear
visualizer
px4
wink
playframework-1.x
liferay-ide
blackberry
apache-commons-cli
catalyst
google-scholar
indoor-positioning-system
dbi
ldd
class-design
particle-system
syslog4j
nimbus
jde
bbedit
ideavim
updating
first-order-logic
winforms-interop
dpkt
p4python
cakephp-2.6
esb-toolkit-2.1
dockerpy
system.io.packaging
javacompiler
ios-enterprise
parallel-port
nvcc
code-complete
8-puzzle
octokit
ojb
latex-suite
architectural-patterns
generator-expression
libx264
feincms
m3u
databus
execjs
windows-xp-embedded
hwnd
hp-trim
html-help-workshop
office-2003
asp.net-1.1

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