是否可以访问WCF服务操作的子集

时间:2011-06-01 15:42:34

标签: c# .net service wcf

我有以下疑问: 请考虑以下事项:

/* My service is formed by several subservices 
   (subfunctionalities). Here is functionality 1 */
[ServiceContract]
public interface IMySubService1 {
   [OperationContract]
   int MyOp11(string opnd);
   [OperationContract]
   int MyOp12(stirng opnd);
}

/* My service is formed by several subservices 
   (subfunctionalities). Here is functionality 2 */
[ServiceContract]
public interface IMySubService2 {
   [OperationContract]
   int MyOp21(string opnd);
   [OperationContract]
   int MyOp22(stirng opnd);
}

/* My service is formed by several subservices 
   (subfunctionalities). Here is functionality 3 */
[ServiceContract]
public interface IMySubService3 {
   [OperationContract]
   int MyOp31(string opnd);
   [OperationContract]
   int MyOp32(stirng opnd);
}

以下内容:

/* My server will implement a complex great 
   service made of the previously introduced subservices. */
[ServiceContract]
public interface IMyService : IMySubService1, IMySubService2, IMySubService3 {
}

好吧,我将实施我的服务:

// Implementing the service
public class MyService : IMyService {
   ...
}

OK!到现在为止,没什么奇怪的! 我的服务将托管在服务器上,我很高兴:) 该服务在svc文件上托管(例如),但请记住该服务为IMyService

现在让我们谈谈: 在我的客户端,我想创建一个客户端,以便访问我的服务的子集。鉴于我的服务是三个子服务的使用,我想只访问一个子服务。

例如,我的客户对IMySubService1感兴趣 我可以这样做吗?

ServiceEndpoint httpEndpoint = new ServiceEndpoint(
   ContractDescription.GetContract(typeof(IMySubService1)), 
   new BasicHttpBinding(), 
   new EndpointAddress("http://tempuri.org/MyService.svc/ServiceCall")
   );
ChannelFactory<IMySubService1> channelFactory = 
   new ChannelFactory<IMySubService1>(httpEndpoint);

IMySubService1svc = channelFactory.CreateChannel();
/* Calling methods in IMySubService1 */
int i1 = svc.MyOp11("A string");
int i2 = svc.MyOp12("Another string");
int i3 = svc.MyOp11("And another string");
int i4 = svc.MyOp12("In the end a string");
int i5 = svc.MyOp11("The final string");

这可能吗?

2 个答案:

答案 0 :(得分:2)

OK! 最后我试了一下!

可以完成!正如我在问题中所表达的那样。

答案 1 :(得分:0)

嗯,有趣。在我看来,它应该像ServiceContract属性的NameNamespace属性一样在较小的子合同上设置。这些值应该与服务期望的组合合同相匹配。