Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get my WCF Service constructor called?

I'm currently trying to get my head around WCF services for an ASP.NET dev environment, and I believe that I'm doing well save for one thing that has me stumped.

Basically, I've got a WCF service set up (let's take the default, with an added constructor):

public class MyService : IMyService
{
    public MyService() { /* blah */ }
    public DoWork() { /* blah */ }
}

The IMyService interface defines the DoWork() method as an [OperationContract], as it should.

So I've got this service referenced in another project (let's say a [Unit] Test Project), via Add Service Reference on the VS2010 UI. This creates a reference to a MyServiceClient which exposes my WCF service methods, as it should.

However, when I do this in my test project:

ServiceReference.IMyService service;
service = new ServiceReference.MyServiceClient();

... the MyService() constructor does not get called, basically because I'm instantiating a MyServiceClient, not a MyService per se.

How do I go about getting that constructor called? I'm planning to use that for initialization purposes (perhaps grabbing a layer in a tiered implementation, for example?).

like image 295
Richard Neil Ilagan Avatar asked Nov 02 '10 15:11

Richard Neil Ilagan


1 Answers

That constructor will be called on the server when you make your request from the client.

Creating a "reference" to a web service (and then using the client classes) is very different to referencing a regular .DLL. All of your service code will run on the server-side, but not until the service is invoked...

like image 123
Dan Puzey Avatar answered Oct 01 '22 11:10

Dan Puzey