0

I'd like to use a web service (created in my main thanks to new ServiceHost(typeof(..)) ) and I would like to retrieve data received by my server to process it in my main.

I've begun with a simple event handler but it is impossible because I have to create an instance instead of using typeof.

Is there another solution?

Rob Hruska
  • 118,520
  • 32
  • 167
  • 192
Marti
  • 35
  • 1
  • 1
  • 3
  • Couldn't you just handle the request, get the data from the caller, put it into a persistent store (e.g. database), and then have your main app retrieve it from there?? – marc_s Aug 30 '11 at 07:31
  • Not sure... because I have to be warn of any update. – Marti Aug 30 '11 at 07:39

1 Answers1

0

You can use a singleton WCF service. You need to decorate the service class with [ServiceBehavior] and specify InstanceContextMode.Single, and then you pass an instance of the service class to the ServiceHost constructor.

Sasha Goldshtein
  • 3,499
  • 22
  • 35
  • 1
    Be aware: either this will **serialize* all requests (handling one request after another), thus potentially creating a major bottleneck in your system; or you have to deal with multi-threaded programming, which is hard and tricky and error-prone.... – marc_s Aug 30 '11 at 07:30
  • Hum, I heard of it but I don't know if I still be able to use it in the futur so I wanted to find something different. There is no other solution? btw thanks! – Marti Aug 30 '11 at 07:31
  • @marc_s: Um, you still have to "deal with multi-threaded programming" if you use a per-call or per-session service. I mean, the service class will of course be thread-safe, but any other resources you still need to protect. So I'm reluctant to consider it relevant here. – Sasha Goldshtein Aug 30 '11 at 10:50
  • If you have a per-call service, and you store data that's received from the caller in a database - where do you need to deal with multi-threading?? Using per-call is **much simpler** than creating a fully multi-threaded singleton..... – marc_s Aug 30 '11 at 10:58
  • @marc_s: Seriously? Storing the data in a database means you don't have to deal with multi-threading anymore? And you can get by with using *only* a database? I'm sorry, but this is an oversimplification. – Sasha Goldshtein Sep 07 '11 at 17:18