访问部分类中的共享成员
我正在编写一个 WCF-Service,并将我的服务类拆分为多个部分类文件,因此每个 ServiceContract-Implementation 都有自己的文件。然而,我有一个文件应该包含每个部分类文件(例如记录器)使用的成员。如果这很重要,则该服务由 IIS 7 托管。
部分类文件 1
<ServiceBehavior(NameSpace:= WCFHelper.SERVICENAMESPACE, AddressFilterMode:= AddressFilterMode.Any)> _
Partial Public Class DataService
#Region "Members"
Private Shared m_Log As log4net.ILog = log4net.LogManager.GetLogger(GetType(DataService))
#End Region
End Class
部分类文件 2 (删除了不必要的代码以保持示例简单)
Partial Public Class DataService
Implements IContractAssets
<SomeCustomAttribute()> _
Public Function GetData(ByVal ID As Int64) As SomeCustomClass Implements IContractAssets.GetData
Try
Return SomeFunction(ID)
Catch ex As Exception
m_Log.Error("SomeError.", ex)
ThrowFault()
End Try
End Function
End Class
代码可以正常编译,但在运行时我得到一个 BC30451: The name m_Log 未声明
错误(不知道确切的单词。我收到一条德语消息;))。我不认为它与 m_Log 或依赖程序集的类型有关,因为如果我用字符串尝试此操作,我会得到相同的错误。
我做错了什么?我怎样才能做到这一点?
编辑:我在一个简单的控制台应用程序中尝试同样的事情,没有任何问题。 :(
I'm writing a WCF-Service and I split up my service class to multiple partial class files, so every ServiceContract-Implementation gets its own file. I have one file however that should contain e.g. members that are used by every partial class file such as a logger. The service is hosted with IIS 7 if this matters in any way.
Partial Class File 1
<ServiceBehavior(NameSpace:= WCFHelper.SERVICENAMESPACE, AddressFilterMode:= AddressFilterMode.Any)> _
Partial Public Class DataService
#Region "Members"
Private Shared m_Log As log4net.ILog = log4net.LogManager.GetLogger(GetType(DataService))
#End Region
End Class
Partial Class File 2 (Snipped unnecessary code to keep the example simple)
Partial Public Class DataService
Implements IContractAssets
<SomeCustomAttribute()> _
Public Function GetData(ByVal ID As Int64) As SomeCustomClass Implements IContractAssets.GetData
Try
Return SomeFunction(ID)
Catch ex As Exception
m_Log.Error("SomeError.", ex)
ThrowFault()
End Try
End Function
End Class
The code compiles fine, but at runtime I get an BC30451: The name m_Log is not declared
Error (Don't know the exact words for it. I get a german message ;) ). I don't think it has something to do with the type of m_Log or a depending assembly because I get the same error if i try this with a String.
What am I doing wrong? How can I make this work?
Edit: I was trying the same thing in a simple console application without any problems. :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最后我弄清楚是什么原因导致了这个错误。
不幸的是,我认为将第二个部分类文件放入
App_Code
目录中是个好主意。嗯,事实并非如此! :) 将文件移至根目录后,一切正常。将部分类文件放在不同的目录中本身似乎不是问题,因为它与另一个子目录一起工作得很好。仅当其中一个文件位于
App_Code
目录(或其他 ASP.NET 文件夹,我没有尝试过)中时,它才不起作用。Finally I figured out what caused this error.
Unfortunately I thought it would be a good idea to put the second partial class file into the
App_Code
directory. Well, it's not! :) After I moved the file to the root directory everything worked fine.Putting the partial class files in different directories seems not to be a problem per se, because it works fine with another subdirectory. It only doesn't work if one of the files is located in the
App_Code
directory (or other ASP.NET folders, I didn't try that).