我正在编写一个 C# 控制台应用程序(Windows 计划任务)来监视 Rabbit MQ 的状态。因此,如果队列关闭(服务关闭、连接超时或任何其他原因),它将发送通知邮件。我使用了 RabbitMQ .Net 客户端(版本 4.1.1)。基本上我正在检查 CreateConnection() 是否成功。
private static void CheckRabbitMQStatus()
{
ConnectionFactory factory = new ConnectionFactory();
factory.Uri = "amqp://guest:guest@testserver:5672/";
IConnection conn = null;
try
{
conn = factory.CreateConnection();
conn.Close();
conn.Dispose();
}
catch (Exception ex)
{
if (ex.Message == "None of the specified endpoints were reachable")
{
//send mail MQ is down
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是实现这一目标的正确方法吗?有多种可用于 Rabbit MQ 的工具和插件,但我想要一个简单的 C# 解决方案。
我有一个 Parallel.ForEach 循环,它循环遍历一个集合。在内部,我进行了多次网络 I/O 调用的循环。我使用了 Task.ContinueWith 并嵌套了后续的 async-await 调用。处理的顺序无关紧要,但每个异步调用的数据都应该以同步方式处理。含义 - 对于每次迭代,从第一个异步调用中检索到的数据应该传递给第二个异步调用。在第二个异步调用完成后,来自两个异步调用的数据应该一起处理。
Parallel.ForEach(someCollection, parallelOptions, async (item, state) =>
{
Task<Country> countryTask = Task.Run(() => GetCountry(item.ID));
//this is my first async call
await countryTask.ContinueWith((countryData) =>
{
countries.Add(countryData.Result);
Task<State> stateTask = Task.Run(() => GetState(countryData.Result.CountryID));
//based on the data I receive in 'stateTask', I make another async call
stateTask.ContinueWith((stateData) =>
{
states.Add(stateData.Result);
// use data from both the async calls pass it to below function for some calculation
// in a synchronized way (for …Run Code Online (Sandbox Code Playgroud) c# asynchronous task-parallel-library async-await parallel.foreach
我正在使用RabbitMQ进行日常交易。我的使用者是部署在多台计算机上的.Net桌面应用程序。每天,交易仅在特定时间段内被排队。除此之外,任何新交易都需要硬停。我设法停止将新事务发送到队列。但是,队列中的现有事务也需要刷新,以便不发送给任何使用者。我尝试搜索此内容,但除了两个选项外,没有任何清除队列的解决方案-
这两种方法都可以实现,但是需要在我的系统上进行大量更改。我想知道是否有更好的方法。
我有一些示例代码如下:
var items = new List<object>();
var testObjectOne = new
{
Valueone = "test1",
ValueTwo = "test2",
ValueThree = "test3"
};
var testObjectTwo = new
{
Valueone = "test1",
ValueTwo = "test2",
ValueThree = "test3"
};
items.Add(testObjectOne);
items.Add(testObjectTwo);
foreach (var obj in items)
{
var val = obj.Valueone;
}
Run Code Online (Sandbox Code Playgroud)
但我无法访问Valueone并得到错误:object'不包含'Valueone'的定义,并且没有扩展方法'Valueone'接受类型'object'的第一个参数可以找到(你是否缺少using指令或装配参考?)
问题:如何迭代此列表并访问ValueOne?非常感谢任何帮助或意见,谢谢
我在理解 MSAL 身份验证和授权时遇到了一些麻烦。我有一个用 React 开发的单页应用程序。我已通过在 Azure AD 上注册 Web 应用程序来设置 MSAL Azure SSO 身份验证。现在,我有一个在单独的应用程序服务上运行的 Web API(在 .Net Core 中)。如何将 React 应用程序的身份验证集成到 Web API?
想到几个问题:
请分享您的想法。如果我能解释得更好,请告诉我。
我是 Rust 新手,最近开始学习。我编写了一个简单的程序,它执行以下操作。
use std::io;
use std::env;
use std::collections::HashMap;
use std::fs;
fn main() {
let path = env::args().nth(1).unwrap();
let contents = read_file(path);
let map = count_words(contents);
print(map);
}
fn read_file(path: String) -> (String){
let contents = fs::read_to_string(path).unwrap();
return contents;
}
fn count_words(contents: String) -> (HashMap<String, i32>){
let split = contents.split(&[' ', '\n'][..]);
let mut map = HashMap::new();
for w in split{
if map.get(w) == None{
map.insert(w.to_owned(), 1);
}
let count = map.get(w);
map.insert(w.to_owned(), count + …Run Code Online (Sandbox Code Playgroud) 我试图从父组件中的按钮单击事件调用子组件中的函数。
父组件:
class Parent extends Component{
constructor(props){
super(props);
this.state = {
//..
}
}
handleSaveDialog = (handleSaveClick) => {
this.handleSaveClick = handleSaveClick;
}
render(){
return(
<div>
<Button onClick={this.openDialog}>Open Dialog</Button>
<Dialog>
<DialogTitle id="form-dialog-title">Child Dialog</DialogTitle>
<DialogContent>
<Child handleSaveData={this.handleSaveDialog}/>
</DialogContent>
<DialogActions>
<Button onClick={this.handleSaveClick} color="primary">
Save
</Button>
</DialogActions>
</Dialog>
</div>
);
}
}
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,父组件在单击按钮时呈现子组件模式对话框(基于 Material-UI)。“保存”按钮是Dialog父组件中组件的一部分,单击时应调用Child组件中的保存函数。如您所见,我handleSaveDialog通过Child名为 的组件 props传递了一个回调函数handleSaveData。handleSaveClick一旦子组件安装并将回调传递给父组件,单击“保存”按钮将调用子组件。
子组件:
class Child extends Component{
constructor(props){
super(props);
this.state = {
//..
}
}
componentDidMount(){
console.log('mount'); …Run Code Online (Sandbox Code Playgroud) 我试图将两个字符串值解析为DateTime.
DateTime processStartTime = DateTime.ParseExact(currentDateTime.Date.ToString("dd-MM-yyyy") + " " + "00:00", "dd-MM-yyyy hh:mm", System.Threading.Thread.CurrentThread.CurrentCulture);
DateTime processEndTime = DateTime.ParseExact(currentDateTime.Date.ToString("dd-MM-yyyy") + " " + "13:00", "dd-MM-yyyy hh:mm", System.Threading.Thread.CurrentThread.CurrentCulture);
Run Code Online (Sandbox Code Playgroud)
第一个语句工作正常,但第二个语句失败,异常 -
字符串未被识别为有效的DateTime
我做错了什么?
c# ×5
rabbitmq ×2
reactjs ×2
async-await ×1
asynchronous ×1
azure-ad-b2c ×1
datetime ×1
hashmap ×1
javascript ×1
material-ui ×1
rust ×1