string currExecutePath=Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
跟踪的文件路径为:C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\vancltpsservicev2\9f9417c6\8a86d3d0\assembly\dl3\17f505d3\f2fe9322_45b0cd01
这当然不是想要的,因为wcf发布的路径为:C:\inetpub\wwwroot\WCFServiceV2 。
第二次采用如下编码:
currExecutePath= System.Web.HttpContext.Current.Server.MapPath("~");结果报异常:空引用对象。很可能是当前上下文环境未找到,为null而引起的。经查相关资料得知: “当没有开启 ASP.NET相容模式 时( aspNetCompatibilityEnabled ="true"),WCF不支持HttpContext, HttpApplication,大部分文章都建议修改<serviceHostingEnvironment aspNetCompatibilityEnabled="true">使WCF可使用ASP.NET传统做法。”
What a baby!
参考 了相关资料,解决方案如下:
1.在WCF服务端使用HttpContext.Current为空的解决方案:
1)在服务端WCF的类上加描述[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
2)在服务端WEBCONFIG的<system.serviceModel>节点里加<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
2.在WCF服务端使用HttpContext.Current.Server.MapPath的替代解决方案:
System.Web.Hosting.HostingEnvironment.MapPath("~/Upload" + fileFolder);
使用currExecutePath = System.AppDomain.CurrentDomain.BaseDirectory;也是可以的,与代码
currExecutePath = System.Web.Hosting.HostingEnvironment.MapPath("~");相比,前者在尾巴处多了个路径分割符合:‘\’;即
AppDomain.CurrentDomain.BaseDirectory => C:\inetpub\wwwroot\WCFServiceV2\Hosting.HostingEnvironment.MapPath=> C:\inetpub\wwwroot\WCFServiceV2