Com class not showing main interface - com-interop

I have an interface and a class in the tyle ibrary that is produced the interface appears and so does the class but the class has no methods exposed on it. so I cannot create an Application object in say VBA in Microsoft Word and call the methods on it, does anyone know what is wrong?
[ComVisible(true), Guid("261D62BE-34A4-4E49-803E-CC3294613505")]
public interface IApplication
{
[DispId(207)]
[ComVisible(true)]
IExporter Exporter { get; }
[DispId(202)]
[ComVisible(true)]
object CreateEntity([In] kEntityType EntityType, [In] object aParent);
[DispId(208)]
[ComVisible(true)]
string GenerateSpoolFileSpec();
}
[ComVisible(true), Guid("BA7F4588-0B51-476B-A885-8E1436EA0768")]
public class Application : IApplication
{
protected Exporter FExporter;
public Application()
{
FExporter = new Exporter();
}
[DispId(207)]
[ComVisible(true)]
public IExporter Exporter
{
get {return FExporter;}
}
[DispId(202)]
[ComVisible(true)]
public object CreateEntity([In] kEntityType EntityType, [In] object aParent)
{
switch (EntityType)
{
case TypeJob:
return new Job(this, aParent);
case kappEntityType.kappEntityTypePage:
return new Page(this, aParent);
}
return null;
}
[DispId(208)]
[ComVisible(true)]
public string GenerateSpoolFileSpec()
{
string path = string.Format(JOB_PARAMS_PATH_SKELETON, SpoolFolder, DateTime.Now.ToString("yyyy.MM.dd.hh.mm.ss.fff"));
return path;
}
}

Got it, don’t let dotnet handle it for you on the interface put an interfacetype e.g.
[ComVisible(true), Guid("261D62BE-34A4-4E49-803E-CC3294613505"), InterfaceType(ComInterfaceType.InterfaceIsDual)]
On the class use a classinterface e.g
[ComVisible(true), Guid("BA7F4588-0B51-476B-A885-8E1436EA0768"), ClassInterface(ClassInterfaceType.None)]

Related

Unable to resolve service for type with our own class

