开发者

Handling Collection<string> on the client with a shared WCF interface

We have been developing a hub/spoke synchronization model using Microsoft Sync Framework开发者_C百科 and WCF, and since we are developing both the client and the server, we would like to put the WCF service contract interface into a shared assembly so that we can define it just once and share it between the client and server. For the most part this works, but the GetSchema method of Sync Framework passes a Collection object of table names which gets serialized and read on the client as string[]. However, since the client proxy has been written to use the server interface it is expecting to receive a Collection object and I am getting a type mismatch.

I would just change the contract to explicitly pass only string[], and manually cast it when calling the sync provider methods, but this leads to an "ambiguous match found" error.

How can I use the same interface on both the client and server and handle the Collection -> string[] serialization correctly?


I solved this by using the ToArray() LINQ extension call on the Collection<string> that is passed in to the proxy:

public override SyncSchema GetSchema(System.Collections.ObjectModel.Collection<string> tableNames, SyncSession syncSession)
{
    return this.ServiceProxy.GetSchema(tableNames.ToArray(), syncSession);
}

The contract on the server specified an IEnumerable<string> instead of a Collection<string>.

public SyncSchema GetSchema(IEnumerable<string> tableNames, SyncSession syncSession)
{
    // Convert IEnumerable<string> to Collection<string>
    Collection<string> tables = new Collection<string>(tableNames.ToList());
    return this.syncProvider.GetSchema(tables, syncSession);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