2011-06-01 57 views
2

我有以下疑问: 考虑以下几点:是否有可能sccessing 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 { 
    ... 
} 

行!直到现在,没有什么奇怪的! 我的服务将托管在服务器上,我很高兴:) 服务托管(例如)在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"); 

这可能吗?

+0

你试过了吗?如果它不起作用,你会得到什么错误? – carlosfigueira 2011-06-01 15:49:34

+0

对不起,这只是一个好奇...我没有尝试:P – Andry 2011-06-01 16:01:55

+0

嗯,只是试试吧,它不应该花很长时间:) – carlosfigueira 2011-06-01 16:50:00

回答

2

好的! 最后我试了一下!

它可以做!正如我在我的问题中所展示的一样。

0

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

相关问题