How to unit test unformatted method in mvc? - unit-testing

I have to unit test one of the very big and unformatted method in ASP.NET MVC 4.0.
Below is the code of action method :-
public ActionResult GetDetails(string ProdName, int ProdArea, int ProdAreaId= 0)
{
if (ProdAreaId == 0 && ProdArea == 1 && System.Web.HttpContext.Current.Session["ResponseProdAreaId"] != null)
{
ProdAreaId = (int)System.Web.HttpContext.Current.Session["ResponseProdAreaId"];
}
if (string.IsNullOrEmpty(ProdName))
{
if (System.Web.HttpContext.Current.Session["ProdName"] == null)
{
ProdName = Guid.NewGuid().ToString();
System.Web.HttpContext.Current.Session["ProdName"] = ProdName;
}
else
{
ProdName = System.Web.HttpContext.Current.Session["ProdName"].ToString();
}
}
else
{
ProdName = ProdName.Replace("___", " ");
}
List<StateDetail> stateList = ProductService.GetAllStates().Where(n => n.FKCountryID == (int)Countries.UnitedStates).ToList();
ProductAddressViewModel model = new ProductAddressViewModel
{
ProdArea = ProdArea,
FKProductID = CurrentProductId
};
model.States = stateList != null ? new SelectList(stateList, "StateID", "StateCode") : null;
if (System.Web.HttpContext.Current.Session[“ProdAddresses”] != null && ProdAreaId == 0 && ProdArea == 1)
{
List<ProductAddressDto> lstprodaddresses = (List<ProductAddressDto>)System.Web.HttpContext.Current.Session[“ProdAddresses”];
if (lstprodaddresses.Count > 0)
{
AddressDto addrDto = lstprodaddresses.First().Address;
//save address in DB
model.Address1 = addrDto.Address1;
model.Address2 = addrDto.Address2;
model.ProdArea = 1;
model.City = addrDto.City;
model.IsDefault = true;
model.ProdName = model.ProdName;
model.SelectedAddressTypeID = (int)AddressType.Street;
model.ZIPCode = addrDto.ZIPCode;
model.SelectedStateId = addrDto.FKStateID;
model.AddressTypes = GetAddressTypes();
}
}
else if (model.FKProductID > 0)
{
ToolDto tool = ToolService.GetToolDetails(model.FKProductID);
if (ProdAreaId > 0)
{
model.AddressTypes = GetAddressTypes();
ProductAddressDto prodaddr = tool.ToolAddresses.First(n => n.Tool_AddressID == ProdAreaId);
model.Address1 = prodaddr.Address.Address1;
model.Address2 = prodaddr.Address.Address2;
model.City = prodaddr.Address.City;
model.SelectedStateId = prodaddr.Address.FKStateID;
model.ZIPCode = prodaddr.Address.ZIPCode;
model.SelectedAddressTypeID = prodaddr.Address.FKAddressTypeID;
model.IsDefault = prodaddr.IsDefault;
model.FKAddressID = prodaddr.FKAddressID;
model.Tool_AddressID = prodaddr.Tool_AddressID;
model.FKProductID = prodaddr.FKProductID;
model.AddressTypes = GetAddressTypes();
}
else
{
//address types
List<int> excludeAddrTypes = new List<int>();
foreach (ProductAddressDto prodadrdto in tool.ToolAddresses)
{
if (prodadrdto.Tool_AddressID != ProdAreaId)
{
excludeAddrTypes.Add(prodadrdto.Address.FKAddressTypeID);
}
}
if (System.Web.HttpContext.Current.Session[“ProdAddresses”] != null)
{
excludeAddrTypes.Add((int)AddressType.Street);
}
var addrtypes = from AddressType e in Enum.GetValues(typeof(AddressType))
where !excludeAddrTypes.Contains((int)e)
select new { Id = (int)e, Name = e.ToString() };
model.AddressTypes = addrtypes.Select(x => new SelectListItem
{
Value = x.Id.ToString(),
Text = x.Name
});
if (tool.ToolAddresses.Count == 0)
{
model.IsDefault = (ProdArea == 1);
}
}
}
else
{
//filter out address types if responsed tool is there
if (System.Web.HttpContext.Current.Session[“ProdAddresses”] != null)
{
List<int> excludeAddrTypes = new List<int>();
excludeAddrTypes.Add((int)AddressType.Street);
var addrtypes = from AddressType e in Enum.GetValues(typeof(AddressType))
where !excludeAddrTypes.Contains((int)e)
select new { Id = (int)e, Name = e.ToString() };
model.AddressTypes = addrtypes.Select(x => new SelectListItem
{
Value = x.Id.ToString(),
Text = x.Name
});
}
else
{
model.AddressTypes = GetAddressTypes();
}
model.IsDefault = (ProdArea == 1);
}
model.ProdName = ProdName;
return PartialView("_AddUpdateAddress", model);
}
May be the method is not in correct format.But i have to do it's unit testing.I have do that in several different ways.But i am not sure about its correctness.
I want to know that how should we do unit test for such a big and unformatted method like this.
Can anyone help me out on this ?
Below is the piece of code of my unit testing method :-
[TestMethod]
public void GetDetailsTest_NotEmpty()
{
var ProdName = random.ToString();
var ProdArea = random.Next();
var ProdAreaId = 0;
var _toolServiceMock = new Mock<IToolService>();
var _lookupServiceMock = new Mock<ILookupService>();
var stateList = new List<StateDto> {
new StateDto() { StateID = random.Next(), StateCode = Guid.NewGuid().ToString(), Description = random.ToString(), FKCountryID = 1 },
new StateDto() { StateID = random.Next(), StateCode = Guid.NewGuid().ToString(), Description = random.ToString(), FKCountryID = random.Next() },
};
_lookupServiceMock.Setup(s => s.GetAllStates()).Returns(stateList); // .Returns(stateList);
//Arrange
CustomerDto cust = _toolService.LookupCustomers("", "").FirstOrDefault();
if (cust != null)
{
ToolDto tool = _toolService.GetToolDetails(cust.Tool.toolId);
if (tool.ToolAddresses.Count > 0 && tool.ToolAddresses.First().Address != null)
{
HttpContext.Current.Session["FKToolID"] = cust.FKToolID;
var controller = new ToolController(_toolServiceMock.Object);
PartialViewResult result = controller.SelectAddress(cust.Tool.Name, 1, tool.ToolAddresses.First().Tool_AddressID) as PartialViewResult;
var viewmodel = (ToolAddressViewModel)((ViewResultBase)(result)).Model;
if (viewmodel != null)
{
//Act
Assert.AreEqual(tool.ToolAddresses.First().Address.Address1, viewmodel.Address1);
Assert.AreEqual("_AddUpdateAddress", result.ViewName);
Assert.IsInstanceOfType(viewmodel, typeof(ToolAddressViewModel));
}
//Act
_lookupServiceMock.VerifyAll();
}
}
}

