我正在運行一個看起來像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實現?
謝謝!這是我的問題的解決方案!但是我想知道是否有另一種連接端點的方式,但實際上並不知道它是EndpointAddress,這裏是:net.pipe:// localhost/Calculator。這與將端點填充到外部世界的ServiceMetadataBehavior有關嗎? – inva