I am trying to write a little console app to display a list of running Service Applications on a SharePoint 2010 site. I have employed Microsoft.SharePoint as well as Microsoft.SharePoint.Administration, but so far I am not having much luck. Below is what I have been fiddling around with. Can anyone give me some pointers on how to properly use SPServiceApplicationCollection?
Thanks in advance!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.ServiceProcess;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
SPServiceApplicationCollection services = new SPServiceApplicationCollection(String, SPFarm.Local.Services);
foreach (SPServiceApplication service in services)
{
Console.WriteLine(service.Name);
if (service is SPWebService)
{
SPWebService webService = (SPWebService)service;
foreach (SPWebApplication webApp in webService.WebApplications)
{
Console.WriteLine(webApp.Name);
Console.ReadLine();
}
}
}
}
}
}
EDIT
After some digging/asking around I came up with a rough solution of what I wanted.
For future reference/anyone else that wishes to do this sort of thing, I was able to get a list of deployed servers as well as the application name by doing the following:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.ServiceProcess;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Administration.Health;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var solution = SPFarm.Local.Solutions["Your Service Application Name.wsp"];
string serverName = string.Empty;
foreach (SPServer server in solution.DeployedServers)
{
serverName += server.Name;
Console.WriteLine(server.Name);
}
if (solution != null)
{
if (solution.Deployed)
{
Console.WriteLine("{0} is currently deployed on: {1}", solution.Name, serverName);
Console.ReadLine();
}
else
{
Console.WriteLine("Error! Solution not deployed!");
Console.ReadLine();
}
}
}
}
}
After some digging/asking around I came up with a rough solution of what I wanted. For future reference/anyone else that wishes to do this sort of thing, I was able to get a list of deployed servers as well as the application name by doing the following:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.ServiceProcess;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Administration.Health;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var solution = SPFarm.Local.Solutions["Your Service Application Name.wsp"];
string serverName = string.Empty;
foreach (SPServer server in solution.DeployedServers)
{
serverName += server.Name;
Console.WriteLine(server.Name);
}
if (solution != null)
{
if (solution.Deployed)
{
Console.WriteLine("{0} is currently deployed on: {1}", solution.Name, serverName);
Console.ReadLine();
}
else
{
Console.WriteLine("Error! Solution not deployed!");
Console.ReadLine();
}
}
}
}
}
Related
I have been locking for a long time on how to get a user's role so I can set permissions for commands. This is my code. I am using Discord.NET in the newer version.
using Discord.Commands;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AmberScript2.Modules
{
public class Kick : ModuleBase<SocketCommandContext>
{
[Command("kick")]
public async Task KickUser(string userName)
{
if (Context.Guild.GetRole(Context.Message.Author.Id).Name == "Administrator")
{
await Context.Channel.SendMessageAsync("Success!");
}
else
{
await Context.Channel.SendMessageAsync("Inadequate permisions.");
}
}
}
}
The error i am getting is object reference not set to an instance of an object. I have been trying to find the source of it and i can't. Thanks.
(And yes i have yet to get rid of excess usings. This code isn't done yet.)
If you want to try to get a role of the user, try using SocketGuildUser instead of a string. (Use var role = (user as IGuildUser).Guild.Roles.FirstOrDefault(x => x.Name == "Role");)
using Discord.Commands;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AmberScript2.Modules
{
public class Kick : ModuleBase<SocketCommandContext>
{
[Command("kick")]
public async Task KickUser(SocketGuildUser userName)
{
var user = Context.User as SocketGuildUser;
var role = (user as IGuildUser).Guild.Roles.FirstOrDefault(x => x.Name == "Role");
if (!userName.Roles.Contains(role))
{
// Do Stuff
if (user.GuildPermissions.KickMembers)
{
await userName.KickAsync();
}
}
}
}
}
That is most of my code for kicking.
With the line RequireUserPermission(GuildPermission.KickMembers) you check whether or not the user has the permission to kick members.
Within GuildPermission there are many different permissions.
Like GuildPermission.ManageRoles etc.
using Discord.Commands;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AmberScript2.Modules
{
public class Kick : ModuleBase<SocketCommandContext>
{
[Command("kick"), RequireUserPermission(GuildPermission.KickMembers)]
public async Task KickUser(SocketGuildUser userName)
{
var user = Context.User as SocketGuildUser;
var role = (user as IGuildUser).Guild.Roles.FirstOrDefault(x => x.Name == "Role");
if (!userName.Roles.Contains(role))
{
// Do Stuff
if (user.GuildPermissions.KickMembers)
{
await userName.KickAsync();
}
}
}
}
I have the same problem...
To my knowledge Linq needs to be used with IGuildUser or SocketGuildUser.
I have not yet been able to create functioning code to check if the person performing the command does have a role named "Admin"or some other name
Edit: This might be helpful
https://discord.foxbot.me/docs/api/Discord.WebSocket.SocketGuildUser.html
I've been working on a small MVC-project which since I converted to more of an MVC-approach has stopped working.
The program consuming the web-service is a simple console-application with its own controller:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ConsoleApplication.ServiceReference1;
namespace ConsoleApplication
{
class ConsoleController
{
ServiceReference1.WebService2SoapClient webservice = new ServiceReference1.WebService2SoapClient();
public List<Employee> GetEmployees()
{
return webservice.GetEmployees().ToList();
}
}
}
The web-service consists of a Employee-class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace webservice
{
public class Employee
{
public string _socialSN { get; set; }
public string _lastName { get; set; }
public string _firstName { get; set; }
public Employee(string socialSN, string firstName, string lastName)
{
this._firstName = firstName;
this._lastName = lastName;
this._socialSN = socialSN;
}
}
}
A data access layer:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
namespace webservice
{
public class DAL
{
private SqlConnection con;
private SqlCommand cmd;
public void GetConnection()
{
con = new SqlConnection("Data Source=****;Initial Catalog=DB2015;Persist Security Info=True;User ID=****;Password=***********");
}
public List<Employee> GetEmployees()
{
GetConnection();
con.Open();
cmd = con.CreateCommand();
cmd.CommandText = "SELECT SocialSN, Name, Surname FROM Employee";
SqlDataReader reader = cmd.ExecuteReader();
List<Employee> employeeList = new List<Employee>();
if (reader.HasRows)
{
while (reader.Read())
{
employeeList.Add(new Employee(reader.GetString(0), reader.GetString(1), reader.GetString(2)));
}
}
else
{
employeeList = null;
}
reader.Close();
con.Close();
reader.Dispose();
con.Dispose();
return employeeList;
}
}
}
A BackController:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace webservice
{
class BackController
{
DAL dal = new DAL();
public List<Employee> GetEmployees()
{
return dal.GetEmployees();
}
}
}
And finally the webservice-code itself:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Data.SqlClient;
namespace webservice
{
/// <summary>
/// Summary description for WebService2
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class WebService2 : System.Web.Services.WebService
{
BackController bcontroller = new BackController();
[WebMethod]
public List<Employee> GetEmployees()
{
return bcontroller.GetEmployees();
}
}
}
Whenever I try to add a new service-reference to my console-application I get this error:
Error part 1 http://i.imgur.com/UVw1cAO.png
Error part 2 http://i.imgur.com/N0dF159.png
I cannot figure out what is wrong. It worked fine before I changed to MVC. I've tried to clean the projects, rebuild them and create them all from scratch again and just simply won't work.
Please let me know if you've got any ideas what could be wrong or if you need additional information.
Thanks!
It sounds like you've upgraded some components but the code is still trying to latch onto the old version. Essentially something in your code is looking for say version 1.1.0.1 and you've got version 2.1.0.1. or something like that. You'll need to either redirect to the correct assembly in your web config or find another way to get your application to accept the newer version.
Here are some links you can check out for some tips but these can be elusive bugs to sort out.
From stackoverflow:
The located assembly's manifest definition does not match the assembly reference.
that link has this one inside it:
http://blogs.msdn.com/b/junfeng/archive/2004/03/25/95826.aspx
And here is an independent blogger's take on it:
http://www.codingdefined.com/2014/10/error-located-assemblys-manifest.html
I have created one web service to save error log
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Configuration;
using System.Data.Common;
using System.Data;
using System.Net.Mail;
using System.IO;
namespace TestErrorHandling
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
public class Service1 : IService1
{
public int SaveErrorLog(CompositeType objCom)
{
int messageId = 0;
try
{
SqlDatabase _errDBConnection = null;
_errDBConnection = new SqlDatabase(ConfigurationManager.ConnectionStrings["ErrorLogConnStr"].ToString());
DbCommand dbCommand = _errDBConnection.GetStoredProcCommand("usp_SaveErrorLog");
_errDBConnection.AddInParameter(dbCommand, "#i_ApplicationId", DbType.Int32, objCom.AppId);
_errDBConnection.AddInParameter(dbCommand, "#i_ExceptionType", DbType.String, objCom.ExceptionType);
_errDBConnection.AddOutParameter(dbCommand, "#O_MESSAGEID", DbType.Int32, 4);
_errDBConnection.ExecuteReader(dbCommand);
messageId = Convert.ToInt32(_errDBConnection.GetParameterValue(dbCommand, "#O_MESSAGEID"));
}
catch (Exception ex)
{
throw new FaultException(ex.Message);
}
return messageId;
}
}
}
Now I am calling this service in my web application
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.Collections.Specialized;
using Test.ServiceReference1;
namespace Test
{
public partial class _Default : BasePage
{
ServiceReference1.Service1Client obj1 = new ServiceReference1.Service1Client;
obj1.
}
But after typing obj1. its not showing SaveErrorLog method of Service.
Please help on this where I am doing wrong.
added like
change
ServiceReference1.Service1Client obj1 = new ServiceReference1.Service1Client;
to
ServiceReference1.Service1Client obj1 = new ServiceReference1.Service1Client();
and then use
obj1.<method name>
also add reference using add service references
like this..
I'm new at .NET remoting and C#. I need a client/server application and want to handle this with .NET Remoting. I've wrote a class library for the remoting object, the EchoServer class, with some test methods.
The class library I've added to my server project in Visual Studio. The assembly "System.Runtime.Remoting" I've added, too.
The following is the code of my server:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using Remoting; //Namespace Lib
namespace Server
{
public partial class Server : Form
{
public Server()
{
InitializeComponent();
TcpChannel serverChannel = null;
try
{
serverChannel = new TcpChannel(9998);
lvStatus.Items.Add("Server is listening on port 8089...");
string strIn = "";
ChannelServices.RegisterChannel(serverChannel, true);
RemotingConfiguration.RegisterWellKnownServiceType(Type.GetType("Remoting.EchoServer, remoting_dll"), "Echo", WellKnownObjectMode.SingleCall);
}
catch (Exception ex)
{
ChannelServices.UnregisterChannel(serverChannel);
MessageBox.Show(ex.Message.ToString());
}
}
}
}
If I start the server, I will get an exception:
The value cannot be NULL
Parametername: type
I've tried some other code of a tutorial, but I will get the same excetion, equal if the class for the remoting object is implented as a class library or it is as a class directly in my project.
Can you post implementation of Remoting?
I thing that your mistake is next:
"Remoting.EchoServer, remoting_dll"
So, you should use Type.GetType correctly.
Example of working code:
static void Main(string[] args)
{
Server();
}
static void Server()
{
Console.WriteLine("Server started...");
var httpChannel = new HttpChannel(9998);
ChannelServices.RegisterChannel(httpChannel);
RemotingConfiguration.RegisterWellKnownServiceType(Type.GetType("Server.Program+SomeClass"), "SomeClass", WellKnownObjectMode.SingleCall);
Console.WriteLine("Press ENTER to quit");
Console.ReadLine();
}
public interface ISomeInterface
{
string GetString();
}
public class SomeClass : MarshalByRefObject, ISomeInterface
{
public string GetString()
{
const string tempString = "ServerString";
Console.WriteLine("Server string is sended: {0}", tempString);
return tempString;
}
}
I am aware that HTTP 1.1 can close a connection using the "Connection: close" header in basic socket programming.
Is it possible to create a persistent http connection or session using WCF service? For example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TestServiceInstance
{
class ServiceTest :IServiceTest
{
private int i = 0;
public ServiceTest()
{
++i;
}
public int PrintNumber()
{
return i;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ServiceTestImplementation.ServiceRef;
namespace ServiceTestImplementation
{
class Program
{
static void Main(string[] args)
{
ServiceTestClient client = new ServiceTestClient();
for (int i = 0; i < 10; i++)
{
Console.WriteLine(client.PrintNumber());
}
Console.Read();
}
}
}
It always print 1 - but I would like it if the service instance can remember its value...
Thanks!
Yes, WCF allows you to persist sessions between client calls.
You can use WCF sessions to accomplish this.
http://msdn.microsoft.com/en-us/library/ms733040.aspx