1、引用:
using System.ServiceModel;
using System.ServiceModel.Description;
2、申明接口
///这是接口
[ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples")]public interface MyJK
{
[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);
}
3、实现接口
//实现契约和接口
public class AABB : MyJK
{
public double Add(double n1, double n2)
{
double result = n1 + n2;
Console.WriteLine("Received Add({0},{1})", n1, n2);
Console.WriteLine("Return: {0}", 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);
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);
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);
return result;
}
}
4、定义服务、启动服务
Uri baseAddress = new Uri("http://localhost:8989/aaa/"); //随便写
ServiceHost serviceHost = new ServiceHost(typeof(AABB), baseAddress);
serviceHost.AddServiceEndpoint(typeof(MyJK), new WSHttpBinding(), "BBCCDD");
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
serviceHost.Description.Behaviors.Add(smb);
serviceHost.Open();