I use wsDualHttpBinding in a wcf service, and i use the following code to store client channels:
public static void Subscribe()
{
try
{
// Subscribe the guest to the beer inventory
var guest = OperationContext.Current.GetCallbackChannel<ILoggingServiceCallBack>();
if (!CallbackList.Contains(guest))
{
CallbackList.Add(guest);
}
}
catch (Exception ex)
{
//do stuff
}
finally
{
//do stuff
}
}
and i call back clients like that:
public static void LoggingCallBack(Action<ILoggingServiceCallBack> callbackNotification)
{
try
{
if (OperationContext.Current != null)
{
var guest = OperationContext.Current.GetCallbackChannel<ILoggingServiceCallBack>();
if (!CallbackList.Contains(guest))
{
CallbackList.Add(guest);
}
}
foreach (var LoggingCallBack in CallbackList)
{
var temp = LoggingCallBack;
try
{
new Thread(() =>
{
try
{
callbackNotification(temp);
}
catch (Exception ex)
{
//do something
}
}).Start();
}
catch (Exception ex)
{
//do somethin
}
}
}
catch (Exception ex)
{
//doing something
}
finally
{
//doing something
}
}
im running through some troubles:
i have no way to tell if the client is online or not before i call
it back.
i need to be able to remove the client after no activity from the
list i guess i would be able to do that if i achieved number 1.
what is the best way to identify clients, in other words what is
the best unique identifier i can identify the client with?
if a connection with the client faulted i don't know how to detect
that and start new one from the client as if i tried to do that it refuse to and it throw exception that the connection is faulted.
sorry if i have asked more than one question, please give your opinion about the code i posted and any answer to any question of the above.
Related
I am using SignalR to send a message to my client. The way I am doing this is in a static function inside my hub file:
public static void CallTestFunction(int UserID, short TestParam)
{
try
{
var hubContext = GlobalHost.ConnectionManager.GetHubContext<TestHub>();
hubContext.Clients.User(UserID.ToString()).TestFunction(TestParam);
}
catch (Exception ex)
{
Logger.LogError(ex);
}
}
This works prefectly fine. However, this does not throw error if I pass a random userID to this function like "123456789". What I want to do is something like:
public static void CallTestFunction(int UserID, short TestParam)
{
try
{
var hubContext = GlobalHost.ConnectionManager.GetHubContext<TestHub>();
if(// Some sort of check like hubContext.Clients.User.Exists(UserID.ToString())
hubContext.Clients.User(UserID.ToString()).TestFunction(TestParam);
else
//Throw exception saying that user does not exist.
}
catch (Exception ex)
{
Logger.LogError(ex);
}
}
What do I do in the second code's if statement to make that code work?
I'm new to RabbitMq and I just followed internet resource as a tutorial. here I have created two separate API for OrderRequest and OrderConsume. from the order request, I'm calling something like this.
IRequestClient<OrderRequest> _client;
public OrderRepo(IRequestClient<OrderRequest> requestClient)
{
_client = requestClient;
}
public async Task<string> GetOrderList(OrderRequest orderRequest)
{
string datas = "";
try
{
using (var request = _client.Create(orderRequest))
{
var response = await request.GetResponse<OrderReponse>();
datas = response.Message.orderName;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
throw;
}
return datas;
}
and from the OrderConsume, I did something like this.
public class OrderConsumer : IConsumer<OrderRequest>
{
public Task Consume(ConsumeContext<OrderRequest> context)
{
OrderReponse request = new();
try
{
var data = context.Message.orderName + " HelloWorld";
request.orderName = data;
}
catch (System.Exception ex)
{
System.Console.WriteLine(ex.Message);
throw;
}
return Task.CompletedTask;
}
}
When I debugging the code Consume part working fine, but here you can see, I'm not passing any response from there. there for from the request API. I'm getting time out error.
Timeout waiting for response, RequestId: a1670000-a53b-c025-bd0b-08d9d52ca58a.
Actually I don't need any response from Consumer side, and just wanted to pass the request to consumer. I think I need to change my code here?
using (var request = _client.Create(orderRequest))
{
var response = await request.GetResponse<OrderReponse>();
datas = response.Message.orderName;
}
is it correct? but actually i don't know how to do it. please help me to solve this.
If your order command does not require a response, don't use the request client. Just publish the command and forget about it.
IPublishEndpoint _publishEndpoint;
public OrderRepo(IPublishEndpoint publishEndpoint)
{
_publishEndpoint = publishEndpoint;
}
public async Task<string> GetOrderList(OrderRequest orderRequest)
{
string datas = "";
try
{
await _publishEndpoint.Publish<OrderRequest>(orderRequest);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
throw;
}
return datas;
}
Of course, in your original code you were actually using the response, so...
I am trying to see the status of multiple services in my string array.
The service may not always exist on the machines I will be using it on hence the try
Code:
public string[] service = { "MSSQL$AMTECHFASTTEST", "SQLBrowser" };
public void stopService()
{
int i = 0;
ServiceController[] scServices;
scServices = ServiceController.GetServices(service[i]);
try
{
foreach (ServiceController services in scServices)
{
MessageBox.Show(service[i]+" " + services.Status.ToString(), "Service Status");
i++;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "Error");
}
}
I think it is the ServiceController.GetServices(service[i]) line that is causing the errors, but I cannot work it out.
Thanks,
The ServiceController.GetServices(string machineName) method retrieves the services running at the host machineName.
If you want to get the services that are running on the same machine as your program, use ServiceController.GetServices() without a parameter.
So I think what you want to do is something like this:
public string[] wantedServices = { "MSSQL$AMTECHFASTTEST", "SQLBrowser" };
public void stopService()
{
ServiceController[] services = ServiceController.GetServices()
.Where(svc => wantedServices.Contains(svc.ServiceName))
.ToArray();
try
{
foreach (ServiceController svc in services)
{
MessageBox.Show($"{svc.ServiceName} {svc.Status}", "Service Status");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "Error");
}
}
This gets all the services on the current machine that have names contained in your wantedServices array (I changed some variable names for clarity).
I am creating a utility class that will be used in my Facebook application for tasks that are commonly done, such as retrieving a Facebook Page ID from a URL. I am unsure if the below code is the correct way to throw and catch exceptions. Could someone please advise, thanks.
Utility Class:
public static class FacebookUtilities
{
public static string GetPageIDFromGraph(string pageUri, string accessToken)
{
try
{
FacebookClient client = new FacebookClient(accessToken);
dynamic result = client.Get(GetPageIDFromUri(pageUri), new { fields = "id" });
return result.ToString();
}
catch (FacebookOAuthException)
{
throw;
}
catch (FacebookApiException)
{
throw;
}
}
public static string GetPageIDFromUri(string pageUri)
{
if (pageUri.Contains('/'))
pageUri = pageUri.Substring(pageUri.LastIndexOf('/') + 1);
if (pageUri.Contains('?'))
return pageUri.Substring(0, pageUri.IndexOf('?'));
else
return pageUri;
}
}
Program class, just testing:
- Note "input" and "output" are just textboxes.
private void btnGetPageID_Click(object sender, EventArgs e)
{
try
{
output.Text = FacebookUtilities.GetPageIDFromGraph(input.Text, "Some Access Token Goes Here");
}
catch (FacebookOAuthException ex)
{
if (ex.ErrorCode == 803)
{
output.Text = "This page does not exist";
}
}
catch (FacebookApiException ex)
{
if (ex.ErrorCode == 100)
{
output.Text = "The request was not supported. The most likely cause for this is supplying an empty page ID.";
}
}
}
Is it correct to simply rethrow the exception from the utility class so that the calling class can catch it and do what needs to be done?
It seems that you do nothing with catched exceptions - so dont catch them. There are a lot of discussions about exception handling, but in general you should catch exceptions when you have something to do with them, or at least using finally to clean up resourses.
Since you aren't handling the exceptions in any way, your code can just be:
public static string GetPageIDFromGraph(string pageUri, string accessToken)
{
FacebookClient client = new FacebookClient(accessToken);
dynamic result = client.Get(GetPageIDFromUri(pageUri), new { fields = "id" });
return result.ToString();
}
You should only catch exceptions when you can meaningfully handle them, and it doesn't look like you can in your GetPageIDFromGraph method, so you should just propagate them.
i have a plugin for an activity.
on pre create i check few conditions and if i get true so i throw OperationCanceledException for stop the create execution.
but the record was saved, why? how do i can to cancel the creation? also tried to throw InvalidPluginExecutionException, but it's stil executed..
the code:
public void Execute(IPluginExecutionContext context)
{
try
{
switch (context.MessageName)
{
case "Create":
if (context.Stage == MessageProcessingStage.BeforeMainOperationOutsideTransaction)
{
bool shouldnotcreateactivity = Create(context, service);
if (shouldnotcreateactivity)
throw new OperationCanceledException();
}
if (context.OutputParameters.Properties.Contains("id"))
{
//continue...
}
break;
}
}
catch (OperationCanceledException cancled)
{
}
catch (InvalidPluginExecutionException invalid)
{
}
catch (SoapException ex)
{
}
catch (Exception ex)
{
}
}
Sounds like you are doing something wrong. You should probably post some of your code realting to the pre-checking and saving of the activity
You should have some logic like the following...
if(ValidateActivity()){
CreateAndSaveActivity();
}
else
throw execption;