勇敢不是无所畏惧,而是心怀恐惧,却依然向前!
Calculator.svc代码:
public class Calculator : ICalculator
{
public double AddNumbers(double number1, double number2)
{
double result = number1 + number2;
return result;
}
public double SubstractNumbers(double number1, double number2)
{
double result = number1 - number2;
return result;
}
public double MultiplyNumbers(CalculatorModel calculatorModel)
{
double result = calculatorModel.number1 * calculatorModel.number2;
return result;
}
public double DivisionNumbers(double number1, double number2)
{
double result = number1 / number2;
return result;
}
}
ICalculator.cs代码:
[ServiceContract]
public interface ICalculator
{
[OperationContract]
double AddNumbers(double number1, double number2);
[OperationContract]
double SubstractNumbers(double number1, double number2);
[OperationContract]
double MultiplyNumbers(CalculatorModel calculatorModel);
[OperationContract]
double DivisionNumbers(double number1, double number2);
}
[DataContract]
public class CalculatorModel
{
[DataMember]
public double number1 { get; set; }
[DataMember]
public double number2 { get; set; }
}
Web.config
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5"/>
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="WcfService.Calculator">
<endpoint address="" contract="WcfService.ICalculator" binding="basicHttpBinding"/>
<endpoint address="mex" contract="IMetadataExchange" binding="mexHttpBinding"/>
</service>
</services>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>
Program.cs代码:
class Program
{
static void Main(string[] args)
{
CalculatorClient proxy = new CalculatorClient()
Console.WriteLine("输入Number1")
double number1 = Convert.ToInt32(Console.ReadLine())
Console.WriteLine("输入Number2")
double number2 = Convert.ToInt32(Console.ReadLine())
Console.Write("\n\n")
Console.Write("结果\n")
Console.Write("===================================\n")
double addResult = proxy.AddNumbers(number1, number2)
Console.WriteLine("加法运算: {0}\n",addResult)
double subResult = proxy.SubstractNumbers(number1, number2)
Console.WriteLine("减法运算 : {0}\n", subResult)
double mulResult = proxy.MultiplyNumbers(new CalculatorModel() { number1 = number1, number2 = number2 })
Console.WriteLine("乘法运算 : {0}\n", mulResult)
double divResult = proxy.DivisionNumbers(number1, number2)
Console.WriteLine("除法运算 : {0}\n", divResult)
Console.Write("===================================\n")
Console.Write("请Enter退出...")
Console.ReadKey(true)
proxy.Close()
}
}
App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_ICalculator" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:15468/Calculator.svc" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_ICalculator" contract="CalculatorService.ICalculator"
name="BasicHttpBinding_ICalculator" />
</client>
</system.serviceModel>
</configuration>
项目运行结果如图:
