Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WCF获取入口程序集_C#_.net_Wcf_Reflection_.net Assembly - Fatal编程技术网

C# WCF获取入口程序集

C# WCF获取入口程序集,c#,.net,wcf,reflection,.net-assembly,C#,.net,Wcf,Reflection,.net Assembly,我有一个WCF服务,比如说程序集a,它托管在IIS中。WCF服务引用了另一个程序集,比如说程序集B,它需要访问程序集a的程序集对象 原因:我想用额外的契约和该契约的默认实现扩展现有WCF服务,该契约可以返回WCF服务程序集的文件版本。 请参见下面的我的代码: 组件A(WCF服务): [ServiceContract] public interface IServiceContract { [OperationContract] string ServiceVersion(); }

我有一个WCF服务,比如说程序集a,它托管在IIS中。WCF服务引用了另一个程序集,比如说程序集B,它需要访问程序集a的程序集对象

原因:我想用额外的契约和该契约的默认实现扩展现有WCF服务,该契约可以返回WCF服务程序集的文件版本。

请参见下面的我的代码:

组件A(WCF服务):

[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;
    }   
}
web.config:除了地址“B”下现有的2个端点之外,还添加了一个新端点

组件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服务)的程序集?

注:

  • 我想使用程序集B中的契约+实现,只需最少的努力。这意味着我不想改变现有的服务实现,除了让类从新契约的默认实现(程序集B中的实现)扩展之外。 提前谢谢
      正常的方式不应该强迫您使用反射和
      Assembly.GetSomething
      方法。您不能将程序集A中的某些对象注入到具有某些共享接口(契约)程序集的程序集B方法中吗?下面是一个非常粗略的解决方案:

      大会A

      大会B

      汇编C-接口


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

      似乎程序集A从未真正调用程序集B,只是扩展了它的类。因此,
      GetCallingAssembly
      返回
      System.ServiceModel
      (WCF)。我认为您需要检查
      的类型,如下所示:

      [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;
          }   
      }
      

      程序集A如何调用程序集B?请发布一个示例代码来说明您的问题。我用示例代码澄清了我的问题,希望能有所帮助。您是否尝试过
      var assemblyLocation=this.GetType().Assembly.Location
      ?这并没有回答我的问题。我想从引用的程序集B中获取程序集A的程序集对象。然后注入程序集对象本身?我不想更改程序集A中的现有实现,只想让它从默认实现扩展。请参阅我更新的代码示例。我希望能够使用程序集B扩展多个WCF服务。
      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
      }
      
      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
              // ...
          }
      }
      
      interface ISomeSharedContract
      {
          void SomeMethod();
      }
      
      [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;
          }   
      }