Quick Start of WCF (2) and Quick Start of wcf
I. code structure:
2. Service Interface Contract and entity Domain
INoticeService:
Using Domain; using System. collections. generic; using System. linq; using System. serviceModel; using System. text; using System. threading. tasks; namespace Contract {// <summary> // notification announcement // </summary> [ServiceContract] public interface INoticeService {// <summary> // get an announcement information // </summary> [OperationContract] Notice GetNotice ();}}View Code
Notice:
Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; namespace Domain {/// <summary> /// notification announcement /// </summary> public class Notice {/// <summary> /// title /// </ summary> public string Title {get; set ;}//< summary >/// Content //</summary> public string Content {get; set ;} /// <summary> /// Data /// </summary> public byte [] Data {get; set ;}}}View Code
Iii. WcfService and host WcfHost
NoticeService:
Using Contract; using Domain; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; namespace WcfService {/// <summary> /// notification announcement service /// </summary> public class NoticeService: INoticeService {// <summary> /// obtain an announcement. // </summary> public Notice GetNotice () {Notice notice = new Notice (); notice. title = "test Title"; notice. content = "Test Content"; notice. data = new byte [200000]; for (int I = 0; I <200000; I ++) {notice. data [I] = (byte) 100;} return notice ;}}}View Code
Program:
Using System; using System. collections. generic; using System. configuration; using System. IO; using System. linq; using System. serviceModel; using System. text; using System. threading; using System. threading. tasks; using WcfService; namespace WcfServer {class Program {static void Main (string [] args) {string path = AppDomain. currentDomain. baseDirectory + "Service"; string [] fileArr = Directory. getFiles (path); int port = int. parse (ConfigurationManager. appSettings ["ServerPort"]); foreach (string fileName in fileArr) {string url = string. format ("http: // localhost: {0}/Service/{1}", port, Path. getFileName (fileName); Uri [] uri = new Uri [] {new Uri (url)}; ServiceHost host = new ServiceHost (typeof (NoticeService), uri); host. open ();} Console. writeLine ("service started successfully"); Console. read ();}}}View Code
Server App. config Configuration:
<? Xml version = "1.0" encoding = "UTF-8"?> <Configuration> <startup> <supportedRuntime version = "v4.0" sku = ". NETFramework, Version = v4.5 "/> </startup> <etettings> <add key =" ServerPort "value =" 8998 "/> </appSettings> <system. serviceModel> <services> <service name = "WcfService. noticeService "behaviorConfiguration =" ServiceBehavior "> <endpoint contract =" Contract. INoticeService "address =" "binding =" basicHttpBinding "bindingConfiguration =" ServiceBinding "> <identity> <dns value =" localhost "/> </identity> </endpoint> <endpoint address = "mex" binding = "mexHttpBinding" contract = "IMetadataExchange"/> </service> </services> <bindings> <basicHttpBinding> <binding name = "ServiceBinding" sendTimeout =" 00:00:20 "maxcompute edmessagesize =" 2147483646 "> </binding> </strong> </bindings> <behaviors> <serviceBehaviors> <behavior name =" ServiceBehavior "> <serviceMetadata httpGetEnabled =" true ""/> </behavior> </serviceBehaviors> </behaviors> </system. serviceModel> </configuration>View Code
Iv. Client
NoticeProxy:
Using Contract; using Domain; using System. collections. generic; using System. linq; using System. serviceModel; using System. text; using System. threading. tasks; namespace WcfClient {/// <summary> /// notification announcement /// </summary> public class NoticeProxy: INoticeService {private ChannelFactory <INoticeService> channelFactory; private INoticeService server; public NoticeProxy () {channelFactory = new ChannelFactory <INoticeService> ("NoticeService"); server = channelFactory. createChannel () ;}/// <summary> /// obtain an announcement message /// </summary> public Notice GetNotice () {return server. getNotice ();}}}View Code
The client calls the service:
Using Domain; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. linq; using System. text; using System. threading. tasks; using System. windows. forms; using WcfClient; namespace WinformClient {public partial class Form1: Form {public Form1 () {InitializeComponent ();} private void btnTest_Click (object sender, EventArgs e) {try {NoticeProxy proxy = new NoticeProxy (); Notice notice = proxy. getNotice (); MessageBox. show (notice. data. length. toString ();} catch (Exception ex) {MessageBox. show (ex. message );}}}}View Code
Client App. config Configuration:
<? Xml version = "1.0" encoding = "UTF-8"?> <Configuration> <startup> <supportedRuntime version = "v4.0" sku = ". NETFramework, Version = v4.5 "/> </startup> <system. serviceModel> <behaviors> <endpointBehaviors> <behavior name = "ServiceBehavior"> <symbol maxItemsInObjectGraph = "2147483646"/> </behavior> </endpointBehaviors> </behaviors> <bindings <basicHttpBinding> <binding name = "ServiceBinding" maxcompute edmessagesize = "2147483646"> <readerQuotas maxArrayLength = "65242880" maxStringContentLength = "65242880"/> </binding> </basicHttpBinding>/ bindings> <client> <endpoint name = "NoticeService" contract = "Contract. INoticeService "address =" http: // localhost: 8998/Service/NoticeService. svc "behaviorConfiguration =" ServiceBehavior "binding =" basicHttpBinding "bindingConfiguration =" ServiceBinding "/> </client> </system. serviceModel> </configuration>View Code