开发者

Access shared members in partial classes

开发者 https://www.devze.com 2023-02-11 06:36 出处:网络
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.

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. member开发者_如何学编程s 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. :(


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消