文章出處

    需求:在同一臺機子上,有一個B/S程序,和一個C/S程序(不要問為什么,事實就是這樣),B/S程序需要主動和C/S程序通信(C/S程序主動與B/S程序通信的情況這里暫不討論)。

    下面以最快的速度寫一個B/S程序和一個C/S程序實現,具體細節不解釋,自己翻書看去。

    一、建了兩個工程,如下圖所示:

    二、先看C/S程序,跑起來就是這樣的,如下圖所示(簡單吧):

程序結構如下圖:

WCF的接口WCFServer、數據結構DataStruct、Winform程序Client

IClientServer:

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

namespace WCFServer
{
    [ServiceContract]
    public interface IClientServer
    {
        [OperationContract]
        TestData Test(string param);
    }
}
View Code

ClientServer:

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

namespace WCFServer
{
    [ServiceBehavior()]
    public class ClientServer : IClientServer
    {
        /// <summary>
        /// 測試
        /// </summary>
        public TestData Test(string param)
        {
            TestData data = new TestData();
            data.Name = param;
            data.Code = "0101";
            return data;
        }
    }
}
View Code

TestData:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;

namespace DataStruct
{
    [DataContract]
    public class TestData
    {
        [DataMember]
        public string Name { get; set; }

        [DataMember]
        public string Code { get; set; }
    }
}
View Code

Form1.cs:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WCFServer;

namespace CSServer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            OpenClientServer();
        }

        /// <summary>
        /// 啟動服務
        /// </summary>
        private void OpenClientServer()
        {
            WSHttpBinding wsHttp = new WSHttpBinding();
            wsHttp.MaxBufferPoolSize = 524288;
            wsHttp.MaxReceivedMessageSize = 2147483647;
            wsHttp.ReaderQuotas.MaxArrayLength = 6553600;
            wsHttp.ReaderQuotas.MaxStringContentLength = 2147483647;
            wsHttp.ReaderQuotas.MaxBytesPerRead = 6553600;
            wsHttp.ReaderQuotas.MaxDepth = 6553600;
            wsHttp.ReaderQuotas.MaxNameTableCharCount = 6553600;
            wsHttp.CloseTimeout = new TimeSpan(0, 1, 0);
            wsHttp.OpenTimeout = new TimeSpan(0, 1, 0);
            wsHttp.ReceiveTimeout = new TimeSpan(0, 10, 0);
            wsHttp.SendTimeout = new TimeSpan(0, 10, 0);
            wsHttp.Security.Mode = SecurityMode.None;

            Uri baseAddress = new Uri("http://127.0.0.1:9999/clientserver");
            ServiceHost host = new ServiceHost(typeof(ClientServer), baseAddress);

            ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
            smb.HttpGetEnabled = true;
            host.Description.Behaviors.Add(smb);

            ServiceBehaviorAttribute sba = host.Description.Behaviors.Find<ServiceBehaviorAttribute>();
            sba.MaxItemsInObjectGraph = 2147483647;

            host.AddServiceEndpoint(typeof(IClientServer), wsHttp, "");

            host.Open();
        }
    }
}
View Code

C/S程序跑起來就OK了。

三、B/S程序

程序結構如下圖:

WCF的接口DataService、Web程序

DataService需要引用數據結構DataStruct和WCFServer,是C/S程序編譯后的dll拿過來的,Web需要引用DataStruct,如下圖:

ClientServer:

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

namespace DataService
{
    public class ClientServer
    {
        ChannelFactory<IClientServer> channelFactory;
        IClientServer proxy;

        /// <summary>
        /// 創建連接客戶終端WCF服務的通道
        /// </summary>
        public void CreateChannel()
        {
            string url = "http://127.0.0.1:9999/clientserver";
            WSHttpBinding wsHttp = new WSHttpBinding();
            wsHttp.MaxBufferPoolSize = 524288;
            wsHttp.MaxReceivedMessageSize = 2147483647;
            wsHttp.ReaderQuotas.MaxArrayLength = 6553600;
            wsHttp.ReaderQuotas.MaxStringContentLength = 2147483647;
            wsHttp.ReaderQuotas.MaxBytesPerRead = 6553600;
            wsHttp.ReaderQuotas.MaxDepth = 6553600;
            wsHttp.ReaderQuotas.MaxNameTableCharCount = 6553600;
            wsHttp.SendTimeout = new TimeSpan(0, 10, 0);
            wsHttp.Security.Mode = SecurityMode.None;

            channelFactory = new ChannelFactory<IClientServer>(wsHttp, url);
            foreach (OperationDescription op in channelFactory.Endpoint.Contract.Operations)
            {
                DataContractSerializerOperationBehavior dataContractBehavior = op.Behaviors.Find<DataContractSerializerOperationBehavior>() as DataContractSerializerOperationBehavior;

                if (dataContractBehavior != null)
                {
                    dataContractBehavior.MaxItemsInObjectGraph = 2147483647;
                }
            }
        }

        /// <summary>
        /// 測試
        /// </summary>
        public TestData Test(string param)
        {
            proxy = channelFactory.CreateChannel();

            try
            {
                return proxy.Test(param);
            }
            catch (Exception ex)
            {
                return null;
            }
            finally
            {
                (proxy as ICommunicationObject).Close();
            }
        }
    }
}
View Code

TestController:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DataService;
using DataStruct;

namespace BSClient.Controllers
{
    public class TestController : Controller
    {

        public ActionResult Index()
        {
            ClientServer client = new ClientServer();
            client.CreateChannel();
            ViewData["data"] = client.Test("ABCD");

            return View();
        }

    }
}
View Code

Index.cshtml:

@using DataStruct;
@{
    ViewBag.Title = "Index";
    TestData data = ViewData["data"] as TestData;
    if (data == null) { data = new TestData(); }
}

<h2>Index</h2>

<h2>@data.Name</h2>
<h2>@data.Code</h2>
View Code

先啟動C/S程序,再啟動B/S程序,B/S的運行結果如下圖所示:

上面以最快的速度寫了一個WCF的簡單的Demo,程序跑起來了,后面的再慢慢學吧。

 


文章列表




Avast logo

Avast 防毒軟體已檢查此封電子郵件的病毒。
www.avast.com


arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()