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?
IService1.cs
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);
}
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; }
}
}
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;
}
}
}
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:
Then, add this to IService1.cs:
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.
[OperationContract]
string GetData(int value);
[OperationContract]
CompositeType GetDataUsingDataContract(CompositeType composite);
// TODO: Add your service operations here
Then, add this to IService1.cs:
[OperationContract]
void WriteFile(string filePath, string fileName);
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.
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
Service1.cs
IPlugin.cs <--- C# Class Library
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);
}
}
Service1.cs
public void WriteFile(string filepath, string filename)
{
Console.Write("File info: " + filepath + filename);
}
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.*********
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I was trying to follow this example here:
https://xrmcenter.wordpress.com/2016/03/27/request-for-the-permission-of-type-system-net-webpermission-call-wcf-in-plugin-with-isolation-mode/
https://xrmcenter.wordpress.com/2016/03/27/request-for-the-permission-of-type-system-net-webpermission-call-wcf-in-plugin-with-isolation-mode/