First of all, your method is a way too complicated and too long.
Make your actions short and with one responsability to respect SOLID principle (http://en.wikipedia.org/wiki/SOLID_(object-oriented_design)).
It will make your methods easier to test.
To help you with your problem, you can do something quick with your code :
Split your actions in internal virtual methods with one concern each (internal to be testable in you unit test project, virtual to be able to mock them).
Put the [assembly: InternalsVisibleTo("YourTestAssembly")] on your controller
In your unit test project, you will be able to test any of your internal methods separately.
Finally, to test your action, use a mocking framework (RhinoMock, Moq) to mock all your internal virtual methods and test the logic of your action (proxies generated by mocking framework will let you mock virtual methods).
It's the easiest way you can test your logic without breaking the existing application.
The inconvenient is that your logic is in internal methods wich let the whole assembly able to use it.

Related

How to use Moq in unit test that calls another method in same EF Repository

In my project I am using Repository.
I'm trying to unit test a SAVE method and return the call value via the Get method.
I'm having a Repository Query mockup issue when calling through the Get method. Can you help me in this case?
I Have a Class:
public class ClientRoleBo
{
private readonly IRepositoryBase<ClientRole> _repository;
public ClientRoleBo(IRepositoryBase<ClientRole> repository)
{
_repository = repository;
}
public Task<ClientRoleResp?> Get(Guid clientGuid, Guid id)
{
return Task.FromResult(
_repository.Query(x => x.Guid == id && x.ClientGuid == clientGuid && !x.IsDeleted)
.Select(x => new ClientRoleResp
{
Code = x.Code,
Guid = x.Guid,
IsActive = x.IsActive,
Name = x.Name
}).FirstOrDefault()
);
}
public async Task<ClientRole> Save(Guid clientGuid, Guid? guid, ClientRoleReq req)
{
ClientRole? data = null;
var existItem = _repository.Query(x => x.Code == req.Code && x.ClientGuid == clientGuid).FirstOrDefault();
if (existItem != null)
throw new HttpResponseException(400, "Exist clientrole");
data = new()
{
Code = req.Code,
Name = req.Name,
IsActive = req.IsActive,
ModifiedDate = DateTime.Now,
CreatedDate = DateTime.Now,
ClientGuid = clientGuid
};
await _repository.AddAsync(data);
return (await Get(clientGuid, data!.Guid))!;
}
}
I have a issue when code call method "Save" return data of method "Get" same Repository
My Mock Repository:
public class TestClientRole{
public static IRepositoryBase<TEntiy> MockRepo<TEntiy>(TEntiy[] data, Expression<Func<TEntiy, bool>> returnExpression = null) where TEntiy : class
{
var mock = new Mock<IRepositoryBase<TEntiy>>();
mock.Setup(x => x.Query(
It.IsAny<Expression<Func<TEntiy, bool>>>()
)).Returns(returnExpression != null ? data.AsQueryable().Where(returnExpression).AsEnumerable() : data.AsEnumerable());
return mock.Object;
}
[Fact]
public void Save()
{
var clientRoles = new ClientRole[]
{
new ClientRole
{
Code = "123",
Name = "Role1",
Guid = Guid.NewGuid(),
},
new ClientRole
{
Code = "1234",
Name = "Role2",
Guid = Guid.NewGuid(),
}
};
var mockRepo = MockRepo<ClientRole>(clientRoles, x => x.Guid == clientRoles[0].Guid);
var svc = new ClientRoleBo(mockRepo);
var res = svc.Save(Guid.NewGuid, null, new ClientRoleReq { Code = "Role", Name = "Role" }).GetAwaiter().GetResult();
Assert.True(res.Guid == clientRoles[0].Guid);
}
}

Unit test All the Actions Methods in Controller in .Net core visual studio 2017

public async Task<IActionResult> Create(DonorViewModel be, IFormFile pic)
{
be.RegCampId = Convert.ToInt32(TempData["Camp"]);
if (ModelState.IsValid)
{
DONOR entity = new DONOR();
#region Insert Entities
entity.Address = be.Address;
entity.BarCode = be.BarCode;
entity.BloodGroupId = be.BloodGroupId;
entity.CityId = be.CityId;
entity.CNIC = be.CNIC;
entity.DOB = be.DOB;
entity.Email = be.Email;
entity.EmergencyContact = be.EmergencyContact;
entity.FullName = be.FullName;
entity.GenderId = be.GenderId;
entity.HomeNo = be.HomeNo;
entity.IsActive = true;
entity.IsDeleted = false;
entity.LastDonDate = be.LastDonDate;
entity.MaritalStatus = be.MaritalStatus;
entity.MobileNo = be.MobileNo;
entity.Occupation = be.Occupation;
entity.PreDonCount = be.PreDonCount;
if (be.RegCampId != 0) { entity.RegCampId = be.RegCampId; entity.RegistrationTypeId = 3; }
if (be.RegLocId != 0) { entity.RegLocId = be.RegLocId; entity.RegistrationTypeId = 2; }
entity.SignPic = entity.SignPic;
entity.WhatsApp = be.WhatsApp;
entity.CreatedBy = (int)HttpContext.Session.GetInt32("UserId");
entity.CreatedDateTime = DateTime.Now;
#endregion
flag = await _donorContext.AddAsync(entity);
if (pic == null || pic.Length <= 0)
be.Pic = Path.Combine(_hostingEnvironment.WebRootPath, "images", "Avatar.png").Replace(_hostingEnvironment.WebRootPath, "").Replace("\\", "/");
if (pic != null && pic.Length > 0)
{
var path = Path.Combine(new string[]
{
_hostingEnvironment.WebRootPath,
"Reservoir","Donor",entity.Id.ToString(),
entity.Id + Path.GetExtension(pic.FileName)
});
Directory.CreateDirectory(Path.GetDirectoryName(path));
using (var stream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
pic.CopyTo(stream);
}
path = path.Replace(_hostingEnvironment.WebRootPath, "").Replace("\\", "/");
entity.Pic = path;
entity.CreatedBy = entity.CreatedBy;
entity.CreatedDateTime = entity.CreatedDateTime;
entity.IsActive = true;
entity.IsDeleted = false;
await _donorContext.UpdateAsync(entity);
}
if (flag)
{
TempData["Message"] = "Donor is Added Successfully.";
if (be.RegCampId != 0)
{
return RedirectToAction("Create", "Donor", new { CampId = be.RegCampId });
}
else
{
return RedirectToAction("Create", "Donor");
}
}
}
ViewData["RegCampId"] = new SelectList(_context.BLOOD_CAMP, "Id", "City", be.RegCampId);
ViewData["BloodGroupId"] = new SelectList(_bloodGroupContext.GetAll(), "Id", "Value", be.BloodGroupId);
ViewData["CityId"] = new SelectList(_cityContext.GetAll(), "Id", "Name", be.CityId);
ViewData["ScreenedBy"] = new SelectList(_context.EMPLOYEE, "Id", "FirstName", be.ScreenedBy);
ViewData["GenderId"] = new SelectList(_genderContext.GetAll(), "Id", "Name", be.GenderId);
ViewData["RegLocId"] = new SelectList(_locationService.GetAll(), "Id", "Name",be.RegLocId);
return View(be);
}
This is My Create method In Controller How to unit test it using UnitTest.
using HMS_Presentation.Controllers;
using Microsoft.AspNetCore.Mvc;
using Microsoft.VisualStudio.TestTools.UnitTesting;
//Unit Test code .
namespace HMS_UnitTest
{
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
DonorController Controller = new DonorController();
ViewResult result = Controller.Create() as ViewResult;
Assert.AreEqual("",????);
}
}
}
This is my unit test class code how to use my controller object to check the actions and test it . WHAT should i write the in the assert . I SEARCH it on internet but do not find any proper solution kindly check the code below and tell me what should i have to write in the assert . i am using visual studio 2017 and .NET CORE 2.0 AND adding a project of unit test in my solution.
The link i followed .
https://learn.microsoft.com/en-us/visualstudio/test/getting-started-with-unit-testing?view=vs-2017
In ASPNET Core 2.1 introduced feature called Functional testing of MVC applications.
To help streamline in-memory end-to-end testing of MVC applications using TestServer.
See below example
using Xunit;
namespace TestingMvc.Tests
{
public class TestingMvcFunctionalTests : IClassFixture<WebApplicationTestFixture<Startup>>
{
public TestingMvcFunctionalTests(WebApplicationTestFixture<Startup> fixture)
{
Client = fixture.CreateClient();
}
public HttpClient Client { get; }
[Fact]
public async Task GetHomePage()
{
// Arrange & Act
var response = await Client.GetAsync("/");
// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
}
}
}
Read more about functional testing of MVC Applications click here

