C# WCF服务客户端

C# WCF服务客户端,c#,.net,wcf,interface,C#,.net,Wcf,Interface,大家好。我以前从未在这些类型的网站上发表过文章,不过让我们看看它是如何发展的 今天我第一次开始使用WCF,我看了一些关于它的屏幕广播,现在我准备开始我的第一个实现它的解决方案。尽管我的问题是在调用程序/客户机中创建WCFServiceClient时出现的,但一切都很好,到目前为止一切正常 假设在定义向客户机公开的方法的ServiceContract/接口中,有许多方法,每个方法都与某个实体对象相关。如何在逻辑上将特定实体的所有相关方法组合在一起,以便在代码中 e、 g 而不是 WCFServi

大家好。我以前从未在这些类型的网站上发表过文章,不过让我们看看它是如何发展的

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

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

e、 g

而不是

WCFServiceClient.Insert();
WCFServiceClient.Delete();
WCFServiceClient.GetAll();
WCFServiceClient.GetById(int id);
WCFServiceClient.AddSomething();
WCFServiceClient.RemoveSomething();
WCFServiceClient.SelectSomething();
我希望这是有道理的。我搜索过谷歌,我尝试过自己的逻辑推理,但没有运气。任何想法都将不胜感激

射门
胡安

你真的不能那样做。您所能做的最好的事情就是将所有“实体1”方法放在一个服务契约中,将所有“实体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 ServiceImplementation1 : IEntity1Services
{
    // implementation of four methods for Entity1 here
}

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

这有帮助吗?

欢迎!发布代码时,请单击工具栏上的“设置代码格式”按钮(
101
)以正确设置代码格式。这将是我的下一个计划,但这意味着在我的客户端中,我必须在web.config中为Service2添加另一个引用,对吗?@Juan:不。这是一个服务,有两个合同。好吧,我有点迷路了。在WCF服务配置编辑器中,我将如何为多个合同配置此服务?是的,非常感谢。我昨天做的是创建一个分部类,该类的每个实现实现一个不同的servicecontract,然后为同一服务下的每个契约创建一个端点。
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
}