gpt4 book ai didi

.net - 以编程方式配置 wcf 服务

转载 作者:行者123 更新时间:2023-12-02 06:06:34 24 4
gpt4 key购买 nike

我有一个远程 wcf 服务,我通过 WSHttpBinding 连接它。如果我使用空的服务构造函数,这意味着它将从 app.config 中获取所有配置,一切正常,(我的意思是 MyService s = new MyService())。现在我想以编程方式配置 wcf。在我到达身份验证问题之前很简单,但很难做到这一点。这是我使用的 app.config,你可以在那里看到我的安全配置。

<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="SecuredEndPoint" closeTimeout="00:01:00" openTimeout="00:01:00"
receiveTimeout="00:10:00" sendTimeout="00:01:00" bypassProxyOnLocal="false"
transactionFlow="false" hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
allowCookies="false">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<reliableSession ordered="true" inactivityTimeout="00:10:00"
enabled="true" />
<security mode="Message">
<transport clientCredentialType="Windows" proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName" negotiateServiceCredential="true"
algorithmSuite="Default" />
</security>
</binding>
</wsHttpBinding>
</bindings>
<client>
<endpoint address="http://MyWcfService.svc"
binding="wsHttpBinding" bindingConfiguration="SecuredEndPoint"
contract="ServiceReference1.IMyService" name="SecuredEndPoint">
<identity>
<certificate encodedValue="*******************************************************************" />
</identity>
</endpoint>
</client>
</system.serviceModel>

最佳答案

我已经这样做了,您可能需要修改配置中的安全模式代码

public virtual ChannelFactory<T> Proxy<T>(string address) {
//Validate Address
if (string.IsNullOrEmpty(address)) throw new ArgumentNullException("Address can not be null or empty.");
//Address
EndpointAddress endpointAddress = new EndpointAddress(address);

//Binding
WSHttpBinding wsHttpBinding = new WSHttpBinding(SecurityMode.None, false);
wsHttpBinding.OpenTimeout = wsHttpBinding.CloseTimeout = new TimeSpan(0, 1, 0);
wsHttpBinding.ReceiveTimeout = wsHttpBinding.SendTimeout = new TimeSpan(0, 10, 0);
wsHttpBinding.MaxReceivedMessageSize = wsHttpBinding.MaxBufferPoolSize = 2147483647;
wsHttpBinding.BypassProxyOnLocal = wsHttpBinding.AllowCookies = wsHttpBinding.TransactionFlow = false;
wsHttpBinding.MessageEncoding = WSMessageEncoding.Text;
wsHttpBinding.TextEncoding = Encoding.UTF8;
wsHttpBinding.UseDefaultWebProxy = true;
wsHttpBinding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
wsHttpBinding.ReaderQuotas = new XmlDictionaryReaderQuotas(); //ReaderQuotas, setting to Max
wsHttpBinding.ReaderQuotas.MaxArrayLength = wsHttpBinding.ReaderQuotas.MaxBytesPerRead = 2147483647;
wsHttpBinding.ReaderQuotas.MaxStringContentLength = wsHttpBinding.ReaderQuotas.MaxNameTableCharCount = 2147483647;
wsHttpBinding.ReaderQuotas.MaxDepth = 2147483647;

//Create the Proxy
ChannelFactory<T> proxy = new ChannelFactory<T>(wsHttpBinding, endpointAddress);

//Sets the MaxItemsInObjectGraph, so that client can receive large objects
foreach (var operation in proxy.Endpoint.Contract.Operations) {
DataContractSerializerOperationBehavior operationBehavior = operation.Behaviors.Find<DataContractSerializerOperationBehavior>();
//If DataContractSerializerOperationBehavior is not present in the Behavior, then add
if (operationBehavior == null) {
operationBehavior = new DataContractSerializerOperationBehavior(operation);
operation.Behaviors.Add(operationBehavior);
}
//IMPORTANT: As 'operationBehavior' is a reference, changing anything here will automatically update the value in list, so no need to add this behavior to behaviorlist
operationBehavior.MaxItemsInObjectGraph = 2147483647;
}
return proxy;
}

在这个 proxy 对象上,您需要执行 .CreateChannel() 才能使用它。

希望这对您有所帮助。

关于.net - 以编程方式配置 wcf 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8820105/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com