【发布时间】:2010-05-12 04:45:57
【问题描述】:
我需要将一些与 Silverlight 不兼容的对象转换为模型以通过 WCF 服务发送。我尝试使用 ServiceKnownType,但结果仍然是对象。检索结果后,我可以将对象转换为模型,但我不想每次都这样做。这是我尝试过的一个示例。
这是一个好方法吗?为什么 WCF 不将 BikeModel 作为对象返回。
界面
public interface IBike
{
string BikeName { get; set;}
}
型号
[DataContract]
public class BikeModel : IBike
{
[DataMember]
public string BikeName { get; set; }
}
由于基类中的 MarshalObjectByRef 等而无法在 Silverlight 中使用的其他对象
public class Bike : IBike, UnusableBaseClass
{
....
}
IService1
[ServiceContract]
[ServiceKnownType(typeof(BikeModel))]
public interface IService1
{
[OperationContract]
IBike GetBike();
}
服务
public class Service1 : IService1
{
public IBike GetBike()
{
Bike b = BikeManager.GetFirstBikeInTheDataBase();
return b;
}
}
MainPage.xaml.cs -
public MainPage()
{
InitializeComponent();
this.Loaded += (s, a) =>
{
ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();
client.GetBikeCompleted += (sender, e) =>
{
var d = e.Result;
// this cast works
BikeModel model = (BikeModel)d;
};
client.GetBikeAsync();
};
}
【问题讨论】:
标签: silverlight wcf