WCF服务客户端

时间:2010-02-17 13:10:42

标签: c# .net wcf interface

美好的一天的人。我以前从未在这些类型的网站上发布,但是让我们看看它是怎么回事。

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

让我们说一下我的ServiceContract /接口定义了暴露给客户端的方法,有很多方法都与某个实体对象有关。如何在逻辑上将特定实体的所有相关方法组合在一起,以便在我的代码中看起来像

e.g。

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();

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

射击 涓

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

  

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
}

这有帮助吗?