没有配置文件的WCF配置

没有配置文件的WCF配置

WCF Configuration without a config file

有谁知道一个很好的例子,说明如何在不使用配置文件的情况下以编程方式公开WCF服务? 我知道现在有了WCF,服务对象模型变得更加丰富,所以我知道这是可能的。 我只是没有看到如何做的例子。 相反,我想看看在没有配置文件的情况下如何进行消费。

在有人问之前,我非常需要在没有配置文件的情况下执行此操作。 我通常不建议这样做,但是正如我所说,在这种情况下有非常特殊的需要。


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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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" ) );
}

如果您有兴趣消除web.config中用于IIS托管的System.ServiceModel部分的用法,请在此处发布一个示例,以了解如何执行此操作(http://bejabbers2.blogspot.com/2010/02/wcf -zero-config-in-net-35-part-ii.html)。我将展示如何自定义ServiceHost来创建元数据和wshttpbinding端点。我以通用方式执行此操作,不需要其他编码。对于不立即升级到.NET 4.0的用户来说,这可能非常方便。


在这里,这是完整且有效的代码。我认为这对您有很大帮助。我一直在搜索,却找不到完整的代码,这就是为什么我尝试放置完整且有效的代码的原因。祝好运。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
public class ValidatorClass
{
    WSHttpBinding BindingConfig;
    EndpointIdentity DNSIdentity;
    Uri URI;
    ContractDescription ConfDescription;

    public ValidatorClass()
    {  
        // In constructor initializing configuration elements by code
        BindingConfig = ValidatorClass.ConfigBinding();
        DNSIdentity = ValidatorClass.ConfigEndPoint();
        URI = ValidatorClass.ConfigURI();
        ConfDescription = ValidatorClass.ConfigContractDescription();
    }


    public void MainOperation()
    {
         var Address = new EndpointAddress(URI, DNSIdentity);
         var Client = new EvalServiceClient(BindingConfig, Address);
         Client.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.PeerTrust;
         Client.Endpoint.Contract = ConfDescription;
         Client.ClientCredentials.UserName.UserName ="companyUserName";
         Client.ClientCredentials.UserName.Password ="companyPassword";
         Client.Open();

         string CatchData = Client.CallServiceMethod();

         Client.Close();
    }



    public static WSHttpBinding ConfigBinding()
    {
        // ----- Programmatic definition of the SomeService Binding -----
        var wsHttpBinding = new WSHttpBinding();

        wsHttpBinding.Name ="BindingName";
        wsHttpBinding.CloseTimeout = TimeSpan.FromMinutes(1);
        wsHttpBinding.OpenTimeout = TimeSpan.FromMinutes(1);
        wsHttpBinding.ReceiveTimeout = TimeSpan.FromMinutes(10);
        wsHttpBinding.SendTimeout = TimeSpan.FromMinutes(1);
        wsHttpBinding.BypassProxyOnLocal = false;
        wsHttpBinding.TransactionFlow = false;
        wsHttpBinding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
        wsHttpBinding.MaxBufferPoolSize = 524288;
        wsHttpBinding.MaxReceivedMessageSize = 65536;
        wsHttpBinding.MessageEncoding = WSMessageEncoding.Text;
        wsHttpBinding.TextEncoding = Encoding.UTF8;
        wsHttpBinding.UseDefaultWebProxy = true;
        wsHttpBinding.AllowCookies = false;

        wsHttpBinding.ReaderQuotas.MaxDepth = 32;
        wsHttpBinding.ReaderQuotas.MaxArrayLength = 16384;
        wsHttpBinding.ReaderQuotas.MaxStringContentLength = 8192;
        wsHttpBinding.ReaderQuotas.MaxBytesPerRead = 4096;
        wsHttpBinding.ReaderQuotas.MaxNameTableCharCount = 16384;

        wsHttpBinding.ReliableSession.Ordered = true;
        wsHttpBinding.ReliableSession.InactivityTimeout = TimeSpan.FromMinutes(10);
        wsHttpBinding.ReliableSession.Enabled = false;

        wsHttpBinding.Security.Mode = SecurityMode.Message;
        wsHttpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;
        wsHttpBinding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.None;
        wsHttpBinding.Security.Transport.Realm ="";

        wsHttpBinding.Security.Message.NegotiateServiceCredential = true;
        wsHttpBinding.Security.Message.ClientCredentialType = MessageCredentialType.UserName;
        wsHttpBinding.Security.Message.AlgorithmSuite = System.ServiceModel.Security.SecurityAlgorithmSuite.Basic256;
        // ----------- End Programmatic definition of the SomeServiceServiceBinding --------------

        return wsHttpBinding;

    }

    public static Uri ConfigURI()
    {
        // ----- Programmatic definition of the Service URI configuration -----
        Uri URI = new Uri("http://localhost:8732/Design_Time_Addresses/TestWcfServiceLibrary/EvalService/");

        return URI;
    }

    public static EndpointIdentity ConfigEndPoint()
    {
        // ----- Programmatic definition of the Service EndPointIdentitiy configuration -----
        EndpointIdentity DNSIdentity = EndpointIdentity.CreateDnsIdentity("tempCert");

        return DNSIdentity;
    }


    public static ContractDescription ConfigContractDescription()
    {
        // ----- Programmatic definition of the Service ContractDescription Binding -----
        ContractDescription Contract = ContractDescription.GetContract(typeof(IEvalService), typeof(EvalServiceClient));

        return Contract;
    }
}

在服务器端这并不容易。

对于客户端,可以使用ChannelFactory


所有WCF配置都可以通过编程完成。因此,无需配置文件即可创建服务器和客户端。

我推荐Juval Lowy撰写的" Programming WCF Services"一书,其中包含许多编程配置示例。


在客户端和服务器端都非常容易做到。 Juval Lowy的书有很好的例子。

关于您对配置文件的评论,我想说配置文件是在代码中仅次于可怜的人。当您控制将连接到服务器的每个客户端并确保它们已更新且用户无法找到它们并进行任何更改时,配置文件非常有用。我发现WCF配置文件模型是受限制的,设计起来有点困难,并且是维护的噩梦。总而言之,我认为MS将配置文件设置为默认的处理方式是一个非常糟糕的决定。

编辑:您无法使用配置文件执行的操作之一是使用非默认构造函数创建服务。这导致WCF中出现静态/全局变量和单例以及其他类型的废话。


我发现下面这个主题的链接上的博客帖子非常有趣。

我喜欢的一个想法是,可以仅将绑定或行为或地址XML部分从配置传递到适当的WCF对象,并让它处理属性的分配-当前您不能执行此操作。

像网络上的其他应用程序一样,我遇到了一些问题,需要我的WCF实现使用与托管应用程序(这是.NET 2.0 Windows服务)不同的配置文件。

http://salvoz.com/blog/2007/12/09/programmatically-setting-wcf-configuration/


推荐阅读

    linux命令重命名配置?

    linux命令重命名配置?,图片,名称,名字,文件,软件,代码,命令,文件名,脚本,批

    linux配置双ip命令?

    linux配置双ip命令?,地址,系统,代码,网络,设备,信息,中心,电脑,密码,命令,Lin

    linux服务器下载命令?

    linux服务器下载命令?,服务,密码,系统,档案,工具,网络,公共,百度,地址,认证,l

    linux服务端退出命令?

    linux服务端退出命令?,档案,命令,环境,异常,标准,网络,模式,终端,编辑,文件,l

    linux配置自定义命令?

    linux配置自定义命令?,服务,系统,状态,策略,周期,地方,标准,新增,环境,工具,L

    linux中启动服务命令?

    linux中启动服务命令?,服务,系统,命令,信息,工作,设备,网络,标准,名称,密码,l

    linux服务器常用命令?

    linux服务器常用命令?,工作,系统,地址,信息,命令,目录,管理,标准,设备,功能,

    linux筛选服务命令?

    linux筛选服务命令?,服务,系统,状态,软件,环境,主体,技术,号码,发行,名称,查

    linux双网卡配置命令?

    linux双网卡配置命令?,网络,状态,地址,信息,通信,标准,通讯,对外,机电,环境,L

    linux服务器保存命令?

    linux服务器保存命令?,时间,状态,档案,电脑,命令,信息,位置,编辑,文件,模式,L

    linux服务器扫盘命令?

    linux服务器扫盘命令?,地址,工作,命令,目录,数据,单位,名称,系统,管理,信息,L

    linux命令行图形编程?

    linux命令行图形编程?,系统,不了,情况,密码,工具,地方,百度,管理,图形界面,

    linux编程执行命令?

    linux编程执行命令?,电脑,系统,环境,命令,基础,发行,工具,代码,地址,名称,lin

    linux命令配置串口?

    linux命令配置串口?,设备,系统,平台,工具,名字,通信,电脑,地址,项目,信息,Lin

    linux命令切换服务器?

    linux命令切换服务器?,地址,名称,系统,环境,实时,命令,服务器,脚本,路径,版

    linux服务器搭建命令?

    linux服务器搭建命令?,系统,服务,软件,地址,平台,在线,密码,工具,环境,百度,l

    服务器重启命令linux?

    服务器重启命令linux?,工作,标准,设备,服务,系统,名称,命令,百度,网络,密码,

    linux终端命令行编程?

    linux终端命令行编程?,系统,工作,命令,终端,概念,时间,第一,代码,发行,地方,L

    linux服务端常用命令?

    linux服务端常用命令?,工作,地址,系统,网络,基础,命令,标准,工具,信息,管理,l

    linux的配置网络命令?

    linux的配置网络命令?,地址,系统,网络,代码,服务,管理,密码,信息,基础,命令,l