Jop*_*Jop 5 .net c# reflection wcf .net-assembly
我有一个WCF服务,让我们说在IIS中托管的程序集A. WCF服务引用另一个程序集,比如程序集B,它需要访问程序集A的Assembly对象.
原因:我想通过额外的合同和该合同的默认实现来扩展现有的WCF服务,这可以返回WCF服务程序集的FILE VERSION.
请参阅下面的代码:
程序集A(WCF服务):
web.config:除地址"B"下的现有2个端点外,还添加了新端点
<configuration>
<system.serviceModel>
<services>
<service name="ExistingServiceHandler">
<endpoint address="mex" kind="mexEndpoint"/>
<endpoint address="" binding="basicHttpBinding" contract="IExistingContract"/>
<endpoint address="B" binding="basicHttpBinding" contract="AssemblyB.IServiceContract"/>
</service>
...
Run Code Online (Sandbox Code Playgroud)
现有实现:现在扩展程序集B中的"实现".
public class ExistingServiceHandler : Implementation, IExistingContract
{
// Whatever methods are implemented here
}
Run Code Online (Sandbox Code Playgroud)
程序集B(C#类库):
[ServiceContract]
public interface IServiceContract
{
[OperationContract]
string ServiceVersion();
}
public class Implementation : IServiceContract
{
public string ServiceVersion()
{
string assemblyLocation = Assembly.GetEntryAssembly().Location;
FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(assemblyLocation);
string fileVersion = fvi.FileVersion;
return fileVersion;
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试了以下(在程序集B中),但没有一个返回正确的程序集:
Assembly.GetEntryAssembly(); // returns NULL
Assembly.GetExecutingAssembly(); // returns assembly B
Assembly.GetCallingAssembly(); // returns System.ServiceModel
Run Code Online (Sandbox Code Playgroud)
那么,我如何获得程序集"A"(WCF服务)?
注意:
看来Assembly A从未真正调用过Assembly B,而只是扩展了它的类。因此,GetCallingAssembly返回System.ServiceModel(WCF)。我认为您需要this按如下方式检查 的类型:
[ServiceContract]
public interface IServiceContract
{
[OperationContract]
string ServiceVersion();
}
public class Implementation : IServiceContract
{
public string ServiceVersion()
{
string assemblyLocation = this.GetType().Assembly.Location;
FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(assemblyLocation);
string fileVersion = fvi.FileVersion;
return fileVersion;
}
}
Run Code Online (Sandbox Code Playgroud)