How to edit a list of object c# linq to xml

<Team Side="Home" TeamRef="ref123">
<Goal PlayerRef="p1111" Time="10" >
<Assist PlayerRef="p9999">p9999</Assist>
</Goal>
<Goal PlayerRef="p4444" Time="11" >
<Assist PlayerRef="p9999">p9999</Assist>
</Goal>
<Goal PlayerRef="p7777 Time="13" >
<Assist PlayerRef="p9999">p9999</Assist>
</Goal>
<Goal PlayerRef="p7777 Time="17" >
<Assist PlayerRef="p9999">p9999</Assist>
</Goal>
</Team>
public void GetScorer(string side, string OCompetition, string OSeason, string OGameId)
{
try
{
var xDoc = XDocument.Load(test);
var query = from q in xDoc.Descendants("Team")
where (string)q.Attribute("Side") == side
from d in q.Elements("Goal")
select new
{
TeamRef = q.Attribute("TeamRef").Value,
PlayerRef = d.Attribute("PlayerRef").Value,
Time = d.Attribute("Time").Value
};
var count = 0;
foreach (var qq in query)
{
if (side == "Home")
{
if (HomeSlateScorerList[count].PlayerRef != qq.PlayerRef)
{
HomeSlateScorerList.Add(new Scorer() { PlayerRef = qq.PlayerRef, Time = qq.Time, LastName = GetPlayerNameSlate(qq.PlayerRef, OSeason, OCompetition, OGameId) });
}
else
{
HomeSlateScorerList[count].Time = HomeSlateScorerList[count].Time + "' ";
}
}
if (side == "Away")
{
AwaySlateScorerList.Add(new Scorer() { PlayerRef = qq.PlayerRef, Time = qq.Time, LastName = GetPlayerNameSlate(qq.PlayerRef, OCompetition, OSeason, OGameId) });
}
count++;
}
}
catch (Exception)
{
// ignored
}
}
I would like to edit a player in a list of players
HomeSlateScorerList = new List<Scorer>();
AwaySlateScorerList = new List<Scorer>();
what I would like to achieve is for e.g. there are two players with the ref of "p7777" so in the list of object I would like to have one player with the playerref of "p7777" so if the player exist the format will be
playerref = "p7777"
Time = 13' 17'
or if one player its
Time = 13'
or if another goal is added to the xml its
Time = 13' 17' 25'
HomeSlateScorerList = HomeSlateScorerList
.GroupBy(s => s.PlayerRef)
.Select(g => new Scorer { PlayerRef = g.Key, Time = string.Join(", ", g.Select(v => v.Time)) })
.ToList();
Thanks to: #SergeyS SergeyS

