WCF没有配置文件的配置 [英] WCF Configuration without a config file

查看:276
本文介绍了WCF没有配置文件的配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人知道如何在不使用配置文件的情况下以编程方式公开WCF服务的一个很好的例子?我知道服务对象模型现在与WCF更丰富,所以我知道这是可能的。我只是没有看到一个如何这样做的例子。相反,我想看看如何消耗没有配置文件也是如此。

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.

推荐答案

没有配置文件的Web服务非常简单,我已经发现。您只需要创建一个绑定对象和地址对象,并将它们传递给客户端代理的构造函数或通用ChannelFactory实例。您可以查看默认的app.config来查看要使用的设置,然后在实例化您的代理的地方创建一个静态助手方法:

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" ) );
}

这篇关于WCF没有配置文件的配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