70 lines to implement 'hosting a WCF Service in a console application'

Source: Internet
Author: User

Background: MS has something called CCF (I remember more than one person asked me why I have never heard of it... in fact, it is hidden deep by Ms). It is an Enterprise LibrarySome functions of the C/S architecture... DesktopProgramPrototype.
Can you imagine that this program is to deploy and debug the client in a different environment than the development machine like SharePoint? Now, I will give you a chance to think about it.
Can you endure a Windows program that has a longer debugging time than a web program? (Difficult for individuals)
Is there a solution?
I'm wondering...
The following is a title-related content. I don't want you to know it's 70 rows.CodeThere is nothing special about each sentence.

1 Using System;
2 Using System. configuration;
3 Using System. diagnostics;
4 Using System. IO;
5 Using System. LINQ;
6 Using System. reflection;
7 Using System. servicemodel;
8 Using System. servicemodel. configuration;
9
10 Namespace Eclipse. CCF
11 {
12 Class Program
13 {
14 Static   Void Main ( String [] ARGs)
15 {
16 Loadassembliesinexecutingdirtory ();
17 VaR hosttypelist = From serviceendpointelement endpoint
18 In
19 (From serviceelement Service In
20 (Servicessection)
21 Configurationmanager. getsection ( " System. servicemodel/services " ). Services
22 Let endpoints = Service. endpoints
23 Select (from serviceendpointelement endpoint2 In Endpoints
24 Where Endpoint2.address. originalstring. startswith ( " HTTP " )
25 Select endpoint2). First ())
26 Select New {Type = Findtypeinrefassemblies (endpoint. Contract), Uri = Endpoint. Address };
27 Foreach (VAR hosttype In Hosttypelist)
28 {
29 VaR servicehost =   New Servicehost (hosttype. type, hosttype. Uri );
30 Servicehost. open ();
31 }
32 Console. readkey ();
33 }
34
35 Static   Void Loadassembliesinexecutingdirtory ()
36 {
37 VaR assemblies = Appdomain. currentdomain. getassemblies ();
38 Foreach (VAR path In Directory. getfiles (appdomain. currentdomain. setupinformation. applicationbase ))
39 {
40 If (Path. getextension (PATH). Equals ( " . Dll " , Stringcomparison. currentcultureignorecase ))
41 {
42 If ( ! Assemblies. Any (ass => Ass. getname (). Name = Path. getfilenamewithoutextension (PATH )))
43 Try {
44 Assembly. LoadFile (PATH );
45 }
46 Catch {Debug. Fail ( " Loaded an unknown DLL " );}
47 }
48 }
49 }
50
51 Static Type findtypeinrefassemblies ( String Typename)
52 {
53 Type type =   Null ;
54
55 Debug. Assert (appdomain. currentdomain. getassemblies (). Any (ass =>
56 {
57 If ( ! Ass. fullname. startswith ( " Microsoft. CCF. CSR. WebServices " ))
58 Return   False ;
59 Type = Ass. GetType (typename );
60 Return Type ! =   Null ;
61 }));;
62 Return Type;
63 }
64 }
65 }
66
67
68
69
70

in terms of weakness, there are actually no 70 rows. In order to generate an integer, I intentionally add a few spaces behind my ass.

in this way, the WCF originally built under IIS can be successfully hosted in a console program. How can I use a SVC to try it? Very successful. You can open the champagne.
although the type in the assembly of a row of WS is not used, the IDE will automatically copy the related assembly to the target output for reference, so that the reference relationship can be very clear.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.