Link to home
Start Free TrialLog in
Avatar of teknovation
teknovation

asked on

C# how to get my interface working?

Hi everyone, can someone tell me how I can properly get this to work? I'm having some trouble with WCF Web service using Interface.

Here's the solution structure:
- IPlugin.cs (contains the IService and Service1 classes) <--- service library
- OrderLibrary.cs (is a class library which will need to reference one of the methods (writefile) in the IPlugin.cs class)

Currently, the OrderLibrary is using a Reference (Microsoft...dll) so the public class header of the code looks like this:
public class Plugin : IPlugin
{
..............
}

I guess the part that's confusing is the function, I'm creating is a void.

This is the method I want to add to my code, but not sure how to add it. can someone help?
 void WriteFile(string filePath, string fileName)
        {
            if (File.Exists(fileName))
                File.Create(filePath + "\\" + fileName + DateTime.Now.ToShortDateString());
            else
                File.Create(filePath + "\\" + fileName);
        }

Open in new window

   

IService1.cs
  public interface IService1
    {
        [OperationContract]
        string GetData(int value);

        [OperationContract]
        CompositeType GetDataUsingDataContract(CompositeType composite);

        // TODO: Add your service operations here
         
   }
 public class CompositeType
    {
        bool boolValue = true;
        string stringValue = "Hello ";

        [DataMember]
        public bool BoolValue
        {
            get { return boolValue; }
            set { boolValue = value; }
        }

        [DataMember]
        public string StringValue
        {
            get { return stringValue; }
            set { stringValue = value; }
        }
       
   }
    
   

Open in new window


 public class Service1 : IService1
    {
        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;
        }

        public writeFileClass wf(writeFileClass wf)
        {

            return wf;
        }

      
         
    
    } 
    



    }
 

Open in new window

Avatar of Easwaran Paramasivam
Easwaran Paramasivam
Flag of India image

Why your interface does not have [ServiceContract] attribute and Class does not have [DataContract] attribute? To add the method, what problem you face?
First, remove the unnecessary code from your interface and service class. For instance, you don't need this in IService1.cs:
[OperationContract]
        string GetData(int value);

        [OperationContract]
        CompositeType GetDataUsingDataContract(CompositeType composite);

        // TODO: Add your service operations here

Open in new window


Then, add this to IService1.cs:
[OperationContract]
        void WriteFile(string filePath, string fileName);

Open in new window


Then, add your method to the Service class which implements IService1.cs.

It's a good practice to change the names of IService1 and Service1 to something more meaningful.
Avatar of teknovation
teknovation

ASKER

Thanks, I did all of that but I still cannot reference the OrderLibrary namespace from the IPlugin.cs.

Error Message:
cannot create instance of the abstract class or interface OrderLibrary.IService1

Thoughts?

My new code is this:

OrderLibrary.cs  <---- WCF service reference
IService1.cs
 
namespace OrderLibrary
{
 public interface IService1 
    {
        

        // TODO: Add your service operations here

      [OperationContract]
        void WriteFile(string filepath, string filename);
         
   }
}
    

Open in new window


Service1.cs
 public void WriteFile(string filepath, string filename)
        {
            Console.Write("File info: " + filepath + filename);

        }

Open in new window


IPlugin.cs  <--- C# Class Library
 namespace IPluginsNamespace
{
    public class MainPlugin : IPlugin  
    {
        
        //Entry point for plugins
        public void Execute(IServiceProvider serviceProvider)
        {
OrderLibrary.IService1 filewrite = new OrderLibrary.IService1();
filewrite.    ***** this is where I cannot locate the WriteFile(string filepath, string filename) in the OrderLibrary namespace.*********
}
}
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Jeevan Bordoloi
Jeevan Bordoloi
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial