[英]Configuring a WCF service binding in code
我有一個用代碼創建的自托管Web服務:
protected void StartService(Type serviceType, Type implementedContract, string serviceDescription)
{
Uri addressTcp = new Uri(_baseAddressTcp + serviceDescription);
ServiceHost selfHost = new ServiceHost(serviceType, addressTcp);
Globals.Tracer.GeneralTrace.TraceEvent(TraceEventType.Information, 0, "Starting service " + addressTcp.ToString());
try
{
selfHost.AddServiceEndpoint(implementedContract, new NetTcpBinding(SecurityMode.None), "");
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
selfHost.Description.Behaviors.Add(smb);
System.ServiceModel.Channels.Binding binding = MetadataExchangeBindings.CreateMexTcpBinding();
selfHost.AddServiceEndpoint(typeof(IMetadataExchange), binding, "mex");
selfHost.Open();
ServiceInfo si = new ServiceInfo(serviceType, implementedContract, selfHost, serviceDescription);
try
{
lock (_hostedServices)
{
_hostedServices.Add(serviceType, si);
}
}
catch (ArgumentException)
{
//...
}
}
catch (CommunicationException ce)
{
//...
selfHost.Abort();
}
}
這工作正常,但是當我嘗試發送大塊數據時,我得到以下異常:
錯誤:格式化程序在嘗試反序列化消息時拋出異常:嘗試反序列化參數@@@時出錯。 InnerException消息是'反序列化@@@類型的對象時出錯。 讀取XML數據時已超出最大字符串內容長度配額(8192)。 通過更改創建XML閱讀器時使用的XmlDictionaryReaderQuotas對象的MaxStringContentLength屬性,可以增加此配額。 有關更多詳細信息,請參閱InnerException。 at:at System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation,ProxyRpc&rpc)
解決方案似乎是將MaxStringContentLength屬性添加到綁定。 我了解如何在Web.config( 鏈接 )中執行此操作:
... binding name =“wsHttpBindingSettings”maxReceivedMessageSize =“2147483647”>
我正在尋找一種在代碼中修改綁定的maxReceivedMessageSize的方法。 我使用的綁定類型甚至可能嗎?
謝謝。
編輯:在學習了一些之后(並且在我收到的回復的指導下)我理解了問題:我試圖修改服務的MEX部分,它只用於做廣告,見鏈接 。 我應該修改NetTcpBinding的綁定(try語句中的第一行)。 現在我的(工作)代碼如下所示:
...
try
{
//add the service itself
NetTcpBinding servciceBinding = new NetTcpBinding(SecurityMode.None);
servciceBinding.ReaderQuotas.MaxStringContentLength = 256 * 1024;
servciceBinding.ReaderQuotas.MaxArrayLength = 256 * 1024;
servciceBinding.ReaderQuotas.MaxBytesPerRead = 256 * 1024;
selfHost.AddServiceEndpoint(implementedContract, servciceBinding, "");
...
var binding = new NetTcpBinding(SecurityMode.None);
binding.MaxReceivedMessageSize = 2147483647;//this your maxReceivedMessageSize="2147483647"
binding.ReaderQuotas.MaxStringContentLength = 2147483647;//this property need set by exception
selfHost.AddServiceEndpoint(implementedContract, binding , "");
你需要查看綁定下的<ReaderQuotas>
子元素 - 這是MaxStringContentLength
設置所在的位置....
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="test">
<readerQuotas maxStringContentLength="65535" /> <== here's that property!
</binding>
</netTcpBinding>
</bindings>
</system.serviceModel>
在代碼中,您可以像這樣設置:
NetTcpBinding binding = new NetTcpBinding(SecurityMode.None);
binding.ReaderQuotas.MaxStringContentLength = 65535;
然后將此綁定用於服務端點...
selfHost.AddServiceEndpoint(implementedContract, binding, "");
我的解決方案是一個托管Silverlight客戶端的ASP.NET站點,其中服務客戶端引用位於Portable項目中。 服務使用用戶名驗證通過HTTPS運行。
我在WCF上發送圖片(byte [])時遇到了一些問題,但解決方法如下:
我的網站的web.config有一個綁定(在system.serviceModel下)定義如下:
<bindings>
<customBinding>
<binding name="WcfServiceBinding" receiveTimeout="00:10:00" sendTimeout="00:10:00" closeTimeout="00:10:00" openTimeout="00:10:00">
<security authenticationMode="UserNameOverTransport" />
<binaryMessageEncoding></binaryMessageEncoding>
<httpsTransport maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" keepAliveEnabled="true" />
</binding>
</customBinding>
</bindings>
在我的便攜式 lib中,我得到了一個WCF服務引用,並在代碼中定義了我的綁定:
public static CustomBinding ServiceBinding
{
get
{
if (binding != null)
{
return binding;
}
binding = new CustomBinding
{
CloseTimeout = new TimeSpan(0, 2, 0),
ReceiveTimeout = new TimeSpan(0, 3, 0),
SendTimeout = new TimeSpan(0, 5, 0)
};
var ssbe = SecurityBindingElement.CreateUserNameOverTransportBindingElement();
binding.Elements.Add(ssbe);
binding.Elements.Add(new BinaryMessageEncodingBindingElement());
binding.Elements.Add(
new HttpsTransportBindingElement { MaxReceivedMessageSize = 2147483647, MaxBufferSize = 2147483647 });
return binding;
}
}
要創建我的客戶端,我得到靜態綁定定義:
private static DataServiceClient CreateClient()
{
var proxy = new DataServiceClient(ServiceUtility.ServiceBinding, ServiceUtility.DataServiceAddress);
proxy.ClientCredentials.SetCredentials();
return proxy;
}
對我來說很棒。 祝好運。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.