wcf小实例(2)

//用app.config实现wcf调用
定义contrat interface


using System;
using System.ServiceModel;
using System.Text;

namespace DataSericesContract
{
    [ServiceContract(Name = "DataServicesContract", Namespace = "www.msdn.com")]
    public interface IDataObj
    {
        [OperationContract(Name = "MethodA", Action = "www.msdn.com/DataServicesContract/NameMethod", ReplyAction = "www.msdn.com")]
        string NameMethod();
        [OperationContract(Name = "MethodB", Action = "www.msdn.com/DataServicesContract/AdminMethod", ReplyAction = "www.msdn.com")]
        string AdminMethod();
    }
}
///实现该接口类

using System;
using System.ServiceModel;
using System.Text;
using DataSericesContract;

namespace DataSerices
{
   public class DataObj : IDataObj
    {

        #region IDataObj 成员

        public string NameMethod()
        {
            return "IDataOjb.NameMethod invoked";
        }

        public string AdminMethod()
        {
            return "IDataOjb.AdminMethod invoked";
        }

        #endregion
    }
}


///service host

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ServiceModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DataSerices;
using DataSericesContract;

namespace ServiceHostWCF
{
    public partial class HostWCF : Form
    {
        public HostWCF()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ServiceHost host = null;
            try
            {
                using (host = new ServiceHost(typeof(DataObj)))
                {
                    host.Open();
                    this.button1.Enabled = false;
                    this.toolstart.Text = "services start aucceed !";
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                this.button1.Enabled = true;
                this.toolstart.Text = "Services start fail";
            }
            finally
            {
                if (host != null)
                    host.Close();
            }
        }
    }
}


///app.config//
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <services>
      <service name="DataSerices.DataObj" behaviorConfiguration="serviceBehavior">
        <endpoint address="DataObj" contract="DataSericesContract.IDataObj" binding="basicHttpBinding" />
        <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex"/>
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8900"/>
          </baseAddresses>
        </host>
      </service>
    </services>

    <behaviors>
      <serviceBehaviors>
        <behavior name="serviceBehavior">
          <serviceMetadata httpGetEnabled="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>

  </system.serviceModel>
</configuration>

转载于:https://www.cnblogs.com/server126/archive/2011/06/23/2088205.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值