We are getting this exception when calling a web api controller:
InvalidOperationException: Unable to resolve service for type 'SDS.Lambda.Interfaces.ISecretManager' while attempting to activate 'SDS.Lambda.Controllers.SapController'.\r\n <p class="location">Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy, bool isDefaultParameterRequired)
StartUp.cs contains the following:
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<ISecretManager, SecretManager>();
}
The controller has this constructor:
public class SapController : Controller
{
public SapController(ISecretManager secretManager)
{
_secretManager = secretManager;
}
}
We have the same issue with other types being injected into the constructor, but the IConfiguration instance can be injected, for example that parameter does not cause an exception:
public SapController(IConfiguration configuration, ISecretManager secretManager)
The ISecretManager interface looks like this (yes, it really does):
namespace SDS.Lambda.Interfaces
{
public interface ISecretManager
{
}
}
And the class (yes, really - I reduced it down to avoid complexity):
namespace SDS.Lambda.Interfaces
{
public class SecretManager : ISecretManager
{
}
}
Are we providing the interface/concrete type incorrectly?
Is there a way to retrieve the concrete type to test whether it has been provided properly?
When execution reaches the bottom of ConfigureServices, if we look at the services instance result enumeration, in the debugger view, the types we are injecting are listed, so we can't see why they are failing to be instantiated.
UPDATE
To elaborate and explain the issue with another class/dependency in the same solution:
Controller:
namespace SDS.Lambda.Controllers
{
[Route("api/[controller]")]
public class SapController : Controller
{
readonly IHelper helper;
public SapController(IHelper helpme)
{
helper = helpme;
}
...
}
Interface:
namespace SDS.Lambda.Interfaces
{
public interface IHelper
{
}
}
Class:
namespace SDS.Lambda.Helpers
{
public class Helper : IHelper
{
public Helper()
{
}
}
}
StartUp:
namespace SDS.Lambda
{
public class Startup
{
public static IConfiguration Configuration { get; private set; }
private readonly AppSettings _appSettings;
public Startup(IConfiguration configuration)
{
Configuration = configuration;
_appSettings = configuration.GetSection("AppSettings").Get<AppSettings>();
}
public void ConfigureServices(IServiceCollection services)
{
services.AddLogging(logger => logger.AddLambdaLogger());
services.AddSingleton<IHelper, Helper>();
services.AddControllers();
}
...
}

Add interfaces to groovy enums?

I am unable to add interfaces to groovy enums.
example:
interface DeviceType.groovy
public interface DeviceType{
public String getDevice()
}
enum Device.groovy
public enum Devices implements DeviceType {
PHONE{
public String getDevice(){
return "PHONE"
}
}, ALARM {
public String getDevice(){
return "ALARM"
}
}
}
Simple Test
public class MainTest(){
public static void main(String [] args) {
System.out.println(Devices.PHONE.getDevice());
//should print phone
}
}
This is pseudo code, but a pretty good example.
When I use it with Groovy, I get an error from IntelliJ that I need to make the interface abstract.
If I make it abstract, maven won't compile saying it can't be both static and final.
Any tips?
You need to define getDevice() in the enum. Then you can override it, like this:
enum Device.groovy
public enum Devices implements DeviceType {
PHONE{
public String getDevice(){
return "PHONE"
}
}, ALARM {
public String getDevice(){
return "ALARM"
}
};
public String getDevice(){
throw new UnsupportedOperationException();
}
}
Since an enum is a class, and your class is implementing the interface, it needs to implement the function. Right now what you have is an enum that's NOT implementing the function, whose instances are each subclasses that do have a function of the same name. But since the enum itself doesn't have it, that's not good enough.
I'd like to offer my preferred syntax for a situation such as this:
public enum Devices implements DeviceType {
PHONE("PHONE"), ALARM("ALARM")
private final String devName
public String getDevice() { return devName }
private Devices(devName) { this.devName = devName }
}
Or, if the "device" is always going to match the name of the enum instance, you might as well just return that:
public enum Devices implements DeviceType {
PHONE, ALARM
public String getDevice() { return name() }
}

How Get data from a derived class?

Get data from a derived class..
My samples.
public class Maintest
{
public string name = "2";
}
public class test : Maintest
{
string bla = name;
}
or
public class test : Maintest
{
test child = new test();
string bla = child.name;
}
Please reply
or
Share a link to explore
For example there is the main class
and I have a derived class that will output data of the first class.
As an example, I just wanted to pass the value of the derivative in the main class. For a proper understanding
If you return the field from a property, it might look a little something like this.
using System;
public class Program
{
public void Main()
{
var test = new Test();
Console.WriteLine(test.greeting);
}
}
// make this abstract if you're never directly instantiate MainTest
public abstract class MainTest
{
public string name = "world";
}
public class Test : MainTest
{
public string greeting {get { return "Hello " + name;}}
}
http://dotnetfiddle.net/R8kwh3
Also, you can enforce a contract by doing something like
public abstract class MainTest
{
public string name = "world";
// create an abstract property to ensure it gets implemented in the inheriting class
public abstract string greeting {get; private set;}
}
public class Test : MainTest
{
public override string greeting {get { return "Hello " + name;}}
}
Maybe you want get data in method? Therefore, you can use this:
public class test : Maintest
{
public string GetData()
{
return name;
}
}

Service isn't implementing interface member noobie

I am re engineering an MVC3 app to take all linq out of controllers and in to proper layers.
I have got this as my structure SQL --> EF --> Repository --> Service --> Controller. I am using interfaces.
When compiling I am getting this error:
gpc.data.service.roleService does not implement interface member gpc.data.interfaces.iroleservice.HolderNamesbyRoleID(int).
I am totally new to proper architecture so apologies if this is blindingly obvious lol. Here is some code:
Repository:
namespace gpc.Data.Repositories
{
public class roleRepository :gpc.Data.Interfaces.IRoleRepository
{
private gpc.Models.gpcEntities _entities = new Models.gpcEntities();
public HolderNames HolderNamesbyRoleID(int roleid)
{
return (from i in _entities.HolderNames
where i.roleid == roleid select i).FirstOrDefault();
}
}
}
I then have an interface:
namespace gpc.Data.Interfaces
{
public interface IRoleRepository
{
HolderNames HolderNamesbyRoleID(int roleid);
}
}
Then I have the service:
namespace gpc.Data.Service
{
public class roleService : gpc.Data.Interfaces.IRoleService
{
private ModelStateDictionary _modelState;
private gpc.Data.Interfaces.IRoleRepository _repository;
public roleService(ModelStateDictionary modelState)
{
_modelState = modelState;
_repository = new gpc.Data.Repositories.roleRepository();
}
public roleService(ModelStateDictionary modelState,
gpc.Data.Repositories.roleRepository repository)
{
_modelState = modelState;
_repository = repository;
}
public HolderNames HolderNames(int roleid)
{
return _repository.HolderNamesbyRoleID(roleid);
}
}
}
I then have another interface:
namespace gpc.Data.Interfaces
{
public interface IRoleService
{
HolderNames HolderNamesbyRoleID(int roleid);
}
}
I created a very simple ienumerable in this structure and I was able to get data on to the view through the controller as i would expect. I guess that as this one is a bit more complicated that a select everything and throw it at a view I must have missed something. I don't know if it makes a difference, but "holdernames" is a SQL view as opposed to a table.
Any help greatly appreciated
It's basically just what your compiler error shows. Your IRoleService interface defines a method named HolderNamesbyRoleID, but in your implementation you only have a method named HolderNames.
I assume this is just a mistype on your part.
Interface only contains the signature. You have to write actual implementation in class where you have implemented your interface. In your case you have define method HolderNamesbyRoleID in IRoleRepository but you have not implemented this method in roleService class. You must have to implement HolderNamesbyRoleID in roleService class.
Your roleService class code becomes like below.
namespace gpc.Data.Service
{
public class roleService : gpc.Data.Interfaces.IRoleService
{
private ModelStateDictionary _modelState;
private gpc.Data.Interfaces.IRoleRepository _repository;
public roleService(ModelStateDictionary modelState)
{
_modelState = modelState;
_repository = new gpc.Data.Repositories.roleRepository();
}
public roleService(ModelStateDictionary modelState,
gpc.Data.Repositories.roleRepository repository)
{
_modelState = modelState;
_repository = repository;
}
public HolderNames HolderNamesbyRoleID(int roleid)
{
return _repository.HolderNamesbyRoleID(roleid);
}
}
}
Refer interface for more info.

Entity framework: ObjectContext and inheritance

I need to have a CRUd operations on my class (CompetenceSpecific).
Competence has three derived classes - CompetenceFunction, CompetenceArea and CompetenceSpecifc
The error I recieved:
There are no EntitySets defined for the specified entity type 'CompetencyManagement.Domain.Entities.CompetenceFunction'. If 'CompetencyManagement.Domain.Entities.CompetenceFunction' is a derived type, use the base type instead. Parameter name: TEntity
How should I correct this? Please suggest a solution that would solve my problem. Thanks
Please check the code below, I removed some parts of the code for simplicity.
--MODEL
public class Competence
{
public int CompetenceID { get; set; }
public int CourseID { get; set; }
...
}
public class CompetenceFunction : Competence
{
}
--REPOSITORY and interfaces
public interface IRepository<T> where T : class
{
T GetById(object id);
IEnumerable<T> GetAll();
IEnumerable<T> Query(Expression<Func<T, bool>> filter);
void Add(T entity);
void Remove(T entity);
}
public abstract class Repository<T> : IRepository<T>
where T : class
{
protected IObjectSet<T> _objectSet;
public Repository(ObjectContext context)
{
_objectSet = context.CreateObjectSet<T>();
}
...
}
public class CompetenceFunctionRepository : Repository<CompetenceFunction>
{
public CompetenceFunctionRepository(ObjectContext context)
: base(context)
{
}
public override CompetenceFunction GetById(object id)
{
return _objectSet.SingleOrDefault(s => s.CompetenceID == (int)id);
}
}
--UNIT oF WORK
public interface IUnitOfWork
{
IRepository<CompetenceFunction> CompetenceFunctions { get; }
IRepository<CompetenceArea> CompetenceAreas { get; }
IRepository<CompetenceSpecific> CompetenceSpecifics { get; }
void Commit();
}
public class UnitOfWork : IUnitOfWork, IDisposable
{
private CompetenceFunctionRepository _competencefunction;
private CompetenceAreaRepository _competencearea;
private CompetenceSpecificRepository _competencespecifc;
public UnitOfWork(ObjectContext context)
{
if (context == null)
{
throw new ArgumentNullException("Context was not supplied");
}
_context = context;
}
#region IUnitOfWork Members
public IRepository<CompetenceFunction> CompetenceFunctions
{
get
{
if (_competencefunction == null)
{
_competencefunction = new CompetenceFunctionRepository(_context);
}
return _competencefunction;
}
}
public IRepository<CompetenceArea> CompetenceAreas
{
get
{
if (_competencearea == null)
{
_competencearea = new CompetenceAreaRepository(_context);
}
return _competencearea;
}
}
public IRepository<CompetenceSpecific> CompetenceSpecifics
{
get
{
if (_competencespecifc == null)
{
_competencespecifc = new CompetenceSpecificRepository(_context);
}
return _competencespecifc;
}
}
--Im getting an error in this part of Repository
public Repository(ObjectContext context)
{
_objectSet = context.CreateObjectSet<T>();
}
There are no EntitySets defined for the specified entity type 'CompetencyManagement.Domain.Entities.CompetenceFunction'. If 'CompetencyManagement.Domain.Entities.CompetenceFunction' is a derived type, use the base type instead. Parameter name: TEntity
Here's how I implement in the controller
private IUnitOfWork _unitOfWork;
var a = _unitOfWork.CompetenceFunctions.GetAll();
return View(a);
You have to get derived type by the OfType function, e.g.
context.CreateObjectSet<Competence>().OfType<CompetenceFunction>()
In your case that would mean that there is only a CompetenceRepository that serves all derivatives of Competence.
Edit
(After your comment)
First, UoW is meant for temporarily storing changes that should be dealt with in one batch (like changes to be committed to the database). GetAll and similar functions are repository stuff.
But do you need repositories? I like this post. When beginning to know EF, I would focus on the ins and outs of EF without getting distracted too much by surrounding architecture. E.g. start with services that at the inside communicate directly with the context and expose methods like GetCompetenceFunctions, GetCompetenceAreas (using OfType), and SaveCompetenceFunction, ....
You can address these service methods directly from action methods in the MVC controllers.

Resources