70 lines to Implement 'Hosting a WCF Service in a Console Application'
Posted on 2008-04-25 16:06 A.Z 阅读(1656) 评论(6) 编辑 收藏 举报背景:ms有一个叫CCF的东西(记得不止一个人问我怎么没有听说过...其实它被ms藏的很深),是一个用到了Enterprise Library
你能想象这个程序要像sharepoint一样的在不同于开发机的环境去做客户端的部署和调试吗,现在给你一个尽情联想的机会。
你能忍受一个调试时间比web程序还要长的windows程序吗?(个人比较困难)
有没有解决方案?
我正在琢磨...
以下是一个和标题有关的内容,我想大家不用想也知道是70行代码,没有什么特别的地方,每一句都有人看见过。
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
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
弱弱的说,其实这里是没有70行的,为了凑个整数故意在屁股后面补了几个空格。
这样本来构建在IIS下面的WCF已经可以顺利地在一个控制台程序中寄宿,用一个svc试试?非常的成功,可以开香槟了。
注意虽然没有用到一行WS的程序集内的类型,引用的话IDE会自动把相关程序集帮您COPY到目标输出,这样可以非常的明确引用关系。