将接口定义成服务契约后,接口的方法成员并不能自动成为服务的操作。我们需要在相应的操作方法上面显式地应用OperationContractAttribute特性。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
namespace Calculator.Service
{
[ServiceContract]
public interface ICalculator
{
[OperationContract]
double Add(double x, double y);
[OperationContract]
double Subtract(double x, double y);
[OperationContract]
double Multiply(double x, double y);
[OperationContract]
double Divide(double x, double y);
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Calculator.Service
{
public class CalculatorService:ICalculator
{
public double Add(double x, double y)
{
return x + y;
}
public double Subtract(double x, double y)
{
return x - y;
}
public double Multiply(double x, double y)
{
return x * y;
}
public double Divide(double x, double y)
{
return x / y;
}
}
}using Calculator.Service;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Text;
using System.Windows.Forms;
namespace Calculator.Host
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
ServiceHost host = null;
private void btnOpen_Click(object sender, EventArgs e)
{
host = new ServiceHost(typeof(CalculatorService));
host.AddServiceEndpoint(typeof(ICalculator), new WSHttpBinding(), "http://localhost:8008/Calculator");
if (host.Description.Behaviors.Find<ServiceMetadataBehavior>()==null)
{
ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
behavior.HttpGetEnabled = true;
behavior.HttpGetUrl = new Uri("http://localhost:8008/Calculator/metadata");
host.Description.Behaviors.Add(behavior);
}
host.Opened += delegate { label1.Text = "服务已经启动!"; };
host.Open();
}
private void btnClose_Click(object sender, EventArgs e)
{
if (host.State != CommunicationState.Closed)
{
host.Closed += delegate { label1.Text = "服务已经停止!"; };
host.Close();
}
}
}
}using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Calculator.Client
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.Text = "+";
}
private void button1_Click(object sender, EventArgs e)
{
CalculatorService.CalculatorClient client = new CalculatorService.CalculatorClient();
double x = Convert.ToDouble (textBox1.Text);
double y = Convert.ToDouble(textBox2.Text);
double result=0;
string operater = comboBox1.Text;
switch (operater )
{
case "+":
result = client.Add(x, y);
break;
case "-":
result = client.Subtract(x, y);
break;
case "*":
result = client.Multiply(x, y);
break;
case "/":
if (y==0)
{
MessageBox.Show("除数不能为0!");
return;
}
result = client.Divide(x, y);
break;
}
label1.Text = textBox1.Text + comboBox1.Text + textBox2.Text + " = " + Convert.ToString(result);
}
}
}
原文地址:http://blog.csdn.net/ry513705618/article/details/45337853