LinqToTwitter Search Never Returns - linq-to-twitter

I am attempting to use LinqToTwitter to search twitter. It works fine as run in an NUnit test but it does not work with ASP.NET or as a WinForm app. I am not sure what Authorizer to use.
public async Task<Search> SearchTwitter(string searchWords)
{
var twitterCtx = BuildTwitterContext();
Task<Search> searchResponse = (from search in twitterCtx.Search
where search.Type == SearchType.Search &&
search.Query == searchWords
select search)
.SingleOrDefaultAsync();
return await searchResponse;
}
private static TwitterContext BuildTwitterContext()
{
IAuthorizer authorizer;
if (HttpContext.Current == null)
authorizer = new PinAuthorizer();
else
authorizer = new AspNetSignInAuthorizer();
InMemoryCredentialStore credentialStore = new InMemoryCredentialStore();
credentialStore.ConsumerKey = consumerKey;
credentialStore.ConsumerSecret = consumerSecret;
credentialStore.OAuthToken = accessToken;
credentialStore.OAuthTokenSecret = accessTokenSecret;
authorizer.CredentialStore = credentialStore;
var twitterCtx = new TwitterContext(authorizer);
return twitterCtx;
}

ASP.NET is different because of the page redirections where you start the authorization and then finish after Twitter redirects back. Here's the LINQ to Twitter documentation that will explain how OAuth works and give you a better idea on which authorizers to use:
https://github.com/JoeMayo/LinqToTwitter/wiki/Learning-to-use-OAuth
The L2T source code also has demos. Here's an OAuth controller demo:
https://github.com/JoeMayo/LinqToTwitter/blob/master/New/Linq2TwitterDemos_Mvc/Controllers/OAuthController.cs
public class OAuthController : AsyncController
{
public ActionResult Index()
{
return View();
}
public async Task<ActionResult> BeginAsync()
{
//var auth = new MvcSignInAuthorizer
var auth = new MvcAuthorizer
{
CredentialStore = new SessionStateCredentialStore
{
ConsumerKey = ConfigurationManager.AppSettings["consumerKey"],
ConsumerSecret = ConfigurationManager.AppSettings["consumerSecret"]
}
};
string twitterCallbackUrl = Request.Url.ToString().Replace("Begin", "Complete");
return await auth.BeginAuthorizationAsync(new Uri(twitterCallbackUrl));
}
public async Task<ActionResult> CompleteAsync()
{
var auth = new MvcAuthorizer
{
CredentialStore = new SessionStateCredentialStore()
};
await auth.CompleteAuthorizeAsync(Request.Url);
// This is how you access credentials after authorization.
// The oauthToken and oauthTokenSecret do not expire.
// You can use the userID to associate the credentials with the user.
// You can save credentials any way you want - database,
// isolated storage, etc. - it's up to you.
// You can retrieve and load all 4 credentials on subsequent
// queries to avoid the need to re-authorize.
// When you've loaded all 4 credentials, LINQ to Twitter will let
// you make queries without re-authorizing.
//
//var credentials = auth.CredentialStore;
//string oauthToken = credentials.OAuthToken;
//string oauthTokenSecret = credentials.OAuthTokenSecret;
//string screenName = credentials.ScreenName;
//ulong userID = credentials.UserID;
//
return RedirectToAction("Index", "Home");
}
}
Notice that it uses a WebAuthorizer/SessionStateCredentials pair and separates the start of authorization with a separate action method (specified via callback) for completion.
The following demo shows how to perform OAuth in a WinForms app:
https://github.com/JoeMayo/LinqToTwitter/blob/master/New/Demos/Linq2TwitterDemos_WindowsForms/OAuthForm.cs
public partial class OAuthForm : Form
{
PinAuthorizer pinAuth = new PinAuthorizer();
public OAuthForm()
{
InitializeComponent();
}
async void OAuthForm_Load(object sender, EventArgs e)
{
pinAuth = new PinAuthorizer
{
// Get the ConsumerKey and ConsumerSecret for your app and load them here.
CredentialStore = new InMemoryCredentialStore
{
ConsumerKey = ConfigurationManager.AppSettings["consumerKey"],
ConsumerSecret = ConfigurationManager.AppSettings["consumerSecret"]
},
// Note: GetPin isn't used here because we've broken the authorization
// process into two parts: begin and complete
GoToTwitterAuthorization = pageLink =>
OAuthWebBrowser.Navigate(new Uri(pageLink, UriKind.Absolute))
};
await pinAuth.BeginAuthorizeAsync();
}
async void SubmitPinButton_Click(object sender, EventArgs e)
{
await pinAuth.CompleteAuthorizeAsync(PinTextBox.Text);
SharedState.Authorizer = pinAuth;
// This is how you access credentials after authorization.
// The oauthToken and oauthTokenSecret do not expire.
// You can use the userID to associate the credentials with the user.
// You can save credentials any way you want - database, isolated storage, etc. - it's up to you.
// You can retrieve and load all 4 credentials on subsequent queries to avoid the need to re-authorize.
// When you've loaded all 4 credentials, LINQ to Twitter will let you make queries without re-authorizing.
//
//var credentials = pinAuth.CredentialStore;
//string oauthToken = credentials.OAuthToken;
//string oauthTokenSecret = credentials.OAuthTokenSecret;
//string screenName = credentials.ScreenName;
//ulong userID = credentials.UserID;
//
Close();
}
}
In this case, you can use a PinAuthorizer with an InMemoryCredentialStore. If you look at that demo, it uses a Web Browser control to navigate to Twitter and manage the OAuth flow.
Look at the URL above for the Learning to use OAuth for examples of other IAuthorizer derived types that you can use in different scenarios. Also, download the source code and step through with the debugger to get a feel for the OAuth workflow.

