首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WCF服务客户端

WCF服务客户端
EN

Stack Overflow用户
提问于 2010-02-17 21:10:42
回答 2查看 182关注 0票数 2

大家好。我以前从来没有在这些类型的网站上发表过帖子,但是让我们看看它是如何进行的。

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

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

例如:

代码语言:javascript
复制
WCFServiceClient.Entity1.Insert();
WCFServiceClient.Entity1.Delete();
WCFServiceClient.Entity1.GetAll();
WCFServiceClient.Entity1.GetById(int id);

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

..。

而不是

代码语言:javascript
复制
WCFServiceClient.Insert();
WCFServiceClient.Delete();
WCFServiceClient.GetAll();
WCFServiceClient.GetById(int id);
WCFServiceClient.AddSomething();
WCFServiceClient.RemoveSomething();
WCFServiceClient.SelectSomething();

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

拍摄胡安

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-02-18 00:54:14

WCFServiceClient.Entity1.Insert();

WCFServiceClient.Entity2.AddSomething();

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

代码语言:javascript
复制
[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);
}

如果您愿意,您可以让单个服务类实际实现这两个接口--这是完全可能的,也是有效的。

代码语言:javascript
复制
class ServiceImplementation : IEntity1Services, IEntity2Services
{
    // implementation of all seven methods here
}

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

代码语言:javascript
复制
class ServiceImplementation1 : IEntity1Services
{
    // implementation of four methods for Entity1 here
}

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

这真的有帮助吗?

票数 0
EN

Stack Overflow用户

发布于 2010-02-17 21:12:59

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2280835

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档