【问题标题】:WCF. Create generic Servicecontract. Fill from clientWCF。创建通用服务合同。客户填写
【发布时间】:2015-06-15 11:16:41
【问题描述】:

也许它是重复的,但我无法成功找到所需的答案。

实际上,我正在尝试构建通用包装器,以便通过 wcf 使用 Entity/L2Sql。所以在服务器端:

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
public class MyDbContext<TTable> : IMyDbContext<TTable> where TTable:class 
{  
    private readonly DataContext _dbContext;
    private readonly Table<Device> _table;

    public MyDbContext()
    {
        _dbContext = new WCF_DataContext(connection);
        _table = _dbContext.GetTable<TTable>();
    }

    public void InsertOnSubmit(TTable table)
    {
        _table.InsertOnSubmit(table);
    }
    ...and the same stuff further...
}

在客户端我是这样使用的

var client = new ServiceRef.MyDbContextClient<ServiceRef.PersonTable>();
client.GetData(); client.insertOnSubmit(); ...other stuff...

我了解 WCF 为通用的东西、不同的语言创建了什么,并且自然禁止直接 T,但也许有一种方法可以通过一些属性或描述来解决这个问题(例如 where T:DataContractAttribute,但这是无效的)。

或者是我开始想错了=)

对于某些视图模型,我需要每个视图模型一个 DbContext,而不是使用短的 (..dbcontext..)

【问题讨论】:

    标签: c# .net wcf generics servicecontract


    【解决方案1】:

    也许您可以在合同中使用已知类型属性。

    您可以在这里找到更多信息:

    https://msdn.microsoft.com/en-us/library/ms730167(v=vs.110).aspx

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 2010-11-29
      • 2012-09-29
      • 2014-02-20
      • 2014-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多