2015-03-28 3 views
0

У меня есть служба ASP.NET asmx, которая имеет AssemblyInfo.cs внутри папки «Свойства». У меня возникли проблемы с доступом к информации внутри этого файла.Как получить информацию о сборке из DLL

Мое предположение было то, что я должен быть в состоянии назвать

Assembly.GetExecutingAssembly(); 

, а затем получить информацию из этого файла, однако я получить некоторые другие собрания. Чтобы решить эту проблему, я переместил свои атрибуты сборки из этого файла на мою страницу Global.asax.cs. После выполнения этого вызова указанная выше строка вернула мне ожидаемые значения.

1. Нужно ли мне это делать, или я только что посмотрел что-то относительно файла AssemblyInfo.cs?

На этом веб-сайте также есть DLL, которая пытается использовать информацию из той же AssemblyInfo.cs. Я не могу понять, как получить нужную мне информацию из DLL.

Assembly.GetEntryAssembly() = null 
Assembly.GetCallingAssembly() = mscorlib 
Assembly.GetExecutingAssembly() = DLL Assembly 

2. Как я могу получить информацию по сборке сайта?

using System.Runtime.InteropServices; 
using GatewayProtocol; 
using Steno; 
[assembly: Guid("3d5900ae-aaaa-bbbb-cccc-d9e4606ca793")] 
//If I remove the above line, the below Debug line **does not** return the expected Guid 

namespace GWS 
{ 

public class Global : System.Web.HttpApplication 
{ 
    protected void Application_Start(object sender, EventArgs e) 
    { 
     Logger.Init(); 

     var assembly = Assembly.GetExecutingAssembly(); 
     var attribute = (GuidAttribute)assembly.GetCustomAttributes(typeof(GuidAttribute), true)[0]; 
     Debug.WriteLine(attribute.Value) //3d5900ae-aaaa-bbbb-cccc-d9e4606ca793 
    } 
} 

}

Внутри Logger.Init() начинается нить с петлей чтения очереди

var guid = ((GuidAttribute)Assembly.GetExecutingAssembly.GetCustomAttributes(typeof(GuidAttribute), true)[0]).Value; 

Это возвращает идентификатор GUID в DLL, который не то, что я хочу ,

ответ

0

Это работает для меня:

AssemblyInfo.cs в веб-приложения

[assembly: Guid("df21ba1d-f3a6-420c-8882-92f51cc31ae1")] 

Global.asax.cs

public class Global : HttpApplication 
{ 
    void Application_Start(object sender, EventArgs e) 
    { 
     Logger.Init(); 
     string assemblyName = Logger.AssemblyGuid; // to test under debug 
    } 
} 

WebService1.asmx.cs

namespace WebApplicationSO2 
{ 
    [WebService(Namespace = "http://tempuri.org/")] 
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
    [System.ComponentModel.ToolboxItem(false)] 
    public class WebService1 : System.Web.Services.WebService 
    { 

     [WebMethod] 
     public string HelloWorld() { 
      Assembly a = Assembly.GetExecutingAssembly(); 
      var attribute = (GuidAttribute)a.GetCustomAttributes(typeof(GuidAttribute), true)[0]; 
      var guidFromDll = Logger.AssemblyGuid; 
      return "My Guid: " + attribute.Value + " Guid from Dll: " + guidFromDll; // returns 'My Guid: df21ba1d-f3a6-420c-8882-92f51cc31ae1 Guid from Dll: df21ba1d-f3a6-420c-8882-92f51cc31ae1' 

     } 
    } 
} 

Dll:

namespace ClassLibrary1 
{ 
    public class Logger 
    { 
     public static string AssemblyGuid; 

     public static void Init() { 
      Assembly a = Assembly.GetCallingAssembly(); 
      var attribute = (GuidAttribute)a.GetCustomAttributes(typeof(GuidAttribute), true)[0]; 
      AssemblyGuid = attribute.Value; 
     } 
    } 
}