基於.NET的WebService的實現和WCF的實現

1.新建一個MVC web項目。html

2.點擊項目,【右鍵】→【添加】→【新建項】web

3.點擊【Web】→【Web服務】架構

4.恭喜,Web Service已經新建成功,裏面的方法就能夠參考着根據本身的須要進行修改了,是否是很簡單。ide

5.Web Serice建成以後固然是開始調用了。在【引用】上【右鍵】,添加【服務引用】網站

6.開始引用。this

7.恭喜服務已經引用成功。spa

再看配置文件,會多出一些代碼,這些都是自動生成的,能夠看看理解理解。3d

 

8.開始在程序中調用方法了code

 9.到此爲止Web Service的創建到調用已經所有完成。xml

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,程序正常運行。

若是遇到下面的問題,請以管理員身份運行就能夠了。

其餘的WCF文章 

1. WCF入門

2. 個人第一個WCF程序

3. Web Service 和WCF的比較

4. 基於.NET的WebService的實現和WCF的實現

相關文章
相關標籤/搜索