Hyper-V Cluster - Shared Disk replication througth WMI - c#

First of all, I wanna say thanks to this awesome site, saved my life more than a hundred times.
To introduce a little the problem we're having, I'll begin explaining our infrastructure.
Actually, we've got a client that has 4 Hyper-V host(s) using Failover Cluster feature, with Windows 2016.
Microsoft says in releases notes that shared disk replication is only available througth the WMI class Msvm_ReplicationCollectionService, so said, we started working on it.
-- Msvm_CollectionReplicationService Class
https://msdn.microsoft.com/en-us/library/mt167787(v=vs.85).aspx
We've implemented the class following the old model of this same class.
Old namespace: "root/virtualization/v2"
(this works well, but no shared disk replication)
New namespace: "root/HyperVCluster/v2"
(fails)
As first step we try to create a relationship between the two virtual machines sharing the disk, using previous created collection (ReplicaTestGroup), then we try to call "CreateReplicationRelationship" method of the class.
Issued command: PS C:\REPLICA\1\1> .\ReplicaSamples.exe CreateReplicationRelationship REPLICA1 REPLICA2 7ccff3fe-da17-436a-8f38-8a34833b31e6 hypervdrhost.mydom.local
Method fails with: "The method call failed.", without more details.
Does anyone has any idea of what's going on? Is there any way of debugging WMI so I can see what's wrong?
ERROR:
Unhandled Exception: System.Management.ManagementException: The method
call failed. at
Microsoft.Samples.HyperV.Common.WmiUtilities.ValidateOutput(ManagementBaseObject
outputParameters, ManagementScope scope, Boolean throwIfFailed,
Boolean printErrors) in
C:\Users\isarria\Documents\Replica\cs\WmiUtilities.cs:line 136 at
Microsoft.Samples.HyperV.Common.WmiUtilities.ValidateOutput(ManagementBaseObject
outputParameters, ManagementScope scope) in
C:\Users\isarria\Documents\Replica\cs\WmiUtilities.cs:line 59 at
Microsoft.Samples.HyperV.Replica.ManageReplication.CreateReplicationRelationship(String
name, String name2, String CollectionID, String recoveryServerName) in
C:\Users\isarria\Documents\Replica\cs\ManageReplication.cs:line 85 at
Microsoft.Samples.HyperV.Replica.Program.Main(String[] args) in
C:\Users\isarria\Documents\Replica\cs\Program.cs:line 107
USED CODE:
/// <summary>
/// Enables replication for a collection of virtual machines to a specified DRserver using
/// integrated authentication.
/// </summary>
/// <param name="name">Name of VM 1</param>
/// <param name="name2">Name of VM 2</param>
/// <param name="CollectionID">CollectionID to enable replication.</param>
/// <param name="recoveryServerName">The name of the recovery server.</param>
internal static void
CreateReplicationRelationship(
string name,
string name2,
string CollectionID,
string recoveryServerName)
{
ManagementScope scope = new ManagementScope(#"root\HyperVCluster\v2");
ManagementScope oldScope = new ManagementScope(#"root\virtualization\v2");
//
// Retrieve the Msvm_VirtualSystemCollection.
//
using (ManagementObject vm = WmiUtilities.GetVirtualMachine(name, oldScope))
{
using (ManagementObject vm2 = WmiUtilities.GetVirtualMachine(name2, oldScope))
{
string vmPath = vm.Path.Path;
string vmPath2 = vm2.Path.Path;
using (ManagementObject collection = WmiUtilities.GetVMGroup(CollectionID, scope))
{
using (ManagementObject replicationSettingData =
ReplicaUtilities.GetReplicationSettings(vm))
{
replicationSettingData["RecoveryConnectionPoint"] = recoveryServerName;
replicationSettingData["AuthenticationType"] = 1;
replicationSettingData["RecoveryServerPortNumber"] = 80;
replicationSettingData["CompressionEnabled"] = 1;
// Keep 24 recovery points.
replicationSettingData["RecoveryHistory"] = 24;
// Replicate changes after every 300 seconds.
replicationSettingData["ReplicationInterval"] = 300;
// Take VSS snapshot every one hour.
replicationSettingData["ApplicationConsistentSnapshotInterval"] = 1;
// Include all disks for replication.
replicationSettingData["IncludedDisks"] = WmiUtilities.GetVhdSettings(vm);
//replicationSettingData["IncludedDisks"] = WmiUtilities.GetVhdSettings(vm2);
Console.WriteLine(replicationSettingData["IncludedDisks"].ToString());
string settingDataEmbedded =
replicationSettingData.GetText(TextFormat.WmiDtd20);
using (ManagementObject replicationService =
ReplicaUtilities.GetVirtualMachineReplicationService(scope))
{
using (ManagementBaseObject inParams =
replicationService.GetMethodParameters("CreateReplicationRelationship"))
{
inParams["Collection"] = collection;
inParams["CollectionReplicationSettingData"] = settingDataEmbedded;
using (ManagementBaseObject outParams =
replicationService.InvokeMethod("CreateReplicationRelationship",
inParams,
null))
{
WmiUtilities.ValidateOutput(outParams, scope);
}
}
}
Console.WriteLine(string.Format(CultureInfo.CurrentCulture,
"Replication is successfully enabled for vmGroup: \"{0}\"", CollectionID));
}
}
}
}
}
/// <summary>
/// Gets the Msvm_VirtualSystemCollection instance that matches the requested VMGroup name.
/// </summary>
/// <param name="CollectionID">The CollectionID to retrieve the path for.</param>
/// <param name="scope">The ManagementScope to use to connect to WMI.</param>
/// <returns>The Msvm_VirtualSystemCollection instance.</returns>
public static ManagementObject
GetVMGroup(
string CollectionID,
ManagementScope scope)
{
return GetCollectionObject(CollectionID, "Msvm_VirtualSystemCollection", scope);
}
/// <summary>
/// Gets the Msvm_ComputerSystem instance that matches the requested virtual machine name.
/// </summary>
/// <param name="name">The name of the virtual machine to retrieve the path for.</param>
/// <param name="scope">The ManagementScope to use to connect to WMI.</param>
/// <returns>The Msvm_ComputerSystem instance.</returns>
public static ManagementObject
GetVirtualMachine(
string name,
ManagementScope scope)
{
return GetVmObject(name, "Msvm_ComputerSystem", scope);
}
/// <summary>
/// Gets the first virtual machine object of the given class with the given name.
/// </summary>
/// <param name="name">The name of the virtual machine to retrieve the path for.</param>
/// <param name="className">The class of virtual machine to search for.</param>
/// <param name="scope">The ManagementScope to use to connect to WMI.</param>
/// <returns>The instance representing the virtual machine.</returns>
private static ManagementObject
GetVmObject(
string name,
string className,
ManagementScope scope)
{
string vmQueryWql = string.Format(CultureInfo.InvariantCulture,
"SELECT * FROM {0} WHERE ElementName=\"{1}\"", className, name);
SelectQuery vmQuery = new SelectQuery(vmQueryWql);
using (ManagementObjectSearcher vmSearcher = new ManagementObjectSearcher(scope, vmQuery))
using (ManagementObjectCollection vmCollection = vmSearcher.Get())
{
if (vmCollection.Count == 0)
{
throw new ManagementException(string.Format(CultureInfo.CurrentCulture,
"No {0} could be found with name \"{1}\"",
className,
name));
}
//
// If multiple virtual machines exist with the requested name, return the first
// one.
//
ManagementObject vm = GetFirstObjectFromCollection(vmCollection);
return vm;
}
}
/// <summary>
/// Gets the replication settings object for a collection.
/// </summary>
/// <param name="systemCollection">The Msvm_VirtualSystemCollection mache object.</param>
/// <returns>The replication settings object.</returns>
internal static ManagementObject
GetReplicationSettings(
ManagementObject systemCollection)
{
using (ManagementObjectCollection settingsCollection =
systemCollection.GetRelated("Msvm_ReplicationSettingData"))
{
ManagementObject replicationSettings =
WmiUtilities.GetFirstObjectFromCollection(settingsCollection);
return replicationSettings;
}
}
/// <summary>
/// Gets the first virtual machine object of the given class with the given name.
/// </summary>
/// <param CollectionID="CollectionID">The name of the virtual machine to retrieve the path for.</param>
/// <param name="className">The class of virtual machine to search for.</param>
/// <param name="scope">The ManagementScope to use to connect to WMI.</param>
/// <returns>The instance representing the virtual machine.</returns>
private static ManagementObject
GetCollectionObject(
string CollectionID,
string className,
ManagementScope scope)
{
string vmQueryWql = string.Format(CultureInfo.InvariantCulture,
"SELECT * FROM {0} WHERE CollectionID=\"{1}\"", className, CollectionID);
SelectQuery vmQuery = new SelectQuery(vmQueryWql);
using (ManagementObjectSearcher vmSearcher = new ManagementObjectSearcher(scope, vmQuery))
using (ManagementObjectCollection vmCollection = vmSearcher.Get())
{
ManagementObject CollectionObject = null;
if (vmCollection.Count == 1)
{
foreach (ManagementObject managementObject in vmCollection)
{
CollectionObject = managementObject;
}
}
else
{
throw new ManagementException(string.Format(CultureInfo.CurrentCulture,
"No {0} could be found with ID \"{1}\"",
className,
CollectionID));
}
return CollectionObject;
}
}
/// <summary>
/// Gets the virtual system replication service.
/// </summary>
/// <param name="scope">The scope to use when connecting to WMI.</param>
/// <returns>The collection virtual machine replication service.</returns>
public static ManagementObject
GetVirtualMachineReplicationService(
ManagementScope scope)
{
ManagementScope scope2 = new ManagementScope(#"root\HyperVCluster\v2");
SelectQuery query = new SelectQuery("select * from Msvm_CollectionReplicationService");
using (ManagementObjectSearcher queryExecute = new ManagementObjectSearcher(scope2, query))
using (ManagementObjectCollection serviceCollection = queryExecute.Get())
{
if (serviceCollection.Count == 0)
{
throw new ManagementException("Cannot find the collection replication service object. " +
"Please check that the Hyper-V Virtual Machine Management service is running.");
}
return WmiUtilities.GetFirstObjectFromCollection(serviceCollection);
}
}
Also, here is ValidateOutput method.
/// <summary>
/// Validates the output parameters of a method call and prints errors, if any.
/// </summary>
/// <param name="outputParameters">The output parameters of a WMI method call.</param>
/// <param name="scope">The ManagementScope to use to connect to WMI.</param>
/// <returns><c>true</c> if successful and not firing an alert; otherwise, <c>false</c>.</returns>
public static bool
ValidateOutput(
ManagementBaseObject outputParameters,
ManagementScope scope)
{
return ValidateOutput(outputParameters, scope, true, false);
}
/// <summary>
/// Validates the output parameters of a method call and prints errors, if any.
/// </summary>
/// <param name="outputParameters">The output parameters of a WMI method call.</param>
/// <param name="scope">The ManagementScope to use to connect to WMI.</param>
/// <param name="throwIfFailed"> If true, the method throws on failure.</param>
/// <param name="printErrors">If true, Msvm_Error messages are displayed.</param>
/// <returns><c>true</c> if successful and not firing an alert; otherwise, <c>false</c>.</returns>
public static bool
ValidateOutput(
ManagementBaseObject outputParameters,
ManagementScope scope,
bool throwIfFailed,
bool printErrors)
{
bool succeeded = true;
string errorMessage = "The method call failed.";
if ((uint)outputParameters["ReturnValue"] == 4096)
{
//
// The method invoked an asynchronous operation. Get the Job object
// and wait for it to complete. Then we can check its result.
//
using (ManagementObject job = new ManagementObject((string)outputParameters["Job"]))
{
job.Scope = scope;
while (!IsJobComplete(job["JobState"]))
{
Thread.Sleep(TimeSpan.FromSeconds(1));
//
// ManagementObjects are offline objects. Call Get() on the object to have its
// current property state.
//
job.Get();
}
if (!IsJobSuccessful(job["JobState"]))
{
succeeded = false;
//
// In some cases the Job object can contain helpful information about
// why the method call failed. If it did contain such information,
// use it instead of a generic message.
//
if (!string.IsNullOrEmpty((string)job["ErrorDescription"]))
{
errorMessage = (string)job["ErrorDescription"];
}
if (printErrors)
{
PrintMsvmErrors(job);
}
if (throwIfFailed)
{
throw new ManagementException(errorMessage);
}
}
}
}
else if ((uint)outputParameters["ReturnValue"] != 0)
{
succeeded = false;
if (throwIfFailed)
{
throw new ManagementException(errorMessage);
}
}
return succeeded;
}

Related

.net core websockets obtain DbContext

I am testing out .net core and making a small sample app using .net core + websockets to push some data into my app. I want to save this data in the database using a dbcontext.
However I have issues getting the dbcontext in my websocket handler. So how can I create a dbcontext to use.
My startup Configure method contains this:
...
app.Map("/ws", WSHandler.Map);
...
This is my WSHandler class that implements actually reading from the incoming connection. I need a DbContext here that I can use to read/write from the database.
/// <summary>
/// Handler for an incoming websocket client
/// </summary>
public class WSHandler {
/// <summary>
/// Max size in bytes of an incoming/outgoing message
/// </summary>
public const int BufferSize = 4096;
/// <summary>
/// The socket of the current connection
/// </summary>
WebSocket socket;
/// <summary>
/// Constructor, assign socket to current instance and adds socket to ConnectedClients.
/// </summary>
/// <param name="socket"></param>
WSHandler(WebSocket socket) {
this.socket = socket;
}
/// <summary>
/// Configure app to use websockets and register handler.
/// </summary>
/// <param name="app"></param>
public static void Map(IApplicationBuilder app) {
app.UseWebSockets();
app.Use((WSHandler.Acceptor);
}
/// <summary>
/// Accept HttpContext and handles constructs instance of WSHandler.
/// </summary>
/// <param name="hc">The HttpContext</param>
/// <param name="n">Task n</param>
/// <returns></returns>
static async Task Acceptor(HttpContext hc, Func<Task> n) {
if (hc.WebSockets.IsWebSocketRequest == false) {
return;
}
var socket = await hc.WebSockets.AcceptWebSocketAsync();
var h = new WSHandler(socket);
await h.Loop();
}
/// <summary>
/// Wait's for incoming messages
/// </summary>
/// <returns></returns>
async Task Loop() {
var buffer = new Byte[BufferSize];
ArraySegment<Byte> segment = new ArraySegment<byte>(buffer);
while (this.socket.State == WebSocketState.Open) {
WebSocketReceiveResult result = null;
do {
result = await socket.ReceiveAsync(segment, CancellationToken.None);
} while (result.EndOfMessage == false);
// do something with message here. I want to save parse and save to database
}
}
}
Since there is some interests in this post I have added the solution I used.
You have access to all services via the HttpContext. So what I did is get the context options from this service and then create context when I need them. Be aware though that long living contexts are not advised and if an error occurs a DbContext is no longer useable. In the end I implemented a different database caching layer and write to that instead of using DbContext itself in the websocket handler.
Here is the code above extended to create DbContexts. I have not tested it since right now I don't have visual studio available...
<summary>
/// Handler for an incoming websocket client
/// </summary>
public class WSHandler {
/// <summary>
/// Max size in bytes of an incoming/outgoing message
/// </summary>
public const int BufferSize = 4096;
/// <summary>
/// The socket of the current connection
/// </summary>
WebSocket socket;
/// <summary>
/// The options to create DbContexts with.
/// </summary>
DbContextOptions<AssemblyServerContext> ctxOpts;
/// <summary>
/// Constructor, assign socket to current instance and adds socket to ConnectedClients.
/// </summary>
/// <param name="socket"></param>
/// <param name="ctxOpts"></param>
WSHandler(WebSocket socket, DbContextOptions<AssemblyServerContext> ctxOpts) {
this.ctxOpts = ctxOpts;
this.socket = socket;
}
/// <summary>
/// Configure app to use websockets and register handler.
/// </summary>
/// <param name="app"></param>
public static void Map(IApplicationBuilder app) {
app.UseWebSockets();
app.Use((WSHandler.Acceptor);
}
/// <summary>
/// Accept HttpContext and handles constructs instance of WSHandler.
/// </summary>
/// <param name="hc">The HttpContext</param>
/// <param name="n">Task n</param>
/// <returns></returns>
static async Task Acceptor(HttpContext hc, Func<Task> n) {
if (hc.WebSockets.IsWebSocketRequest == false) {
return;
}
var socket = await hc.WebSockets.AcceptWebSocketAsync();
var ctxOptions = hc.RequestServices.GetService<DbContextOptions<AssemblyServerContext>>();
var h = new WSHandler(socket, ctxOptions);
await h.Loop();
}
/// <summary>
/// Wait's for incoming messages
/// </summary>
/// <returns></returns>
async Task Loop() {
var buffer = new Byte[BufferSize];
ArraySegment<Byte> segment = new ArraySegment<byte>(buffer);
while (this.socket.State == WebSocketState.Open) {
WebSocketReceiveResult result = null;
do {
result = await socket.ReceiveAsync(segment, CancellationToken.None);
} while (result.EndOfMessage == false);
// do something with message here. I want to save parse and save to database
using (var ctx = new AssemblyServerContext(ctxOpts)) {
}
}
}
}

How to read WMI data of a USB (PnP) device, in this case the hardware ids, in C#?

How can I get the hardware id from an USB device? I know how to get that id with device-manager, but I want to get it via C#. Is that possible? This is what I´ve done already, but that delivers me not the hardware if which consists of the vendor id (VID) and the product id (PID):
ManagementObjectSearcher USB = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive WHERE InterfaceType='USB'");
ManagementObjectCollection USBinfo = USB.Get();
foreach (ManagementObject MO in USBinfo)
{
serialNumber = (string)MO["DeviceID"];
name = (string)MO["Name"];
Drives.Add(new KeyValuePair<String, String>(name, serialNumber));
}
I also tried it with "PNPDeviceID" and "SerialNumber" instead of "DeviceID", but that also doesn´t replys the hardware-id that I need.
Original answer (see updated answer below)
You need to take a look at the Win32_PnPEntity WMI class. According to the mentioned description you can see, that you just need to check for HardwareID.
The following code show's that in a "quick and dirty" way, I would suggest creating a Win32_PnpEntity class which exposes the data via properties.
ManagementObjectSearcher searcher = new ManagementObjectSearcher("Select * From Win32_PnPEntity");
ManagementObjectCollection deviceCollection = searcher.Get();
foreach (var device in deviceCollection)
{
try
{
string caption = (string)device.GetPropertyValue("Caption");
if (caption == null)
continue;
Console.WriteLine(caption);
string[] hardwareIDs = (string[])device.GetPropertyValue("HardwareID");
if (hardwareIDs == null)
continue;
foreach (string hardwareID in hardwareIDs)
{
Console.WriteLine(hardwareID);
}
Console.WriteLine();
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
Updated answer
As the generic question is How to read WMI data of USB (PnP) device in C#, in this case the hardware ids?, I've written a Win32_PnpEntity class (stripped of comments as the post is limited to 30000 characters? If anyone want's the commented version, drop me a line) which takes a ManageementBasObject and provides all data of that given entity as properties.
Usage:
ManagementObjectSearcher searcher = new ManagementObjectSearcher("Select * From Win32_PnPEntity");
ManagementObjectCollection deviceCollection = searcher.Get();
foreach (var entity in deviceCollection)
{
Win32_PnPEntity device = new Win32_PnPEntity(entity);
Console.WriteLine($"Caption: {device.Caption}");
Console.WriteLine($"Description: {device.Description}");
Console.WriteLine($"Number of hardware IDs: {device.HardwareID.Length}");
foreach (string hardwareID in device.HardwareID)
{
Console.WriteLine(hardwareID);
}
Console.WriteLine();
}
The Win32_PnpEntity class:
public class Win32_PnPEntity
{
public enum AvailabilityEnum
{
/// <summary>
/// Represents the meaning "Other".
/// </summary>
Other = 0x01,
/// <summary>
/// Represents the meaning "Unknown".
/// </summary>
Unknown = 0x02,
/// <summary>
/// Represents the meaning "Running or Full Power".
/// </summary>
RunningOrFullPower = 0x03,
/// <summary>
// Represents the meaning "Warning".
/// </summary>
Warning = 0x04,
/// <summary>
/// Represents the meaning "In Test".
/// </summary>
InTest = 0x05,
/// <summary>
/// Represents the meaning "Not Applicable".
/// </summary>
NotApplicable = 0x06,
/// <summary>
/// Represents the maning "Power Off".
/// </summary>
PowerOff = 0x07,
/// <summary>
/// Represents the meaning "Off Line".
/// </summary>
OffLine = 0x08,
/// <summary>
/// Represents the meaning "Off Duty".
/// </summary>
OffDuty = 0x09,
/// <summary>
/// Represents the meaning "Degraded".
/// </summary>
Degraded = 0x0A,
/// <summary>
/// Represents the meaning "Not Installed".
/// </summary>
NotInstalled = 0x0B,
/// <summary>
/// Represents the meaning "Install Error".
/// </summary>
InstallError = 0x0C,
/// <summary>
/// Represents the meaning "Power Save - Unknown".
/// The device is known to be in a power save mode, but its exact status is unknown.
/// </summary>
PowerSave_Unknown = 0x0D,
/// <summary>
/// Represents the meaning "Power Save - Low Power Mode".
/// The device is in a power save state but still functioning, and may exhibit degraded performance.
/// </summary>
PowerSave_LowPowerMode = 0x0E,
/// <summary>
/// Represents the meaning "Power Save - Standby".
/// The device is not functioning, but could be brought to full power quickly.
/// </summary>
PowerSave_Standyby = 0x0F,
/// <summary>
/// Represents the meaning "Power Cycle".
/// </summary>
PowerCycle = 0x10,
/// <summary>
/// Represents the meaning "Power Save - Warning".
/// The device is in a warning state, though also in a power save mode.
/// </summary>
PowerSave_Warning = 0x11
}
public enum ConfigManagerErrorCodeEnum
{
/// <summary>
/// Represents the meaning "Unknown", not supported in the original WMI class.
/// </summary>
Unknown = 0xFF,
/// <summary>
/// Represents the meaning "Device is working properly.".
/// </summary>
WorkingProperly = 0x00,
/// <summary>
/// Represents the meaning "Device is not configured correctly.".
/// </summary>
DeviceNotConfiguredError = 0x01,
/// <summary>
/// Represents the meaning "Windows cannot load the driver for this device.".
/// </summary>
CannotLoadDriverError = 0x02,
/// <summary>
/// Represents the meaning "Driver for this device might be corrupted, or the system may be low on memory or other resources.".
/// </summary>
DriverCorruptedError = 0x03,
/// <summary>
/// Represents the meaning "Device is not working properly. One of its drivers or the registry might be corrupted.".
/// </summary>
NotWorkingProperlyError = 0x04,
/// <summary>
/// Represents the meaning "Driver for the device requires a resource that Windows cannot manage.".
/// </summary>
DriverResourceError = 0x05,
/// <summary>
/// Represents the meaning "Boot configuration for the device conflicts with other devices.".
/// </summary>
BootConfigurationError = 0x06,
/// <summary>
/// Represents the meaning "Cannot filter.".
/// </summary>
CannotFilterError = 0x07,
/// <summary>
/// Represents the meaning "Driver loader for the device is missing.".
/// </summary>
DriverLoaderMissingError = 0x08,
/// <summary>
/// Represents the meaning "Device is not working properly. The controlling firmware is incorrectly reporting the resources for the device.".
/// </summary>
DeviceNotWorkingProperlyFirmwareError = 0x09,
/// <summary>
/// Represents the meaning "Device cannot start.".
/// </summary>
DeviceCannotStartError = 0x0A,
/// <summary>
/// Represents the meaning "Device failed.".
/// </summary>
DeviceFailedError = 0x0B,
/// <summary>
/// Represents the meaning "Device cannot find enough free resources to use.".
/// </summary>
DeviceTooFewResourcesError = 0x0C,
/// <summary>
/// Represents the meaning "Windows cannot verify the device's resources.".
/// </summary>
VerifyDeviceResourceError = 0x0D,
/// <summary>
/// Represents the meaning "Device cannot work properly until the computer is restarted.".
/// </summary>
DeviceCannotWorkProperlyUnitlRestartError = 0x0E,
/// <summary>
/// Represents the meaning "Device is not working properly due to a possible re-enumeration problem.".
/// </summary>
DeviceNotWorkingProperlyReenumerationError = 0x0F,
/// <summary>
/// Represents the meaning "Windows cannot identify all of the resources that the device uses.".
/// </summary>
IdentifyResourcesForDeviceError = 0x10,
/// <summary>
/// Represents the meaning "Device is requesting an unknown resource type.".
/// </summary>
UnknownResourceTypeError = 0x11,
/// <summary>
/// Represents the meaning "Device drivers must be reinstalled.".
/// </summary>
DeviceDriversMustBeResinstalledError = 0x12,
/// <summary>
/// Represents the meaning "Failure using the VxD loader.".
/// </summary>
FailureUsingVxDLoaderError = 0x13,
/// <summary>
/// Represents the meaning "Registry might be corrupted.".
/// </summary>
RegistryMightBeCorruptedError = 0x14,
/// <summary>
/// Represents the meaning "System failure. If changing the device driver is ineffective, see the hardware documentation. Windows is removing the device.".
/// </summary>
SystemFailureRemovingDeviceError = 0x15,
/// <summary>
/// Represents the meaning "Device is disabled.".
/// </summary>
DeviceDisabledError = 0x16,
/// <summary>
/// Represents the meaning "System failure. If changing the device driver is ineffective, see the hardware documentation.".
/// </summary>
SystemFailureError = 0x17,
/// <summary>
/// Represents the meaning "Device is not present, not working properly, or does not have all of its drivers installed.".
/// </summary>
DeviceNotPresentError = 0x18,
/// <summary>
/// Represents the meaning "Windows is still setting up the device.".
/// </summary>
StillSettingUpTheDeviceError = 0x19,
/// <summary>
/// Represents the meaning "Windows is still setting up the device.".
/// </summary>
StillSettingUpTheDeviceError_2 = 0x1A,
/// <summary>
/// Represents the meaning "Device does not have valid log configuration.".
/// </summary>
InvalidDeviceLogConfigurationError = 0x1B,
/// <summary>
/// Represents the meaning "Device drivers are not installed.".
/// </summary>
DeviceDriversNotInstalledError = 0x1C,
/// <summary>
/// Represents the meaning "Device is disabled. The device firmware did not provide the required resources.".
/// </summary>
DeviceDisabledDueToFirmwareResourceError = 0x1D,
/// <summary>
/// Represents the meaning "Device is using an IRQ resource that another device is using.".
/// </summary>
IRQConflictError = 0x1E,
/// <summary>
/// Represents the meaning "Device is not working properly. Windows cannot load the required device drivers.".
/// </summary>
DeviceNotWorkingProperlyCannotLoadDrivers = 0x1F
}
public enum StatusInfoEnum
{
/// <summary>
/// Represents the meaning "Other".
/// </summary>
Other = 0x01,
/// <summary>
/// Represents the meaning "Unknown".
/// </summary>
Unknown = 0x02,
/// <summary>
/// Represents the meaning "Enabled".
/// </summary>
Enabled = 0x03,
/// <summary>
/// Represents the meaning "Disabled".
/// </summary>
Disabled = 0x04,
/// <summary>
/// Represents the meaning "Not Applicable".
/// </summary>
NotApplicable = 0x05
}
private ManagementBaseObject managementObject;
public Win32_PnPEntity(ManagementBaseObject managementObject)
{
if (managementObject == null)
{
throw new ArgumentNullException(nameof(managementObject));
}
this.managementObject = managementObject;
}
public AvailabilityEnum Availability
{
get
{
try
{
Int16 value = (Int16)this.managementObject.GetPropertyValue("Availability");
if (!Enum.IsDefined(typeof(AvailabilityEnum), value))
{
// Handle not supported values here
throw new NotSupportedException($"The value {value} is not supported for conversion to the {nameof(AvailabilityEnum)} enumeration.");
}
return (AvailabilityEnum)value;
}
catch
{
// Handle exception caused by accessing the property value.
return AvailabilityEnum.Unknown;
}
}
}
public string Caption
{
get
{
try
{
return (string)this.managementObject.GetPropertyValue("Caption");
}
catch
{
// Handle exception caused by accessing the property value.
return "Unknown";
}
}
}
public string ClassGuid
{
get
{
try
{
return (string)this.managementObject.GetPropertyValue("ClassGuid");
}
catch
{
// Handle exception caused by accessing the property value.
return "Unknown";
}
}
}
public string[] CompatibleID
{
get
{
try
{
string[] value = (string[])this.managementObject.GetPropertyValue("ClassGuid");
if (value == null)
// Handle null value.
return new string[0];
return value;
}
catch
{
// Handle exception caused by accessing the property value.
return new string[0];
}
}
}
public ConfigManagerErrorCodeEnum ConfigManagerErrorCode
{
get
{
try
{
Int16 value = (Int16)this.managementObject.GetPropertyValue("ConfigManagerErrorCode");
if (!Enum.IsDefined(typeof(ConfigManagerErrorCodeEnum), value))
{
// Handle not supported values here
throw new NotSupportedException($"The value {value} is not supported for conversion to the {nameof(ConfigManagerErrorCodeEnum)} enumeration.");
}
return (ConfigManagerErrorCodeEnum)value;
}
catch
{
// Handle exception caused by accessing the property value.
return ConfigManagerErrorCodeEnum.Unknown;
}
}
}
public bool ConfigManagerUserConfig
{
get
{
try
{
return (bool)this.managementObject.GetPropertyValue("ConfigManagerUserConfig");
}
catch
{
// Handle exception caused by accessing the property value.
return false;
}
}
}
public string CreationClassName
{
get
{
try
{
return (string)this.managementObject.GetPropertyValue("CreationClassName");
}
catch
{
// Handle exception caused by accessing the property value.
return "Unknown";
}
}
}
public string Description
{
get
{
try
{
return (string)this.managementObject.GetPropertyValue("Description");
}
catch
{
// Handle exception caused by accessing the property value.
return "Unknown";
}
}
}
public string DeviceID
{
get
{
try
{
return (string)this.managementObject.GetPropertyValue("DeviceID");
}
catch
{
// Handle exception caused by accessing the property value.
return "Unknown";
}
}
}
public bool ErrorCleared
{
get
{
try
{
return (bool)this.managementObject.GetPropertyValue("ErrorCleared");
}
catch
{
// Handle exception caused by accessing the property value.
return false;
}
}
}
public string ErrorDescription
{
get
{
try
{
return (string)this.managementObject.GetPropertyValue("ErrorDescription");
}
catch
{
// Handle exception caused by accessing the property value.
return "Unknown";
}
}
}
public string[] HardwareID
{
get
{
try
{
string[] value = (string[])this.managementObject.GetPropertyValue("HardwareID");
if (value == null)
// Handle null value.
return new string[0];
return value;
}
catch
{
// Handle exception caused by accessing the property value.
return new string[0];
}
}
}
public DateTime InstallDate
{
get
{
try
{
DateTime value = (DateTime)this.managementObject.GetPropertyValue("InstallDate");
if (value == null)
// Handle null value.
return DateTime.MinValue;
return value;
}
catch
{
// Handle exception caused by accessing the property value.
return DateTime.MinValue;
}
}
}
public UInt32 LastErrorCode
{
get
{
try
{
return (UInt32)this.managementObject.GetPropertyValue("LastErrorCode");
}
catch
{
// Handle exception caused by accessing the property value.
return 0;
}
}
}
public string Manufacturer
{
get
{
try
{
return (string)this.managementObject.GetPropertyValue("Manufacturer");
}
catch
{
// Handle exception caused by accessing the property value.
return "Unknown";
}
}
}
public string Name
{
get
{
try
{
return (string)this.managementObject.GetPropertyValue("Name");
}
catch
{
// Handle exception caused by accessing the property value.
return "Unknown";
}
}
}
public string PNPClass
{
get
{
try
{
return (string)this.managementObject.GetPropertyValue("PNPClass");
}
catch
{
// Handle exception caused by accessing the property value.
return "Unknown";
}
}
}
public string PNPDeviceID
{
get
{
try
{
return (string)this.managementObject.GetPropertyValue("PNPDeviceID");
}
catch
{
// Handle exception caused by accessing the property value.
return "Unknown";
}
}
}
public UInt16[] PowerManagementCapabilities
{
get
{
try
{
UInt16[] value = (UInt16[])this.managementObject.GetPropertyValue("PowerManagementCapabilities");
if (value == null)
// Handle null value.
return new UInt16[0];
return value;
}
catch
{
// Handle exception caused by accessing the property value.
return new UInt16[0];
}
}
}
public bool PowerManagementSupported
{
get
{
try
{
return (bool)this.managementObject.GetPropertyValue("PowerManagementSupported");
}
catch
{
// Handle exception caused by accessing the property value.
return false;
}
}
}
public bool Present
{
get
{
try
{
return (bool)this.managementObject.GetPropertyValue("Present");
}
catch
{
// Handle exception caused by accessing the property value.
return false;
}
}
}
public string Service
{
get
{
try
{
return (string)this.managementObject.GetPropertyValue("Service");
}
catch
{
// Handle exception caused by accessing the property value.
return "Unknown";
}
}
}
public string Status
{
get
{
try
{
return (string)this.managementObject.GetPropertyValue("Status");
}
catch
{
// Handle exception caused by accessing the property value.
return "Unknown";
}
}
}
public StatusInfoEnum StatusInfo
{
get
{
try
{
Int16 value = (Int16)this.managementObject.GetPropertyValue("StatusInfo");
if (!Enum.IsDefined(typeof(StatusInfoEnum), value))
{
// Handle not supported values here
throw new NotSupportedException($"The value {value} is not supported for conversion to the {nameof(StatusInfoEnum)} enumeration.");
}
return (StatusInfoEnum)value;
}
catch
{
// Handle exception caused by accessing the property value.
return StatusInfoEnum.NotApplicable;
}
}
}
public string SystemCreationClassName
{
get
{
try
{
return (string)this.managementObject.GetPropertyValue("SystemCreationClassName");
}
catch
{
// Handle exception caused by accessing the property value.
return "Unknown";
}
}
}
public string SystemName
{
get
{
try
{
return (string)this.managementObject.GetPropertyValue("SystemName");
}
catch
{
// Handle exception caused by accessing the property value.
return "Unknown";
}
}
}
}
In Console application, add refrence-->.Net-->system.Management-->add
namespace ConsoleApplication1
{
using System;
using System.Collections.Generic;
using System.Management; // need to add System.Management to your project references.
class Program
{
static void Main(string[] args)
{
var usbDevices = GetUSBDevices();
foreach (var usbDevice in usbDevices)
{
Console.WriteLine("Device ID: {0}, PNP Device ID: {1}, Description: {2}",
usbDevice.DeviceID, usbDevice.PnpDeviceID, usbDevice.Description);
}
Console.Read();
}
static List<USBDeviceInfo> GetUSBDevices()
{
List<USBDeviceInfo> devices = new List<USBDeviceInfo>();
ManagementObjectCollection collection;
using (var searcher = new ManagementObjectSearcher(#"Select * From Win32_USBHub"))
collection = searcher.Get();
foreach (var device in collection)
{
devices.Add(new USBDeviceInfo(
(string)device.GetPropertyValue("DeviceID"),
(string)device.GetPropertyValue("PNPDeviceID"),
(string)device.GetPropertyValue("Description")
));
}
collection.Dispose();
return devices;
}
}
class USBDeviceInfo
{
public USBDeviceInfo(string deviceID, string pnpDeviceID, string description)
{
this.DeviceID = deviceID;
this.PnpDeviceID = pnpDeviceID;
this.Description = description;
}
public string DeviceID { get; private set; }
public string PnpDeviceID { get; private set; }
public string Description { get; private set; }
}
}

Is there a newer way to accomplish what this article recommends?

I like the idea of this Agent Service that allows me to add jobs without affecting existing code, but the article is pretty old. Has something come along since 2005 that would be a better way of accomplishing the same thing?
I am limited to .Net, but I can use any version of the framework.
http://visualstudiomagazine.com/articles/2005/05/01/create-a-net-agent.aspx
Edit: Here is a summary of what I'm trying to do
Have a .Net service that can read a configuration file to dynamically load DLLs to perform tasks.
The service has a dynamic object loader that maps XML to classes in the DLLs to perform the tasks.
An example of the xml file and the class it maps is below. Basically, the service can read the Job from the xml, instantiate an instance of the CustomerAttendeeCreateNotificationWorker and call the Run() method. As a developer, I can make any number of these classes and implement whatever I want in the Run() method. Also note that the entry for "CustomerAttendanceNotificationTemplateName" gets assigned to a property with the same name on the instantiated object.
<?xml version="1.0" encoding="utf-8"?>
<Manager type="Task.RemotingManager, TaskClasses">
<Jobs type="Task.TaskJob, TaskBase" Name="CustomerAttendeeCreateNotificationWorker Worker">
<Worker type="TaskWorkers.CustomerAttendeeCreateNotificationWorker, TaskWorkers"
Description="Inserts records into NotificationQueue for CustomerAttendees"
CustomerAttendanceNotificationTemplateName ="CustomerAttendanceNotificationTemplateName"
/>
<Schedulers type="Task.FixedIntervalScheduler, TaskClasses"
DaysOfWeek="Everyday"
Frequency="00:05:00"
StartTime="00:00:00"
EndTime="23:55:00"
/>
</Jobs>
<Jobs type="Task.TaskJob, TaskBase" Name="SendNotificationWorker Worker">
<Worker type="TaskWorkers.SendNotificationWorker, TaskWorkers"
Description="Sends messages from NotificationQueue"
/>
<Schedulers type="Task.FixedIntervalScheduler, TaskClasses"
DaysOfWeek="Everyday"
Frequency="00:05:00"
StartTime="00:00:00"
EndTime="23:55:00"
/>
</Jobs>/>
</Manager>
/// <summary>
/// The customer attendee create notification worker.
/// </summary>
[Serializable]
public class CustomerAttendeeCreateNotificationWorker : TaskWorker
{
#region Constants
/// <summary>
/// The stat e_ critical.
/// </summary>
private const int StateCritical = 1;
/// <summary>
/// The stat e_ ok.
/// </summary>
private const int StateOk = 0;
#endregion
#region Fields
/// <summary>
/// The customer notification setting name.
/// </summary>
private string customerAttendanceNotificationTemplateName;
#endregion
#region Public Properties
/// <summary>
/// The customer notification setting name.
/// </summary>
public string CustomerAttendanceNotificationTemplateName
{
get
{
return this.customerAttendanceNotificationTemplateName;
}
set
{
this.customerAttendanceNotificationTemplateName = value;
}
}
/// <summary>
/// Gets or sets the logger.
/// </summary>
public ILogger Logger { get; set; }
#endregion
#region Public Methods and Operators
/// <summary>
/// The run.
/// </summary>
/// <returns>
/// The <see cref="TaskResult" />.
/// </returns>
public override TaskResult Run()
{
StringBuilder errorStringBuilder = new StringBuilder();
string errorLineTemplate = "Time: {0} Database: {1} Error Message: {2}" + Environment.NewLine;
bool isError = false;
IUnitOfWork configUnitOfWork = new GenericUnitOfWork<DCCShowConfigContext>();
TradeshowService tradeshowService = new TradeshowService(configUnitOfWork);
List<Tradeshow> tradeshows = tradeshowService.GetAll().Where(t => t.Status == "OPEN").ToList();
string connectionStringTemplate = ConfigurationManager.ConnectionStrings["DCCTradeshow"].ConnectionString;
int customerNotificationSettingGroupId = Convert.ToInt32(ConfigurationManager.AppSettings["NotificationSettingGroupId"]);
foreach (Tradeshow tradeshow in tradeshows)
{
try
{
string connectionString = string.Format(connectionStringTemplate, tradeshow.DbName);
IUnitOfWork unitOfWork = new TradeshowUnitOfWork(connectionString);
NotificationService notificationService = new NotificationService(unitOfWork);
notificationService.InsertCustomerAttendanceNotifications(tradeshow.TradeshowId, customerNotificationSettingGroupId, this.customerAttendanceNotificationTemplateName);
}
catch (Exception exception)
{
isError = true;
errorStringBuilder.AppendLine(string.Format(errorLineTemplate, DateTime.Now, tradeshow.DbName, exception.Message));
}
}
TaskResult result;
if (isError)
{
result = new TaskResult(StateOk, TaskResultStatus.Exception, "Errors Occured", errorStringBuilder.ToString());
}
else
{
result = new TaskResult(StateOk, TaskResultStatus.Ok,"All Good", "All Good");
}
return result;
}
#endregion
}`

How to get parent AppDomain?

I need to do something like this in c# (pseudo):
static var ns = new Non_Serializable_Nor_Marshal()
var app = new AppDomain();
app.execute(foo)
void foo()
{
var host = AppDomain.Current.Parent; //e.g. the original one
host.execute(bar)
}
void bar()
{
ns.Something();
}
IOW I have a non serializeable nor marshal object in one appdomain.
I want to create a second domain and execute foo(). From within that second domain I want to execute bar() on the original domain.
How do I pass the original domain to the child one?
If you don't want to use interop, you can also use a little trick using AppDomainManager. You can basically automatically 'wire' the 'primary' domain into any domains automatically - albiet the way I do it means you discard your real primary domain.
Here is the class that does all the magic:
/// <summary>
/// Represents a <see cref="AppDomainManager"/> that is
/// aware of the primary application AppDomain.
/// </summary>
public class PrimaryAppDomainManager : AppDomainManager
{
private static AppDomain _primaryDomain;
/// <summary>
/// Gets the primary domain.
/// </summary>
/// <value>The primary domain.</value>
public static AppDomain PrimaryDomain
{
get
{
return _primaryDomain;
}
}
/// <summary>
/// Sets the primary domain.
/// </summary>
/// <param name="primaryDomain">The primary domain.</param>
private void SetPrimaryDomain(AppDomain primaryDomain)
{
_primaryDomain = primaryDomain;
}
/// <summary>
/// Sets the primary domain to self.
/// </summary>
private void SetPrimaryDomainToSelf()
{
_primaryDomain = AppDomain.CurrentDomain;
}
/// <summary>
/// Determines whether this is the primary domain.
/// </summary>
/// <value>
/// <see langword="true"/> if this instance is the primary domain; otherwise, <see langword="false"/>.
/// </value>
public static bool IsPrimaryDomain
{
get
{
return _primaryDomain == AppDomain.CurrentDomain;
}
}
/// <summary>
/// Creates the initial domain.
/// </summary>
/// <param name="friendlyName">Name of the friendly.</param>
/// <param name="securityInfo">The security info.</param>
/// <param name="appDomainInfo">The AppDomain setup info.</param>
/// <returns></returns>
public static AppDomain CreateInitialDomain(string friendlyName, Evidence securityInfo, AppDomainSetup appDomainInfo)
{
if (AppDomain.CurrentDomain.DomainManager is PrimaryAppDomainManager)
return null;
appDomainInfo = appDomainInfo ?? new AppDomainSetup();
appDomainInfo.AppDomainManagerAssembly = typeof(PrimaryAppDomainManager).Assembly.FullName;
appDomainInfo.AppDomainManagerType = typeof(PrimaryAppDomainManager).FullName;
var appDomain = AppDomainManager.CreateDomainHelper(friendlyName, securityInfo, appDomainInfo);
((PrimaryAppDomainManager)appDomain.DomainManager).SetPrimaryDomainToSelf();
_primaryDomain = appDomain;
return appDomain;
}
/// <summary>
/// Returns a new or existing application domain.
/// </summary>
/// <param name="friendlyName">The friendly name of the domain.</param>
/// <param name="securityInfo">An object that contains evidence mapped through the security policy to establish a top-of-stack permission set.</param>
/// <param name="appDomainInfo">An object that contains application domain initialization information.</param>
/// <returns>A new or existing application domain.</returns>
/// <PermissionSet>
/// <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="ControlEvidence, ControlAppDomain, Infrastructure"/>
/// </PermissionSet>
public override AppDomain CreateDomain(string friendlyName, Evidence securityInfo, AppDomainSetup appDomainInfo)
{
appDomainInfo = appDomainInfo ?? new AppDomainSetup();
appDomainInfo.AppDomainManagerAssembly = typeof(PrimaryAppDomainManager).Assembly.FullName;
appDomainInfo.AppDomainManagerType = typeof(PrimaryAppDomainManager).FullName;
var appDomain = base.CreateDomain(friendlyName, securityInfo, appDomainInfo);
((PrimaryAppDomainManager)appDomain.DomainManager).SetPrimaryDomain(_primaryDomain);
return appDomain;
}
}
And you need to alter your Main() (application entry) slightly:
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main(string[] args)
{
new Program().Run(args);
}
void Run(string[] args)
{
var domain = PrimaryAppDomainManager.CreateInitialDomain("PrimaryDomain", null, null);
if (domain == null)
{
// Original Main() code here.
}
else
{
domain.CreateInstanceAndUnwrap<Program>().Run(args);
}
}
Now at any point you can get PrimaryAppDomainManager.PrimaryDomain to get a reference to the primary domain, just remember that it isn't the inital domain created by the .Net runtime - it's one we create immediately.
You can look at the comments in my blog post for an way to get the .Net runtime to hook this in for you automatically using the app.config.
Edit: I forgot to add the extension method I use, here it is:
/// <summary>
/// Creates a new instance of the specified type.
/// </summary>
/// <typeparam name="T">The type of object to create.</typeparam>
/// <param name="appDomain">The app domain.</param>
/// <returns>A proxy for the new object.</returns>
public static T CreateInstanceAndUnwrap<T>(this AppDomain appDomain)
{
var res = (T)appDomain.CreateInstanceAndUnwrap(typeof(T));
return res;
}
You could try referencing mscoree and then using its methods. I have used this in one of my projects.
mscoree will keep track of your AppDomains without any input.
/// <summary>
/// Returns the primary application domain.
/// </summary>
/// <returns>The primary application domain.</returns>
public static AppDomain GetPrimaryAppDomain()
{
return GetAppDomain(Process.GetCurrentProcess().MainModule.ModuleName);
}
/// <summary>
/// Returns the application domain with the given friendly name.
/// </summary>
/// <param name="friendlyName">The friendly name of the application domain.</param>
/// <returns>The application domain with the given friendly name.</returns>
/// <exception cref="System.ArgumentNullException">Thrown if friendlyName is null.</exception>
public static AppDomain GetAppDomain(string friendlyName)
{
if (friendlyName == null)
{
throw new ArgumentNullException("friendlyName");
}
IntPtr handle = IntPtr.Zero;
CorRuntimeHostClass host = new CorRuntimeHostClass();
try
{
host.EnumDomains(out handle);
object domain = null;
while (true)
{
host.NextDomain(handle, out domain);
if (domain == null)
{
return null;
}
AppDomain appDomain = (AppDomain)domain;
if (appDomain.FriendlyName == friendlyName)
{
return appDomain;
}
}
}
finally
{
host.CloseEnum(handle);
Marshal.ReleaseComObject(host);
host = null;
}
}
(Adapted from http://www.dolittle.com/blogs/einar/archive/2007/05/18/cross-appdomain-singleton.aspx)

Stored Procedure loses connection

I have an ASP.NET MVC project in which the model is managed through .NET entities and it seems that some times it loses the connection, but this happens only on stored procedures.
I get the following error:
Execution of the command requires an open and available connection. The connection's current state is broken.
Why is this happening?
Code
public ObjectResult<Categories> GetCategoriesStructure() {
return ObjectContext.getCategoriesStructure();
}
var catss = GetCategoriesStructure().ToList();
this exception occurs when I am trying to assign the List to catss variable
Object Context Instantiation
public abstract class ObjectContextManager {
/// <summary>
/// Returns a reference to an ObjectContext instance.
/// </summary>
public abstract TObjectContext GetObjectContext<TObjectContext>()
where TObjectContext : ObjectContext, new();
}
public abstract class BaseDAO<TObjectContext, TEntity> : IBaseDAO<TObjectContext, TEntity>
where TObjectContext : System.Data.Objects.ObjectContext, new()
where TEntity : System.Data.Objects.DataClasses.EntityObject {
private ObjectContextManager _objectContextManager;
/// <summary>
/// Returns the current ObjectContextManager instance. Encapsulated the
/// _objectContextManager field to show it as an association on the class diagram.
/// </summary>
private ObjectContextManager ObjectContextManager {
get { return _objectContextManager; }
set { _objectContextManager = value; }
}
/// <summary>
/// Returns an ObjectContext object.
/// </summary>
protected internal TObjectContext ObjectContext {
get {
if (ObjectContextManager == null)
this.InstantiateObjectContextManager();
return ObjectContextManager.GetObjectContext<TObjectContext>();
}
}
/// <summary>
/// Default constructor.
/// </summary>
public BaseDAO() { }
/// <summary>
/// Instantiates a new ObjectContextManager based on application configuration settings.
/// </summary>
private void InstantiateObjectContextManager() {
/* Retrieve ObjectContextManager configuration settings: */
Hashtable ocManagerConfiguration = ConfigurationManager.GetSection("ObjectContextManagement.ObjectContext") as Hashtable;
if (ocManagerConfiguration != null && ocManagerConfiguration.ContainsKey("managerType")) {
string managerTypeName = ocManagerConfiguration["managerType"] as string;
if (string.IsNullOrEmpty(managerTypeName))
throw new ConfigurationErrorsException("The managerType attribute is empty.");
else
managerTypeName = managerTypeName.Trim().ToLower();
try {
/* Try to create a type based on it's name: */
Assembly frameworkAssembly = Assembly.GetAssembly(typeof(ObjectContextManager));
Type managerType = frameworkAssembly.GetType(managerTypeName, true, true);
/* Try to create a new instance of the specified ObjectContextManager type: */
this.ObjectContextManager = Activator.CreateInstance(managerType) as ObjectContextManager;
} catch (Exception e) {
throw new ConfigurationErrorsException("The managerType specified in the configuration is not valid.", e);
}
} else
throw new ConfigurationErrorsException("ObjectContext tag or its managerType attribute is missing in the configuration.");
}
/// <summary>
/// Persists all changes to the underlying datastore.
/// </summary>
public void SaveAllObjectChanges() {
this.ObjectContext.SaveChanges();
}
/// <summary>
/// Adds a new entity object to the context.
/// </summary>
/// <param name="newObject">A new object.</param>
public virtual void Add(TEntity newObject) {
this.ObjectContext.AddObject(newObject.GetType().Name, newObject);
}
/// <summary>
/// Deletes an entity object.
/// </summary>
/// <param name="obsoleteObject">An obsolete object.</param>
public virtual void Delete(TEntity obsoleteObject) {
this.ObjectContext.DeleteObject(obsoleteObject);
}
public void Detach(TEntity obsoleteObject) {
this.ObjectContext.Detach(obsoleteObject);
}
/// <summary>
/// Updates the changed entity object to the context.
/// </summary>
/// <param name="newObject">A new object.</param>
public virtual void Update(TEntity newObject) {
ObjectContext.ApplyPropertyChanges(newObject.GetType().Name, newObject);
ObjectContext.Refresh(RefreshMode.ClientWins, newObject);
}
public virtual TEntity LoadByKey(String propertyName, Object keyValue) {
IEnumerable<KeyValuePair<string, object>> entityKeyValues =
new KeyValuePair<string, object>[] {
new KeyValuePair<string, object>(propertyName, keyValue) };
// Create the key for a specific SalesOrderHeader object.
EntityKey key = new EntityKey(this.ObjectContext.GetType().Name + "." + typeof(TEntity).Name, entityKeyValues);
return (TEntity)this.ObjectContext.GetObjectByKey(key);
}
#region IBaseDAO<TObjectContext,TEntity> Members
public bool validation(TEntity newObject) {
return newObject.GetType().Name.ToString() == "Int32";
}
#endregion
}
Without knowing how you are instantiating your ObjectContext, I'll throw something in the answer bucket here.
This is how I do my Entity Framework commands and connections (for small simple projects at least):
using (MyEntities context = new MyEntities())
{
return context.getCategoriesStructure();
}
You can also optionally pass in a connection string when instantiating your context (if not, it will use the one in your app.config):
new MyEntities("...connection string...")
If this does not help your issue, please help us understand your code a little better by posting how you are creating your ObjectContext. You could at least attempt to do it this way to see if it works; that will tell you whether it is an issue with your connection string or not.

Categories

Resources