ArticleDirectory
- Create a project
- Write a service
- Write host
- Write Client
- Reference
Address: http://www.cnblogs.com/Realh/archive/2012/04/27/2473932.html
A company project requires an app for Android and server interaction, and the simpler the server, the better. It is better not to use IIS.
WCF meets this requirement. Its host can either be hosted in IIS or in a common application.Program.
Let's introduce my work below.
Create a project
Create the following three projects:
Service: A class library project.ServicesAndContracts(Contract) is put in it, note to addSystem. servicemodelAndSystem. servicemodel. WebTwo references.
Host: A command line application that carries the service. Naturally, you must add a reference to the service.
Client: A winform application for interacting with the host to test the service availability.
Write a service
The Service consists of two parts: Service and contract:
View code
[Servicecontract]Public InterfaceIservice {[operationcontract] imageinfo getimageinfo (StringID); [operationcontract] stream getimagecontent (StringID );}
View code
Public Class Service: iservice {[webget (uritemplate = " /Imageinfo/{ID }/ " , Responseformat = Webmessageformat. JSON)] Public Imageinfo getimageinfo ( String ID ){ Return New Imageinfo {ID = ID, name =" Noname " , };} [Webget (uritemplate = " /Imagecontent/{ID }/ " )] Public Stream getimagecontent ( String ID) {weboperationcontext. Current. outgoingresponse. contenttype = " Image/PNG " ; String File = path. Combine (environment. currentdirectory, " Picture.png " ); Return File. openread (File );}}
The definition of complex types is also required. Pay attention to adding the classDatacontractFeature to add attributes to be serializedDatamemberFeatures:
View code
[Datacontract]Public ClassImageinfo {[datamember]Public StringId {Get;Set;} [Datamember]Public StringName {Get;Set;}}
Write host
HostCodeYou can simply start the WCF Service:
View code
Static VoidMain (){Using(VaRHost =NewServicehost (Typeof(Imageservice. Service. Service) {Host. open (); console. writeline ("The service has been started!"); Console. Read ();}}
Then add the configuration file app. config:
View code
<? XML version = "1.0" ?> < Configuration > < System. servicemodel > < Services > < Service Name = "Imageservice. Service. Service" > < Host > < Baseaddresses > < Add Baseaddress = "Http: // MAID: 8888 /" /> </ Baseaddresses > </ Host > < Endpoint Address = "" Binding = "Webhttpbinding" Contract = "Imageservice. Service. iservice" Behaviorconfiguration = "Behavior" > </ Endpoint > </ Service > </ Services > < Behaviors > < Endpointbehaviors > < Behavior Name = "Behavior" > < Webhttp /> </ Behavior > </ Endpointbehaviors > </ Behaviors > </ System. servicemodel > < Startup > < Supportedruntime Version = "V4.0" SKU = ". Netframework, version = v4.0" /> </ Startup > </ Configuration >
Note: Fill in the complete service class name and contract Interface Name:
Write Client
The client has no difficulty, that is, communication with the host. Directly paste the Code as follows:
View code
void btngetimageinfo_click (Object sender, eventargs E) {var DATA = getdata ("/imageinfo/1/"); var S = encoding. default. getstring (data); imageinfo. TEXT = s;} void btngetimagecontent_click (Object sender, eventargs e) {var DATA = getdata ("/imagecontent/1/"); Using (var ms = new memorystream (data )) {var image = new Bitmap (MS); imagecontent. image = image;} byte [] getdata (string URI) {var request = (httpwebrequest) webrequest. create ("http: // 127.0.0.1: 8888" + URI); var response = request. getresponse (); Using (var ms = new memorystream () {var buffer = new byte [1, 10240]; using (var ns = response. getresponsestream () {int I = NS. read (buffer, 0, buffer. length); While (I> 0) {Ms. write (buffer, 0, I); I = NS. read (buffer, 0, buffer. length) ;}} return Ms. toarray () ;}
Last,
Reference
My WCF journey (1): create a simple WCF Program
Call restful WCF services Asynchronously
Call WCF for upload and download in restful Mode
Summary of WCF deployment issues (solutions and deployment of BAT scripts that cannot be registered by HTTP)