I have a COM object that I am trying to wrap in a C# class in order to make it more readily available for other applications that wish to consume it.
I have the following code that creates an instance of the COM object, and then using reflection makes a call to a method to retrieve user data. This code works fine when it is located in an aspx page.
object jdObj = Server.CreateObject("jd_api.UserCookie");
string username = jdObj.GetType().InvokeMember("GetUserName", System.Reflection.BindingFlags.InvokeMethod, null, jdObj , null).ToString();
However, when I move the code to a class file (JD_API.cs) in order to abstract it from the actual website, I can no longer get it to work. For example, I have the following static method that is declared like such:
public static string GetUserName() {
object jdObj = Server.CreateObject("jd_api.UserCookie");
string username = jdObj.GetType().InvokeMember("GetUserName",
System.Reflection.BindingFlags.InvokeMethod, null, jdObj , null).ToString();
return username;
}
Unfortunately, the Server object is restricted to some ASP.NET libraries that are included by default in web applications, and so the above code was a no-go. So at this point I decided to try to create an instance of the COM object like such:
public static string GetUserName() {
Type type = Type.GetTypeFromProgID("jd_api.UserCookie");
object jdObj = Activator.CreateInstance(type);
string username = jdObj.GetType().InvokeMember("GetUserName", System.Reflection.BindingFlags.InvokeMethod, null, jdObj , null).ToString();
return username;
}
However at runtime I get an error that says, "Attempted to read or write protected memory. This is often an indication that other memory is corrupt.".
I'm not sure where to go from here. Any help on how to abstract creating an instance of this COM object to a layer that is not within the web application itself would greatly appreciated. Thanks!!
Declare DLL functions within a class. Then define a static method for each DLL function you want to call.
The following code sample creates a wrapper named Win32MessageBox that calls the MessageBox function in User32.dll each time a .NET app calls the object Show method.
It requeres the System.Runtime.InteropServices namespace.
using System;
using System.Runtime.InteropServices;
class Win32MessageBox
{
[DllImport("user32.dll")]
private static extern int MessageBox(IntPtr hWnd, String text,
String caption, uint type);
public static void Show(string message, string caption)
{
MessageBox(new IntPtr(0), message, caption, 0);
}
}
To call it, just type:
Win32MessageBox.Show("StackOverflow!", "my stack box");
The method where you call the above line doesn't need to be aware that it's a actually calling a function in an unmanaged DLL.
Resources: the MCTS Self-Paced Training Kit (Exam 70-536) by Tony Northrup.
Hove you tried usinsing interoperating
I've done the following in the past (working from memory so you might need to fiddle with this a bit):
Right Click "References" in your project
Select "Add Reference"
Selelct the "Com" Tab
Find and add your Com Instnace
In your class file
using yourComName;
public static string GetUserName()
{
yourComName.yourComClass jdObj = new yourComClass();
string username = jdObj.GetUserName(someParameters);
return username;
}
Hope this a) works and b) helps!
Related
can anyone tell me what I am doing wrong?
I am wanting to use Mehdime.Entity from https://www.nuget.org/packages/Mehdime.Entity in order to manage my DBContext-derived classes in a Console Application. I am also using NInject.
The connection strings for my DBContext-derived classes are partially generated from standard app.config ConnectionStrings and also by an AppDomain value that (in my Console App case) comes in via a command line argument.
My DBContext-derived classes have their connection strings prepared using a program-implemented class which takes into account of the command line argument as follows:
public class TaskManagementDbContext : DbContext
{
public TaskManagementDbContext(IConnectionStringResolver csr) :
base(csr.GetConnectionString("Default"))
{
}
}
(IConnectionStringResolver basically implements GetConnectionString() which returns the connection string by using given named standard app.config ConnectionString and the command line argument.
This is fine when I use NInject to instantiate the DbContext directly but when trying to use with Mehdime.Entity, it is AmbientDbContextLocator that is doing the instantiation and it throws a MissingMethodException because it requires my DBContext-derived class to have a parameterless constructor:
public class TaskRepository : ITaskRepository
{
private readonly IAmbientDbContextLocator _ambientDbContextLocator;
private TaskManagementDbContext DbContext
{
get
{
// MissingMethodException thrown "No parameterless constructor defined for this object"
var dbContext = _ambientDbContextLocator.Get<TaskManagementDbContext>();
...
}
}
How should I provide a connection string to my DBContext-derived classes at run-time in this situation? I suspect I am going about this the wrong way. Thanks.
OK. I've worked out the solution and I'm putting it here for anyone else with this issue:
Create your own implementation of IDbContextFactory (see below). I put this in the same class library as my Data Access Layer (i.e. my DbContexts). You will see in my example how I "look for" a specific constructor prototype (in my case, 1 parameter of type IDbContextFactory - your's will no doubt be different). If found, get the actual parameters and invoke a new instance of your DBContext-derived class. If not found, you can throw an exception or in my case, try to call the default constructor (if exists).
Code:
using System;
using System.Data.Entity;
using Mehdime.Entity;
using Ninject;
using TaskProcessor.Common;
namespace TaskProcessor.Data.Connection
{
public class DbContextWithCSRFactory : IDbContextFactory
{
public TDbContext CreateDbContext<TDbContext>() where TDbContext : DbContext
{
// Try to locate a constuctor with a single IConnectionStringResolver parameter...
var ci = typeof(TDbContext).GetConstructor(new[] { typeof(IConnectionStringResolver) });
if(ci != null)
{
// Call it with the actual parameter
var param1 = GlobalKernel.Instance.Get<IConnectionStringResolver>();
return (TDbContext)ci.Invoke(new object[] { param1 });
}
// Call parameterless constuctor instead (this is the default of what DbContextScope does)
return (TDbContext)Activator.CreateInstance<TDbContext>();
}
}
}
Create a binding in NInject so that your IDbContextFactory implementation is called:
Code:
private void AddBindings(IKernel kernel)
{ ...
kernel.Bind<IDbContextFactory>().To<Data.Connection.DbContextWithCSRFactory>().InSingletonScope();
}
Everything now falls into place.
To avoid web service's problem of not being able to pass complex objects like dictionaries and trees, I created a small struct inside the class with a few values fields. However, the web service is in a seperate project in the solution and I'm unsure how the behind code that calls the webService function would know what the struct is. Should I copy the struct to the behind code file? Can I import it?
Here's a small example:
namespace mYWebService{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class Service1 : System.Web.Services.WebService
{
struct TreeData
{
private readonly string text;
private readonly string parent;
private string val;
public TreeData (string Text, string Parent)
{
this.text = Text;
this.parent = Parent;
this.val = "";
}
public TreeData (string Text, string Parent, string Value)
{
this.text = Text;
this.parent = Parent;
this.val = Value;
}
public string Text { get { return text; } }
public string Parent { get { return parent; } }
public string Value { get { return val; } }
}
[WebMethod]`
public TreeData getTree(){
TreeData myTree = new TreeData("1","2","3");
return myTree;
}}
When you generate the binding in the client code, all necessary complex data types will get created automatically, because they are described in the service's metadata. However, you should rather use WCF these days providing there's no hard requirement to use the old-fashioned .NET 2.0 web services (i.e. the WebService class).
You will have a hard time compiling this because you are exposing a private struct in a public method. In the very least, the struct must be made public. I also recommend that you put your struct outside of the class, since inner classes / structs /etc is bad practice (this is my personal opinion, however you do not see them used much in e.g. the .net framework, indicating that Microsoft doesn't like them much either).
Keep in mind that web services are distributed by nature, thus you should not have to rely on references to the class directly. This is a SOAP service (I think), and the framework will expose the metadata of the service. This metadata can be used by Visual Studio to auto generate a proxy client which can be used to call the service.
Here's a simple way to set up a proxy:
Start the web service project executable (not in debug mode, you will still need to be able to use Visual Studio for the next steps)
Select the project where your web service client (the code that calls the service) is located and add a service reference
This will open a dialog where you can enter the service endpoint (url). Enter the endpoint where the service is running, and you should be able to select in this dialog
Once the reference is added, some autogenerated proxy code should be generated for you. This will give you access to your Method.
Finally I do agree with Ondrej Tucny that you should look into WCF instead
When calling an extension method from another extension method, my solution was building ok, but in the published site (or the virtual asp.net server) I was getting the Compile Error "Ambiguous call".
public static string ExtensionMethodA(this ObjectToExtend myObj){//code here}
public static string ExtensionMethodB(this ObjectToExtend myObj){
string a = myObj.ExtensionMethodA(); // this line causes the error.
return a;
}
I haven't read enough to know exactly why, but here is the solution:
public static string ExtensionMethodA(this ObjectToExtend myObj){//code here}
public static string ExtensionMethodB(this ObjectToExtend myObj){
string a = ExtensionMethodA(myObj); // correct call.
return a;
}
How can I retrieve the Entity Framework 4 connection string from a custom config file, not web.config?
Edit:
Is it reasonable to delete the default constructor generated code and recreate it in a partial class to use the pulled in connection string?
I would really like to avoid changing all references to the EF context with an overloaded method including the connection string.
#BrokenGlass: This is what we ended up with:
public partial class STARSEntities
{
private const string _connectionStringFormat = #"metadata=res://*/STARS.EntityModel.STARSModel.csdl|res://*/STARS.EntityModel.STARSModel.ssdl|res://*/STARS.EntityModel.STARSModel.msl;provider=System.Data.SqlClient;provider connection string='Data Source={0};MultipleActiveResultSets=True'";
/// <summary>
/// Initializes a new STARSEntities object using the connection string found in the STARS.xml configuration file.
/// </summary>
/// <remarks>
/// If the STARSEntities class is regenerated from the database, the default constructor needs to be removed from the generated file.
/// </remarks>
public STARSEntities() : base(GetConnectionString(), "STARSEntities")
{
this.ContextOptions.LazyLoadingEnabled = true;
OnContextCreated();
}
private static string GetConnectionString()
{
return string.Format(_connectionStringFormat, ApplicationConfiguration.GetConnectionString("STARS"));
}
}
There's a constructor overload for DataContext that you can pass a connection string - in that case you can take the setting from anywhere you like.
Edit based on updated question:
I would really like to avoid changing
all references to the EF context with
an overloaded method including the
connection string.
The problem is that the Entities context created by the T4 script generates a const property that is used as connection string
public const string ConnectionString = "name=FooEntities";
public FooEntities()
: base(ConnectionString, ContainerName)
{
this.ContextOptions.LazyLoadingEnabled = true;
}
Since you can't override the default constructor of the partial class, your only other option would be to change the T4 script itself - you should see the following in your .TT script file:
public <#=code.Escape(container)#>()
: base(ConnectionString, ContainerName)
{
<#
WriteLazyLoadingEnabled(container);
#>
}
To force your connection string to be used you could modify the constructor call to determine the connection string by calling a static method that you define in a separate file (but for the same partial class FooEntities):
public <#=code.Escape(container)#>()
: base(GetCustomConnectionString(), ContainerName)
{
<#
WriteLazyLoadingEnabled(container);
#>
}
Now GetCustomConnectionString() can be defined separately
public partial class FooEntities : ObjectContext
{
public static string GetCustomConnectionString()
{
return "Foobar"; //however you want to determine connection string here
}
}
You see this is getting complicated and fragile very fast, so I would not advise doing this - but you could.
Don't know if this is what you ask for, but you can use the "configSource" attribute of the connectionStrings element:
<connectionStrings configSource="connection.config">
</connectionStrings>
Something like this perhaps?
// replace the following line with any business logic you need to get to the file
// with your connection string. If it is a config-style file, you can use the
// Frameworks's helper classes as well
string connectionString= File.ReadAllText("alternative path");
Entities ent = new Entity(connectionString);
Here's a post on Microsoft Connect that explains this.
Are you able to read the connection string from this custom config file? if so, you can use the constructor for your DataContext that takes ConnectionString.
NorthWindDataContext nwdc = new NorthWindDataContext(alternateConnectionString);
You can use EntityConnectionStringBuilder:
var ecb = new EntityConnectionStringBuilder();
ecb.Metadata = "res://*/Model.MyModel.csdl|res://*/Model.MyModel.ssdl|res://*/Model.MyModel.msl";
ecb.Provider = "System.Data.SqlClient";
ecb.ProviderConnectionString = connectionStringFromFancySource;
return new MyModel(ecb.ToString());
I'm new to Flex and BlazeDS and I'm trying to implement a simple application which uses Flex on the front end and a Spring/Hibernate application on the back end, with communication between the two going over a BlazeDS channel.
I'm seeking direction as to the best and/or simplest way to approach this. I have the UI set up in such a way that the user is presented with a file chooser in which they pick the image file they want to upload. When this is chosen and submitted (as a form submission) then the server side should receive the image file data as well as some related metadata such as a description and date, then populate a Hibernate entity/POJO with the image file data and related metadata, and then persist the entity/POJO into the database.
I have found some examples of how you would do a file upload and download using servlets here and the FileReference class (here and here) but these don't appear to address the problem in a way which leverages BlazeDS and/or Spring/Hibernate. I want to put the image file data and related metadata (description, capture date, etc.) into a value object within the Flex application and then send this over BlazeDS to a service provided by my Spring/Hibernate application running on Tomcat. In this service I want to extract the image data (both the actual JPG/PNG/GIF data and the related metadata such as description, etc.) from the value object sent from the Flex app into an entity/POJO which is then persisted via Hibernate in my database.
Can this be done, and if so what's the best way to go about it? Am I mistaken in assuming that if I use BlazeDS then I am somehow bypassing the need to provide HTTP-based services such as servlets on the server side and instead I can use my Java services as "RemoteObjects"? Is there necessarily a one-to-one mapping between Java POJO/entity class and the Flex value object class when making this sort of transfer? If so is there a tool which creates corresponding Flex value objects from Java POJOs or vice versa.
Thanks in advance for your help, comments, suggestions, etc.
--James
Update: Some code to make this more clear:
I have this as my value object in Flex:
package valueobjects
{
import flash.utils.ByteArray;
[Bindable]
[RemoteClass(alias="com.abc.example.persistence.entity.Image")]
public class Image
{
public var id:Number;
public var captureDate:Date;
public var description:String;
public var imageData:ByteArray;
public function Image() {}
}
I am assuming that this can be used as a one-to-one mapping to the POJO class used by my service and DAO classes on the server-side, which looks like this:
package com.abc.example.persistence.entity;
import java.sql.Blob;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
#Entity(name = "IMAGE")
public class Image
extends AbstractBaseEntity<Long>
{
private String description;
private Date captureDate;
private Blob imageData;
#Column(name = "CAPTURE_DATE", nullable = true)
public Date getCaptureDate()
{
return captureDate;
}
#Column(name = "DESCRIPTION", nullable = true)
public String getDescription()
{
return description;
}
#Column(name = "IMAGE_DATA", nullable = true)
public Blob getImageData()
{
return imageData;
}
public void setCaptureDate(final Date captureDate)
{
this.captureDate = captureDate;
}
public void setDescription(final String description)
{
this.description = description;
}
public void setImageData(final Blob imageData)
{
this.imageData = imageData;
}
}
In my Flex application I populate the fields of an Image object with a description string, date, and image file data (based on the user's file selection and text input for the description) and then call a method on the RemoteObject which is mapped to the service running on Tomcat. I make the RemoteObject service call within my Flex code using the Image value object as the argument, but the service method running on the servier side actually expects an argument of the POJO/entity type, and it's here that I am thinking that some sort of conversion/transformation between the Flex value object and the Java POJO will occur (by virtue of the RemoteClass alias setting on the value object's class declaration), but it doesn't seem to be happening that way because when I debug the application the Java service is only getting null values when the service call is made.
In my Flex application I have a FileReference and Image value object as public, bindable variables:
[Bindable]
public var imageToBeArchivedFileReference:FileReference = new FileReference();
[Bindable]
public var imageToBeArchivedValueObject:valueobjects.Image = new valueobjects.Image();
There is also an event handler to browse for a file when the user clicks on a file select button:
protected function imageFileSelectButton_clickHandler(event:MouseEvent):void
{
var imageFileFilter:FileFilter = new FileFilter("Images (*.jpg, *.jpeg, *.gif, *.png)", "*.jpg;*.jpeg;*.gif;*.png");
var fileTypes:Array = new Array();
fileTypes.push(imageFileFilter);
imageToBeArchivedFileReference.addEventListener(Event.SELECT, imageToBeArchived_fileSelectHandler);
imageToBeArchivedFileReference.browse(fileTypes);
}
There is an event handler which builds the value object when the image file has been selected:
private function imageToBeArchived_fileSelectHandler(event:Event):void
{
imageToBeArchivedFileReference.load();
imageToBeArchivedValueObject = new valueobjects.Image()
imageToBeArchivedValueObject.imageData = imageToBeArchivedFileReference.data;
imageToBeArchivedValueObject.description = imageToBeArchivedDescription.text;
imageToBeArchivedValueObject.captureDate = imageToBeArchivedFileReference.creationDate;
}
and there's an event handler which is invoked when the user clicks on the submit button to perform the image save/upload:
protected function archiveImageButton_clickHandler(event:MouseEvent):void
{
imageArchivalService.archiveImage(imageToBeArchived);
}
On the server side my Java class is doing a simple save of the POJO:
public void archiveImage(final Image image)
{
imageDao.saveOrUpdate(image);
}
When I set a breakpoint in the method above and look at the image variable it looks to be empty, so I'm assuming that the transformation from the Flex value object to the Java POJO did not go as expected and that there's more to it than just adding a RemoteClass alias in the Flex value object class.
Check out this example, it is all there.
http://biemond.blogspot.com/2008/08/flex-upload-and-download-with-blazeds.html
Don't use the loader class, use the readBytes call.
Make sure you go to the comments, there are valuable info there.
Cheers