2010-02-17 176 views
2

美好的一天人们。我从来没有在这些类型的网站上发布过,但是让我们看看它是如何发展的。WCF服务客户端

今天我第一次开始使用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(); 

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

射击 胡安

+0

欢迎!发布代码时,单击工具栏上的格式代码按钮('101')以正确格式化。 – SLaks 2010-02-17 13:15:19

回答

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 
} 

这有帮助吗?

+0

是的,谢谢很多人。我昨天做的是创建一个部分类,每个类的实现实现不同的服务合同,并为同一服务下的每个合同创建一个端点。 – Juan 2010-02-18 07:00:40

2

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

+0

这将是我的下一个计划,但是这意味着在我的客户端,我将不得不在web.config中添加另一个参考Service2的权利? – Juan 2010-02-17 13:17:28

+0

@Juan:没有。这是一项服务,有两份合同。 – 2010-02-17 13:21:02

+0

好吧,我有点失落了。在WCF服务配置编辑器中,我将如何为多个合同配置此服务? – Juan 2010-02-17 13:53:06