如何:在配置中指定服务绑定
在本示例中,为基本计算器服务定义 ICalculator
协定,在 CalculatorService
类中实现该服务,然后在 Web.config 文件中配置其终结点,该文件中指定此服务使用 BasicHttpBinding。 有关如何使用代码而不是配置来配置此服务的说明,请参阅如何:在代码中指定服务绑定。
通常,最佳做法是以声明方式在配置中指定绑定和地址信息,而不是在代码中强制指定。 在代码中定义终结点通常是不可行的,因为已部署服务的绑定和地址通常与在部署服务时所用的绑定和地址不同。 一般说来,通过将绑定和寻址信息放置在代码之外,无需重新编译或重新部署应用程序即可更改这些信息。
可以使用配置编辑器工具 (SvcConfigEditor.exe) 来执行以下所有配置步骤。
有关此示例的源副本,请参阅 BasicBinding。
指定要用于配置服务的 BasicHttpBinding
为该类型的服务定义服务协定。
[ServiceContract] 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); }
<ServiceContract()> _ Public Interface ICalculator <OperationContract()> _ Function Add(ByVal n1 As Double, ByVal n2 As Double) As Double <OperationContract()> _ Function Subtract(ByVal n1 As Double, ByVal n2 As Double) As Double <OperationContract()> _ Function Multiply(ByVal n1 As Double, ByVal n2 As Double) As Double <OperationContract()> _ Function Divide(ByVal n1 As Double, ByVal n2 As Double) As Double End Interface
在服务类中实现该服务协定。
public class CalculatorService : ICalculator { public double Add(double n1, double n2) { return n1 + n2; } public double Subtract(double n1, double n2) { return n1 - n2; } public double Multiply(double n1, double n2) { return n1 * n2; } public double Divide(double n1, double n2) { return n1 / n2; } }
Public Class CalculatorService Implements ICalculator Public Function Add(ByVal n1 As Double, _ ByVal n2 As Double) As Double Implements ICalculator.Add Return n1 + n2 End Function Public Function Subtract(ByVal n1 As Double, _ ByVal n2 As Double) As Double Implements ICalculator.Subtract Return n1 - n2 End Function Public Function Multiply(ByVal n1 As Double, _ ByVal n2 As Double) As Double Implements ICalculator.Multiply Return n1 * n2 End Function Public Function Divide(ByVal n1 As Double, _ ByVal n2 As Double) As Double Implements ICalculator.Divide Return n1 / n2 End Function End Class
注意
未在该服务的实现内部指定地址或绑定信息。 而且,不必编写码就可以从配置文件中获取该信息。
创建一个 Web.config 文件来配置使用
CalculatorService
的 WSHttpBinding 的终结点。<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <services> <service name=" CalculatorService" > <!-- Leave the address blank to be populated by default --> <!-- from the hosting environment,in this case IIS, so --> <!-- the address will just be that of the IIS Virtual --> <!-- Directory. --> <!-- Specify the binding configuration name for that --> <!-- binding type. This is optional but useful if you --> <!-- want to modify the properties of the binding. --> <!-- The bindingConfiguration name Binding1 is defined --> <!-- below in the bindings element. --> <endpoint address="" binding="wsHttpBinding" bindingConfiguration="Binding1" contract="ICalculator" /> </service> </services> <bindings> <wsHttpBinding> <binding name="Binding1"> <!-- Binding property values can be modified here. --> <!-- See the next procedure. --> </binding> </wsHttpBinding> </bindings> </system.serviceModel> </configuration>
创建一个包含下行的 Service.svc 文件,然后将其放到 Internet 信息服务 (IIS) 虚拟目录中。
<%@ServiceHost language=c# Service="CalculatorService" %>
修改绑定属性的默认值
若要修改 WSHttpBinding 的默认属性值之一,请先在 <wsHttpBinding> 元素中创建一个新的绑定配置名称 -
<binding name="Binding1">
,然后在此绑定元素中为绑定属性设置新值。 例如,若要将默认打开和关闭超时值从 1 分钟更改为 2 分钟,需要将下列内容添加到配置文件中。<wsHttpBinding> <binding name="Binding1" closeTimeout="00:02:00" openTimeout="00:02:00"> </binding> </wsHttpBinding>