WCF列子

添加默认的服务库


定义一个控制台程序实现寄宿。

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //ServiceReference1.Service1Client proxy = new ServiceReference1.Service1Client();
            //string strMsg = proxy.GetData(12);
            //Console.WriteLine(strMsg);
            //Console.Read();

            //创建宿主的基地址
            Uri baseAddress = new Uri("http://localhost:8080/User");
            //创建宿主
            using (ServiceHost host = new ServiceHost(typeof(Service1), baseAddress))
            {
                //向宿主中添加终结点
                host.AddServiceEndpoint(typeof(IService1), new WSHttpBinding(), "");
                //将HttpGetEnabled属性设置为true
                ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
                smb.HttpGetEnabled = true;
                //将行为添加到Behaviors中
                host.Description.Behaviors.Add(smb);
                //打开宿主
                host.Open();
                Console.WriteLine("WCF中的HTTP监听已启动....");
                Console.ReadLine();
                host.Close();
            }
        }
    }
}



在建立个Web站点



引用服务,刚刚寄宿定义的地址


调用,这里的控制台一定要打开,否则就会请求失败

你可能感兴趣的:(WCF列子)