Windows Service connecting to TFS. IGroupSecurityService instance creation fails with no exception - c#

I have a running service installed on a Windows 2012 R2 server that is trying to connect to a TFS instance.
After turning on remote debugging and looking at the code I noticed the section below which had a breakpoint set. As soon as I clicked step into at the first line, the debugger behaves as though I clicked continue, I exit my step through and the debugger claims the code is continuing. There is no exception thrown.
This code is being called every five minute by a service timer, I think that the code called by the timer never completes, after the first line it experiences some kind of crash.
Five minutes later, the code tries to run again, so I know it has not crashed the entire service.
I have looked at the event view, there is not information there.
IGroupSecurityService gss = (IGroupSecurityService)TfsConfiguration._tfsProjectCollection.GetService(typeof(IGroupSecurityService));
Identity SIDS = gss.ReadIdentity(SearchFactor.AccountName, "Project Collection Valid Users", QueryMembership.Expanded);
List<Identity> FoundIds = gss.ReadIdentities(SearchFactor.Sid, SIDS.Members, QueryMembership.None).ToList();

Check this case:
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.Server;
using Microsoft.TeamFoundation.VersionControl.Client;
using Microsoft.TeamFoundation.Framework.Client;
namespace API
{
class Program
{
static void Main(string[] args)
{
string project = "http://xxx.xxx.xxx.xxx:8080/tfs";
TfsTeamProjectCollection tpc = new TfsTeamProjectCollection(new Uri(project));
var tps = tpc.GetService<VersionControlServer>();
var ttt = tps.GetTeamProject("ProjectName");
ISecurityService securityService = tpc.GetService<ISecurityService>();
System.Collections.ObjectModel.ReadOnlyCollection<SecurityNamespace> securityNamespaces = securityService.GetSecurityNamespaces();
IGroupSecurityService gss = tpc.GetService<IGroupSecurityService>();
Identity SIDS = gss.ReadIdentity(SearchFactor.AccountName, "GroupName", QueryMembership.Expanded);//GourName format: [ProjectName]\\GourpName
IdentityDescriptor id = new IdentityDescriptor("Microsoft.TeamFoundation.Identity", SIDS.Sid);
List<SecurityNamespace> securityList = securityNamespaces.ToList<SecurityNamespace>();
string securityToken;
foreach (SecurityNamespace sn in securityList)
{
if (sn.Description.DisplayName == "Project")
{
securityToken = "$PROJECT:" + ttt.ArtifactUri.AbsoluteUri;
sn.SetPermissions(securityToken, id, 115, 0, true);
}
}
}
}
}

My assumptions ended up being way off base. This is not the source of the problems.... I'll post in another question the real case. This has been an two week issue now, apologies for the waste of a question here.

Related

Acumatica Prepare Replenishment Screen ProcessAll Action not working

I am on Acumatica 5.30.1672 and am using the SOAP API for Screen IN508000 to try and run the Prepare Replenishment process. I am setting the WarehouseID for where I want to prepare replenishment. The API call is returning the correct InventoryIDs in the response object but when I tell it to ProcessAll, it is not. There is no error, just no processing. I feel like I am missing something trivial here but I just cannot see it. When I do this using the GUI, everything works perfectly.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AcumaticaTest.AcumaticaWebReference;
namespace AcumaticaTest
{
class Program
{
static void Main(string[] args)
{
Screen context = new Screen();
context.CookieContainer = new System.Net.CookieContainer();
context.Timeout = 1200000;
context.Url = "url to SOAP API Endpoint";
LoginResult lresult = context.Login("<username>", "<password>");
IN508000Content IN508000 = context.IN508000GetSchema();
context.IN508000Clear();
var commands = new Command[]
{
new Value {Value = "<WarehouseID>", LinkedCommand = IN508000.Selection.Warehouse, Commit= true },
new Value {Value = "false", LinkedCommand = IN508000.Selection.Me, Commit= true },
IN508000.ItemsRequiringReplenishment.InventoryID,
IN508000.Actions.ProcessAll
};
var response = context.IN508000Submit(commands);
var status = context.IN508000GetProcessStatus();
while (status.Status == ProcessStatus.InProcess)
{
status = context.IN508000GetProcessStatus();
}
}
}
}
I tested your code and it is working just fine - the system prepares the replenishment plans for the items of the requested warehouse, and the response object contains all the items that would be listed if you opened the Prepare Replenishment screen with the parameters you set. If you want to e-mail me URL and credentials to your site (gmichaud at acumatica) I can take a look to see what's wrong.
Please note that you don't need to explicitly set Commit = true for these two fields (the schema object already has them set to true). I would also recommend to add System.Threading.Thread.Sleep(1000) in your loop to avoid hammering the server with requests while it is processing.

