Ok, ale chyba wciąż coś jest nie tak z moim kontraktem. Wkleję cały kod:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Runtime.Serialization;
namespace MyTypes
{
[DataContract(Namespace = "http://Microsoft.ServiceModel.Samples")]
class LastResults
{
private double result;
[DataMember]
public double ResultValue
{
get { return result; }
set { result = value; }
}
}
}
namespace Microsoft.ServiceModel.Samples
{
[ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples")]
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);
}
class CalculatorService : ICalculator
{
MyTypes.LastResults lastResult;
public double Add(double n1, double n2)
{
double result = n1 + n2;
Console.WriteLine("Received Add({0},{1})", n1, n2);
// Code added to write output to the console window.
Console.WriteLine("Return: {0}", result);
lastResult.ResultValue = result;
return result;
}
public double Subtract(double n1, double n2)
{
double result = n1 - n2;
Console.WriteLine("Received Subtract({0},{1})", n1, n2);
Console.WriteLine("Return: {0}", result);
lastResult.ResultValue = result;
return result;
}
public double Multiply(double n1, double n2)
{
double result = n1 * n2;
Console.WriteLine("Received Multiply({0},{1})", n1, n2);
Console.WriteLine("Return: {0}", result);
lastResult.ResultValue = result;
return result;
}
public double Divide(double n1, double n2)
{
double result = n1 / n2;
Console.WriteLine("Received Divide({0},{1})", n1, n2);
Console.WriteLine("Return: {0}", result);
lastResult.ResultValue = result;
return result;
}
}
class Program
{
static void Main(string[] args)
{
Uri baseAddress = new Uri("http://localhost:8000/ServiceModelSamples/Service");
ServiceHost selfHost = new ServiceHost(typeof(CalculatorService), baseAddress);
try
{
selfHost.AddServiceEndpoint(typeof(ICalculator), new WSHttpBinding(), "CalculatorService");
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
selfHost.Description.Behaviors.Add(smb);
selfHost.Open();
Console.WriteLine("The service is ready.");
Console.WriteLine("Press <ENTER> to terminate service.");
Console.WriteLine();
Console.ReadLine();
// Close the ServiceHostBase to shutdown the service.
selfHost.Close();
}
catch (CommunicationException ce)
{
Console.WriteLine("An exception occurred: {0}", ce.Message);
selfHost.Abort();
}
}
}
}
Widzę wszystkie Operation Contracty, ale Data Contract za nic nie mogę zawołać :/ Zapewne ma to związek z tym, że jedno ma dodane atrybuty kontraktu w interfejsie a drugie nie...? Powinnam to do interfejsu dorzucić? Ale czy nowego, czy tego samego? Czy może być przy jednym interfejsie zarówno atrybut Service Contract jak i Data Contract...?
edit:
To wydzielenie do oddzielnego namespace'a to już wynik moich kombinacji, z początku było w tym samym, ale to raczej nie ma znaczenia - nie działa tak czy siak :P