以管理员运行VS
创建WCF服务端项目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);
}
}
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;
}
}
}
将第 14 行更改为
将第 17 行更改为
< add baseAddress =“http://localhost:8000/GettingStarted/CalculatorService” />
将第 22 行更改为
< endpoint address="" binding=“wsHttpBinding” contract=“GettingStartedLib.ICalculator”/>
.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 to terminate the client." );
Console.ReadLine();
client.Close();
}
}
}
要先运行之前的GettingStarted项目!
添加服务引用右键添加里面有,服务引用