开发者

Beginner WCF implementation question

Within my main VS solution I have the following 3 projects:

Beginner WCF implementation question

The host hosts the WCFService in a console app (will be changed to a Windows Service after debugging). The host solution also has an App.config which sets up the NetPipe Binding for IDCCContract, and 开发者_StackOverflow社区MEX NetPipe Binding for IMetaDataExchange configurations.

My question: I would like the host to start the DCC Service as soon as it runs, and then clients can use the WCF contract to access data from that instance of DCC Service. Currently, a new instance of DCC Service is created with ever Client that runs, which is what I do not want.

How can I have once instance of my DCC Service that is created when the Host begins, and clients can connect and read data from that instance using the WCF contract?


Change your host console test application to use this overload of the ServiceHost ctor:

public ServiceHost(
    Object singletonInstance,
    params Uri[] baseAddresses
)

and supply the singleton service instance yourself.

EDIT: i.e. replace the code in the host app which sets up the service host with something like:

var singletonInstance = new DCCService();
//... you could add stuff here to initialise your singleton instance as you would like it 
using (ServiceHost host = ServiceHost(singletonInstance, baseAddresses))
{
...

You'll also need to set the InstanceContextMode of the service to Single, for example by applying this attribute to the service class:

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]

All WCF communications from clients to the DCC Service will then be handled by the specific instance of DCCService which you instantiated.


On your Solution, right-click and select 'Setup StartUpProjects' there you can select which projects start and the order. Always start the Service first, then select the Client (checkbox)

Thanks, Sebastian Castaldi

For more information, look at this MSDN article for detail in configuring self-hosted services for debugging.


May be this http://www.codeproject.com/KB/WCF/Sessions_in_WCF.aspxcould could be helpful to understand Chris's proposal. Also if you want have make sure that you have one instance for client refer to this link How to maintain Session and same channel in WCF WebApplication.


Ryan,

To test your services and see how they communicate is helpful to have the Service code in one solution and the Client code on another instance of Visual Studio, that way you don’t have to deal with what starts first or who call what.. also is a good way to separate concerns

1 Open a new instance of visual studio, create a Solution called WCFService add the DCC Service and the Host Console Test App (set it as Startup Project).

2 Open a new instance of visual studio and create a solution called WCFClient and the Client Console Test App.

3 On the Client 'Console Test App' add a Service Reference to 'Host Console Test App' or use svcutil to generate the proxy code.

Thanks, Sebastian

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