Why can you publish private methods publicly in the WCF service?

Why can we put the [ OperationContract] attribute in private methods in wcf services. From the very beginning of my programming, I taught private methods - those that are not available outside the class. Now in the WCF service, you can publish a private method publicly.

    [ServiceContract]
    public class MyServices
    {
        [OperationContract]
        private int add(int a,int b)
        {
            return a + b;
        }
    }
+4
source share
3 answers

Not sure why it is designed this way, but if you check the source, line 336 says

internal const BindingFlags ServiceModelBindingFlags = BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance;

Pay attention to the NonPublic flag in particular. This is then used later on at line 678 when the WCF uses reflection to figure out which methods to expose:

   static List<MethodInfo> GetMethodsInternal(Type interfaceType)
   {
            List<MethodInfo> methods = new List<MethodInfo>();
            foreach (MethodInfo mi in interfaceType.GetMethods(ServiceModelBindingFlags))
            {
                if (GetSingleAttribute<OperationContractAttribute>(mi) != null)
                {
                    methods.Add(mi);
                }
                ...

, WCF , NonPublic.

http://referencesource.microsoft.com/#System.ServiceModel/System/ServiceModel/Description/ServiceReflector.cs,c1358e6f97071bfa

+6

. , , . WCF , OperationContract . , . , WCF, slide no, 36.

MSDN WCF, , OperationContract . , , , .

ServiceContract OperationContract , :

[ServiceContract]
public interface IMyService 
{
    [OperationContract]
    string GetData();
}

public class MyService : IMyService 
{
    public string GetData() { ... }
    private string ComputeData() { ... } // this one is not visible to clients
}
+4

2 :

SOAP, WCF, #. "" - , #. Service, OperationContracts ( ), , SOAP. , , , SOAP-, WCF OperationContract DataMember .

DataMember of DataContract:

[DataMember]
private int _personAge;
0

Source: https://habr.com/ru/post/1568193/


All Articles