How to: Create a Service with a Contract Interface

How to: Create a Service with a Contract Interface

The preferred way to create a Windows Communication Foundation (WCF) contract is by using an interface. This contract specifies the collection and structure of messages required to access the operations the service offers. This interface defines the input and output types by applying the ServiceContractAttribute class to the interface and the OperationContractAttribute class to the methods that you want to expose.

For more information about service contracts, see Designing Service Contracts.

Creating a WCF contract with an interface

  1. Create a new interface using Visual Basic, C#, or any other common language runtime language.

  2. Apply the ServiceContractAttribute class to the interface.

  3. Define the methods in the interface.

  4. Apply the OperationContractAttribute class to each method that must be exposed as part of the public WCF contract.

Example

The following code example shows an interface that defines a service contract.

 
C#
using System.ServiceModel;

[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);
}

The methods that have the OperationContractAttribute class applied use a request-reply message pattern by default. For more information about this message pattern, see How to: Create a Request-Reply Contract. You can also create and use other message patterns by setting properties of the attribute. For more examples, see How to: Create a One-Way Contract and How to: Create a Duplex Contract.

See Also

转载于:https://www.cnblogs.com/MayGarden/archive/2010/01/12/1645071.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值