文章目录
1. 以管理员运行VS
创建WCF服务端项目
2. 覆盖IService1.cs
using System;
using System.ServiceModel;
namespace GettingStartedLib
{
[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);
}
}
3. 覆盖Service1.cs
using System;
using System.ServiceModel;
namespace GettingStartedLib
{
public class CalculatorService : ICalculator
{
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);
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.修改app.config
将第 14 行更改为
<servicename=“GettingStartedLib.CalculatorService”>
将第 17 行更改为
< add baseAddress =“http://localhost:8000/GettingStarted/CalculatorService” />
将第 22 行更改为
< endpoint address="" binding=“wsHttpBinding” contract=“GettingStartedLib.ICalculator”/>
5. 在当前解决方案中新建控制台应用
6. 添加刚刚wcf服务库的引用
7. 创建成功,打开对应网页
8. .net framework
创建客户端
using ConsoleApp4.ServiceReference1;
using System;
using System.ServiceModel;
namespace GettingStartedClient
{
class Program
{
static void Main(string[] args)
{
//Step 1: Create an instance of the WCF proxy.
EndpointAddress epAddress = new EndpointAddress("http://localhost:8000/GettingStarted/CalculatorService");
CalculatorClient client = new CalculatorClient(new WSHttpBinding(), epAddress);
//CalculatorClient client = new CalculatorClient();
// Step 2: Call the service operations.
// Call the Add service operation.
double value1 = 100.00D;
double value2 = 15.99D;
double result = client.Add(value1, value2);
//Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result);
// Call the Subtract service operation.
value1 = 145.00D;
value2 = 76.54D;
result = client.Subtract(value1, value2);
Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result);
// Call the Multiply service operation.
value1 = 9.00D;
value2 = 81.25D;
result = client.Multiply(value1, value2);
Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, result);
// Call the Divide service operation.
value1 = 22.00D;
value2 = 7.00D;
result = client.Divide(value1, value2);
Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result);
// Step 3: Close the client to gracefully close the connection and clean up resources.
Console.WriteLine("\nPress <Enter> to terminate the client.");
Console.ReadLine();
client.Close();
}
}
}
9. 要先运行之前的GettingStarted项目!
添加服务引用
右键添加里面有,服务引用