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

查看:41
本文介绍了没有配置文件的 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天全站免登陆