When service reference is added in the client application, many files are added to client as part of service reference like Reference.cs, ServiceReferences.ClientConfig, .svcinfo files, .datasource files, .disco files, .wsdl files, .xsd files etc.
Default Constructor & Parameterized Constructors are created within the Reference.cs
public ServiceAPIClient() :
this(defaultBinding, defaultAddress) {
}
public ServiceAPIClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) :
base(binding, remoteAddress) {
}
Default Constructor ‘ServiceAPIClient()’ can be used for creating proxy for normal cases. It will use the default EndpointAddress and Binding from the Reference.cs file.
private static System.ServiceModel.Channels.Binding defaultBinding = new System.ServiceModel.BasicHttpBinding();
private static System.ServiceModel.EndpointAddress defaultAddress = new System.ServiceModel.EndpointAddress("http://localhost:56789/Service/ServiceAPI.svc");
In case if the service EndpointAddress to be changed, Usually Service reference is updated and used.
Constructor with Parameters can be used in this case, by defining the EndpointAddress and Binding Type.
public ServiceAPIClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) :
base(binding, remoteAddress) {
}
Code:
Binding binding = new BasicHttpBinding();
EndpointAddress address = new EndpointAddress("http://localhost:56789/Service/ServiceAPI.svc");
ServiceAPI.ServiceAPIClient(binding, address);
Here there is no need to Configure the service again if there is no change in the service part. Only the service host address is changed.
Thursday, March 27, 2008
WCF Client Proxy - Default Constructor & Parameterized Constructors
Subscribe to:
Post Comments (Atom)
1 comment:
Nice post! Thanks!
Post a Comment