none
Regarding IDIsposable implementation in Service contract implementation class RRS feed

  • Question

  • Hi,

    I have a servicecontract interface called 

        [ServiceContract] publicinterfaceIService1 {     [OperationContract]     Info GetData(int value);           //[OperationContract]//void Dispose(); }

    and i am providing the implementation in other class

    publicclassService1 : IService1,IDisposable {     publicInfo GetData(int value)     {         returnnewInfo();     }     publicvoid Dispose()     {        reset.Dispose();     }     privateAutoResetEvent reset=newAutoResetEvent(false); } [DataContract] publicclassInfo {     [DataMember]     publicDateTime dt=DateTime.Now; }

    Because in the Service1 class i am using autoresetevent,I have to dispose this type right.

    In this case do i need to provide a operation contract insiode the IServie1 interface

     or the wcf frame work will do the cleanup.Whatever i am doing right now is enough or do i need anything specific.Plz help me

               


           

    Latheesh K Contact No:+91-9747369936


    • Edited by La07K Tuesday, December 12, 2017 9:05 AM
    Tuesday, December 12, 2017 9:04 AM

Answers

  • Hi Latheesh,

    >>but one doubt there is rule called "CA1001: Types that own disposable fields should be disposable"

    For this rule, it is caused when A class declares and implements an instance field that is a System.IDisposable type and the class does not implement IDisposable.

    #CA1001: Types that own disposable fields should be disposable

    https://msdn.microsoft.com/en-us/library/ms182172.aspx

    Do you have any issue with Services like below

        public class Service1 : IService1 , IDisposable
        {
            private AutoResetEvent rest = new AutoResetEvent(false);
    
            public void Dispose()
            {
                rest.Dispose();
            }
    
            public string GetData(int value)
            {
                return string.Format("You entered: {0}", value);
            }
    
            public CompositeType GetDataUsingDataContract(CompositeType composite)
            {
                if (composite == null)
                {
                    throw new ArgumentNullException("composite");
                }
                if (composite.BoolValue)
                {
                    composite.StringValue += "Suffix";
                }
                return composite;
            }
        }
    

    Or

        public class Service1 : IService1 
        {
            private AutoResetEvent rest = new AutoResetEvent(false);      
    
            public string GetData(int value)
            {
                return string.Format("You entered: {0}", value);
            }
    
            public CompositeType GetDataUsingDataContract(CompositeType composite)
            {
                if (composite == null)
                {
                    throw new ArgumentNullException("composite");
                }
                if (composite.BoolValue)
                {
                    composite.StringValue += "Suffix";
                }
                return composite;
            }
        }
    

    Best Regards,

    Tao Zhou


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by La07K Wednesday, December 13, 2017 6:02 AM
    Wednesday, December 13, 2017 5:39 AM

All replies

  • or the wcf frame work will do the cleanup.Whatever i am doing right now is enough or do i need anything specific.Plz help me

    You should remove the Dispose. It is not needed.

    Tuesday, December 12, 2017 12:22 PM
  • Ok...Thanks but one doubt there is rule called "CA1001: Types that own disposable fields should be disposable"

    This will fail right.Now as part of static code analysis says this rule is violating???


    Latheesh K Contact No:+91-9747369936

    Tuesday, December 12, 2017 12:28 PM
  • Ok...Thanks but one doubt there is rule called "CA1001: Types that own disposable fields should be disposable"

    This will fail right.Now as part of static code analysis says this rule is violating???


    Latheesh K Contact No:+91-9747369936

    Where did you get the code or even come up with what you have posted? I think that concerning WCF Operational contracts, you don't see what you are trying to implement with Disposing. I suggest that you look at various WCF tutorials.

    Tuesday, December 12, 2017 3:53 PM
  • Hi Latheesh,

    >>but one doubt there is rule called "CA1001: Types that own disposable fields should be disposable"

    For this rule, it is caused when A class declares and implements an instance field that is a System.IDisposable type and the class does not implement IDisposable.

    #CA1001: Types that own disposable fields should be disposable

    https://msdn.microsoft.com/en-us/library/ms182172.aspx

    Do you have any issue with Services like below

        public class Service1 : IService1 , IDisposable
        {
            private AutoResetEvent rest = new AutoResetEvent(false);
    
            public void Dispose()
            {
                rest.Dispose();
            }
    
            public string GetData(int value)
            {
                return string.Format("You entered: {0}", value);
            }
    
            public CompositeType GetDataUsingDataContract(CompositeType composite)
            {
                if (composite == null)
                {
                    throw new ArgumentNullException("composite");
                }
                if (composite.BoolValue)
                {
                    composite.StringValue += "Suffix";
                }
                return composite;
            }
        }
    

    Or

        public class Service1 : IService1 
        {
            private AutoResetEvent rest = new AutoResetEvent(false);      
    
            public string GetData(int value)
            {
                return string.Format("You entered: {0}", value);
            }
    
            public CompositeType GetDataUsingDataContract(CompositeType composite)
            {
                if (composite == null)
                {
                    throw new ArgumentNullException("composite");
                }
                if (composite.BoolValue)
                {
                    composite.StringValue += "Suffix";
                }
                return composite;
            }
        }
    

    Best Regards,

    Tao Zhou


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by La07K Wednesday, December 13, 2017 6:02 AM
    Wednesday, December 13, 2017 5:39 AM
  • public class Service1 : IService1 , IDisposable { private AutoResetEvent rest = new AutoResetEvent(false); public void Dispose() { rest.Dispose(); } public string GetData(int value) { return string.Format("You entered: {0}", value); } public CompositeType GetDataUsingDataContract(CompositeType composite) { if (composite == null) { throw new ArgumentNullException("composite"); } if (composite.BoolValue) { composite.StringValue += "Suffix"; } return composite; } }

    I dont have any issue with the above implementation and below one also

    public class Service1 : IService1 { private AutoResetEvent rest = new AutoResetEvent(false); public string GetData(int value) { return string.Format("You entered: {0}", value); } public CompositeType GetDataUsingDataContract(CompositeType composite) { if (composite == null) { throw new ArgumentNullException("composite"); } if (composite.BoolValue) { composite.StringValue += "Suffix"; } return composite; } }

    So you are telling both will work fine right?

    or specify which one i have to use???


    Latheesh K Contact No:+91-9747369936

    Wednesday, December 13, 2017 6:02 AM
  • Its just an example i am facing similar kind of issue in my project.I cannot share my project code so i created a sample code.

    Latheesh K Contact No:+91-9747369936

    Wednesday, December 13, 2017 6:05 AM
  • Hi latheesh,

    Could you reproduce your issue with these two simple code?

    I made a test with both code, they works. I assume your issue is not related with Dispose, and you may consider sharing us a simple demo which could reproduce your issue.

    Best Regards,

    Tao Zhou


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, December 13, 2017 6:09 AM