這是凌晨2點,我一直想弄清楚整個晚上:我如何在使用ProtoBuf-net串行器的Silverlight中使用WCF服務?如何在使用ProtoBuf-net序列化程序的Silverlight中使用WCF服務?
只是爲了澄清一件事:從我的測試控制檯應用程序調用它們時,服務很好用。
頂級服務是通過使用BasicHttpEndpoint(爲了可壓縮性)公開的,但是如果解決了我的問題,我對其他綁定非常開放。
不管怎樣,在.NET(不是Silverlight的),我可以消費服務內容如下:
我有一個通用的方法,GetService的,這對我創建服務:
... Code removed ...
EndpointAddress endpointAddress = new EndpointAddress(address);
BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.TransportWithMessageCredential)
{
MaxReceivedMessageSize = int.MaxValue,
MaxBufferSize = int.MaxValue,
};
binding.ReaderQuotas.MaxArrayLength = int.MaxValue;
ContractDescription contract = ContractDescription.GetContract(typeof(TServiceType));
ServiceEndpoint serviceEndpoint = new ServiceEndpoint(contract, binding, new EndpointAddress(address));
factory = new ChannelFactory<TServiceType>(serviceEndpoint);
AddBehaviors(serviceEndpoint);
ClientCredentials defaultCredentials = factory.Endpoint.Behaviors.Find<ClientCredentials>();
factory.Endpoint.Behaviors.Remove(defaultCredentials);
factory.Endpoint.Behaviors.Add(credentials);
... Code removed ...
然後addBehavior給提取方法:
private static void AddBehaviors(ServiceEndpoint serviceEndpoint, int maxItemsInObjectGraph = int.MaxValue)
{
foreach (OperationDescription operation in serviceEndpoint.Contract.Operations)
{
DataContractSerializerOperationBehavior operationBehavior = operation.Behaviors.Find<DataContractSerializerOperationBehavior>();
if (operationBehavior != null)
{
operation.Behaviors.Remove(operationBehavior);
}
ProtoOperationBehavior protoOperationBehavior = operation.Behaviors.Find<ProtoOperationBehavior>();
if (protoOperationBehavior == null)
{
protoOperationBehavior = new ProtoOperationBehavior(operation);
operation.Behaviors.Add(protoOperationBehavior);
}
protoOperationBehavior.MaxItemsInObjectGraph = int.MaxValue;
}
}
不幸的是這是不可能做到這一點在Silverlight :(所以,我需要一些其他的方法來達到同樣的事情,而現在我被困任何sugg。 estions高度讚賞!
太棒了!我會立即檢查它! –