Get SvcHost assembly location (path) using SvcImplementation assembly

。_饼干妹妹 提交于 2020-01-16 05:47:27

问题


I have windows 8.1 x64, Visual Studio 2012 x86, IIS 8.5.

I have Wcf Service Host project (contains svc file)

<%@ ServiceHost Language="C#" Debug="true" Service="WcfServiceImplementation.MyService"  %>

I have Wcf Service implementation project.

Wcf Service hosted in IIS

I have 2 assemblies: WcfService.Host.dll and WcfServiceImplementation.dll

This code in WcfServiceImplementation.dll:

1)

Assembly.GetEntryAssembly()

gets null.

2)

Assembly.GetExecutingAssembly()

gets WcfServiceImplementation.

3)

Assembly.GetCallingAssembly()

gets System.ServiceModel.

How can I get Service.Host assembly location programatically in the code that execute WcfServiceImplementation assembly?

In general, for any Wcf Service or Web Application, how get the "main assembly" (the wcf service host, the web application) ?

My trace (log):

ProcessName: w3wp
Is64BitOperatingSystem: True
Is64BitProcess: True
SystemConfigurationFile: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.config
ConfigurationFile: C:\TFS\Tests\WcfService\web.config
MainModule.FileName: c:\windows\system32\inetsrv\w3wp.exe
MainModule.ModuleName: w3wp.exe
BaseDirectory: C:\TFS\Tests\WcfService\
ApplicationBase: C:\TFS\Tests\WcfService\
FriendlyName: /LM/W3SVC/2/ROOT-5-130404032112324911
ApplicationName: c36153ea



Code in WcfServiceImplementation assembly:

Assembly.GetEntryAssembly():
GetAssemblyExtendedData. Assembly null

Assembly.GetExecutingAssembly():
Location: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\2090c609\c36153ea\assembly\dl3\92d3ec19\b0fa1582_c649cf01\WcfServiceImplementation.dll
ProcessorArchitecture: MSIL

Assembly.GetCallingAssembly():
Location: C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.ServiceModel\v4.0_4.0.0.0__b77a5c561934e089\System.ServiceModel.dll
ProcessorArchitecture: MSIL

Suggestions ?


回答1:


This one worked for me in WCF service:

AppDomain.CurrentDomain.RelativeSearchPath

For example:

string certFilePath = AppDomain.CurrentDomain.RelativeSearchPath + "\" + CERTIFICATES_DIRECTORY_NAME + "\" + CERTIFICATE_FILE_NAME;

You can see the files are copied under sub-directory and these files in the solution are marked as build-action "Copy to output directory" which copies directory as well as files (which I wanted for my case, otherwise use resources as the link in the project).



来源:https://stackoverflow.com/questions/22691426/get-svchost-assembly-location-path-using-svcimplementation-assembly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!