How to sync WebService operation in silverlight?

I am now on a project regarding controlling devices by using silverlight and web service(asmx page). The project flows like below:
pressing the button on the silverlight UI, it will send out a package by using socket to the middlewire. Then middlewire will accept the package and deconstructe it and return back another package to silverlight UI. I am using below code to load the button state, which will trigger database query:
private ButtonStateModel _buttonStateModel = new ButtonStateModel() { BtnOneImage = "Skins/images/flag/QU.png", BtnOneVisible = Visibility.Visible, BtnTwoVisible = Visibility.Collapsed };
public ButtonStateModel ButtonStateModel
{
get
{
ButtonStateModel btnState = null;
dataService.GetPR(BusEquipmentPermission.Control.RtuID, BusEquipmentPermission.Control.DevID, (result) =>
{
if (result != null && result.Count > 0)
{
var flag = result[0].PRFlag;
if (flag == 1)
{
btnState = new ButtonStateModel()
{
BtnOneImage = "Skins/images/flag/OP.png",
BtnOneVisible = Visibility.Visible,
BtnTwoImage = "Skins/images/flag/OFF.png",
BtnTwoVisible = Visibility.Visible
};
}
else if (flag == 2)
{
btnState = new ButtonStateModel()
{
BtnOneImage = "Skins/images/flag/OFF.png",
BtnOneVisible = Visibility.Visible,
BtnTwoImage = "Skins/images/flag/OR.png",
BtnTwoVisible = Visibility.Visible
};
}
}
});
return btnState;
}
set
{
if (value == _buttonStateModel)
{
return;
}
_buttonStateModel = value;
RaisePropertyChanged("ButtonStateModel");
}
}
Now the problem is, whenever I load the silverlight app, the button on the UI can't load its state correctly. I know the reason is because that the GetPR function is from webservice(asmx), it's very oddly that I can't do sync operation by using AutoResetEvent in silverlight generated client code:
public void GetPR(string rtuID, string devID, Action<List<BusControlPR>> action)
{
ServiceSoapClient proxy = new ServiceSoapClient();
proxy.GetPRAsync(rtuID, devID);
proxy.GetPRCompleted += (sender, args) =>
{
//I cannt do Sync Operation Here by using AutoResetEvent.
if (action != null)
action(args.Result.ToList());
};
}
I am using webservice (asmx page) instead of WCF ria service.
Above problem is what i meet, Anyone can give me some light?
The "GetPR" method is still running asynchronously, so the "ButtonStateModel" getter will return null immediately (the "completed" action will then have no effect). And, you do not want to use any kind of blocking inside your getters, as that will block the UI. Instead, you should put the "GetPR" in the initialization, and use the to set the "ButtonStateModel" property to the appropriate value:
public class TheViewModel
{
public ButtonStateModel ButtonStateModel
{
get
{
return _buttonStateModel;
}
set
{
if (value == _buttonStateModel)
{
return;
}
_buttonStateModel = value;
RaisePropertyChanged("ButtonStateModel");
}
}
public TheViewModel()
{
Initialize();
}
private void Initialize()
{
dataService.GetPR(BusEquipmentPermission.Control.RtuID, BusEquipmentPermission.Control.DevID, (result) =>
{
ButtonStateModel btnState = null;
if (result != null && result.Count > 0)
{
var flag = result[0].PRFlag;
if (flag == 1)
{
btnState = new ButtonStateModel()
{
BtnOneImage = "Skins/images/flag/OP.png",
BtnOneVisible = Visibility.Visible,
BtnTwoImage = "Skins/images/flag/OFF.png",
BtnTwoVisible = Visibility.Visible
};
}
else if (flag == 2)
{
btnState = new ButtonStateModel()
{
BtnOneImage = "Skins/images/flag/OFF.png",
BtnOneVisible = Visibility.Visible,
BtnTwoImage = "Skins/images/flag/OR.png",
BtnTwoVisible = Visibility.Visible
};
}
}
ButtonStateModel = btnState;
});
}
}

