开发者

How to use WCF to solve a server-client system that will share the same data source but will have separate instances/session for each client

I am creating a server client system in which the server is to host a service, I tried to use .net remoting as a means of communication but have encountered problems. I've used remoting in past projects bu开发者_如何学运维t have decided to leave the remoting framework and switch over to WCF.

The system is designed as follows: The server is to host a service which allows clients to retrieve data from it. The service also store specific client instance variables that will allow it to treat clients separately (i.e different sessions). The service is meant to source this data from a database(compact database i.e .sdf) perform some logic and pass this data through classes/structs that can be used by the clients.

The most important goals are: -Make sure each client has its own separate session on the server. -Make sure all clients are serviced using the same instance of data source.

The other goal is to transport this data in binary format.

I have tried to read and understand want single-call and singleton mode means but each time I read a different material, the more I get confused and I'm still not convinced if either mode would solve my problem.

If there is anyone who understands my problem please explain to me the approach to use.

Thanks in advance.


Thanx a lot. I've gone through the first article (by @Timothy Khouri) and the features introduced is what I was looking for.

Secondly, I don't want the clients to share some data between themselves. If I understand the concept of session based clients, each client has a copy of its server object on the server machine. What I want is for this server objects to use the same linq2sql data object. Do I make the data object (linq2sql object) as a private static member? Or how should I go about it.

Also I wanted to use binary serialization because I tought it was more secure. I'm trying to prevent a situation where a hacker would get access to the shared classes and then have the ability to create his own version of the client app because I've assigned some security issues to be handled on the client side. Does/can the serialization method have any impact on this?


This is exactly what I did for a company I worked for the past 3 years. Their architecture used to be a bunch of ASMX soap calls, and I changed it to a CONNECTED (which is what you're looking for), session-aware architecture using net.tcp (WCF).

I wrote some articles a few years back that explains is AND HAS A SAMPLE DOWNLOAD APP AT THE END OF THE ARTICLE that should get you started.

Client Server Programming with WCF

Duplex WCF Services Hosted in IIS Using Net.Tcp

The first one is probably what you're looking for in particular.


I agree with @Timothy Khouri

I think both Singelton and Session mode will meet your requirments, but it's all about system scalability, We shouldn't use Singleton mode except if we really need that.

As @johnny g : http://www.stackoverflow.com/questions/1756487/should-wcf-service-typically-be-singleton-or-not

"If you DO NOT need to share business logic or memory between consumers, then DO NOT use a Singleton, the "model" DOES NOT fit the problem. It's like forcing a glass slipper on a step-sister's foot! And no one should ever have to see that"

So, I think in your case , you may use Session Mode


I'd like to start by saying I'd prefer a stateless service whenever possible, and unless you really require a stateful service I'd discourage it. It's just another potential issue.

Having said that I know that sometimes you do need a proper session. This is not an uncommon scenario, and is not that difficult.

Firstly, to ensure a session per client your WCF services can / should be decorated with an instance context mode, like so:

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
public class MyService : IMyService
{ 

This means that when a client opens a channel they'll instantiate a service instance; provided they retain that channel the instance will persist and they'll reuse it.

And to use binary transport you should simply configure your endpoint to use net.tcp binding -

<service name="MyService">
    <endpoint binding="netTcpBinding" contract="IMyService ... />

The specific configuration implementation can be frustrating for a WCF newbie - use the Visual Studio WCF configuration tool if you aren't having any success.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