不用配置文件的wcf实例

1、根据需求建立一个类库,这里命名为ComputeServe

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

namespace ComputeServe
{
    [ServiceContract]
    public interface ICompute
    {
       [OperationContract]
        double AddData(double x, double y);
    }
}

上面需要添加引用System.ServiceModel

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ComputeServe
{
    public class Compute : ICompute
    {
        public double AddData(double x, double y)
        {
            return x + y;
        }
    }
}


将类库生成dll

 

 

2、建立服务端

添加刚生成的dll和System.ServiceModel

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;


namespace wcf1
{
    class Program
    {
        static void Main(string[] args)
        {
            using (ServiceHost host = new ServiceHost(typeof(ComputeServe.Compute)))
            {
                //定义绑定
                System.ServiceModel.Channels.Binding httpBinding = new BasicHttpBinding();
                //定义终结点
                host.AddServiceEndpoint(typeof(ComputeServe.ICompute), httpBinding, "http://localhost:8001/");//所以服务端的URI也应该用这个
                if (host.Description.Behaviors.Find<System.ServiceModel.Description.ServiceMetadataBehavior>() == null)
                {
                    host.Opened += delegate
                    {
                        Console.WriteLine("Service is already open");
                    };
                    //运行
                    host.Open();
                    Console.ReadKey();
                    //关闭
                    host.Close();
                }
            }
        }
    }
}


 

 

3、客户端

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

namespace ComputeClient
{
    [ServiceContract]
    interface ICompute
    {
        [OperationContract]
        double AddData(double x, double y);
    }
}


 

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

namespace ComputeClient
{
    class Compute:System.ServiceModel.ClientBase<ICompute>,ICompute
    {
        public double AddData(double x, double y)
        {
            return base.Channel.AddData(x, y);
        }
    }
}


注:取名ICompute,Compute这个是因为dll里面是这样命名的

 

 

调用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;

namespace ComputeClient
{
    class Program
    {
        static void Main(string[] args)
        {
            string address = "http://localhost:8001/";
            ChannelFactory<ICompute> channel =
                new ChannelFactory<ICompute>(
                    new BasicHttpBinding(),
                    new EndpointAddress(
                        new Uri(address)));
            ICompute proxy = channel.CreateChannel();
            Console.WriteLine("WCF调用结果为:{0}", proxy.AddData(2, 3));

            Console.ReadKey();

        }
    }
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值