2011-06-01 41 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屬性一樣在較小的子合同上設置。這些值應該與服務所期望的合成合同相匹配。

相關問題