简体   繁体   English

如何不重复地分割WCF合同?

[英]How to split WCF contract without duplication?

I want to refactor service into multiple sub-services, separated by their business scope: 我想将服务重构为多个子服务,并按其业务范围分开:

[ServiceContract]
public interface IMyService
{
    [OperationContract]
    int Method1();
    [OperationContract]
    int Method2();
}

And some users already using it , so I can't just bash in and say "goodbye" to them, refactoring everything on my way. 而且有些用户已经在使用它 ,所以我不能随便跟他们说声“再见”,而是按照自己的方式重构一切。

So, to avoid duplication, I use abstractions and interfaces beforehand and in this case I tried to separate contract to multiple interfaces and leaving main one as aggregator: 因此,为避免重复,我事先使用了抽象和接口,在这种情况下,我尝试将协定分离为多个接口,并将主要接口保留为聚合器:

[ServiceContract]
public interface IMyService : IMySubService1, IMySubService2
{
}

[ServiceContract]
public interface IMySubService1
{
    [OperationContract]
    int Method1();
}

[ServiceContract]
public interface IMySubService2
{
    [OperationContract]
    int Method2();
}

I thought this will do the thing, but NO - it's breaking those clients, because now those methods located on different paths in WSDL, even though Im hosting only IMyService : 我以为这样做会成功,但是不,这会破坏那些客户端,因为现在这些方法位于WSDL中的不同路径上,即使Im 仅托管IMyService

It was: net.tcp://foobar/IMyService/Method1 它是: net.tcp://foobar/IMyService/Method1

It became: net.tcp://foobar/IMySubService1/Method1 它变为: net.tcp://foobar/IMySubService1/Method1

And that's a problem. 那是个问题。 I can't separate my contract into interfaces without duplication of code (one for implementation, and one explicitly aggregated for contract), any way I can solve it? 我无法在没有代码重复的情况下将我的合同分为多个接口(一个用于实现,一个显式地聚合用于合同),有什么办法可以解决?

Managed to do it this way: 设法做到这一点:

[ServiceContract(Name = nameof(IMyService))]
public interface IMyService : IMySubService1, IMySubService2
{
}

[ServiceContract(Name = nameof(IMyService))]
public interface IMySubService1
{
    [OperationContract]
    int Method1();
}

[ServiceContract(Name = nameof(IMyService))]
public interface IMySubService2
{
    [OperationContract]
    int Method2();
}

Now WSDL generated correctly. 现在,WSDL正确生成了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM