WCF获取入口程序集

5
我有一个WCF服务,假设它的程序集名为A,它托管在IIS中。这个WCF服务引用了另一个程序集B,B需要访问A程序集的Assembly对象。
原因:我想通过额外的契约和默认实现来扩展现有的WCF服务,该实现可以返回WCF服务程序集的文件版本。
请看我的代码:
程序集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>
      ...

现有实现方式:现在从程序集B扩展‘Implementation’。
public class ExistingServiceHandler : Implementation, IExistingContract
{
    // Whatever methods are implemented here
}

汇编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;
    }   
}

我已经尝试了以下内容(在汇编B中),但是它们都没有返回正确的汇编代码:

Assembly.GetEntryAssembly(); // returns NULL
Assembly.GetExecutingAssembly(); // returns assembly B
Assembly.GetCallingAssembly(); // returns System.ServiceModel

那么,我该如何获取程序集'A'(WCF服务)的Assembly?

注意:

  • 我希望尽可能少地使用来自程序集B的契约+实现。这意味着我不想修改现有的服务实现,除了让类扩展我的新契约的默认实现(程序集B中的实现)。
  • 谢谢!

汇编语言 A 如何调用汇编语言 B?请发布一个示例代码来说明您的问题。 - Wagner DosAnjos
我用样例代码澄清了我的问题,希望这有所帮助。 - Jop
你尝试过 var assemblyLocation = this.GetType().Assembly.Location 吗? - Kris Vandermotten
2个回答

1

看起来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;
    }   
}

0
通常情况下,你不应该被迫使用反射和 Assembly.GetSomething 方法。难道你不能通过一些共享接口(契约)的程序集,将程序集 A 中的某些对象注入到程序集 B 的方法中吗?这里有一个非常粗糙的解决方案:

程序集 A

class Service
{
    public void ServiceMethod1()
    {
        // Here you create type from Assembly B but inject object from Assembly A
        var obj = new SomeClass(new SomeSharedClass()); 

    }
}

class SomeSharedClass : ISomeSharedContract
{
    // SomeMethod implementation
}

汇编语言 B
class SomeClass
{
    private ISomeSharedContract dependency;
    public SomeClass(ISomeSharedContract dependency)
    {
        this.dependency = dependency;
    }

    private HereYourMethod(...)
    {
        // ...
        this.dependency.SomeMethod(); // Here you gain access to Assembly A object
        // ...
    }
}

汇编C - 接口
interface ISomeSharedContract
{
    void SomeMethod();
}

您可以通过使用一些常见的依赖注入容器(如Unity、ninject或Windsor Castle)来扩展此示例...


这并没有回答我的问题。我想从引用程序集B中获取程序集A的Assembly对象。 - Jop
那么注入Assembly对象本身? - Konrad Kokosa
我不想改变汇编A中现有的实现,除了让它从我的默认实现中扩展。请参见我的更新代码示例。我希望能够使用汇编B来扩展多个WCF服务。 - Jop

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接