【发布时间】:2011-05-05 20:11:56
【问题描述】:
我为我现在的雇主开发了一个客户端/服务器应用程序,其中一个要求是客户端部分作为 DLL 分发。由于 DLL 不会“执行”dllName.dll.config 文件,因此我需要将配置移动到代码中。我已经尽我所能做到了,但我现在遇到了这个例外;
X.509 证书 CN=ComputerName 链构建失败。
使用的证书具有无法 得到验证。更换证书或更改 certificateValidationMode。 发生内部证书链接错误。
我已经用谷歌搜索过,或者到处搜索解决这个问题,但到目前为止,我找不到任何对我见过的各种接近但不是真的解决方案的足够清晰的解释。
无论如何,My LocalStore 中有一个证书,但它实际上只由服务器使用(目前在同一个 XP Pro 盒子上测试客户端和服务器),至少据我所知。 (对 WCF 来说仍然很新)(请注意下面的客户端配置文件如何没有获得或以其他方式向客户端指出上述证书。
客户端的 app.config 如下所示,通过我们的测试 EXE 运行良好,其中包含此配置文件的部分。
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="MyTcpBinding_IFileXferService"
receiveTimeout="02:00:00"
sendTimeout="02:00:00"
transferMode="Streamed"
maxBufferSize="65536"
maxReceivedMessageSize="2147483647">
<readerQuotas maxStringContentLength="2147483647"
maxArrayLength="2147483647"
maxBytesPerRead="65536" />
<security mode="Transport">
<transport clientCredentialType="None" />
</security>
</binding>
</netTcpBinding>
</bindings>
<behaviors>
<endpointBehaviors>
<behavior name="ClientConfigBehavior">
<dataContractSerializer maxItemsInObjectGraph="6553600" />
<clientCredentials>
<serviceCertificate>
<authentication certificateValidationMode="None" />
</serviceCertificate>
</clientCredentials>
</behavior>
</endpointBehaviors>
</behaviors>
<client>
<endpoint name="ClientConfig"
binding="netTcpBinding"
bindingConfiguration="MyTcpBinding_IFileXferService"
behaviorConfiguration="ClientConfigBehavior"
contract="ServiceRefs.IFileXferService" />
</client>
</system.serviceModel>
</configuration>
但是现在我已经将配置信息移到了代码中(这样 DLL 可以在没有 .config 文件的情况下分发),我再次遇到了上面提到的异常。 (顺便说一句,如果我们发现给定的设置需要更改,我可以将其添加到我们自己的 xml 配置文件中。)
用于替换上述配置的代码如下所示:
...
netTcpBinding = new NetTcpBinding();
endpointAddr = new EndpointAddress(HostURI);
updateMaxItemsBehavior(); // method below this snippet
TimeSpan tsTwoHours = new TimeSpan(2,0,0);
netTcpBinding.ReceiveTimeout = tsTwoHours;
netTcpBinding.SendTimeout = tsTwoHours;
netTcpBinding.TransferMode = TransferMode.Streamed;
netTcpBinding.MaxBufferSize = 65536;
netTcpBinding.MaxReceivedMessageSize = 2147483647;
netTcpBinding.ReaderQuotas.MaxStringContentLength = 2147483647;
netTcpBinding.ReaderQuotas.MaxArrayLength = 2147483647;
netTcpBinding.ReaderQuotas.MaxBytesPerRead = 65536;
// double the 64k default
netTcpBinding.MaxBufferPoolSize = 131072;
netTcpBinding.Security.Mode = SecurityMode.Transport;
netTcpBinding.Security.Transport.ClientCredentialType =
TcpClientCredentialType.None;
// now to instantiate the service proxy client
svcClient = new FileXferServiceClient(netTcpBinding, endpointAddr);
我已经尝试过使用这些下一位的各种组合,但不能 绕过该死的异常。
// Set the certificate for the client.
X509Certificate2 cert = new X509Certificate2();
svcClient.ClientCredentials.ClientCertificate.Certificate = cert;
//svcClient.ClientCredentials.ClientCertificate.SetCertificate(
// StoreLocation.LocalMachine,
// StoreName.My,
// X509FindType.FindByThumbprint,
// "ThumbprintThumbprintThumbprintThumbprint");
// here's this
//
private void updateMaxItemsBehavior()
{
svcEndpoint = new ServiceEndpoint(
new ContractDescription("IFileXferService"));
foreach (OperationDescription operation in
svcEndpoint.Contract.Operations)
{
operation.Behaviors
.Find<DataContractSerializerOperationBehavior>()
.MaxItemsInObjectGraph = 6553600;
}
}
总之,我们的测试 Winforms 应用程序(以及我们的 WCF 应用程序的客户端)与 wfappname.exe.config 文件中的配置设置配合得很好,但是现在我已经尝试将这一切都移到代码中,它显然要求证书。我想我真正希望的是“如何在代码中复制上面的 app.config,以便我的 WCF 客户端不需要 app.config。”
谁能帮我解决这个问题? (我会成为你最好的朋友!); )
谢谢, 斯科特
【问题讨论】:
标签: c# wcf app-config client-side