WCF and return Custom collection
How any method of WCF application can return custom collection to calling environment. please hel开发者_如何学编程p with sample code.
thanks.
First of all, you need to define an interface as a ServiceContract and the method that returns the Custom Collection as an OperationContract. I will give you the code in VB.net, bus is very very easy to translate it to c#
Imports System.ServiceModel
<ServiceContract()>
Public Interface IClientContract
<OperationContract()>
Function GetClientList() As IList(Of POCOClients)
End Interface
Here, the IList(of POCOClients) is the custom collection. Then, implement the interface.
Public Class ClientContractImplementation
Implements IClientContract
Private ClientOp As IClientsOperations
Sub New()
'I use a IoC container here, but you can make a standar New() at this point'
ClientOp = BLIoC.Container.Resolve(Of IClientsOperations)()
End Sub
Public Function GetClientList() As System.Collections.Generic.IList(Of ServiceLayerContract.POCOClients) Implements ServiceLayerContract.IClientContract.GetClientList
Return ClientOp.SearchClients()
End Function
End Class
And, then you need to configure the app.config to expose the WCF service, in the Service.ServiceModel section:
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" name="MEX" contract="IMetadataExchange"/>
<host>
<baseAddresses>
<add baseAddress="http://localhost:81/Client"/>
</baseAddresses>
</host>
Now, when a client calls to the service, a custom collection of IList(Of POCOClients) (or whatever you want) will be returned.
Unless I am msitaken, it depends largely on whether you are using core generated proxies at the client, or using assembly sharing (using the same code at server and client). With proxies - simply: you can't - they are just shallow objects representing the public state. All you have in the mex/wsdl is "a set of items of type X", which the code-gen layer interprets (depending on your condiguration) as List<T>
, ObservableCollection<T>
, etc.
If you are using assembly sharing, you should already have the correct types at the client, so it should just work - but this is less pure in terms of abstraction. Best avoided on a public API, but fine for internal apps that share a platform.
精彩评论