VersionOne API Client not recognizing asset types?

I am running into a problem with the VersionOneAPIClient in that it will not recognize anything I give it ass an asset type. I understand the Attribute definitions probably don't make any sense but I've been trying pretty much everything. My end goal would be to query TeamRooms and get team names from all the teams in the team room.
It's my understanding from the documentation on asset types and how to query that this should work but that's what we all say.
I am using:
C# ASP.NET, VersionOneAPIClient 15.0.0.0
Strings I have tried:
TeamRoom
Task
Scope
Project
public bool APIgetTeams()
{
IAssetType teamroomType = services.Meta.GetAssetType("Task");
Query query = new Query(teamroomType);
IAttributeDefinition teamAttribute = teamroomType.GetAttributeDefinition("Children:Room.Team.Name");
query.Selection.Add(teamAttribute);
IAttributeDefinition scheduleAttribute = teamroomType.GetAttributeDefinition("Children:Scope.Room.Schedule.Name");
query.Selection.Add(scheduleAttribute);
query.Find = new QueryFind(scheduleName, new AttributeSelection(scheduleAttribute));
query.Paging.PageSize = 1;
query.Paging.PageSize = 0;
teamRoomAsset = (Asset)services.Retrieve(query).Assets.ToArray().GetValue(0);
return true;
}
My definition of services and the connector:
public static V1Connector connector = V1Connector
.WithInstanceUrl("http://versionone.cscinfo.com/VersionOneProd/")
.WithUserAgentHeader("New Dashboard?", "1.0")
.WithWindowsIntegrated()
.Build();
public IServices services = new Services(connector);
And these are my Errors / Stack Traces:
The error is likely simple and right in my face but I can't figure it out.
You have a couple of things going on here. I will address your statement "My end goal would be to query TeamRooms and get team names from all the teams in the team room."
Here is a working chunk of code that reads all of your TeamRooms and prints the name of the Team Room and the Team Name. Once you get this working on your machine, attempt to do the paging. Add filtering incrementally to keep the debug cycles low.
static void Main(string[] args)
{
V1Connector connector = V1Connector
.WithInstanceUrl("https://www.MyV1INstance")
.WithUserAgentHeader("HappyApp", "0.1")
.WithUsernameAndPassword("login", "pwd")
.Build();
IServices services = new Services(connector);
IAssetType trType = services.Meta.GetAssetType("TeamRoom");
Query query = new Query(trType);
IAttributeDefinition teamAttribute = trType.GetAttributeDefinition("Team.Name");
IAttributeDefinition nameAttribute = trType.GetAttributeDefinition("Name");
query.Selection.Add(teamAttribute);
query.Selection.Add(nameAttribute);
QueryResult result = services.Retrieve(query);
Asset teamRooms = result.Assets[0];
foreach (Asset story in result.Assets)
{
Console.WriteLine(story.Oid.Token);
Console.WriteLine(story.GetAttribute(teamAttribute).Value);
Console.WriteLine(story.GetAttribute(nameAttribute).Value);
Console.WriteLine();
}
Addendum
I just realized that you were using WithWindowsIntegrated() instead of WithUsernameAndPassword().
Just change that in my sample but then confirm that you are logged into the machine as a Member that is already setup in VersionOne. The windows int auth is trusting IIS' decision to trust you but then immediately after allowing auth, you have to have an active Member account in VersionOne to have access to VersionOne assets.

Update Data Source and DataSet reference for SSRS 2012 Deployment from C#

EDIT: I think I can simplify this question a bit to ask for only what is needed to know:
I am working with C# using the SSRS 2010 Web Service:
'ReportService2010.asmx' http://technet.microsoft.com/en-us/library/ee640743.aspx
I can use the method 'CreateDataSource' to create a Datasource on an instance of an SSRS Server http:// (servername)/ReportServer.
I can also use the method 'CreateCatalogItem' to create a report on a server from referencing a project's RDL local file to serialize it to a byte array and then pass that as a 'Definition' to the method to create it on the server.
Now everything I do works with a caveat, and a major one. I can only deploy everything to the same folder. If I deploy a Data Source to say the 'Data Sources' folder and then a report to say: 'Test Reports', the report does not know it has a shared data source to reference at a different location. So I dug a little at the technet articles and have tried to 'GetItemDataSources' method but it only gives a name and a type for the ReportingService2010.DataSource return type. Does anyone know the method to link up a 'Report' or 'Dataset's CatalogItem property of 'DataSource', so it points to a reference in a different folder on the SSRS Server when deploying? There has to be a way to do it as I know I can deploy from Business Intelligence Development Studio and it can do this.
I've had similar issues when deploying report files; when deploying through rs.exe or code you run into these issues where reports lose their link to a Data Source.
We solved this by explicitly pointing the report to the server-side Data Source immediately after being deployed by our application; is this similar to what you're trying to do?
Anyway, here's the slightly adapted code we use in our report deployment application:
static void SetReportDataSource(string reportPath)
{
string dsPath = CombinePath(DataSourcePath, DataSourceFolder, DataSourceName);
DataSourceReference dsRef = new DataSourceReference()
{
Reference = dsPath
};
DataSource ds = new DataSource();
ds.Item = dsRef as DataSourceDefinitionOrReference;
ds.Name = DataSourceName;
var rptDataSources = Server.GetItemDataSources(reportPath);
foreach (var rptDs in rptDataSources)
{
Server.SetItemDataSources(filePath, new DataSource[] { ds });
}
}
So, basically we have variables that define information like the Data Source name, Data Source location on server, and the same for a report. They can be in different folders.
Based on this, we create a new reference to a Data Source and then repoint the report to this using SetItemDataSources.
This sorted out the Data Source issue for me, anyway. Not sure about Shared Datasets and how they handle all of this, but hopefully this will be of some help.
Also, just thought that this would be using the ReportService2005 endpoint, but it's probably not too different for ReportService2010.
Edit:
For the paths mentioned here, these are relative to the server, e.g. /Reports/. You don't need the fully qualified name as you define the Url property of the ReportService2010 object which contains the destination.
Maybe this might be some help. I used it to reset the DataSource for all the reports in a given Parent Folder, and it's subfolders:
using System;
using GetPropertiesSample.ReportService2010;
using System.Diagnostics;
using System.Collections.Generic; //<== required for LISTS
using System.Reflection;
namespace GetPropertiesSample
{
class Program
{
static void Main(string[] args)
{
GetListOfObjectsInGivenFolder_and_ResetTheReportDataSource("0_Contacts"); //<=== This is the parent folder
}
private static void GetListOfObjectsInGivenFolder_and_ResetTheReportDataSource(string sParentFolder)
{
// Create a Web service proxy object and set credentials
ReportingService2010 rs = new ReportingService2010();
rs.Credentials = System.Net.CredentialCache.DefaultCredentials;
CatalogItem[] reportList = rs.ListChildren(#"/" + sParentFolder, true);
int iCounter = 0;
foreach (CatalogItem item in reportList)
{
iCounter += 1;
Debug.Print(iCounter.ToString() + "]#########################################");
if (item.TypeName == "Report")
{
Debug.Print("Report: " + item.Name);
ResetTheDataSource_for_a_Report(item.Path, "/DataSources/Shared_New"); //<=== This is the DataSource that I want them to use
}
}
}
private static void ResetTheDataSource_for_a_Report(string sPathAndFileNameOfTheReport, string sPathAndFileNameForDataSource)
{
//from: http://stackoverflow.com/questions/13144604/ssrs-reportingservice2010-change-embedded-datasource-to-shared-datasource
ReportingService2010 rs = new ReportingService2010();
rs.Credentials = System.Net.CredentialCache.DefaultCredentials;
string reportPathAndName = sPathAndFileNameOfTheReport;
//example of sPathAndFileNameOfTheReport "/0_Contacts/207_Practices_County_CareManager_Role_ContactInfo";
List<ReportService2010.ItemReference> itemRefs = new List<ReportService2010.ItemReference>();
ReportService2010.DataSource[] itemDataSources = rs.GetItemDataSources(reportPathAndName);
foreach (ReportService2010.DataSource itemDataSource in itemDataSources)
{
ReportService2010.ItemReference itemRef = new ReportService2010.ItemReference();
itemRef.Name = itemDataSource.Name;
//example of DataSource i.e. 'itemRef.Reference': "/DataSources/SharedDataSource_DB2_CRM";
itemRef.Reference = sPathAndFileNameForDataSource;
itemRefs.Add(itemRef);
}
rs.SetItemReferences(reportPathAndName, itemRefs.ToArray());
}
}
}

How to find number of TFS test cases for a given bug with API

I am trying to use the TFS API to scan through all my bug workitems, and see if there are any with no associated test cases. I was thinking about using bug.Fields[26].Value == 0 to see how many Related Links there are, but I am not sure if test cases are the only things that are considered related links.
If there are no test cases associated with the bug, I want to create a test case for it. I already know how to create a test case in general, but not one that is associated with an existing bug.
Can someone tell me how to do these things? Thanks.
Any work item that is linked to your Bug is considered a RelatedLink. Check here for the available types that derive from Link and also this SO-post by #bryanmac.With the following you should be able to retrieve the type of each related work item of your bug 123456.
using System;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
namespace WorkItemLinksOfAWorkItem
{
class Program
{
static void Main()
{
TfsTeamProjectCollection teamProjectCollection = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri("http://TFSURI"));
var workItemStore = (WorkItemStore)teamProjectCollection.GetService(typeof(WorkItemStore));
var workItem = workItemStore.GetWorkItem(123456);
LinkCollection links = workItem.Links;
foreach (Link link in links)
{
if (!(link is RelatedLink))
continue;
var relLink = link as RelatedLink;
var relatedWI = workItemStore.GetWorkItem(relLink.RelatedWorkItemId);
Console.WriteLine(relatedWI.Id+" "+relatedWI.Type.Name);
}
}
}
}
(I had originally found the base of these sources in Scrum Dashboard)
In order to generate a new Test Case for your Bug 123456 you could try something like:
var workItemTypes = workItemStore.Projects["TeamProjectName"].WorkItemTypes;
var workItem = new WorkItem(workItemTypes["Test Case"]) {Title = "Programmatically constructed via TFS-SDK"};
var relatedLinkToBug = new RelatedLink(123456);
var links = workItem.Links;
links.Add(relatedLinkToBug);
workItem.Save();

How to access WinRM in C#

I'd like to create a small application that can collect system information (Win32_blablabla) using WinRM as opposed to WMI. How can i do that from C#?
The main goal is to use WS-Man (WinRm) as opposed to DCOM (WMI).
I guess the easiest way would be to use WSMAN automation. Reference wsmauto.dll from windwos\system32 in your project:
then, code below should work for you. API description is here: msdn: WinRM C++ API
IWSMan wsman = new WSManClass();
IWSManConnectionOptions options = (IWSManConnectionOptions)wsman.CreateConnectionOptions();
if (options != null)
{
try
{
// options.UserName = ???;
// options.Password = ???;
IWSManSession session = (IWSManSession)wsman.CreateSession("http://<your_server_name>/wsman", 0, options);
if (session != null)
{
try
{
// retrieve the Win32_Service xml representation
var reply = session.Get("http://schemas.microsoft.com/wbem/wsman/1/wmi/root/cimv2/Win32_Service?Name=winmgmt", 0);
// parse xml and dump service name and description
var doc = new XmlDocument();
doc.LoadXml(reply);
foreach (var elementName in new string[] { "p:Caption", "p:Description" })
{
var node = doc.GetElementsByTagName(elementName)[0];
if (node != null) Console.WriteLine(node.InnerText);
}
}
finally
{
Marshal.ReleaseComObject(session);
}
}
}
finally
{
Marshal.ReleaseComObject(options);
}
}
hope this helps, regards
I've got an article that describes an easy way to run Powershell through WinRM from .NET at http://getthinktank.com/2015/06/22/naos-winrm-windows-remote-management-through-net/.
The code is in a single file if you want to just copy it and it's also a NuGet package that includes the reference to System.Management.Automation.
It auto manages trusted hosts, can run script blocks, and also send files (which isn't really supported but I created a work around). The returns are always the raw objects from Powershell.
// this is the entrypoint to interact with the system (interfaced for testing).
var machineManager = new MachineManager(
"10.0.0.1",
"Administrator",
MachineManager.ConvertStringToSecureString("xxx"),
true);
// will perform a user initiated reboot.
machineManager.Reboot();
// can run random script blocks WITH parameters.
var fileObjects = machineManager.RunScript(
"{ param($path) ls $path }",
new[] { #"C:\PathToList" });
// can transfer files to the remote server (over WinRM's protocol!).
var localFilePath = #"D:\Temp\BigFileLocal.nupkg";
var fileBytes = File.ReadAllBytes(localFilePath);
var remoteFilePath = #"D:\Temp\BigFileRemote.nupkg";
machineManager.SendFile(remoteFilePath, fileBytes);
Hope this helps, I've been using this for a while with my automated deployments. Please leave comments if you find issues.
I would like to note that this shows an interop error by default in Visual Studio 2010.
c.f. http://blogs.msdn.com/b/mshneer/archive/2009/12/07/interop-type-xxx-cannot-be-embedded-use-the-applicable-interface-instead.aspx
There appear to be two ways to solve this. This first is documented in the article listed above and appears to be the correct way to handle the problem. The pertinent changes for this example is:
WSMan wsManObject = new WSMan();
This is in lieu of IWSMan wsman = new WSManClass(); which will throw the error.
The second resolution is to go to the VS2010—>Solution Explorer—>Solution—>Project—>References and select WSManAutomation. Right click or hit Alt-Enter to access the properties. Change the value of the "Embed Interop Types" property of the wsmauto reference.

Categories