Getting VMWare DataStore inventory data (inventory traversal)

I'm attempting to get typical properties (capacity, free space, name) from the DataStores in my VMware ESXi server. I'm having trouble getting the TraversalSpec, ObjectSpec and PropertySpecs.
Can someone please tell me what I'm doing wrong?
public void GetDataStoreValues()
{
PropertyFilterSpec spec = GetDataStoreQuery();
ObjectContent[] objectContent = _service.RetrieveProperties(_sic.propertyCollector, new[] { spec } );
foreach (ObjectContent content in objectContent)
{
if (content.obj.type == "DataStore")
{
//... get values
}
}
}
private PropertyFilterSpec GetDataStoreQuery()
{
try
{
// Traversal to get to the host from ComputeResource
TraversalSpec tSpec = new TraversalSpec
{
name = "HStoDS",
type = "HostSystem",
path = "dataStore",
skip = false
};
// Now create Object Spec
var objectSpec = new ObjectSpec
{
obj = _sic.rootFolder,
skip = true,
selectSet = new SelectionSpec[] { tSpec }
};
var objectSpecs = new[] { objectSpec };
// Create PropertyFilterSpec using the PropertySpec and ObjectPec
// created above.
// Create Property Spec
string[] propertyArray = new[] {
"summary.capacity"
,"summary.freeSpace"
,"summary.name"
};
var propertySpec = new PropertySpec
{
all = true,
pathSet = propertyArray,
type = "DataStore"
};
var propertySpecs = new[] { propertySpec };
var propertyFilterSpec = new PropertyFilterSpec
{
propSet = propertySpecs,
objectSet = objectSpecs
};
return propertyFilterSpec;
}
catch (Exception)
{
}
return null;
}
Also, are object type names case sensitive? I seem to see all sorts of cases when I look at samples.
Thanks for any suggestions.
First question: You can use the following code to get properties of DataStore. I tested this code on vCenter 5.1
public void Test()
{
var properties = GetProperties(
new ManagedObjectReference { type = "Datastore", Value = "<your_datastore_key>" },
new[] {"summary.capacity", "summary.freeSpace", "summary.name"});
}
private List<DynamicProperty> GetProperties(ManagedObjectReference objectRef, string[] properties)
{
var typesAndProperties = new Dictionary<string, string[]> { { objectRef.type, properties } };
var objectContents = RetrieveResults(typesAndProperties, new List<ManagedObjectReference> { objectRef });
return ExtractDynamicProperties(objectRef, objectContents);
}
private List<ObjectContent> RetrieveResults(Dictionary<string, string[]> typesAndProperties, List<ManagedObjectReference> objectReferences)
{
var result = new List<ObjectContent>();
var tSpec = new TraversalSpec { path = "view", skip = false };
var oSpec = new ObjectSpec { skip = true, selectSet = new SelectionSpec[] { tSpec } };
oSpec.obj = service.CreateListView(serviceContent.viewManager, objectReferences.ToArray());
tSpec.type = "ListView";
var fSpec = new PropertyFilterSpec
{
objectSet = new[] { oSpec },
propSet = typesAndProperties.Keys.Select(typeName => new PropertySpec { type = typeName, pathSet = typesAndProperties[typeName] }).ToArray()
};
PropertyFilterSpec[] pfs = { fSpec };
var retrieveResult = service.RetrievePropertiesEx(serviceContent.propertyCollector, pfs, new RetrieveOptions());
if (retrieveResult != null)
{
result.AddRange(retrieveResult.objects);
while (!String.IsNullOrEmpty(retrieveResult.token))
{
retrieveResult = service.ContinueRetrievePropertiesEx(serviceContent.propertyCollector, retrieveResult.token);
result.AddRange(retrieveResult.objects);
}
service.DestroyView(oSpec.obj);
}
return result;
}
private static List<DynamicProperty> ExtractDynamicProperties(ManagedObjectReference objectRef, IEnumerable<ObjectContent> objectContents)
{
var result = new List<DynamicProperty>();
foreach (var objectContent in objectContents)
{
if (objectContent.propSet == null) continue;
if (objectContent.obj == null) continue;
if (objectContent.obj.type != objectRef.type || objectContent.obj.Value != objectRef.Value) continue;
result.AddRange(objectContent.propSet);
}
return result;
}
How to run the sample:
Initialize the service by object of VimService class and the serviceContent by object of ServiceContent class.
Login to vCenter or ESX using service.
Replace <your_datastore_key> with Key of your datastore. You can use the Managed Object Browser to find keys of their datastores. To get a description of datastore object, go to following links in MOB: content -> rootFolder -> childEntity -> datastoreFolder -> childEntity. Value of "Managed Object ID" on top of page is correct Key (like datastore-46).
Second question: Yes, the type of ManagedObjectReference is case sensitive.