【问题标题】:WCF Service ClientsWCF 服务客户端
【发布时间】:2010-02-17 13:10:41
【问题描述】:

大家好。我以前从未在这类网站上发过帖子,但让我们看看它是怎么回事。

今天我第一次开始使用 WCF,我观看了一些关于它的截屏视频,现在我准备好进入我的第一个实现它的解决方案。一切都很好,到目前为止一切正常,尽管当我在调用程序/客户端中创建 WCFServiceClient 时出现了我的问题。

假设在定义我向客户端公开的方法的 ServiceContract/ 接口中,有许多方法,每个方法都与某个实体对象相关。如何在逻辑上将特定实体的所有相关方法组合在一起,以便在我的代码中看起来像

例如

WCFServiceClient.Entity1.Insert();
WCFServiceClient.Entity1.Delete();
WCFServiceClient.Entity1.GetAll();
WCFServiceClient.Entity1.GetById(int id);

WCFServiceClient.Entity2.AddSomething();
WCFServiceClient.Entity2.RemoveSomething();
WCFServiceClient.Entity2.SelectSomething();

...

代替

WCFServiceClient.Insert();
WCFServiceClient.Delete();
WCFServiceClient.GetAll();
WCFServiceClient.GetById(int id);
WCFServiceClient.AddSomething();
WCFServiceClient.RemoveSomething();
WCFServiceClient.SelectSomething();

我希望这是有道理的。我搜索了谷歌,我尝试了自己的逻辑推理,但没有运气。任何想法将不胜感激。

射击 娟

【问题讨论】:

  • 欢迎您!发布代码时,单击工具栏上的格式化代码按钮 (101) 以正确格式化。

标签: c# .net wcf interface


【解决方案1】:

你真的不能那样做。最好的办法是将所有“实体 1”方法放在一个服务合同中,将所有“实体 2”方法放在另一个服务合同中。一个服务可以实现多个服务契约。

【讨论】:

  • 那将是我的下一个计划,但这意味着在我的客户端中,我必须在 web.config 中为 Service2 添加另一个引用,对吧?
  • @Juan:没有。这是一项服务,有两个合同。
  • 好吧,我有点迷路了。在 WCF 服务配置编辑器中,我将如何为多个合同配置此服务?
【解决方案2】:

WCFServiceClient.Entity1.Insert();

WCFServiceClient.Entity2.AddSomething();

这闻起来像两个独立的服务接口——让每个服务契约(接口)处理你需要的单一实体类型的所有方法:

[ServiceContract]
interface IEntity1Services
{ 
    [OperationContract]
    void Insert(Entity1 newEntity);
    [OperationContract]
    void Delete(Entity1 entityToDelete);
    [OperationContract]
    List<Entity1> GetAll();
    [OperationContract]
    Entity1 GetById(int id);
}

[ServiceContract]
interface IEntity2Services
{ 
    [OperationContract]
    void AddSomething(Entity2 entity);
    [OperationContract]
    void RemoveSomething(Entity2 entity);
    [OperationContract]
    SelectSomething(Entity2 entity);
}

如果您愿意,您可以使用一个服务类来实际实现这两个接口 - 这是完全可行且有效的。

class ServiceImplementation : IEntity1Services, IEntity2Services
{
    // implementation of all seven methods here
}

或者您可以创建两个单独的服务实现类 - 这完全取决于您。

class ServiceImplementation1 : IEntity1Services
{
    // implementation of four methods for Entity1 here
}

class ServiceImplementation2 : IEntity2Services
{
    // implementation of three methods for Entity2 here
}

这有帮助吗?

【讨论】:

  • 是的,非常感谢。我昨天所做的是创建一个部分类,该类的每个实现都实现一个不同的服务合同,然后为同一服务下的每个合同创建一个端点。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-06
  • 1970-01-01
  • 1970-01-01
  • 2011-07-01
  • 2014-02-20
  • 1970-01-01
相关资源
最近更新 更多