Related

How to refresh view when using signal r to update on database change

I'm using mvc, i have a dashboard for which i have used charthelper and bootstrap admin chart. Now i want to update the data on database change. For which i'm trying to use signal R.
Before
I used repository to get data from database. So had services folder which had methods for it.
Now.
I'm not sure exactly how to do it.
But what i have done so far is created a hub class,
returning
public static void Send()
{
IHubContext context = GlobalHost.ConnectionManager.GetHubContext<DashboardHub>();
context.Clients.All.updateOnDashboard();
}
and on view
<script>
$(function () {
// Declare a proxy to reference the hub.
var chat = $.connection.dashboardHub;
$.connection.hub.logging = true;
chat.client.foo = function () { };
//debugger;
// Create a function that the hub can call to broadcast messages.
chat.client.updateOnDashboard = function () {
getAllDashboardUpdates()
};
$.connection.hub.start().done(function () {
getAllDashboardUpdates();
console.log('Now connected, connection ID=' + $.connection.hub.id);
})
.fail(function () { console.log('Could not connect'); });;
//$.connection.hub.stop();
});
function getAllDashboardUpdates() {
$.ajax({
url: '/Dasdhboard/Index',
contentType: 'application/html ; charset:utf-8',
type: 'GET',
dataType: 'html'
}).success(function (result) {
//$("#refTable").html(result);
}).error(function () {
});
}
controller method
public ActionResult Index(int? page)
{
IEnumerable<test> newlist = null;
newlist = GetAlltest();
var data = dashboardService.GetDashboardData(page, User);
if (newlist != null)
{
return View(data);
}
return View(data);
}
to search for dependency
public IEnumerable<test> GetAlltest()
{
var messages = new List<test>();
using (var connection = new SqlConnection(_connString))
{
connection.Open();
using (var command = new SqlCommand(#"SELECT [id],[testid] FROM [dbo].[test]", connection))
{
command.Notification = null;
SqlDependency.Start(_connString);
var dependency = new SqlDependency(command);
dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
if (connection.State == ConnectionState.Closed)
connection.Open();
var reader = command.ExecuteReader();
while (reader.Read())
{
messages.Add(item: new test { id = (int)reader["id"] });
}
}
}
return messages;
}
private void dependency_OnChange(object sender, SqlNotificationEventArgs e)
{
if (e.Type == SqlNotificationType.Change)
{
DashboardHub.Send();
}
}
Even after doing it my view is not refreshed. I'm sure code is redundant. CAn someone show me a better way to do it. Or where i'm going wrong.
This is just one method. I have 2 charts too.
If I understand your code correctly you currently establish a SingalR connection and if receive a updateOnDashboard() on the client firing a AJAX call to get a full HTML view from the server in insert it in the DOM using jQuery.
I would change that so that updateOnDashboard() also receives your new values and render these values on the client side instead calling again the server for HTML code. I would go even further and create a Javascript view-model for those values and using Knockout to databind your dashbord elements to the view-model. The updateOnDashboard() then simply needs to push these values (parameters) into the view-model and the HTML gets updates by Knockout.
I've blogged about some of this in this post...
What I don't see in your code is a peace of code that detects these data-updates. You need something on the server that detecting the changes and sends out those updateOnDashboard() calls.
Please also note that your Hub method Send() is not used anywhere. Hub methods are only used for client-to-server calls (incoming server calls). You probably don't have these so you will not need a hub method - I guess.
Update based on your comment:
I use SinglaR for "live" broadcasting newly added log-items to the web-clients. On the server-side I have a singleton that tests for new data and broadcast them to the web-clients using SignalR. Here the code:
/// <summary>
/// Singleton that periodically checks the log database for new messages and broadcasts them to all
/// connected web-clients (SignalR).
/// </summary>
public class LiveMessageTicker : ILiveMessageTicker, IRegisteredObject
{
private readonly TimeSpan updateInterval = TimeSpan.FromMilliseconds(2000);
private readonly ILogEntriesRepository repository;
private Guid lastLogEntryId = Guid.Empty;
private readonly SemaphoreSlim checkSemaphore = new SemaphoreSlim(1, 2);
private Timer checkTimer;
private readonly IHubContext hub;
/// <summary>
/// Initializes a new instance of the <see cref="LiveMessageTicker"/> class.
/// </summary>
/// <param name="repository">The database repository to use.</param>
/// <exception cref="System.ArgumentNullException"></exception>
public LiveMessageTicker(ILogEntriesRepository repository)
{
if (repository == null) { throw new ArgumentNullException(nameof(repository)); }
this.repository = repository;
// Register this instance to in ASP to free it up on shutdown
HostingEnvironment.RegisterObject(this);
// Get the server-side SignalR hub
hub = GlobalHost.ConnectionManager.GetHubContext<ServerMonitoringHub>();
// Configure a Timer that calls CheckForNewMessages all 2 sec's
checkTimer = new Timer(CheckForNewMessages, null, TimeSpan.Zero, updateInterval);
}
/// <summary>
/// Stops this instance.
/// </summary>
/// <param name="immediate">if set to <c>true</c> immediatelly.</param>
/// <seealso cref="IRegisteredObject"/>
public void Stop(bool immediate)
{
checkTimer.Dispose();
checkTimer = null;
HostingEnvironment.UnregisterObject(this);
}
private void CheckForNewMessages(object state)
{
if (checkSemaphore.Wait(500))
{
try
{
// Get new log entries
var newLogEntries = repository.GetNewLogEntries(lastLogEntryId).ToList();
// If there arent any new log entries
if (!newLogEntries.Any())
{
return;
}
lastLogEntryId = newLogEntries.Last().Id;
// Convert DB entities into DTO's for specific client needs
var logEntries = newLogEntries.Select(l => new
{
id = l.Id,
correlationId = l.CorelationIdentifier,
messageId = l.MessageId,
time = l.Time.ToLocalTime(),
level = (int)l.Level,
messageText = l.Message,
additionalData = l.AdditionalData.Select(a => new { name = a.Name, value = a.Value }).ToArray(),
tags = l.Tags.Select(t => t.Name).ToArray(),
channel = l.Channel.Name,
username = l.Username,
workstation = l.WorkstationName
}).ToList();
// Broadcast all new log entries over SignalR
hub.Clients.All.addLogMessages(logEntries);
}
finally
{
checkSemaphore.Release();
}
}
}
}
This all gets started in Global.asax.cs there I create a single instance of the above class (which registers itself by ASP.Net for proper stopping later on with HostingEnvironment.RegisterObject(this)).
Please note that I don't push rendered HTML-Code or Views to the client. I push the data as JSON. The server does not render it but the client does. To render it on the client I use a Javascript/Typescript view-model that collects the incomming messages in a Knockout ObservableArray. This observablearray is bound to in HTML using the Knockout foreach (see here). So for the data-updates I don't use Razor and ASP.Net to generate the HTML. This is all part of the initially sent view which has data-bindings in it and refers my Javascript/Typescript. It's quite similar as documented in the above liked blog-post.

How to create a login by specifying the database or with several connection strings in a Web API?

How to create a login by specifying the database or with several connection strings in a Web API?
I have the following code and I want the user to be able to choose his database at the beginning of the session, so that he sends the credentials and at the same time the name of the connection string obtained in a drop-down menu in the .html login.
//project AccesoDatos..
//OwinContext Is it unattainable from this project? What I can do?
namespace AccesoDatos
{
public class DaoConexion: MarshalByRefObject
{
public Database DB { get; set; }
public Database DaoConexionDbContext()
{
///Obtain claims from the security database.
var claims = ((ClaimsIdentity)Thread.CurrentPrincipal.Identity);
var claim = claims.Claims.Where(x => x.Type == "ConnectionStringsName").FirstOrDefault();
if (claim ==null)
{
///Get the DefaultConnection of the web.config
///To authenticate the user and return the claims of the security database.
string DefaultConnection;
DefaultConnection = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
return DB =new SqlDatabase(DefaultConnection);
}
return DB = new SqlDatabase(claim.Value);
}
}
}
connection consumption
//project PersonaES..
namespace ServiciosEntidad.PersonaES.Repositorio
{
public class PersonaESRepositorio : DaoConexion, IPersonaESRepositorio
{
public List<Persona> ListarPersonas()
{
DB = DaoConexionDbContext();
DbCommand command = DB.GetStoredProcCommand("spListarPersonas");
DataSet ds = this.DB.ExecuteDataSet(command);
if (ds.Tables[0].AsEnumerable().Count() == 0)
return null;
return ds.Tables[0].AsEnumerable().Select(row => new Persona
{
Tipo = row.Field<string>("Tipo"),
Id = row.Field<int>("Id"),
PrimerNombre = row.Field<string>("PrimerNombre"),
SegundoNombre = row.Field<string>("SegundoNombre"),
PrimerApellido = row.Field<string>("PrimerApellido")
}}}}
img example.
https://drive.google.com/open?id=1kRv5glvHzsr5vNlvgmsFZW2HlJ7YJqDa
My application manages the SOAP architecture. I have a project with the name: DataAccess, with a DaoConexion class. Using the database class of (Microsoft.Practices.EnterpriseLibrary.Data) which in turn implements DatabaseFactory.CreateDatabase, Instancio this project in each of the repositories, each repository is a project within the same solution.
These projects are injected into my Web API but the API has its own connection string. My intention is to modify the connection string used by the DataAccess project, depending on the current connection of the Web API, in the execution of the Web API
//main project Taller (WEB API C#)
namespace Taller.Models
{
public class IdentityContext : IdentityDbContext<IdentityUser>
{
public IdentityContext(string connectionStringName)
: base(connectionStringName)
{
}
public static IdentityContext Create(string connectionStringName)
{
return new IdentityContext(connectionStringName);
}
}
}
The user sends the name of the connection string.
//main project Taller (WEB API C#)
namespace Taller.Controllers
{
public class UtilidadController : ApiController
{
[HttpPost]
public string GetConectarDbEmpresa(Empresas empresas)
{
if (empresas.ConnectionStringsName =="")
{
empresas.ConnectionStringsName = "DefaultConnection";
IdentityContext.Create(empresas.ConnectionStringsName);
}
else
{
IdentityContext.Create(empresas.ConnectionStringsName);
}
return "Te as Conectado a la Empresa Por Favor ingresa tus Credenciales: ";
}}}
the user already specified in the login session what BD to use, I do not know how to save the connectionStringName temporarily while the user has the active session
//main project Taller (WEB API C#)
public partial class Startup{...
public void ConfigureAuth(IAppBuilder app){...
var claims = ((ClaimsIdentity)Thread.CurrentPrincipal.Identity);
var claim = claims.Claims.Where(x => x.Type == "ConnectionStringsName").FirstOrDefault();
if (claim != null)
{
app.CreatePerOwinContext(() => IdentityContext.Create(claim.Value));
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
}
else
{
app.CreatePerOwinContext(() => IdentityContext.Create("DefaultConnection"));
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
}

How to add Logout from all other session

My scenario is there are two different users login with same user id and password.mean time one of the person change the password by giving the current password. And changed the password at same time the another user gives current password what he logged in was incorrect. So i wants to add logout from all other devices in my web application.how can i do that
There is of course more than one approach, my first idea would be to hook the sessions to the ServletContext. Something like
// this can be in some util class, let's call it SessionUtil
static final String SESSION_REGISTER = "session.register";
public static synchronized void registerSession(ServletRequest req, HttpSession ses, String userName) {
ServletContext ctx = req.getServletContext();
Map<String, List<HttpSession>> register = (Map<String, List<HttpSession>>) ctx.getAttribute(SESSION_REGISTER);
if (register == null) {
register = new HashMap<>();
ctx.setAttribute(SESSION_REGISTER, register);
}
List<HttpSession> sessions = register.computeIfAbsent(userName, k -> new ArrayList<>());
sessions.add(ses);
}
Then, in your code, you need to register the user's session after login:
HttpSession ss = request.getSession();
if (isUser(name,password)) {
ss.setAttribute("user",name)
SessionUtil.registerSession(request, ss, name);
}
Finally, you need a method (again in SessionUtil class) to invalidate all the user's sessions (except the current one):
public static synchronized void invalidateSessions(ServletRequest req, HttpSession current, String userName) {
ServletContext ctx = req.getServletContext();
Map<String, List<HttpSession>> register = (Map<String, List<HttpSession>>) ctx.getAttribute(SESSION_REGISTER);
if (register != null) {
List<HttpSession> sessions = register.get(userName);
if (sessions != null) {
for (HttpSession ses : sessions) {
if (!ses.equals(current)) {
ses.invalidate();
}
}
}
}
}
Then you can call this method e.g. when the user changes his/her password.
Note #1: Not a very nice code, it misses some sanity checks and synchronized can be smaller chunks of code. It's just to give you an idea.
Note #2: The functionality of the registerSession(...) method can be done using the HttpSessionAttributeListener, but I've never used it yet, so cannot give an example.

SignalR disconnected with POST request on IE and Firefox

I'm setting up a notification system on my website (ASP.NET MVC). It needs to be stateless so I picked SignalR.
Notifications come from the server side only and I want to show it after refreshing a new page, not in real time. Usually it's an ActionResult that will be triggered and send a notification to client, then redirects or shows a view.
This is the way it proceeds:
The server sends message to the client.
The client stores it to browser local storage.
After redirect, it show notifications from local storage.
I'm using SignalR 2.2.1 with JQuery 1.9.1
NotificationHub.cs
[HubName("notificationHub")]
public class NotificiationHub : Hub
{
public static void Send(string connectionId, string name, string message)
{
var hubContext = GlobalHost.ConnectionManager.GetHubContext<NotificiationHub>();
hubContext.Clients.Client(connectionId).sendNotification(name, message);
}
}
_Layout.cshtml js
<script>
getNotificationStack = function () {
if (localStorage.getItem("notifications") != null) {
return JSON.parse(localStorage.getItem("notifications"));
}
return [];
}
checkNotification = function () {
var notificationStack = getNotificationStack();
var count = notificationStack.length;
for (var i = 0; i < count; i++) {
window.alert(notificationStack.pop());
}
localStorage.removeItem("notifications");
}
initHubConnection = function () {
// Reference the auto-generated proxy for the hub.
$.connection.hub.logging = true;
var notification = $.connection.notificationHub;
notification.client.sendNotification = function (name, message) {
var notificationStack = getNotificationStack();
notificationStack.push(name + ": " + message);
localStorage.setItem("notifications", JSON.stringify(notificationStack));
};
// Start the connection.
$.connection.hub.start().done(function () {
// allows to send notification to one tab through connectionId
document.cookie = "connectionId=" + $.connection.hub.id;
});
}
$(function () {
checkNotification();
initHubConnection();
});
</script>
MyView.cshtml
#using (Html.BeginForm("TestSendNotification", "Home", FormMethod.Post))
{
<input type="submit" value="Press that button"/>
}
HomeController.cs
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult TestSendNotification()
{
NotificiationHub.Send(Request.Cookies["connectionId"].Value, "Info", "Oh you pressed that!");
return RedirectToAction("Index", "Home");
}
}
It works like a charm with Chrome, but not with Microsoft Edge and Firefox.
Microsoft Edge behavior:
Connection is made but client does not get the message.
-> Note: if I debug my app and wait long enough after sending notification, I get the message successfully.
It seems that sending message takes too long, then the page is refreshing so connection is lost and client cannot get the message...
Firefox behavior:
Every time I click the button (= sending POST), connection is lost, and of course client cannot get messages. I have no idea why it does that!
If I'm using ajax POST, it works well, but that's not what I'm looking for.
##############################################################
Update 16th Feb. 2017:
Unfortunately we cannot use simple http POST with this library, it has to be ajax POST in order to work properly.
Got an answer on their git: https://github.com/SignalR/SignalR/issues/3869#issuecomment-280074464
We had to deal with this issue as well.
The solution is: keep track of new and old connectionIds.
Inside a controller:
public ActionResult RegisterConnectionId(string newConnectionId, string oldConnectionId)
{
Session["SignalRConnectionId"] = newConnectionId;
if(!string.IsNullOrEmpty(oldConnectionId))
RemapConnectionId(newConnectionId, oldConnectionId);
}
We introduced an expiring MemoryCache as a victim cache for old ConnectionId's.
public void RemapConnectionId(string newConnectionId, string oldConnectionId)
{
if(newConnectionId != null && newConnectionId != oldConnectionId)
MemoryCache.Default.Set(oldConnectionId, newConnectionId, ...);
}
You can get the current ConnectionId from an old one and take care of potential MULTIPLE changes like this:
public string ResolveConnectionId(string connectionId)
{
while (!IsEligibleConnectionId(connectionId) && !string.IsNullOrEmpty(connectionId))
connectionId = MemoryCache.Default.Get(connectionId) as string;
return connectionId;
}
Of course, you need to provide the new and the old connection ids to the server when the hub starts.

Consume Server Sent in C#

I am using ServiceStack.Client to consume, the data pushed by my server(which is an aspx page).
Below is the code which i use to consume the data using ServiceStack Client:
using System;
using System.Net.Sockets;
using System.Net;
using System.Security.Cryptography;
using System.Threading;
using ServiceStack;
using System.Collections.Generic;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
ServerEventConnect connectMsg = null;
var msgs = new List<ServerEventMessage>();
var commands = new List<ServerEventMessage>();
var errors = new List<Exception>();
var client = new ServerEventsClient("https://testing.leadsquared.com/ReferralCampaign/Demo")
{
OnConnect = e => PrintMsg(e),
OnCommand = e => PrintCmdMsg(e),
OnMessage = e => PrintCmMsg(e),
OnException = e => PrintExMsg(e)
}.Start();
Console.Read();
}
private static void PrintCmMsg(ServerEventMessage e)
{
if (e != null)
{
PrintMsg(e.Data);
}
}
private static void PrintExMsg(Exception e)
{
if (e != null)
{
PrintMsg(e.Message);
}
}
private static void PrintCmdMsg(ServerEventMessage e)
{
if (e != null)
{
PrintMsg(e.Data);
}
}
private static void PrintMsg(ServerEventConnect e)
{
if (e!=null)
{
PrintMsg(e.Data);
}
}
private static void PrintMsg(string x)
{
Console.WriteLine(x);
}
}
}
When I run my code , the client does print any message on the console.
The ConnectionDisplayName property is "(not connected)".
If i subscribe to the same URL using javascript EventSource, i get the notifications.
My requirement is that I would want to consume the data by my server in C#.
How can I achieve this?
Firstly the url needs to be the BaseUri where ServiceStack is hosted, i.e. the same url used in JavaScript ServerEvents Client, e.g:
var client = new ServerEventsClient(BaseUrl).Start();
It's not clear if /ReferralCampaign/Demo is the BaseUri or not.
You will also want to call Connect() to wait for the client to make a connection, e.g:
await client.Connect();
Then to see message events you'll need to call a ServiceStack Service that publishes a Notify* Event on IServerEvents API which you can use with a separate JsonServiceClient or the ServiceClient available in ServerEventsClient, e.g:
client.ServiceClient.Post(new PostRawToChannel {
From = client.SubscriptionId,
Message = "Test Message",
Channel = channel ?? "*",
Selector = "cmd.announce",
});
This is an example calling the Chat PostRawToChannel ServiceStack Service:
public class ServerEventsServices : Service
{
public IServerEvents ServerEvents { get; set; }
public void Any(PostRawToChannel request)
{
// Ensure the subscription sending this notification is still active
var sub = ServerEvents.GetSubscriptionInfo(request.From);
if (sub == null)
throw HttpError.NotFound("Subscription {0} does not exist".Fmt(request.From));
// Check to see if this is a private message to a specific user
if (request.ToUserId != null)
{
// Only notify that specific user
ServerEvents.NotifyUserId(request.ToUserId, request.Selector, request.Message);
}
else
{
// Notify everyone in the channel for public messages
ServerEvents.NotifyChannel(request.Channel, request.Selector, request.Message);
}
}
}
I also recommend looking at the C# ServerEventTests for complete stand-alone examples using C# ServerEventClient.

Resources