2011-10-25 36 views
2

這是凌晨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高度讚賞!

回答

0

是的,這是一種痛苦。 Carlos在這方面的回答非常好,但減少的選項可能是「在WCF邊界上傳輸byte[]Stream,並手動處理序列化/反序列化」。在大多數情況下,這是對一個Serializer.*方法的單線調用,所以不要過於棘手。

+0

呵呵..是的,這是一種痛苦:)我試圖避免返回字節[],因爲我必須用他們的方法重寫大約20個服務...呃...我會先檢查卡洛斯郵件。 –

相關問題