1

This may be a basic question since I am new to WCF. I don't even know if this is supported or not.

I have a WCF Service called MyCustomService. I added this service reference in my client solution and now I can create a proxy object by calling:

MyCustomServiceClient myClient = new MyCustomServiceClient();
myClient.GetData();
myClient.GetData();

How, do I layout my Service so that I can pass the parameter during proxy instance creation i.e.

MyCustomServiceClient myClient = new MyCustomServiceClient("SomeString");

I noticed every method call I make creates a new instance of the MyCustomService(i.e I can get the breakpoint hit on MyCustomService Ctor). So, I want the value that I had passed when creating proxy object (i.e. SomeString) to exist for all the calls I make until the lifetime of myClient

Any ideas ?

Frank Q.
  • 6,001
  • 11
  • 47
  • 62
  • What are you trying to set in the constructor? Is the value you're trying to set unique to each client? – Tim Feb 20 '14 at 06:31
  • Yes exactly. The String I am passing is something that is unique for every client. – Frank Q. Feb 20 '14 at 18:33
  • Your best bet is to probably use `InstanceContextMode.PerSession` as Milan wrote in his answer below. You wont' be able to set the value in the constructor, so you will need to pass it in on the first call the client makes (either a specific call to set the value or check to see if it's already been set on each call). If you can provide some more design requirements for why you want/need to do this we may be able to give better answers. – Tim Feb 20 '14 at 19:11

2 Answers2

1

The proxy generation feature of Visual Studio (or svcutil.exe) creates a proxy class with five different constructor signatures. The one that you are asking about, the constructor that takes a single string, is a very useful one, because it allows you to reference different client configurations from your app.config or Web.config file.

Take this extremely simple hypothetical configuration file:

<configuration>
  <system.serviceModel>
    <client>
      <endpoint name="serverABinding" />
      <endpoint name="serverBBinding" />
    </client>
  </system.serviceModel>
</configuration>

You can then control which binding you use for the proxy:

string endpointName = useB ? "serverBBinding" : "serverABinding";

var myClient = new MyCustomServiceClient(endpointName);

Of course, the endpoint bindings I've shown above are too simple to actually be useful, but hopefully you get the idea.

Your motivation isn't exactly clear, but it sounds like you want to control the proxy behavior across all proxy instances of your application. If that's what you want, then don't use a constructor: use the endpoint configuration in your app.config or Web.config file to control the proxy connection. The default endpoint configuration that is generated by Visual Studio uses an endpoint name that matches the default name of the proxy client. Change this endpoint definition, and you'll change the behavior of your service client.

Since you are new to WCF, my advice to you is this: learn all about configuration files. Once you understand a WCF configuration file, you understand WCF.

Paul Keister
  • 12,851
  • 5
  • 46
  • 75
-2

You can not have constructor with parameter in your WCF service, even when you try to create such service you will get below error.

The service type provided could not be loaded as a service because it does not have a default (parameter-less) constructor. To fix the problem, add a default constructor to the type, or pass an instance of the type to the host.

Now if you want the data to persist for a client you can set ServiceInstanceContextMode as below as a attribute on Service Class

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)] 
 public class Service : IService
Milan Raval
  • 1,880
  • 1
  • 16
  • 33
  • That's not entirely true. Notice the last part of what you posted: `or pass an instance of the type to the host.` You can create an instance of the service using a constructor with parameters, and then pass that instance into the service host. Though that wouldn't address OP's issue. – Tim Feb 20 '14 at 06:30
  • `You can not have a constructor with parameter in your WCF service` is a lie, see http://stackoverflow.com/a/2455039/184746 – caesay Nov 22 '15 at 07:44