小编Sve*_*dos的帖子

gRPC 推送和扇出

是否有可能使用 gRPC 作为具有扇出功能的推送服务?在Google给出的示例中,服务器端(C#)有以下代码:

    public override async Task ListFeatures(Rectangle request, IServerStreamWriter<Feature> responseStream, ServerCallContext context)
    {
        var responses = features.FindAll( (feature) => feature.Exists() && request.Contains(feature.Location) );
        foreach (var response in responses)
        {
            await responseStream.WriteAsync(response);
        }
    }
Run Code Online (Sandbox Code Playgroud)

问题就在这里:

  1. 仅当客户端明确要求时才会生成和写入数据。
  2. 只有提出请求的客户端才会获得新数据。

我想我需要的是:

  1. 保留每个请求(订阅)的客户端的所有 IServerStreamWriter。
  2. 当新数据可用时,通过外部事件触发写入。
  3. 写入所有streamWriter

编辑: 根据卡尔的建议,我现在有以下内容:

原型:

service PubSub {
 rpc Subscribe(Subscription) returns (stream Event) {}
 rpc Unsubscribe(Subscription) returns (Unsubscription) {}
}

message Event
{
   string Value = 1;
}
message Subscription
{
  string Id = 1;
}
message Unsubscription
{
  string …
Run Code Online (Sandbox Code Playgroud)

.net grpc

6
推荐指数
2
解决办法
7207
查看次数

System.ValueTuple NuGet 如何扩展我的语言功能?

如果使用 .Net < 4.7 元组不受支持。如果我安装了 NuGet“System.ValueTuple”,我就可以编写如下代码

    public static (string Name, int Age) GetFoo()
    {
        return ("Bar", 99);
    }
Run Code Online (Sandbox Code Playgroud)

使用 .Net 4.6.2。这怎么可能?NuGet (DLL) 如何更改我的可用语言功能?有没有关于这里发生的事情的可用文件?我找不到任何。

c# roslyn valuetuple

6
推荐指数
1
解决办法
98
查看次数

使用 *ngFor 自定义模板渲染

我有一个 History 组件,其中包含一个HistoryEntries.

HistoryComponent 好像:

 @Component({
 selector: 'history',
 template: `
    <div>         
       <historyEntry *ngFor='let entry of historyentries'></historyEntry>
    </div> `,
    directives : [HistoryEntryComponent]
 })
Run Code Online (Sandbox Code Playgroud)

HistoryComponent 类看起来像:

export class HistoryComponent{
   public historyentries = [
                    new HistoryEntryComponent(1, "lalala"),
                    new HistoryEntryComponent(2, "xxxxx")
                    ];
}
Run Code Online (Sandbox Code Playgroud)

然后我有一个 HistoryEntryComponent:

@Component({
  selector: 'historyentry',
  template: `
    <div>
        <h1>ID: {{Id}} , Descr.: {{Description}}</h1>
    </div>
`
})
Run Code Online (Sandbox Code Playgroud)

和一个班级:

export class HistoryEntryComponent {
   constructor(public Id : Number, public Description : string)
   {}
}
Run Code Online (Sandbox Code Playgroud)

如果我渲染它,什么都没有出现。我已经使用 a<li>来显示 id 和描述,它的工作原理与预期一致。但是当然HistoryEntry它本身可能会变得非常复杂并且需要它自己的逻辑等等。所以必须有一种方法可以 …

ngfor angular

5
推荐指数
1
解决办法
5476
查看次数

RX最佳做法:选择具有副作用还是使用订阅?

我将事件(按钮的分页事件)转换为IObservable,并异步接收来自服务的结果。

作为副作用,我必须更新子视图。我有两种解决方案:

  1. 选择中的副作用:

        //Converting events to a "stream" of search results.
        IObservable<SearchResult> resultsFromPageChange = Observable.FromEventPattern<EventArgs>(this,
            "PageChangedEvent")
            .Throttle(TimeSpan.FromMilliseconds(500))
            .Select(async ev =>
             {
                 var input = new SearchResultInput()
                 {
                     PageSize = PageSize,
                     PageIndex = PageIndex,
                     SearchOptions = _currentSearchOptions,
                     SearchText = SearchText.Value
                 };
                 var result = await GetPagedComponents(input);
                 //Side effect
                 ActivateAttributesView(result.Components.FirstOrDefault());
    
                 return result;
             }).Switch(); //flatten and take most recent.
    
    Run Code Online (Sandbox Code Playgroud)
  2. 在subsribe中做副作用:

        //Converting events to a "stream" of search results.
        IObservable<SearchResult> resultsFromPageChange = Observable.FromEventPattern<EventArgs>(this,
            "PageChangedEvent")
            .Throttle(TimeSpan.FromMilliseconds(500))
            .Select(async ev =>
            {
                var input = new SearchResultInput() …
    Run Code Online (Sandbox Code Playgroud)

c# system.reactive

4
推荐指数
1
解决办法
1205
查看次数

部分不同的 Linq

我有一个对象列表。这些对象有一个属性,例如“值”。

       var lst = new List<TestNode>();

       var c1 = new TestNode()
        {
          Value = "A",
        };

        lst.Add(c1);

        var c2 = new TestNode()
        {
            Value = "A",
        };

        lst.Add(c2);

        var c3 = new TestNode()
        {
            Value = "B",
         };

        lst.Add(c3);

        var c4 = new TestNode()
        {
             Value = "B",
        };

        lst.Add(c4);
Run Code Online (Sandbox Code Playgroud)

我想这样说:

lst.PartialDistinct(x => x.Value == "A")
Run Code Online (Sandbox Code Playgroud)

这应该仅通过谓词来区分,并且在打印结果 IEnumerable 的“值”时,结果应该是:

A
B
B
Run Code Online (Sandbox Code Playgroud)

我已经找到了 DistinctBy 的解决方案,可以在其中定义键选择器。但结果当然是:

A
B
Run Code Online (Sandbox Code Playgroud)

Cyral 的第一个答案完成了这项工作。所以我接受了。但 Scott 的回答确实是一个 PartialDistinct() 方法,看起来它解决了我所有的问题。

好吧,以为用 Scott 的解决方案解决了,但事实并非如此。也许我在单元测试时犯了一个错误……或者我不知道。问题是:

if(seen.Add(item)) 
Run Code Online (Sandbox Code Playgroud)

这不会过滤掉值为“A”的其他对象。我认为这是因为它在放入哈希集时依赖于引用相等性。

我最终得到了以下解决方案:

public …
Run Code Online (Sandbox Code Playgroud)

c# linq

3
推荐指数
1
解决办法
193
查看次数

如何实现一个简单的Id-Generator?

我有:

type Id = Id of int
let mutable IdInt = 0

let idGenerator = 
    fun _ ->
        IdInt <-  IdInt + 1
        IdInt |> Id
Run Code Online (Sandbox Code Playgroud)

然后被称为例如:

 printfn "%A" (idGenerator())
Run Code Online (Sandbox Code Playgroud)

在 F# 中生成简单的增量 ID 的最佳实践是什么?

f#

1
推荐指数
1
解决办法
116
查看次数

带有 TextBox 的 GridViewColumn.CellTemplate 不适用于 ListView.ItemContainerStyle

我有一个像这样的列表视图:

      <ListView ItemsSource="{Binding Components}"
                      BorderThickness="0"
                      Margin="0,2,0,0"
                      HorizontalAlignment="Stretch"
                      MinHeight="150"
                      SelectionMode="Single"
                <ListView.View>
                    <GridView>
                        <GridViewColumn Header="Component Name">
                            <GridViewColumn.CellTemplate>

                                <DataTemplate DataType="{x:Type viewModels:ComponentViewModel}">
                                    <TextBox Text="{Binding Name}"
                                             Style="{StaticResource TextBoxInListViewStyle}">
                                    </TextBox>
                                </DataTemplate>

                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                    </GridView>
                </ListView.View>
            </ListView>
Run Code Online (Sandbox Code Playgroud)

这呈现如下所示:

在此输入图像描述

如您所见,使用了 TextBox(我可以选择文本),但 ListViewItemContainer 给了我我不想要的玻璃选择外观。

然后我为 ListView 定义了一个 ItemContainerStyle (ListViewItemStyle) ,其使用方式如下(第 7 行):

      <ListView ItemsSource="{Binding Components}"
                      BorderThickness="0"
                      Margin="0,2,0,0"
                      HorizontalAlignment="Stretch"
                      MinHeight="150"
                      SelectionMode="Single"
                      ItemContainerStyle="{StaticResource ListViewItemStyle}"
                <ListView.View>
                    <GridView>
                        <GridViewColumn Header="Component Name">
                            <GridViewColumn.CellTemplate>

                                <DataTemplate DataType="{x:Type viewModels:ComponentViewModel}">
                                    <TextBox Text="{Binding Name}"
                                             Style="{StaticResource TextBoxInListViewStyle}">
                                    </TextBox>
                                </DataTemplate>

                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                    </GridView>
                </ListView.View>
            </ListView>
Run Code Online (Sandbox Code Playgroud)

这是我的 ListViewItemStyle:

<Style x:Key="ListViewItemStyle"
       TargetType="{x:Type ListViewItem}"> …
Run Code Online (Sandbox Code Playgroud)

c# wpf listview

0
推荐指数
1
解决办法
1634
查看次数

标签 统计

c# ×4

.net ×1

angular ×1

f# ×1

grpc ×1

linq ×1

listview ×1

ngfor ×1

roslyn ×1

system.reactive ×1

valuetuple ×1

wpf ×1