<p>Does anyone know of a good example of how to expose a WCF service programatically without the use of a configuration file? I know the service object model is much richer now with WCF, so I know it's possible. I just have not seen an example of how to do so. Conversely, I would like to see how consuming without a configuration file is done as well.</p>

<p>Before anyone asks, I have a very specific need to do this without configuration files. I would normally not recommend such a practice, but as I said, there is a very specific need in this case.</p>

[{'answer_id': 54583, 'author': 'Gulzar Nazim', 'author_id': 4337, 'author_profile': 'https://Stackoverflow.com/users/4337', 'pm_score': 3, 'selected': False, 'text': '<p>It is not easy on the server <a href="http://blogs.msdn.com/drnick/archive/2006/12/04/overriding-the-default-configuration-file.aspx" rel="nofollow noreferrer">side</a>..</p>\n\n<p>For client side, you can use <a href="http://msdn.microsoft.com/en-us/library/ms734681.aspx" rel="nofollow noreferrer">ChannelFactory</a></p>\n'}, {'answer_id': 56304, 'author': 'Paul Lalonde', 'author_id': 5782, 'author_profile': 'https://Stackoverflow.com/users/5782', 'pm_score': 2, 'selected': False, 'text': '<p>All WCF configuration can be done programatically. So it\'s possible to create both servers and clients without a config file. </p>\n\n<p>I recommend the book "Programming WCF Services" by Juval Lowy, which contains many examples of programmatic configuration.</p>\n'}, {'answer_id': 292810, 'author': 'devios1', 'author_id': 238948, 'author_profile': 'https://Stackoverflow.com/users/238948', 'pm_score': 8, 'selected': True, 'text': '<p>Consuming a web service without a config file is very simple, as I\'ve discovered. You simply need to create a binding object and address object and pass them either to the constructor of the client proxy or to a generic ChannelFactory instance. You can look at the default app.config to see what settings to use, then create a static helper method somewhere that instantiates your proxy:</p>\n\n<pre><code>internal static MyServiceSoapClient CreateWebServiceInstance() {\n    BasicHttpBinding binding = new BasicHttpBinding();\n    // I think most (or all) of these are defaults--I just copied them from app.config:\n    binding.SendTimeout = TimeSpan.FromMinutes( 1 );\n    binding.OpenTimeout = TimeSpan.FromMinutes( 1 );\n    binding.CloseTimeout = TimeSpan.FromMinutes( 1 );\n    binding.ReceiveTimeout = TimeSpan.FromMinutes( 10 );\n    binding.AllowCookies = false;\n    binding.BypassProxyOnLocal = false;\n    binding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;\n    binding.MessageEncoding = WSMessageEncoding.Text;\n    binding.TextEncoding = System.Text.Encoding.UTF8;\n    binding.TransferMode = TransferMode.Buffered;\n    binding.UseDefaultWebProxy = true;\n    return new MyServiceSoapClient( binding, new EndpointAddress( "http://www.mysite.com/MyService.asmx" ) );\n}\n</code></pre>\n'}, {'answer_id': 774811, 'author': 'Tone', 'author_id': 58523, 'author_profile': 'https://Stackoverflow.com/users/58523', 'pm_score': 2, 'selected': False, 'text': '<p>I found the blog post at the link below around this topic very interesting.  </p>\n\n<p>One idea I like is that of being able to just pass in a binding or behavior or address XML section from the configuration to the appropriate WCF object and let it handle the assigning of the properties - currently you cannot do this.  </p>\n\n<p>Like others on the web I am having issues around needing my WCF implementation to use a different configuration file than that of my hosting application (which is a .NET 2.0 Windows service).</p>\n\n<p><a href="http://salvoz.com/blog/2007/12/09/programmatically-setting-wcf-configuration/" rel="nofollow noreferrer">http://salvoz.com/blog/2007/12/09/programmatically-setting-wcf-configuration/</a></p>\n'}, {'answer_id': 774918, 'author': 'Steve', 'author_id': 48552, 'author_profile': 'https://Stackoverflow.com/users/48552', 'pm_score': 2, 'selected': False, 'text': "<p>It's very easy to do on both the client and the server side. Juval Lowy's book has excellent examples. </p>\n\n<p>As to your comment about the configuration files, I would say that the configuration files are a poor man's second to doing it in code. Configuration files are great when you control every client that will connect to your server and make sure they're updated, and that users can't find them and change anything. I find the WCF configuration file model to be limiting, mildly difficult to design, and a maintenance nightmare. All in all, I think it was a very poor decision by MS to make the configuration files the default way of doing things.</p>\n\n<p>EDIT: One of the things you can't do with the configuration file is to create services with non-default constructors. This leads to static/global variables and singletons and other types of non-sense in WCF.</p>\n"}, {'answer_id': 2345064, 'author': 'John Wigger', 'author_id': 280648, 'author_profile': 'https://Stackoverflow.com/users/280648', 'pm_score': 4, 'selected': False, 'text': '<p>If you are interested in eliminating the usage of the System.ServiceModel section in the web.config for IIS hosting, I have posted an example of how to do that here (<a href="http://bejabbers2.blogspot.com/2010/02/wcf-zero-config-in-net-35-part-ii.html" rel="noreferrer">http://bejabbers2.blogspot.com/2010/02/wcf-zero-config-in-net-35-part-ii.html</a>).  I show how to customize a ServiceHost to create both metadata and wshttpbinding endpoints. I do it in a general purpose way that doesn\'t require additional coding. For those who aren\'t immediately upgrading to .NET 4.0 this can be pretty convenient.</p>\n'}, {'answer_id': 8925111, 'author': 'S. M. Khaled Reza', 'author_id': 1158316, 'author_profile': 'https://Stackoverflow.com/users/1158316', 'pm_score': 4, 'selected': False, 'text': '<p>Here, this is complete and working code. I think it will help you a lot. I was searching and never finds a complete code that\'s why I tried to put complete and working code. Good luck.</p>\n\n<pre><code>public class ValidatorClass\n{\n    WSHttpBinding BindingConfig;\n    EndpointIdentity DNSIdentity;\n    Uri URI;\n    ContractDescription ConfDescription;\n\n    public ValidatorClass()\n    {  \n        // In constructor initializing configuration elements by code\n        BindingConfig = ValidatorClass.ConfigBinding();\n        DNSIdentity = ValidatorClass.ConfigEndPoint();\n        URI = ValidatorClass.ConfigURI();\n        ConfDescription = ValidatorClass.ConfigContractDescription();\n    }\n\n\n    public void MainOperation()\n    {\n         var Address = new EndpointAddress(URI, DNSIdentity);\n         var Client = new EvalServiceClient(BindingConfig, Address);\n         Client.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.PeerTrust;\n         Client.Endpoint.Contract = ConfDescription;\n         Client.ClientCredentials.UserName.UserName = "companyUserName";\n         Client.ClientCredentials.UserName.Password = "companyPassword";\n         Client.Open();\n\n         string CatchData = Client.CallServiceMethod();\n\n         Client.Close();\n    }\n\n\n\n    public static WSHttpBinding ConfigBinding()\n    {\n        // ----- Programmatic definition of the SomeService Binding -----\n        var wsHttpBinding = new WSHttpBinding();\n\n        wsHttpBinding.Name = "BindingName";\n        wsHttpBinding.CloseTimeout = TimeSpan.FromMinutes(1);\n        wsHttpBinding.OpenTimeout = TimeSpan.FromMinutes(1);\n        wsHttpBinding.ReceiveTimeout = TimeSpan.FromMinutes(10);\n        wsHttpBinding.SendTimeout = TimeSpan.FromMinutes(1);\n        wsHttpBinding.BypassProxyOnLocal = false;\n        wsHttpBinding.TransactionFlow = false;\n        wsHttpBinding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;\n        wsHttpBinding.MaxBufferPoolSize = 524288;\n        wsHttpBinding.MaxReceivedMessageSize = 65536;\n        wsHttpBinding.MessageEncoding = WSMessageEncoding.Text;\n        wsHttpBinding.TextEncoding = Encoding.UTF8;\n        wsHttpBinding.UseDefaultWebProxy = true;\n        wsHttpBinding.AllowCookies = false;\n\n        wsHttpBinding.ReaderQuotas.MaxDepth = 32;\n        wsHttpBinding.ReaderQuotas.MaxArrayLength = 16384;\n        wsHttpBinding.ReaderQuotas.MaxStringContentLength = 8192;\n        wsHttpBinding.ReaderQuotas.MaxBytesPerRead = 4096;\n        wsHttpBinding.ReaderQuotas.MaxNameTableCharCount = 16384;\n\n        wsHttpBinding.ReliableSession.Ordered = true;\n        wsHttpBinding.ReliableSession.InactivityTimeout = TimeSpan.FromMinutes(10);\n        wsHttpBinding.ReliableSession.Enabled = false;\n\n        wsHttpBinding.Security.Mode = SecurityMode.Message;\n        wsHttpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;\n        wsHttpBinding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.None;\n        wsHttpBinding.Security.Transport.Realm = "";\n\n        wsHttpBinding.Security.Message.NegotiateServiceCredential = true;\n        wsHttpBinding.Security.Message.ClientCredentialType = MessageCredentialType.UserName;\n        wsHttpBinding.Security.Message.AlgorithmSuite = System.ServiceModel.Security.SecurityAlgorithmSuite.Basic256;\n        // ----------- End Programmatic definition of the SomeServiceServiceBinding --------------\n\n        return wsHttpBinding;\n\n    }\n\n    public static Uri ConfigURI()\n    {\n        // ----- Programmatic definition of the Service URI configuration -----\n        Uri URI = new Uri("http://localhost:8732/Design_Time_Addresses/TestWcfServiceLibrary/EvalService/");\n\n        return URI;\n    }\n\n    public static EndpointIdentity ConfigEndPoint()\n    {\n        // ----- Programmatic definition of the Service EndPointIdentitiy configuration -----\n        EndpointIdentity DNSIdentity = EndpointIdentity.CreateDnsIdentity("tempCert");\n\n        return DNSIdentity;\n    }\n\n\n    public static ContractDescription ConfigContractDescription()\n    {\n        // ----- Programmatic definition of the Service ContractDescription Binding -----\n        ContractDescription Contract = ContractDescription.GetContract(typeof(IEvalService), typeof(EvalServiceClient));\n\n        return Contract;\n    }\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/54579', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/5469/']