小编stu*_*rtd的帖子

如何使用Hibernate <subselect>:

我是hibernate的新手.我需要了解以下问题:

(1)什么是hibernate映射中的subselect?

(2)如何在hbm文件中映射subselect?

(3)如果我使用subselect检索值,那么如何在java Action类中获取检索到的值.

hibernate

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

如何动态填充树视图(C#)

我有一个包含3列的表,ID,Name和ParentID.ID列包含运行编号,该编号也用作主键.ID也将是节点的Name属性.Name列包含将成为treenode的Text属性的字符串,而ParentID是包含节点的父ID的列.

这是我的表格的样子:

ID     Name   ParentID
======================
1      A      0
2      A1     1
3      B      0
4      C      0
5      A2     1
6      B1     3
Run Code Online (Sandbox Code Playgroud)

此表显示节点A是节点A1和A2的父节点.ParentID等于"0"表示节点的父节点是根节点(硬编码).例如,节点A,B和C是根节点的子节点.

在填充树视图之前,我按ParentID对行进行排序.我使用这两种方法填充树视图(这里的TreeNode节点是填充到树中的子节点):

    private void SearchParent(TreeView tree, String parentID, TreeNode node)
    {
        // Post: call TraverseParent method to search parent

        TreeNodeCollection collection = tree.Nodes;

        // Search parent recursively
        foreach (TreeNode n in collection)
        {
            TraverseParent(n, parentID, node);
        }
    }

    private void TraverseParent(TreeNode potentialParent, String parentID, TreeNode node)
    {
        // Post: search for parent. …
Run Code Online (Sandbox Code Playgroud)

c# treeview winforms

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

如何在不使用CallerInfo属性的情况下获取CallerFilePath和CallerLineNumber?

对于我的log4net解决方案,我有一个使用CallerInfo属性的API包装器,例如

    public void Write(string message,
                            [CallerMemberName] string memberName = "",
                            [CallerFilePath] string filePath = "",
                            [CallerLineNumber] int lineNumber = 0)
Run Code Online (Sandbox Code Playgroud)

但是,我也使用Unity Interception,以便我可以执行前/后响应的跟踪日志记录,例如在Invoke方法中使用如下所示的ICallHandler.

public class TraceCallHandler : ICallHandler
{
...

   public IMethodReturn Invoke(IMethodInvocation input, 
                               GetNextHandlerDelegate getNext)
    {
        //---- Trace method inputs
        this.LogInfoBeforeInvoke(input);

        //---- invoking the target method
        InvokeHandlerDelegate next = getNext();
        IMethodReturn methodReturn = next(input, getNext);

        //---- invoking the target method
        this.LogInfoAfterInvoke(methodReturn.ReturnValue); 
    }
}
Run Code Online (Sandbox Code Playgroud)

注意:上面的代码绝不是完整/正确的...但只是想告诉你我为Unity Interception做了些什么.

我的问题/挑战是这样的:当我最终调用log.Write(...)时,我想要目标的调用者信息,而不是我的TraceCallHandler信息.

例如,对于方法名称,我可以这样做:

   string methodName = input.MethodBase.Name;
Run Code Online (Sandbox Code Playgroud)

如何获取来电者的文件路径和来电者的线路号码?甚至可以通过反思来做吗?

谢谢!

c# reflection log4net unity-container interception

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

设置MVC ASP.NET应用程序来执行计划任务 - 实现它的方法?

我正在构建一个从xml提要中获取数据的网站.我正在构建它作为MVC AP.NET应用程序.例如,我将需要应用程序每隔2分钟从位于提供程序服务器上的xml文件中获取数据.然后将存储这些数据.我的问题是,从网站上传到服务器的那一刻起,我希望这些程序不会中断.我想在main方法(?)中使用一个计时器来每2分钟获取一次数据.但我在这里搜索了其他主题,我发现使用AppFabric将是解决方案.问题是我是一个绝对的初学者,我发现很难使用它...是否有另一种更简单的方法来实现从xml文件连续更新数据?

c# asp.net asp.net-mvc asp.net-mvc-4

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

使用 C# 从 s3 存储桶下载文件

我对 AWS s3 存储桶概念有点陌生。我想从 s3 存储桶中的文件夹“TODAY FILE1”下载文件并使用它。我知道如何使用命令提示符在命令行中执行此操作。我不知道如何在 C# 中实现。

认为

这是我在命令提示符下执行的操作

C:\> aws s3 cp "s3://mys3bucket-output/TODAY FILE1" . --recursive
Run Code Online (Sandbox Code Playgroud)

这就是我做的 C# 程序并得到错误

string accessKey = "abc123";
string secretKey = "secret123";
string bucketName = "mys3bucket-output"
TransferUtility fileTransferUtility =   new TransferUtility(new AmazonS3Client(accessKey, secretKey, Amazon.RegionEndpoint.USEast2));


BasicAWSCredentials basicCredentials = new BasicAWSCredentials(accessKey,secretKey);
AmazonS3Client s3Client = new AmazonS3Client(new BasicAWSCredentials(accessKey, secretKey), Amazon.RegionEndpoint.USEast2);
ListObjectsRequest request = new ListObjectsRequest();

ListObjectsResponse response = s3Client.ListObjects(request.BucketName= bucketName, request.Prefix="TODAY FILE1/");

foreach (S3Object obj in response.S3Objects)
{
    try
    {
        Console.WriteLine("{0}", obj.Key);
        fileTransferUtility.Download(@"C:\Temp", bucketName, obj.Key); …
Run Code Online (Sandbox Code Playgroud)

c# amazon-s3

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

.NET Core 中的单元测试托管服务

我有一个由 .NET Core 中的托管服务实现的后台任务。这个类中的逻辑很少:

public class IndexingService : IHostedService, IDisposable
{
    private readonly int indexingFrequency;

    private readonly IIndexService indexService;

    private readonly ILogger logger;

    private bool isRunning;

    private Timer timer;

    public IndexingService(ILogger<IndexingService> logger, IIndexService indexService, IndexingSettings indexingSettings)
    {
        this.logger = logger;
        this.indexService = indexService;

        this.indexingFrequency = indexingSettings.IndexingFrequency;
    }

    public void Dispose()
    {
        this.timer?.Dispose();
    }

    public Task StartAsync(CancellationToken cancellationToken)
    {
        this.timer = new Timer(this.DoWork, null, TimeSpan.Zero, TimeSpan.FromSeconds(this.indexingFrequency));
        return Task.CompletedTask;
    }

    public Task StopAsync(CancellationToken cancellationToken)
    {
        this.timer?.Change(Timeout.Infinite, 0);
        return Task.CompletedTask;
    }

    private …
Run Code Online (Sandbox Code Playgroud)

c# unit-testing .net-core asp.net-core-hosted-services

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

正则表达式使用相同字符串模式属性的变量 C#

我们可以替换字符串模式@"^[a-zA-Z''-'\s]{1,40}$"以用作属性中的变量吗?

我尝试将模式设置为字符串,但是当我使用[RegularExpression...]变量时它不起作用。我想使用一个变量,因为它是相同的正则表达式格式。

  private static String regexFormat = @"^[a-zA-Z''-'\s]{1,40}$";
Run Code Online (Sandbox Code Playgroud)

或者

   private static String regexFormat = "^[a-zA-Z''-'\s]{1,40}$";

  
    [Display(Name = "Name", Description = "First Name + Last Name.")]
    [Required(ErrorMessage = "First Name is required.")]
    [RegularExpression(@"^[a-zA-Z''-'\s]{1,40}$", ErrorMessage =
        "Numbers and special characters are not allowed in the name.")]


    [Required(ErrorMessage = "Last Name is required.")]
    [RegularExpression(@"^[a-zA-Z''-'\s]{1,40}$", ErrorMessage =
        "Numbers and special characters are not allowed in the name.")]
Run Code Online (Sandbox Code Playgroud)

我想要这样的东西

[RegularExpression(regexFormat, ErrorMessage ="Error in Format")]
Run Code Online (Sandbox Code Playgroud)

这可能吗,如果可以的话怎么办?

.net c# regex string

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

DynamoDB ExclusiveStartKey 的字典序列化不起作用

对于 Dynamo 的分页响应,我试图保留该ExclusiveStartKey值。在代码示例中,如果我response.LastEvaluatedKey直接使用该值,后续请求可以正常工作。

但是,如果我序列化response.LastEvaluatedKey然后将其序列化回用作值ExclusiveStartKey,则后续请求将失败并显示以下错误消息:

提供的起始键无效:一个或多个参数值无效:空属性值类型必须具有 true 值

反序列化的字典似乎与原始字典具有相同的值...是否需要检查两者之间的差异?

QueryResponse response = null;

do
{

    string gsiPartitionKey = "gsi-pk-value-1";

    var queryRequest = new QueryRequest()
    {
        TableName = "my-table",
        IndexName = "my-index",
        KeyConditionExpression = "IndexPk = :s_gsiPartitionKey",
        ExpressionAttributeValues = new Dictionary<string, AttributeValue>
        {
            {
                ":s_gsiPartitionKey", new AttributeValue { S = gsiPartitionKey}
            }
        },
        Limit = 1
    };

    if (response != null)
    {
        //OPTION 1 - OK - Using LastEvaluatedKey directly works fine
        //queryRequest.ExclusiveStartKey = …
Run Code Online (Sandbox Code Playgroud)

c# serialization dictionary amazon-dynamodb jsonconvert

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

C#"使用"块

我有类似下面的代码...有人在这里提到WebClient,Stream和StreamReader对象都可以从使用块中受益.两个简单的问题:

1:这个小片段看起来如何使用块?我做自己的研究没有问题,所以资源链接很好但是看到一个例子会更快更容易,我会从中理解它.

2:我想养成良好的编码标准的习惯,如果我对使用积木更好的原因有所了解会有所帮助...是否只是让你不必担心关闭或在那里更多原因?谢谢!

WebClient client = new WebClient();
Stream stream = client.OpenRead(originGetterURL);
StreamReader reader = new StreamReader(stream);

JObject jObject = Newtonsoft.Json.Linq.JObject.Parse(reader.ReadLine());
string encryptionKey = (string)jObject["key"];
string originURL = (string)jObject["origin_url"];

stream.Close()
reader.Close()
Run Code Online (Sandbox Code Playgroud)

c# design-patterns using stream streamreader

5
推荐指数
2
解决办法
1663
查看次数

InvalidOperationException:无法将表“ xxxx1”用于实体类型“ xxxx2”,因为它已用于实体类型“ xxxx1”

我试图首先以实体框架代码创建数据库,但是我总是收到这样的错误:

InvalidOperationException:无法将表'Device'用于实体类型'IpMac',因为它已用于实体类型'Device',并且主键{'DeviceIP'}与主键{'DeviceID'}之间没有关系。”

这些是我的模型类:

public class IpMac
{
    [Key]
    public string DeviceIP { get; set; }
    //[ForeignKey("Device")]
    public int DeviceID { get; set; }
    public string DeviceMAC { get; set; }

    //public Device Device { get; set; }
    public ICollection<Device> Device { get; set; }
}
public class Device
{
    //[Key]
    public int DeviceID { get; set; }
    public string DeviceName { get; set; }
    public string UserName { get; set; }
    //public string DeviceMAC { get; set; }

    public IpMac …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-mvc entity-framework visual-studio ef-code-first

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