2011-11-01 17 views
0

我正在運行一個看起來像MS計算器示例的服務。 這是ServiceContract合同接口。NetNamedPipeBinding ActionNotSupportedException

[ServiceContract(Namespace = "http://localhost:8000/Calculator")] 
public interface ICalculator 
{ 
    [OperationContract] 
    double Add(double n1, double n2); 
    [OperationContract] 
    double Subtract(double n1, double n2); 
    [OperationContract] 
    double Multiply(double n1, double n2); 
    [OperationContract] 
    double Divide(double n1, double n2); 
} 

我建立這樣的服務:我成功安裝服務

public partial class Service1: ServiceBase 
{ 
    private static readonly string sNameOfService = "CalculatorService"; 
    public static string NameOfService 
    { 
     get { return sNameOfService; } 
    } 

    public ServiceHost serviceHost = null; 

    public Service1() 
    { 
     InitializeComponent(); 

     this.ServiceName = sNameOfService; 
     this.CanStop = true; 
     this.CanPauseAndContinue = false; 
     this.AutoLog = true; 
    } 

    protected override void OnStart(string[] args) 
    { 
     if (serviceHost != null) 
      serviceHost.Close(); 

     Uri[] baseAddress = new Uri[]{ 
     new Uri("net.pipe://localhost")}; 

     string PipeName = "Calculator"; 

     serviceHost = new ServiceHost(typeof(CalculatorImplementation), 

     serviceHost.AddServiceEndpoint(typeof(ICalculator), new NetNamedPipeBinding(), PipeName); 

     serviceHost.Open(); 
    } 

    protected override void OnStop() 
    { 
     if (serviceHost != null && serviceHost.State != CommunicationState.Closed) 
     { 
      serviceHost.Close(); 
      serviceHost = null; 
     } 
    } 
} 

,這是我的本地機器上運行。 下一步是建立一個客戶端,通過以下方式訪問我所做的服務。

public interface ICalculator 
{ 
    // die einzelnen Teile können auch als Vorgänge bzw. Kanäle verstanden werden 
    // öffentliche Teile des Interfaces definieren 
    // diese werden durch das OperationContract Attribut identifiziert 
    [OperationContract] 
    double Add(double n1, double n2); 
    [OperationContract] 
    double Subtract(double n1, double n2); 
    [OperationContract] 
    double Multiply(double n1, double n2); 
    [OperationContract] 
    double Divide(double n1, double n2); 
} 

class Program 
{ 
    static void Main(string[] args) 
    { 
     ChannelFactory<ICalculator> pipeFactory = new ChannelFactory<ICalculator>(new NetNamedPipeBinding(), new EndpointAddress("net.pipe://localhost/Calculator")); 

     ICalculator pipeProxy = pipeFactory.CreateChannel(); 

     double erg = pipeProxy.Add(5, 6); 

     Console.WriteLine("Ergebnis: {0}", erg.ToString()); 
     Console.ReadLine(); 
    } 
} 

但試圖打電話的時候,我得到一個ActionNotSupportedException「pipeProxy.Add()」和我不知道爲什麼會這樣。 我的服務器設置不正確,或者我在客戶端內有什麼問題,或者我是否錯過了設置一些必需的屬性?我瀏覽了多個使用命名管道的例子,但是我沒有找到任何幫助我解決問題的例子。

此外,我問自己,有什麼區別,我應該在哪裏使用ServiceHost實現和NamedPipeClientStream/NamedPipeServerStream實現?

回答

0

ActionNotSupportedException當您的客戶端使用服務器未知的SOAP操作時會引發。默認情況下,SOAP操作的名稱是從服務合同和操作合同的類型中推斷出來的。

在你的例子中,我看到兩次定義的合同。這是否意味着服務和客戶都有自己的定義ICalculator?在這種情況下,沒有進一步的努力,這兩個合同是不一樣的,因爲它們很可能位於不同的命名空間中,而您的第一個合同指定了它自己的命名空間,而第二個合同沒有。如果你想使用相同的接口,將它分離出來,並從服務和客戶端引用程序集。否則,您將不得不通過ServiceContractOperationContract屬性,並確保NamespaceActionReplyAction的值在兩個接口中都相同。

+0

謝謝!這是我的問題的解決方案!但是我想知道是否有另一種連接端點的方式,但實際上並不知道它是EndpointAddress,這裏是:net.pipe:// localhost/Calculator。這與將端點填充到外部世界的ServiceMetadataBehavior有關嗎? – inva

相關問題