WCF basicHttpBinding 实例

一、定义契约

using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;

namespace Service.Interface
{
    [ServiceContract]
    public interface IFileUpLoad
    {
        [OperationContract]
        string FileUpLoad();
    }
}

二、实现契约

using Service.Dal;
using Service.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;

namespace Service.Implement
{
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Multiple)]
    public class FileUpLoadService : IFileUpLoad
    {
        public string FileUpLoad()
        {
            return FileUpLoadAccessor.FileUpLoad();
        }
    }
}


会话,实例与并发

InstanceContextMode = InstanceContextMode.PerCall

在这种实例化模式下,客户端对服务方法的每一次调用,服务端都会new一个实例,方法调用结束即销毁,这种模式可能要频繁的花费创建对象和销毁对象的花销,因此性能比较差,但是每次调用后服务对象被销毁,对象所把持的资源也就被释放(如文件啦、数据库连接啦),因此伸缩性是最好的。

InstanceContext = PerCall & ConcurrencyMode = Multipl

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
WCF是一种用于创建分布式系统的技术,可与vb.net一起使用。 WCF中可以使用多种协议进行网络通信,例如HTTP,TCP和MSMQ等。下面是一个vb.net的WCF网络通信实例: 首先,要创建一个WCF服务,您可以使用Visual Studio中的模板。在创建服务时,可以选择使用什么类型的绑定和协议。例如,默认情况下会使用基本HTTP绑定和HTTP协议。 在服务中定义操作合同,这些合同将服务公开为web服务,可以使用它们来处理远程请求。 在客户端应用程序中,您需要首先添加服务引用,可以通过Visual Studio的“添加服务引用”向导来完成。然后,您可以使用该服务的客户端代理来调用远程服务,就像在本地应用程序中一样。 以下是一个简单的WCF服务和客户端应用程序示例: 服务代码: ```vb.net Imports System.ServiceModel <ServiceContract()> Public Interface ICalculator <OperationContract()> Function Add(ByVal x As Double, ByVal y As Double) As Double End Interface Public Class Calculator Implements ICalculator Public Function Add(ByVal x As Double, ByVal y As Double) As Double Implements ICalculator.Add Return x + y End Function End Class Sub Main() Dim host As New ServiceHost(GetType(Calculator)) host.Open() Console.WriteLine("Service started. Press any key to stop.") Console.ReadLine() host.Close() End Sub ``` 客户端代码: ```vb.net Module Module1 Sub Main() Dim client As New CalculatorClient() Console.WriteLine("2 + 3 = " & client.Add(2, 3)) client.Close() Console.ReadLine() End Sub End Module <ServiceContract()> Public Interface ICalculator <OperationContract()> Function Add(ByVal x As Double, ByVal y As Double) As Double End Interface Public Class CalculatorClient Inherits ClientBase(Of ICalculator) Implements ICalculator Public Function Add(ByVal x As Double, ByVal y As Double) As Double Implements ICalculator.Add Return Channel.Add(x, y) End Function End Class ``` 在这个例子中,我定义了一个名为“Calculator”的简单计算器服务,它具有一个Add操作。在客户端应用程序中,我使用了一个名为“CalculatorClient”的客户端代理来调用Add操作,该代理使用了WCF中的默认HTTP绑定和HTTP协议来与服务通信。 这就是一个简单的WCF网络通信示例,您可以开始使用WCF来创建自己的分布式应用程序,用于实现您的网络通信需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

tiz198183

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值