views:

7037

answers:

8

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.

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.

+5  A: 

It is not easy on the server side..

For client side, you can use ChannelFactory

Gulzar
+6  A: 

I'm pretty sure Michele Bustamante's book has examples with and without config files:

http://www.amazon.com/Learning-WCF-Hands-Michele-Bustamante/dp/0596101627

Also, Juval Lowy's book is very good about customizing services:

http://www.amazon.com/Programming-WCF-Services-Juval-Lowy/dp/0596526997

Both are very good.

Richard Morgan
+3  A: 

All WCF configuration can be done programatically. So it's possible to create both servers and clients without a config file.

I recommend the book "Programming WCF Services" by Juval Lowy, which contains many examples of programmatic configuration.

Paul Lalonde
A: 

I've just started looking at WCF and came across this http://msdn.microsoft.com/en-us/library/ms730935.aspx

Jeremy Holt
+14  A: 

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:

internal static MyServiceSoapClient CreateWebServiceInstance() {
    BasicHttpBinding binding = new BasicHttpBinding();
    // I think most (or all) of these are defaults--I just copied them from app.config:
    binding.SendTimeout = TimeSpan.FromMinutes( 1 );
    binding.OpenTimeout = TimeSpan.FromMinutes( 1 );
    binding.CloseTimeout = TimeSpan.FromMinutes( 1 );
    binding.ReceiveTimeout = TimeSpan.FromMinutes( 10 );
    binding.AllowCookies = false;
    binding.BypassProxyOnLocal = false;
    binding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
    binding.MessageEncoding = WSMessageEncoding.Text;
    binding.TextEncoding = System.Text.Encoding.UTF8;
    binding.TransferMode = TransferMode.Buffered;
    binding.UseDefaultWebProxy = true;
    return new MyServiceSoapClient( binding, new EndpointAddress( "http://www.mysite.com/MyService.asmx" ) );
}
chaiguy
I personally like this approach for examples when you are going to be using the file in a different matter, for example, if you've encrypted your app.config (or equivalent config file) and don't need to use the built in WCF capabilities of it reading in a connection
Noah
For https usage, add binding.Security.Mode = BasicHttpSecurityMode.Transport;
ciscoheat
+2  A: 

I found the blog post at the link below around this topic very interesting.

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.

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).

http://blog.salvoz.com/2007/12/09/ProgrammaticallySettingWCFConfiguration.aspx

Tone
+2  A: 

It's very easy to do on both the client and the server side. Juval Lowy's book has excellent examples.

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.

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.

Steve
+4  A: 

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 (http://bejabbers2.blogspot.com/2010/02/wcf-zero-config-in-net-35-part-ii.html). 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.

John Wigger
John, I'm sure that's a great blog post, but since there's an accepted answer from 17 months ago, is there really any purpose to your answer?
John Saunders
Since this is my first Stack Overflow answer that may not be the way things are usually done. Being familiar with the Lowy and Bustamante books, which are great references, I think my answer goes well beyond the samples they offer. I primarily use Stack Overflow when googling so I read posts that are older frequently. Having more up to date answers only helps from my perspective. I googled this post before writing my code to avoid re-inventing the wheel.
John Wigger
John Saunders
@John: you may want to see http://meta.stackoverflow.com/questions/40696/what-to-do-about-late-answers-if-anything, where I've used your post as an example.
John Saunders