I’m running a service which looks like the MS Calculator Example.
This is the ServiceContract contract Interface.
[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);
}
I set up the Service like this:
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;
}
}
}
I installed the service successfully and it’s running on my local machine.
The next step was to set up a client, accessing the service which I did in the following way.
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();
}
}
But i get an ActionNotSupportedException when trying to call “pipeProxy.Add()” and I got no idea why this happens.
Is my server not set up correctly or did I something wrong within the client or did I missed to set some needed attributes? I browsed through multiple examples using named pipes but I found nothing that helped me solving my problem.
Furthermore I was asking myself, what is the difference and where should I use the ServiceHost implementation and the NamedPipeClientStream/NamedPipeServerStream implementation?
ActionNotSupportedExceptionis thrown when your client uses SOAP action which is unknown to the server. By default names of SOAP actions are inferred from types of service contracts and operation contracts.In your example I see the contract defined twice. Does it mean that both service and client has their own definition of
ICalculator? In such case without further effort these two contracts are not the same because they most probably sit in different namespaces and your first contract specifies its own namespace whereas the second doesn’t. If you want to use the same interface put it to separate assembly and reference the assembly from both service and client. Otherwise you will have to go throughServiceContractandOperationContractattributes and make sure that values forNamespace,ActionandReplyActionare same in both interfaces.