码迷,mamicode.com
首页 > Web开发 > 详细

Web Service Demo图解和WCF Demo图解对比

时间:2016-06-20 00:25:37      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:

1.新建一个MVC web项目。

2.点击项目,【右键】→【添加】→【新建项】

技术分享

3.点击【Web】→【Web服务】

技术分享

4.恭喜,Web Service已经新建成功,里面的方法就可以参考着根据自己的需要进行修改了,是不是很简单。

技术分享

5.Web Serice建成之后当然是开始调用了。在【引用】上【右键】,添加【服务引用】

技术分享

6.开始引用。

技术分享

7.恭喜服务已经引用成功。

技术分享

再看配置文件,会多出一些代码,这些都是自动生成的,可以看看理解理解。

技术分享

 

8.开始在程序中调用方法了

技术分享

 9.到此为止Web Service的建立到调用已经全部完成。

10.新建WCF与Web Service基本上是一样的,下面直图解不在介绍。

点击项目,【右键】→【添加】→【新建项】→【Web】→【WCF服务】

技术分享

技术分享

11.WCF建成之后当然是开始调用了。在【引用】上【右键】,添加【服务引用】,以后过程即使一模一样的了。

 技术分享

12.WCF和Web Service已经建立成功,那么如何供外部访问呢,当时是发布了,发布在IIS就可以,和发布网站是一样的。

WCF控制台应用程序例子

技术分享

上面这是个简单的架构图。Iservice大家都有的功能的接口(契约),IUserService针对不同的类定义不同的接口,

AbstractService实现IService的公共方法,Uservice实现自己的方法,也可以重写集成的方法。

下面是分层,Web层,大家可以当做不存在,哈哈。

技术分享

下面是IService层

using System;
using System.Collections.Generic;
using System.ServiceModel;

namespace IServiceClassLibrary
{
    [ServiceContract]
    public interface IService<TModel> : IDisposable where TModel : new()
    {
        [OperationContract]
        string Add(TModel tModel);

        [OperationContract]
        string Delete(int id);
        
        [OperationContract]
        string Edit(TModel tModel);

        [OperationContract]
        TModel GetModel(int id);

        [OperationContract]
        IEnumerable<TModel> GetAll();
    }
}
using System;
using System.Runtime.Serialization;

namespace IServiceClassLibrary
{
    [DataContract]
    public class UserData
    {
        [DataMember]
        public int UserID { get; set; }

        [DataMember]
        public string UserName { get; set; }

        [DataMember]
        public string Password { get; set; }

        [DataMember]
        public string Discribe { get; set; }

        [DataMember]
        public DateTime SubmitTime { get; set; }
    }
}
using System;
using System.Collections.Generic;
using System.ServiceModel;

namespace IServiceClassLibrary
{
    [ServiceContract]
    public interface IUserService : IService<UserData>
    {
        [OperationContract]
        IDictionary<int, string> GetUserDict();
    }
}

 下面是Service层

using IServiceClassLibrary;
using System;
using System.Collections.Generic;

namespace ServiceClassLibrary
{
    public abstract class AbstractService<TModel> : IService<TModel> where TModel : new()
    {
        public virtual string Add(TModel tModel)
        {
            //throw new NotImplementedException();
            return "Add";
        }

        public virtual string Delete(int id)
        {
            //throw new NotImplementedException();
            return "Delete";
        }

        public virtual string Edit(TModel tModel)
        {
            //throw new NotImplementedException();
            return "Edit";
        }

        public virtual TModel GetModel(int id)
        {
            TModel tModel = new TModel();
            return tModel;
        }

        public virtual IEnumerable<TModel> GetAll()
        {
            IList<TModel> tModels = new List<TModel>();
            return tModels;
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool dispose)
        {
            if (dispose)
            {
                //CleanUp managed objects by calling thier
            }
        }
    }
}
using IServiceClassLibrary;
using System.Collections.Generic;

namespace ServiceClassLibrary
{
    public class UserService : AbstractService<UserData>, IUserService
    {
        public IDictionary<int, string> GetUserDict()
        {
            IDictionary<int, string> keyValue = new Dictionary<int, string>();
            keyValue.Add(1, "test");
            return keyValue;
        }

        protected override void Dispose(bool dispose)
        {
            if (dispose)
            {
                //CleanUp managed objects by calling thier
            }
        }
    }
}

下面是Server层,这层需要配置了,【新建】→【控制台应用程序】建好之后,【添加】→【新建项】→【WCF 服务】,建号之后,生成的两个文件可以删去了,

查看配置信息,对配置进行修改就可以了,懒人模式,高效准确。

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <behaviors>
            <serviceBehaviors>
                <behavior name="">
                    <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
                    <serviceDebug includeExceptionDetailInFaults="false" />
                </behavior>
            </serviceBehaviors>
        </behaviors>
        <services>
            <service name="ServiceClassLibrary.UserService">
                <endpoint address="" binding="basicHttpBinding" contract="IServiceClassLibrary.IUserService">
                    <identity>
                        <dns value="localhost" />
                    </identity>
                </endpoint>
                <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
                <host>
                    <baseAddresses>
                        <add baseAddress="http://localhost:8733/" />
                    </baseAddresses>
                </host>
            </service>
        </services>
    </system.serviceModel>
</configuration>
using IServiceClassLibrary;
using ServiceClassLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;

namespace WCFServer
{
    class Program
    {
        static void Main(string[] args)
        {
            using (ServiceHost host = new ServiceHost(typeof(UserService)))
            {
                host.AddServiceEndpoint(typeof(IUserService),
                    new BasicHttpBinding(),
                    new Uri("http://localhost:8733/ServiceClassLibrary/UserService/"));
                if (host.State != CommunicationState.Opening)
                {
                    host.Open();
                }
                Console.WriteLine("服务已经启动!");
                Console.ReadLine();
            }
        }
    }
}

下面是客服端

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

namespace WCFClient
{
    class Program
    {
        static void Main(string[] args)
        {
            EndpointAddress ea = new EndpointAddress("http://localhost:8733/ServiceClassLibrary/UserService/");
            IUserService proxy = ChannelFactory<IUserService>.CreateChannel(new BasicHttpBinding(), ea);
            Console.WriteLine(proxy.Delete(0));
            Console.WriteLine(proxy.GetUserDict().First());
            Console.ReadLine();
        }
    }
}

OK,WCF程序已经建立完成,先启动Server,再启动Client,程序正常运行。

如果遇到下面的问题,请以管理员身份运行就可以了。

技术分享

 

Web Service Demo图解和WCF Demo图解对比

标签:

原文地址:http://www.cnblogs.com/zhao123/p/5599251.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!