Click here to Skip to main content
15,891,473 members
Articles / WCF
Technical Blog

Using WCF Service Without Config

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
13 Oct 2016CPOL1 min read 6.4K   3  
How to use WCF service without config

When you use WCF normally, you need to add the system.serviceModel elements to the Solutions Primary project config file. There are many scenarios where this cannot be done or you would prefer not too! I've created a single class so I can reference the required logic, anywhere within my solution. Each element is broken down into its own methods.

Basic Binding

This is a simple method, which will create and return a basic binding object. You can add more elements to the binding.

C#
public static BasicHttpBinding SetBasicBinding() 
{ 
BasicHttpBinding binding = new BasicHttpBinding(); 
binding.TextEncoding = System.Text.Encoding.UTF8;

binding.AllowCookies = true; 
binding.ReceiveTimeout = new TimeSpan(0,30,00); 
binding.SendTimeout = new TimeSpan(0, 30, 00); 
binding.MaxBufferPoolSize = 2147483647; 
binding.MaxBufferSize = 2147483647; 
binding.MaxReceivedMessageSize = 2147483647; 
binding.ReaderQuotas.MaxDepth = 34; 
binding.ReaderQuotas.MaxArrayLength = 2147483647; 
binding.ReaderQuotas.MaxStringContentLength = 2147483647; 
return binding; 
}

Secure Binding

Within the WCF logic, if you want to use HTTPS, you really need to make sure you use the Secure binding not basic.

C#
public static BasicHttpSecurity SetSecureBinding() 
{ 
BasicHttpSecurity binding = new BasicHttpSecurity(); 
binding.Mode = BasicHttpSecurityMode.Transport; 
return binding; 
} 

End Point Address

This is a simple method, which will create and return an endpoint object, with the address you have provided.

C#
public static EndpointAddress SetEndpoint(string url) 
{ 
return new EndpointAddress(url); 
} 

Using the New Methods

Now when you call the WCF, you reference the new method and you won't need the config entry in the primary project.

You will need the references within the sub project that contains the WCF, so you can update your service when needed.

**WCFReferance - This is the name of the reference you have created for the WCF Service reference
C#
private void queryStatus(int transactionId) 
{ 
 
using (WCFReferance.StatusClient wsf = 
       new WCFReferance.StatusClient(Utilities.WCFCallBuilder.SetBasicBinding(), 
Utilities.WCFCallBuilder.SetEndpoint(BusinessObjects.EHC.Queries.Extended.RecordText1 
(BusinessObjects.EHC.ENUMS.Extended.BackEndStatusURL)))) 
{ 
var dataClass = wsf.Latest(transactionId); 
 
}//END using (BackendStatusWCF.StatusClient wsf = new BackendStatusWCF.StatusClient()) 
} 

Source Config File

If you know your WCF always needs binding information which will be above the default value, you can alter the WCF config, by adding the below lines into the basicHttpBinding node under system.serviceModel.

XML
<binding maxReceivedMessageSize="2147483647" receiveTimeout="00:10:00" 
sendTimeout="00:10:00" maxBufferSize="2147483647" maxBufferPoolSize="2147483647">; 
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483646" 
maxArrayLength="2147483647" maxBytesPerRead="2147483647" 
maxNameTableCharCount="2147483647">; 
</binding>

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --